def main(): while True: try: text = input('rafmath: ') except (EOFError, KeyboardInterrupt): break if not text: continue if (text.lower() == 'exit'): break try: lexer = Lexer(text) parser = Parser(lexer) interpreter = Interpreter(parser) result = interpreter.interpret() if isinstance(result, int): print(result) if isinstance(result, float): result *= 1000 result = int(result) result = result / 1000 print(result) except: print('ERROR') pass
def run(source: str, interpreter: Interpreter) -> None: scanner = Scanner(source) scanner.scan_tokens() if scanner.had_error: print(f'Error scanning') return parser = Parser(scanner.tokens) statements = parser.parse() if parser.had_error: print(f'Error parsing') for err in parser.errors: parser_error_print(err, scanner.source_lines) else: resolve_statements(statements) try: interpreter.interpret(statements) except LoxRuntimeError as err: print(f'{err.msg}')
def main(): arg = sys.argv[1] processtime_in_ms = time.time() * 1000 with open(arg) as fs: # Generate Tokens lexer = Lexer(fs.read()) tokens = lexer.generate_tokens() # Generate AST parser = Parser(tokens) ast = parser.parse() # Global conductor instance conductor = Conductor() # Interpret AST interpreter = Interpreter(ast, conductor) interpreter.interpret() print(f"\nPROCESS TIME: {time.time() * 1000 - processtime_in_ms}ms")
def main(): while True: try: # To run under Python3 replace 'raw_input' call # with 'input' text = input('alng >>') except EOFError: break if not text: continue lexer = Lexer(text) parser = Parser(lexer) interpreter = Interpreter(parser) result = interpreter.interpret() print(result)
def main(): while True: try: text = input('rafmath: ') except (EOFError, KeyboardInterrupt): break if text == 'exit': exit('Goodbye') if not text: continue text += ' ' lexer = Lexer(text) parser = Parser(lexer) interpreter = Interpreter(parser) result = interpreter.interpret() print(result)
def run_interpreter(text): result = {} result['output'] = [] with Capturing() as output: try: lexer = Lexer(text) parser = Parser(lexer) tree = parser.parse() sem_an = SemanticAnalyzer(tree) sem_an.analyse() interpreter = Interpreter(tree) result['final_result'] = interpreter.interpret() except Exception as exc: result['exception'] = str(exc) if output: result['output'] = output return result
def main(): while True: try: text = input('>>>RAFMATH ') except (EOFError, KeyboardInterrupt): break if not text: continue lexer = Lexer(text) parser = Parser(lexer) interpreter = Interpreter(parser) result = interpreter.interpret() if result in (True, False): print(result) elif float(result).is_integer(): print("%.0f" % result) else: print("%.3f" % result)
def interpret(expression): """Interpret expression and return the result.""" lexer = Lexer(expression) parser = Parser(lexer) interpreter = Interpreter(parser) return interpreter.interpret()
def test_int_5(tree, capsys): interpreter = Interpreter(tree) with pytest.raises(Exception) as excinfo: result = interpreter.interpret() assert 'Invalid syntax' in str(excinfo)
def test_int_4(tree, capsys): interpreter = Interpreter(tree) result = interpreter.interpret() assert result == 'success' captured = capsys.readouterr() assert captured.out == 'Factorial of 3 is : 24\n'
def test_int_3(tree, capsys): interpreter = Interpreter(tree) result = interpreter.interpret() assert result == 'success' captured = capsys.readouterr() assert captured.out == '3\n'
def test_int_7(tree, capsys): interpreter = Interpreter(tree) with pytest.raises(Exception) as excinfo: result = interpreter.interpret() assert 'Error: Can not run + operation on types str and int' in str(excinfo)