Пример #1
0
def run_code(fname):
    interpreter = Interpreter()
    space.setup(interpreter)
    try:
        source = open(fname).read()
    except (OSError, IOError):
        print "Error reading file %s" % fname
        return 1
    # XXX error handling
    try:
        ast = parser.parse(lexer.lex(fname, source), ParsingState(fname,
                           source))
    except ParseError as pe:
        format_parser_error(pe)
        return 1
    importer = Importer(space, dirname(os.path.abspath(fname)), parser, lexer)
    dotted_name = parse_name(fname)
    w_mod = compile_module(space, fname, dotted_name, source, ast, importer)
    w_mod.setup(space)
    try:
        space.call_method(w_mod, 'main', [])
    except AppError as e:
        os.write(2, format_traceback(space, e))
        return 1
    return 0
Пример #2
0
 def import_module(self, space, path):
     # XXX error handling
     pth = os.path.join(self.basepath, os.path.sep.join(path)) + ".q"
     try:
         source = open(pth).read()
     except (IOError, OSError):
         raise ImportError("foo")
     ast = self.parser.parse(self.lexer.lex(pth, source),
                             ParsingState(pth, source))
     dotted_name = ".".join(['self'] + path)
     w_mod = compile_module(space, pth, dotted_name, source, ast, self)
     return w_mod
Пример #3
0
 def parse(self, code):
     program = reformat_code(code)
     return self.parser.parse(self.lexer.lex(program),
                              ParsingState(program))
Пример #4
0
 def parse(self, body):
     program = reformat_expr(body)
     ast = self.parser.parse(self.lexer.lex(program), ParsingState(program))
     return ast.elements[0].body
Пример #5
0
 def parse(self, expr):
     program = "function foo () { " + expr + "; }"
     ast = self.parser.parse(self.lexer.lex(program), ParsingState(program))
     return ast.elements[0].body[0].expr
Пример #6
0
 def parse_expr(self, expr):
     program = "def foo () { " + expr + "; }"
     ast = self.parser.parse(self.lexer.lex('test', program),
                             ParsingState('test', program))
     return ast.elements[0].body[0].expr
Пример #7
0
 def parse(self, program):
     return self.parser.parse(self.lexer.lex('<test>', program),
                              ParsingState('<test>', program))