Exemple #1
0
class Function(Exp):

    def __init__(self, params, cmd):
        self.params = params
        self.cmd = cmd
        self.this = None
        self.env = None

    def eval(self, env):
        if self.env is None:
            self.env = Env(env)
            self.env.declare('this', self.this)
        return self

    def set_this(self, this):
        self.this = this

    def call(self, args, env):
        if len(args) != len(self.params):
            raise Exception("Invalid count of parameters. Should be %s, is %s."  % (len(self.params), len(args)))
        new_env = Env(self.env)
        values = zip(self.params, args)
        for val in values:
            new_env.declare(val[0], val[1])
        return self.cmd.eval(new_env)

    def __str__(self):
        return "Function(%s, %s)" % (self.params, self.cmd)
Exemple #2
0
 def call(self, args, env):
     if len(args) != len(self.params):
         raise Exception("Invalid count of parameters. Should be %s, is %s."  % (len(self.params), len(args)))
     new_env = Env(self.env)
     values = zip(self.params, args)
     for val in values:
         new_env.declare(val[0], val[1])
     return self.cmd.eval(new_env)
def interpret(code, print_ast=False):
    ast = parse(tokenize(code))
    if print_ast:
        print(ast)
    env = Env()
    env.declare("alloc", Alloc())
    env.declare("readline", ReadLine())
    env.declare("true", 1)
    env.declare("false", 0)
    ast.eval(env)