Exemple #1
0
    def visit_const(self, ast_const):
        if isinstance(ast_const.value, str):
            self.log.error("string in boolean context", loc=ast_const.loc)
        elif pyson.is_number(ast_const.value):
            self.log.error("number '%s' in boolean context", ast_const.value, loc=ast_const.loc)

        return ast_const
Exemple #2
0
def _wait(agent, term, intention):
    # Handle optional arguments.
    args = [pyson.grounded(arg, intention.scope) for arg in term.args]
    if len(args) == 2:
        event, millis = args
    else:
        if pyson.is_number(args[0]):
            millis = args[0]
            event = None
        else:
            millis = None
            event = args[0]

    # Type checks.
    if not (millis is None or pyson.is_number(millis)):
        raise pyson.PysonError("expected timeout for .wait to be numeric")
    if not (event is None or pyson.is_string(event)):
        raise pyson.PysonError("expected event for .wait to be a string")

    # Event.
    if event is not None:
        # Parse event.
        if not event.endswith("."):
            event += "."
        log = pyson.Log(LOGGER, 1)
        tokens = pyson.lexer.TokenStream(pyson.StringSource("<.wait>", event),
                                         log)
        tok, ast_event = pyson.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(pyson.runtime.BuildEventVisitor(log))

    # Timeout.
    if millis is None:
        until = None
    else:
        until = agent.env.time() + millis / 1000

    # Create waiter.
    intention.waiter = pyson.runtime.Waiter(event=event, until=until)
    yield
Exemple #3
0
    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 pyson.is_number(left.value) and pyson.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
Exemple #4
0
    def test_is_number(self):
        self.assertTrue(pyson.is_number(1))
        self.assertTrue(pyson.is_number(-3.7))
        self.assertTrue(pyson.is_number(2**128))

        self.assertFalse(pyson.is_number("a string"))
        self.assertFalse(pyson.is_number(False))
        self.assertFalse(pyson.is_number(True))
Exemple #5
0
    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 pyson.is_number(left.value) and
                    isinstance(right, AstConst) and pyson.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
Exemple #6
0
    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 pyson.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