Beispiel #1
0
    def __call__(self, *args):
        import mage.reader as reader  # Avoid circular imports.

        expected_params = len(self.params)
        received_args = len(args)
        if expected_params != received_args:
            err_fmt = 'fn takes exactly {} arguments ({} given)'
            raise TypeError(err_fmt.format(expected_params, received_args))

        fn_scope_sym = symbol.Symbol('fn__' + str(id(self)))
        closure = Closure(fn_scope_sym, outer=self.outer)
        for param, arg in zip(self.params, args):
            v = closure.intern(param)
            v.root = arg

        return reader.eval(self.body, closure)
Beispiel #2
0
    # Version message.
    print "Mage 0.0.1\n"

    while True:
        try:
            line = raw_input(str(repl_ns) + "=> ")

            if line in ("exit", "quit"):
                print "Bye for now!"
                break

            while unbalanced(line):
                line += "\n" + raw_input(" " * len(str(repl_ns)) + ".. ")

            # Evaluation involves three steps:
            #
            #   1. Tokenize a string.
            #   2. Expand tokens. (Macro expansion, error checking.)
            #   3. Eval expanded tokens.
            parsed = reader.read_string(line)
            expanded = reader.expand(parsed, repl_ns)
            evaled = reader.eval(expanded, repl_ns)
            if evaled is None:
                print "nil"
            else:
                print evaled
        except KeyboardInterrupt:
            print "\n"
        except Exception:
            traceback.print_exc()