def ParseAndEval(code_str): w_parser, _ = parse_lib.MakeParserForCompletion(code_str) #spec = arith_parse.MakeShellSpec() #a_parser = tdop.TdopParser(spec, w_parser) # Calls ReadWord(LexMode.ARITH) #anode = a_parser.Parse() anode = w_parser._ReadArithExpr() # need the right lex state? if not anode: raise ExprSyntaxError("failed %s" % w_parser.Error()) print('node:', anode) mem = cmd_exec.Mem('', []) exec_opts = cmd_exec.ExecOpts() ev = word_eval.CompletionWordEvaluator(mem, exec_opts) arith_ev = expr_eval.ArithEvaluator(mem, ev) ok = arith_ev.Eval(anode) if ok: value = arith_ev.Result() print('value:', value) else: raise AssertionError(code_str) return value
def InitExecutor(): mem = cmd_exec.Mem('', []) status_line = ui.NullStatusLine() builtins = Builtins(status_line) funcs = {} comp_funcs = {} exec_opts = cmd_exec.ExecOpts() return cmd_exec.Executor(mem, builtins, funcs, comp_funcs, exec_opts, parse_lib.MakeParserForExecutor)
def InitEvaluator(): mem = cmd_exec.Mem('', []) val1 = runtime.Str('xxx') val2 = runtime.Str('yyy') pairs = [(ast.LeftVar('x'), val1), (ast.LeftVar('y'), val2)] mem.SetLocals(pairs) exec_opts = cmd_exec.ExecOpts() # Don't need side effects for most things return word_eval.CompletionWordEvaluator(mem, exec_opts)
def InitEvaluator(): mem = cmd_exec.Mem('', []) val1 = Value.FromString('xxx') val2 = Value.FromString('yyy') pairs = [(ast.LeftVar('x'), val1), (ast.LeftVar('y'), val2)] mem.SetLocal(pairs, 0) exec_opts = cmd_exec.ExecOpts() # Don't need side effects for most things return word_eval.CompletionEvaluator(mem, exec_opts)
def InitExecutor(): mem = cmd_exec.Mem('', []) status_line = ui.NullStatusLine() builtins = Builtins(status_line) funcs = {} comp_funcs = {} exec_opts = cmd_exec.ExecOpts() pool = alloc.Pool() arena = pool.NewArena() return cmd_exec.Executor(mem, builtins, funcs, completion, comp_funcs, exec_opts, parse_lib.MakeParserForExecutor, arena)
def OshMain(argv): (opts, argv) = Options().parse_args(argv) state = util.TraceState() if 'cp' in opts.trace: util.WrapMethods(cmd_parse.CommandParser, state) if 'wp' in opts.trace: util.WrapMethods(word_parse.WordParser, state) if 'lexer' in opts.trace: util.WrapMethods(lexer.Lexer, state) if len(argv) == 0: dollar0 = sys.argv[0] # e.g. bin/osh else: dollar0 = argv[0] # the script name # TODO: Create a --parse action or 'osh parse' or 'oil osh-parse' # osh-fix # It uses a different memory-management model. It's a batch program and not # an interactive program. pool = Pool() arena = pool.NewArena() # TODO: Maybe wrap this initialization sequence up in an oil_State, like # lua_State. status_lines = ui.MakeStatusLines() mem = cmd_exec.Mem(dollar0, argv[1:]) builtins = builtin.Builtins(status_lines[0]) funcs = {} # Passed to Executor for 'complete', and passed to completion.Init comp_lookup = completion.CompletionLookup() exec_opts = cmd_exec.ExecOpts() # TODO: How to get a handle to initialized builtins here? # tokens.py has it. I think you just make a separate table, with # metaprogramming. ex = cmd_exec.Executor(mem, builtins, funcs, comp_lookup, exec_opts, parse_lib.MakeParserForExecutor) # NOTE: The rc file can contain both commands and functions... ideally we # would only want to save nodes/lines for the functions. try: rc_path = 'oilrc' with open(rc_path) as f: contents = f.read() arena.AddSourcePath(rc_path) #print(repr(contents)) rc_line_reader = reader.StringLineReader(contents, arena=arena) _, rc_c_parser = parse_lib.MakeParserForTop(rc_line_reader) rc_node = rc_c_parser.ParseWholeFile() if not rc_node: # TODO: Error should return a token, and then the token should have a # arena index, and then look that up in the arena. err = rc_c_parser.Error() ui.PrintError(err, arena, sys.stderr) return 2 # parse error is code 2 status = ex.Execute(rc_node) #print('oilrc:', status, cflow, file=sys.stderr) # Ignore bad status? except IOError as e: if e.errno != errno.ENOENT: raise if opts.command is not None: arena.AddSourcePath('<-c arg>') line_reader = reader.StringLineReader(opts.command, arena=arena) interactive = False elif opts.interactive: # force interactive arena.AddSourcePath('<stdin -i>') line_reader = reader.InteractiveLineReader(OSH_PS1, arena=arena) interactive = True else: try: script_name = argv[0] except IndexError: if sys.stdin.isatty(): arena.AddSourcePath('<interactive>') line_reader = reader.InteractiveLineReader(OSH_PS1, arena=arena) interactive = True else: arena.AddSourcePath('<stdin>') line_reader = reader.StringLineReader(sys.stdin.read(), arena=arena) interactive = False else: arena.AddSourcePath(script_name) with open(script_name) as f: line_reader = reader.StringLineReader(f.read(), arena=arena) interactive = False # TODO: assert arena.NumSourcePaths() == 1 # TODO: .rc file needs its own arena. w_parser, c_parser = parse_lib.MakeParserForTop(line_reader, arena=arena) if interactive: # NOTE: We're using a different evaluator here. The completion system can # also run functions... it gets the Executor through Executor._Complete. ev = word_eval.CompletionWordEvaluator(mem, exec_opts) completion.Init(builtins, mem, funcs, comp_lookup, status_lines, ev) # TODO: Could instantiate "printer" instead of showing ops InteractiveLoop(opts, ex, c_parser, w_parser, line_reader) status = 0 # TODO: set code else: # Parse the whole thing up front #print('Parsing file') # TODO: Do I need ParseAndEvalLoop? How is it different than # InteractiveLoop? node = c_parser.ParseWholeFile() if not node: err = c_parser.Error() ui.PrintError(err, arena, sys.stderr) return 2 # parse error is code 2 if opts.ast_output: if opts.ast_format == 'oheap': if opts.ast_output == '-': if sys.stdout.isatty(): raise RuntimeError( 'ERROR: Not dumping binary data to a TTY.') f = sys.stdout else: f = open(opts.ast_output, 'wb') # implicitly closed enc = encode.Params() out = encode.BinOutput(f) encode.EncodeRoot(node, enc, out) else: # text output if opts.ast_output == '-': f = sys.stdout else: f = open(opts.ast_output, 'w') # implicitly closed if opts.ast_format in ('text', 'abbrev-text'): ast_f = fmt.DetectConsoleOutput(f) elif opts.ast_format in ('html', 'abbrev-html'): ast_f = fmt.HtmlOutput(f) else: raise AssertionError abbrev_hook = (ast.AbbreviateNodes if 'abbrev-' in opts.ast_format else None) tree = fmt.MakeTree(node, abbrev_hook=abbrev_hook) ast_f.FileHeader() fmt.PrintTree(tree, ast_f) ast_f.FileFooter() ast_f.write('\n') if opts.do_exec: status = ex.Execute(node) else: util.log('Execution skipped because --no-exec was passed') status = 0 if opts.fix: fix.PrintAsOil(arena, node, opts.debug_spans) return status
def _MakeTestEvaluator(): mem = cmd_exec.Mem('', []) exec_opts = cmd_exec.ExecOpts() ev = word_eval.CompletionWordEvaluator(mem, exec_opts) return ev