def visit_mKeyword(self, node, children) -> MalString: assert type(node.value) is str assert len(node.value) > 1 return MalString(node.value[1:], keyword=True)
def slurp(filename: MalExpression) -> MalString: assert isinstance(filename, MalString) with open(filename.native(), "r") as the_file: contents = the_file.read() return MalString(contents)
def pr_str(args: List[MalExpression]) -> MalString: result_string = " ".join(map(lambda x: x.readable_str(), args)) return MalString(result_string)
def keyword(arg: MalExpression) -> MalExpression: assert isinstance(arg, MalString) if arg.is_keyword(): return arg else: return MalString(arg.unreadable_str(), keyword=True)
def core_str(args: List[MalExpression]) -> MalString: result = "" for a in args: result += a.unreadable_str() return MalString(result)
def PRINT(x: MalExpression) -> str: return str(x) def rep(x: str) -> str: return PRINT(EVAL(READ(x), repl_env)) if __name__ == "__main__": # repl loop eof: bool = False rep( '(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) "\nnil)")))))' ) mal_argv = MalList([MalString(x) for x in sys.argv[2:]]) repl_env.set("*ARGV*", mal_argv) if len(sys.argv) >= 2: file_str = sys.argv[1] rep('(load-file "' + file_str + '")') exit(0) while not eof: try: line = input("user> ") readline.add_history(line) try: print(rep(line)) except MalUnknownSymbolException as e: print("'" + e.func + "' not found")
def find(self, key): if key in self.data: return self if self.outer is not None: return self.outer.find(key) raise MalException(MalString("'{}' not found".format(key)))