def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Boolean: item = interpreter.visit(actual_params[0]) list_ = param_value = interpreter.visit(actual_params[1]) param_type = type(param_value) if not issubclass(param_type, d.List): raise err.ArgumentTypeError(expected=d.List, given=param_value) result = d.Boolean(item in list_) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.List: count = param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) if not issubclass(param_type, d.Integer) or param_value < d.Integer(0): raise err.ArgumentTypeError(expected=d.NaturalNum, given=param_value) data = interpreter.visit(actual_params[1]) result = d.List(int(count) * [data]) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.List: first = interpreter.visit(actual_params[0]) rest = param_value = interpreter.visit(actual_params[1]) param_type = type(param_value) if not issubclass(param_type, d.List): message = f'cons: second argument must be a list, but received {str(first)} and {str(rest)}' raise err.CustomBuiltinProcError(message=message) result = d.List([first] + list(rest)) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Boolean: param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) result = d.Boolean(issubclass(param_type, d.String)) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Boolean: evaluated_params = [] for idx, param in enumerate(actual_params): param_value = interpreter.visit(param) param_type = type(param_value) if not issubclass(param_type, d.RealNum): raise err.ArgumentTypeError( idx=idx, expected=d.RealNum, given=param_value ) evaluated_params.append(param_value) result = d.Boolean(True) prev_value = evaluated_params[0] for param_value in evaluated_params[1:]: current_value = param_value if not prev_value >= current_value: result = d.Boolean(False) break prev_value = current_value return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Boolean: evaluated_params = [] for idx, param in enumerate(actual_params): param_value = interpreter.visit(param) param_type = type(param_value) if not issubclass(param_type, d.Number): raise err.ArgumentTypeError( idx=idx, expected=d.Number, given=param_value ) evaluated_params.append(param_value) first_param_value = evaluated_params[0] result = d.Boolean(True) for param_value in evaluated_params: if param_value != first_param_value: result = d.Boolean(False) break return result
def text_to_interpreter_result( text: str, should_log_scope: bool = False, should_log_stack: bool = False ) -> tp.Tuple[tp.List[d.Data], tp.List[tp.Tuple[bool, t.Token, d.Data, d.Data]]]: constants.set_globals(should_log_scope=should_log_scope, should_log_stack=should_log_stack) lexer = Lexer(text) lexer.process() parser = Parser(lexer) tree = parser.parse() interpreter = Interpreter() output, test_output = interpreter.interpret(tree) return output, test_output
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Number: evaluated_params = [] for idx, param in enumerate(actual_params): param_value = interpreter.visit(param) param_type = type(param_value) if not issubclass(param_type, d.Number): raise err.ArgumentTypeError( idx=idx, expected=d.Number, given=param_value ) if param_value == d.Integer(0): if idx > 0 or (idx == 0 and len(actual_params) == 1): raise err.BuiltinProcError( error_code=err.ErrorCode.DIVISION_BY_ZERO, token=None ) evaluated_params.append(param_value) if len(actual_params) == 1: result = d.Integer(1)/evaluated_params[0] else: result = evaluated_params[0] for param_value in evaluated_params[1:]: result /= param_value return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Boolean: param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) is_list = issubclass(param_type, d.List) result = d.Boolean(is_list and len(param_value) == 0) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Boolean: param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) if not issubclass(param_type, d.String): raise err.ArgumentTypeError(expected=d.String, given=param_value) return d.Boolean(len(param_value) == 0) or param_value.isspace()
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.String: param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) if not issubclass(param_type, d.String): raise err.ArgumentTypeError(expected=d.String, given=param_value) return param_value.lower()
class Interactive: """ The ultimate goal is to have this be a class which you can pass code and receive responses immediately without having to run through the process of setting up an interpreter, it would have its own internal interpreter which you could reset and interact with. For now it behaves as an interactive session. """ def __init__(self): self.interpreter = None def process(self, text): try: lexer = Lexer(text) lexer.process() parser = Parser(lexer) program = parser.parse() statements = program.statements lines = [] for statement in statements: # TODO: maybe abstract out part that does this in interp and use it here since this is breaking... result = self.interpreter.visit(statement) if result is not None: lines.append(result) return lines except err.Error as e: return [e.message[e.message.index(']') + 2:]] def run(self): while True: self.interpreter = Interpreter() ar = stack.ActivationRecord(name='global', type=stack.ARType.PROGRAM, nesting_level=0) with ar(self.interpreter): with self.interpreter.semantic_analyzer( entering=SemanticAnalyzer.Entering.PROGRAM): while True: text = input('> ') if text.isspace(): pass elif text in ['/reset', '/exit']: break else: lines = self.process(text) for line in lines: print(line) if text == '/exit': break
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.List: evaluated_params = [] for idx, param in enumerate(actual_params): param_value = interpreter.visit(param) evaluated_params.append(param_value) result = d.List(evaluated_params) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.List: param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) if not issubclass(param_type, d.List): raise err.ArgumentTypeError(expected=d.List, given=param_value) result = param_value[::-1] return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Boolean: string = param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) if not issubclass(param_type, d.String): raise err.ArgumentTypeError(expected=d.String, given=param_value) index = param_value = interpreter.visit(actual_params[1]) param_type = type(param_value) if not issubclass(param_type, d.Integer) or index < d.Integer(0): message = f'string-ith: expected a natural number for the second argument, but received {index}' raise err.CustomBuiltinProcError(message=message) if index >= d.Integer(len(string)): message = f'string-ith: expected an exact integer [0, {len(string)}) ' message += f'(i.e., less than the length of the given string) for the second argument, but received {index}' raise err.CustomBuiltinProcError(message=message) return string[index]
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Boolean: param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) if not issubclass(param_type, d.Number): raise err.ArgumentTypeError( expected=d.Number, given=param_value ) result = d.Boolean(param_value == d.Integer(0)) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Integer: param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) if not issubclass(param_type, d.RealNum): raise err.ArgumentTypeError( expected=d.RealNum, given=param_value ) result = d.Integer(round(param_value.value)) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.List: param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) if not issubclass(param_type, d.List) or len(param_value) == 0: raise err.ArgumentTypeError(expected=d.List, given=param_value, min_length=1, max_length=None) result = param_value[1:] return result
def _interpret_nth(interpreter: Interpreter, actual_params: tp.List[ast.AST], idx: int) -> d.Data: param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) if not issubclass(param_type, d.List) or len(param_value) <= idx: raise err.ArgumentTypeError(expected=d.List, given=param_value, min_length=idx + 1, max_length=None) result = param_value[idx] return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Boolean: param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) if not issubclass(param_type, d.String): raise err.ArgumentTypeError(expected=d.String, given=param_value) for char in param_value: if not char.isalpha(): return d.Boolean(False) else: return d.Boolean(True)
def run(self): while True: self.interpreter = Interpreter() ar = stack.ActivationRecord(name='global', type=stack.ARType.PROGRAM, nesting_level=0) with ar(self.interpreter): with self.interpreter.semantic_analyzer( entering=SemanticAnalyzer.Entering.PROGRAM): while True: text = input('> ') if text.isspace(): pass elif text in ['/reset', '/exit']: break else: lines = self.process(text) for line in lines: print(line) if text == '/exit': break
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Number: param_value = interpreter.visit(actual_params[0]) param_type = type(param_value) if not issubclass(param_type, d.Number): raise err.ArgumentTypeError( expected=d.Number, given=param_value ) if param_value < d.Integer(0): raise NotImplementedError('Complex numbers not supported yet.') number = math.sqrt(param_value.value) result = d.InexactNum(number) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Boolean: result = d.Boolean(True) for idx, param in enumerate(actual_params): param_value = interpreter.visit(param) param_type = type(param_value) if not issubclass(param_type, d.Boolean): raise err.ArgumentTypeError(idx=idx, expected=d.Boolean, given=param_value) if param_value == d.Boolean(False): result = param_value break return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Boolean: evaluated_params = [] for idx, param in enumerate(actual_params): param_value = interpreter.visit(param) param_type = type(param_value) if not issubclass(param_type, d.String): raise err.ArgumentTypeError(idx=idx, expected=d.String, given=param_value) evaluated_params.append(param_value) substring = evaluated_params[0] containing_string = evaluated_params[1] result = substring in containing_string return d.Boolean(result)
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.List: evaluated_params = [] for idx, param in enumerate(actual_params): param_value = interpreter.visit(param) param_type = type(param_value) if not issubclass(param_type, d.List): raise err.ArgumentTypeError(idx=idx, expected=d.List, given=param_value) evaluated_params.append(param_value) result = d.List([]) for param in evaluated_params: result.extend(param) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Integer: evaluated_params = [] for idx, param in enumerate(actual_params): param_value = interpreter.visit(param) param_type = type(param_value) if not issubclass(param_type, d.Integer): raise err.ArgumentTypeError( idx=idx, expected=d.Integer, given=param_value ) evaluated_params.append(param_value) number = int(evaluated_params[0]) modulus = int(evaluated_params[1]) remainder = number % modulus result = d.Integer(remainder) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.RealNum: evaluated_params = [] for idx, param in enumerate(actual_params): param_value = interpreter.visit(param) param_type = type(param_value) if not issubclass(param_type, d.RealNum): raise err.ArgumentTypeError( idx=idx, expected=d.RealNum, given=param_value ) evaluated_params.append(param_value) result = evaluated_params[0] for param_value in evaluated_params[1:]: if param_value < result: result = param_value return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Integer: evaluated_params = [] for idx, param in enumerate(actual_params): param_value = interpreter.visit(param) param_type = type(param_value) if not issubclass(param_type, d.Integer): raise err.ArgumentTypeError( idx=idx, expected=d.Integer, given=param_value ) evaluated_params.append(param_value) lcm = 1 for param_value in evaluated_params: lcm = abs(lcm * int(param_value)) // math.gcd(lcm, int(param_value)) result = d.Integer(lcm) return result
def _interpret(interpreter: Interpreter, actual_params: tp.List[ast.AST]) -> d.Number: evaluated_params = [] for idx, param in enumerate(actual_params): param_value = interpreter.visit(param) param_type = type(param_value) if not issubclass(param_type, d.Number): raise err.ArgumentTypeError( idx=idx, expected=d.Number, given=param_value ) evaluated_params.append(param_value) result = d.Integer(1) for param_value in evaluated_params: if param_value == d.Integer(0): result = d.Integer(0) break result *= param_value return result