def __init__(self, stdin, stdout, environment=Environment(), lexer=Lexer(), parser=Parser(), interpreter=Interpreter()): self.stdin = stdin self.stdout = stdout self.env = environment self.lexer = lexer self.parser = parser self.interpreter = interpreter
def setUp(self): self.lexer = Lexer() self.parser = Parser()
def setUp(self): self.lexer = Lexer() self.parser = Parser() self.interpreter = Interpreter() self.env = Environment()
import sys from jink.lexer import Lexer from jink.parser import Parser from jink.optimizer import optimize from jink.interpreter import Interpreter from jink.utils.classes import Environment lexer = Lexer() parser = Parser() interpreter = Interpreter() code = '' if len(sys.argv) > 1: if not sys.argv[1].endswith('.jk'): sys.argv[1] += '.jk' code = open(sys.argv[1]).read() else: raise Exception("File path not given.") if not code: raise Exception("Error in file reading.") env = Environment() env.def_func('print', lambda scope, args: print('\n'.join([str(x) for x in args]) or 'null')) env.def_func('string', lambda args: [str(x or 'null') for x in args][0] if len(args) == 1 else [str(x or 'null') for x in args]) AST = optimize(parser.parse(lexer.parse(code))) interpreter.evaluate(AST, env)
"", "args:", " > -v -- verbose; will output AST." # and if compiling, both optimized and unoptimized LLVM IR.", # " > -c -- compile; will use compiler instead of interpreter." "", "usage:", " > [jink] help -- shows this prompt.", " > [jink] path/to/file[.jk] -- executes interpreter on file.", " > [jink] -v path/to/file[.jk] -- executes interpreter on file verbose mode.", # " > [jink] -c path/to/file[.jk] -- executes compiler on file.", # " > [jink] -c -v path/to/file[.jk] -- executes compiler on file in verbose mode.", " > [jink] -- launches interpreted interactive REPL.", " > [jink] -v -- launches interpreted interactive REPL in verbose mode." ])) else: path = ' '.join(sys.argv) code = get_code_from_path(path) if to_compile: raise NotImplementedError("Compiler not yet implemented.") # Compiler()._eval(code, optimize=True, verbose=verbose) else: AST = optimize(Parser().parse(Lexer().parse(code), verbose=verbose)) env = Environment() env.add_builtins() Interpreter().evaluate(AST, env) if __name__ == "__main__": pass