def visit_const(self, ast_const): if isinstance(ast_const.value, str): self.log.error("string in boolean context", loc=ast_const.loc) elif agentspeak.is_number(ast_const.value): self.log.error("number '%s' in boolean context", ast_const.value, loc=ast_const.loc) return ast_const
def _wait(agent, term, intention): # Handle optional arguments. args = [agentspeak.grounded(arg, intention.scope) for arg in term.args] if len(args) == 2: event, millis = args else: if agentspeak.is_number(args[0]): millis = args[0] event = None else: millis = None event = args[0] # Type checks. if not (millis is None or agentspeak.is_number(millis)): raise agentspeak.AslError("expected timeout for .wait to be numeric") if not (event is None or agentspeak.is_string(event)): raise agentspeak.AslError("expected event for .wait to be a string") # Event. if event is not None: # Parse event. if not event.endswith("."): event += "." log = agentspeak.Log(LOGGER, 1) tokens = agentspeak.lexer.TokenStream( agentspeak.StringSource("<.wait>", event), log) tok, ast_event = agentspeak.parser.parse_event(tokens.next(), tokens, log) if tok.lexeme != ".": raise log.error( "expected no further tokens after event for .wait, got: '%s'", tok.lexeme, loc=tok.loc) # Build term. event = ast_event.accept(agentspeak.runtime.BuildEventVisitor(log)) # Timeout. if millis is None: until = None else: until = agent.env.time() + millis / 1000 # Create waiter. intention.waiter = agentspeak.runtime.Waiter(event=event, until=until) yield
def visit_binary_op(self, ast_binary_op): if ast_binary_op.operator.value.boolean_op: left = ast_binary_op.left.accept(self) right = ast_binary_op.right.accept(self) if (isinstance(left, AstConst) and isinstance(left.value, bool) and isinstance(right, AstConst) and isinstance(right.value, bool)): const = AstConst() const.loc = ast_binary_op.loc const.value = ast_binary_op.operator.value.func(left.value, right.value) return const else: ast_binary_op.left = left ast_binary_op.right = right elif ast_binary_op.operator.value.comp_op: left = ast_binary_op.left.accept(TermFoldVisitor(self.log)) right = ast_binary_op.right.accept(TermFoldVisitor(self.log)) if isinstance(left, AstConst) and isinstance(right, AstConst): const = AstConst() const.loc = ast_binary_op.loc if agentspeak.is_number(left.value) and agentspeak.is_number(right.value): const.value = ast_binary_op.operator.value.func(left.value, right.value) return const elif isinstance(left.value, bool) and isinstance(right.value, bool): const.value = ast_binary_op.operator.value.func(left.value, right.value) return const elif isinstance(left.value, str) and isinstance(right.value, str): const.value = ast_binary_op.operator.value.func(left.value, right.value) return const ast_binary_op.left = left ast_binary_op.right = right return ast_binary_op else: self.log.error("unexpected operator '%s' in boolean context", ast_binary_op.operator, loc=ast_binary_op.loc, extra_locs=[ast_binary_op.left.loc, ast_binary_op.right.loc]) return ast_binary_op
def test_is_number(): assert agentspeak.is_number(1) assert agentspeak.is_number(-3.7) assert agentspeak.is_number(2 ** 128) assert not agentspeak.is_number("a string") assert not agentspeak.is_number(False) assert not agentspeak.is_number(True)
def test_is_number(self): self.assertTrue(agentspeak.is_number(1)) self.assertTrue(agentspeak.is_number(-3.7)) self.assertTrue(agentspeak.is_number(2**128)) self.assertFalse(agentspeak.is_number("a string")) self.assertFalse(agentspeak.is_number(False)) self.assertFalse(agentspeak.is_number(True))
def visit_binary_op(self, ast_binary_op): if ast_binary_op.operator.value.numeric_op: left = ast_binary_op.left.accept(self) right = ast_binary_op.right.accept(self) if (isinstance(left, AstConst) and agentspeak.is_number(left.value) and isinstance(right, AstConst) and agentspeak.is_number(right.value)): try: const = AstConst() const.loc = ast_binary_op.loc const.value = ast_binary_op.operator.value.func(left.value, right.value) return const except ZeroDivisionError as err: self.log.error("%s", err, loc=ast_binary_op.loc, extra_locs=[left.loc, right.loc]) else: ast_binary_op.left = left ast_binary_op.right = right else: self.log.error("unexpected operator '%s' in numeric context", ast_binary_op.operator.value.lexeme, loc=ast_binary_op.loc, extra_locs=[ast_binary_op.left.loc, ast_binary_op.right.loc]) return ast_binary_op
def visit_unary_op(self, ast_unary_op): if ast_unary_op.operator.value.numeric_op: folded = ast_unary_op.operand.accept(self) if isinstance(folded, AstConst) and agentspeak.is_number(folded.value): const = AstConst() const.loc = ast_unary_op.loc const.value = ast_unary_op.operator.value.func(folded.value) return const else: ast_unary_op.operand = folded else: self.log.error("unexpected operator '%s' in numeric context", ast_unary_op.operator.value.lexeme, loc=ast_unary_op.loc, extra_locs=[ast_unary_op.operand.loc]) return ast_unary_op