def fireEvent(self, event, channel=None, target=None): """Fire/Push a new Event into the system (queue) This will push the given Event, Channel and Target onto the Event Queue for later processing. if target is None, then target will be set as the Channel of the current Component, self.channel (defaulting back to None). If this Component's Manager is itself, enqueue on this Component's Event Queue, otherwise enqueue on this Component's Manager. :param event: The Event Object :type event: Event :param channel: The Channel this Event is bound for :type channel: str @keyword target: The target Component's channel this Event is bound for :type target: str or Component """ if channel is None and target is None: if type(event.channel) is TupleType: target, channel = event.channel else: channel = event.channel or event.name.lower() target = event.target or None else: channel = channel or event.channel or event.name.lower() if isinstance(target, Manager): target = getattr(target, "channel", "*") else: target = target or event.target or getattr(self, "channel", "*") event.channel = (target, channel) event.value = Value(event, self) if event.start is not None: self.fire(Start(event), *event.start) self.root._fire(event, (target, channel)) return event.value
def step(self, ctx, pc): self = hint(self, access_directly=True) ops = hint(self.ops, promote=True) lex_env = hint(self.lex_env, promote=True) op = getop(ops, pc) if op == PushConstantOp: idx = getop(ops, pc + 1) self.push(lex_env.constants[idx]) return pc + 2 elif op == DispatchOp: idx = getop(ops, pc + 1) argc = getop(ops, pc + 2) sym_value = lex_env.constants[idx] assert isinstance(sym_value, StringValue) sym = sym_value.value if sym == '+' and argc == 2: rhs = self.pop() lhs = self.pop() assert isinstance(lhs, IntValue) assert isinstance(rhs, IntValue) self.push(IntValue(lhs.value + rhs.value)) elif sym == '-' and argc == 2: rhs = self.pop() lhs = self.pop() assert isinstance(lhs, IntValue) assert isinstance(rhs, IntValue) self.push(IntValue(lhs.value - rhs.value)) elif sym == '<' and argc == 2: rhs = self.pop() lhs = self.pop() assert isinstance(lhs, IntValue) assert isinstance(rhs, IntValue) self.push(BoolValue(lhs.value < rhs.value)) elif sym == 'print' and argc == 1: print(self.pop().__repr__()) self.push(Value()) else: proc_lex_env = lex_env.get_proc_env(sym) if proc_lex_env: overload = proc_lex_env.procs[sym] self.push(ctx.run( overload.lex_env, overload.ops, 10, self.pop(), )) return pc + 3 elif op == ReturnOp: return -1 elif op == PopOp: self.drop() return pc + 1 elif op == JumpFalseOp: cnd = self.pop() if isinstance(cnd, BoolValue) and not cnd.value: idx = getop(ops, pc + 1) return idx else: return pc + 2 elif op == GetOp: idx = getop(ops, pc + 1) self.push(self.get(idx)) return pc + 2 return pc
from lexer import Lexer from parser_ import Parser from evaluator import Evaluator from context import Context from environment import * from values import Value global_env = Environment() global_env.set("null", Value(0)) global_func_env = FunctionEnvironment() context = Context("<program>", global_env, global_func_env) while True: text = input(">>> ") lexer = Lexer("<stdin>", text) tokens, errors = lexer.generate_tokens() if errors == None: # print(tokens) parser = Parser(tokens) ast = parser.parse() if ast.error == None: # print(ast.node) evaluator = Evaluator(ast.node, context) value = evaluator.evaluate() if value.error == None: print(value.value) else: print(value.error.as_string()) else: print(ast.error.as_string())
def gen_list(size=None): if not size: size = randint(5, 365) return [Value(r, randint(1, 10)) for r in range(size)]