def println(*args): s = " ".join([printer.pr_str(arg, False) for arg in args]) print s return None
# compare the first two parameters and return true if they are the same # type and contain the same value. In the case of equal length lists, # each element of the list should be compared for equality and if they are # the same return true, otherwise false. '=': _chain(MalBool, op.eq), # '=': lambda a, b: MalBool(type(a) == type(b) and op.eq(a, b)), # <, <=, >, and >=: # treat the first two parameters as numbers and do the corresponding numeric # comparison, returning either true or false. '<': _chain(MalBool, op.lt), '<=': _chain(MalBool, op.le), '>': _chain(MalBool, op.gt), '>=': _chain(MalBool, op.ge), # string functions 'pr-str': lambda *args: MalString( " ".join(printer.pr_str(arg, print_readably=True) for arg in args)), 'str': lambda *args: MalString( "".join(printer.pr_str(arg, print_readably=False) for arg in args)), 'prn': lambda *args: print( MalString(" ".join(printer.pr_str(arg, print_readably=True) for arg in args))) or nil, 'println': lambda *args: print( " ".join(printer.pr_str(arg, print_readably=False) for arg in args)) or nil, }
elif type(e) == list: return seq_equals(e, other) elif type(e) == maltypes.Vector: return seq_equals(e, other) else: return e == other ns[maltypes.Symbol("=")] = equals ns[maltypes.Symbol("<")] = lambda x, y: x < y ns[maltypes.Symbol("<=")] = lambda x, y: x <= y ns[maltypes.Symbol(">")] = lambda x, y: x > y ns[maltypes.Symbol(">=")] = lambda x, y: x >= y ns[maltypes.Symbol("pr-str")] = lambda *args: " ".join( [printer.pr_str(arg, True) for arg in args]) ns[maltypes.Symbol("str")] = lambda *args: "".join( [printer.pr_str(arg, False) for arg in args]) def prn(*args): s = " ".join([printer.pr_str(arg, True) for arg in args]) print s return None def println(*args): s = " ".join([printer.pr_str(arg, False) for arg in args]) print s return None ns[maltypes.Symbol("prn")] = prn
def PRINT(exp): return printer.pr_str(exp, True)
def _println(*args): print String(" ".join(map(lambda exp: pr_str(exp, False), args))) return None
def prn(*x): print(" ".join([printer.pr_str(v,True) for v in x])) return "nil"
def print_(print_arr): return printer.pr_str(print_arr)
def do_str(*args): return "".join(map(lambda exp: printer.pr_str(exp, False), args))
def PRINT(mt): return printer.pr_str(mt)
def println(*args): print(" ".join(map(lambda exp: printer.pr_str(exp, False), args)))
def pr_str(*args): return " ".join(map(lambda exp: printer.pr_str(exp, True), args))
def prn(*args): print(" ".join(map(lambda exp: printer.pr_str(exp, True), args)))
def test12(self): res = printer.pr_str("\\", True) exp = "\\\\" assert(res == exp), \ err_str.format(exp, res)
def test11(self): res = printer.pr_str("test\\string", True) exp = "test\\\\string" assert(res == exp), \ err_str.format(exp, res)
def PRINT(code): return printer.pr_str(code)
def PRINT(in_: t.MalType) -> str: return pr_str(in_, print_readably=True)
def prn(*args): print(" ".join([pr_str(i, print_readably=True) for i in args])) return mal_types.MalNil()
def PRINT(data): return printer.pr_str(data, print_readably=True)
def PRINT(exp: MalType) -> str: return printer.pr_str(exp, print_readably=True)
rep("(def! not (fn* (a) (if a false true)))", out_print=False) rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", out_print=False) #macro rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))", out_print=False) rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))", out_print=False) if __name__ == '__main__': _args = sys.argv[2:] repl_env.set('*ARGV*', _MalData('LIST', list(_args))) #rep("(def! not (fn* (a) (if a false true)))") if len(sys.argv) >= 2: path = sys.argv[1] rep('(load-file "' + path + '")') sys.exit(0) while True: try: x = input('user> ') rep(x, repl_env) except MalException as e: print("Error:", printer.pr_str(e.object)) except Exception as e: print("".join(traceback.format_exception(*sys.exc_info()))) except KeyboardInterrupt: break
def PRINT(s): return printer.pr_str(s)
def PRINT(ast): txt = printer.pr_str(ast) return txt
def PRINT(mal_type): """ Convert result of mal instructions into string representation. """ return pr_str(mal_type)
def println(*args): print(" ".join([printer.pr_str(e, False) for e in args])) return MalNil()
def PRINT(ast): res = printer.pr_str(ast, print_readably=True) return res
def mal_str(*args): return printer.pr_str(args)
def prn(*arg): print(printer.pr_str(arg[0], True)) return Nil()
def prn(*args): s = " ".join([printer.pr_str(arg, True) for arg in args]) print s return None
def _str(*arg): return "".join(list(map(lambda x: printer.pr_str(x, False), arg)))
def _str(*args): ret = [] for ast in args: ret.append(printer.pr_str(ast, False)) return _MalData("STRING", "".join(ret))
def PRINT(exp): return pr_str(exp)
is_empty, "count": lambda x: mal_types.MalNumber(len(x)), "=": equal, "<": lambda x, y: mal_types.MalBool(x.data < y.data), "<=": lambda x, y: mal_types.MalBool(x.data <= y.data), ">": lambda x, y: mal_types.MalBool(x.data > y.data), ">=": lambda x, y: mal_types.MalBool(x.data >= y.data), "pr-str": lambda *args: mal_types.MalString(" ".join( [pr_str(i, print_readably=True) for i in args])), "str": lambda *args: mal_types.MalString("".join( [pr_str(i, print_readably=False) for i in args])), "prn": prn, "println": println, "read-string": read_string, "slurp": slurp, "atom": atom, "atom?": is_atom,
parser = argparse.ArgumentParser() parser.add_argument('-i', '--interactive', action='store_true') parser.add_argument('filename', nargs='?', help='Filename to be executed') parser.add_argument('prog_args', nargs='*', help='Arguments passed to program') parser.add_argument('--prompt', nargs='?', help='Prompt to show to user') parser.add_argument('--disable-header', action='store_true') args = parser.parse_args() if __name__ == '__main__': arg_to_str = lambda arg: f'"{arg}"' rep(f'(def! *ARGV* {"(list " + " ".join(arg_to_str(arg) for arg in args.prog_args) + ")" })') if args.filename is not None: rep(f'(def! *FILENAME* "{args.filename}")') rep('(load-file *FILENAME*)') if not args.interactive: exit(0) else: if not args.disable_header: rep("""(println (str "Mal [" *host-language* "]"))""") while True: inp = input(args.prompt or 'user> ') try: res = rep(inp) except MalException as e: print("Error:", pr_str(e)) except Exception as e: # compatibility print("Error:", e) else: print(res)
,"*": lambda x,y: x*y ,"/": lambda x,y: x/y ,"nil": "nil" ,"not": lambda x: not ((type(x)==bool and x) or (type(x)!=bool and x!="nil")) ,"list": lambda *l: [*l] ,"list?": lambda lst: isinstance(lst,list) ,"empty?": lambda lst: len(lst)==0 ,"count": lambda lst: len(lst) if isinstance(lst,(list,tuple)) else 0 ,"=": lambda x,y: x==y ,">": lambda x,y: x>y ,"<": lambda x,y: x<y ,">=": lambda x,y: x>=y ,"<=": lambda x,y: x<=y ,"read-string": lambda s: reader.read_str(s.value) ,"slurp": lambda fname:maltype.String(readfile(fname)) ,"pr-str": lambda *x: maltype.String(" ".join([printer.pr_str(v) for v in x])) ,"str": lambda *x: maltype.String("".join([printer.pr_str(v,False) for v in x])) ,"prn": prn ,"println": println ,"atom": lambda v:maltype.Atom(v) ,"atom?": maltype.atomp ,"deref": lambda a:a.value ,"reset!": lambda a,v:a.reset(v) ,"swap!": lambda a,f,*arg:a.reset(f(a.value,*arg) if not isinstance(f,dict) else f["fn"](a.value,*arg)) ,"cons": lambda a,lst: [a,*lst] ,"concat": concat ,"nth": nth ,"first": lambda lst:"nil" if len(lst)==0 or lst=="nil" else nth(lst,0) ,"rest": rest ,"throw": throw ,"apply": apply
def PRINT(result): output = pr_str(result) print(output) return
def println(*x): print(" ".join([printer.pr_str(v,False) for v in x])) return "nil"
def PRINT(output): print(printer.pr_str(output))
def PRINT(dt): return printer.pr_str(dt)
def PRINT(expr): return printer.pr_str(expr)
def PRINT(x): return printer.pr_str(x)
def PRINT(form): return printer.pr_str(form)
def PRINT(ast): return printer.pr_str(ast)
return [] else: return reduce(lambda x, y: x + y, args) ns = { '+': lambda a, b: a + b, '-': lambda a, b: a - b, '*': lambda a, b: a * b, '/': lambda a, b: int(a / b), '<': lambda a, b: a < b, '>': lambda a, b: a > b, '=': equals, '<=': lambda a, b: a <= b, '>=': lambda a, b: a >= b, 'prn': lambda x: pr_str(x), 'list': list2, 'list?': lambda x: type(x) is list, 'count': lambda x: len(x) if x else 0, 'empty?': lambda x: len(x) == 0, 'read-string': read_str, 'pr-str': pr_str, 'str': do_str, 'println': println, 'slurp': slurp, 'atom': lambda x: Atom(x), 'atom?': lambda x: isinstance(x, Atom), 'deref': lambda x: x.val, 'reset!': reset, 'swap!': 1, 'cons': lambda x, y: [x] + y,
def PRINT(print_in): return printer.pr_str(print_in)
def do_str(*args): return "".join(map(lambda exp: pr_str(exp, False), args))
def _str(*args): return String("".join(map(lambda exp: pr_str(exp, False), args)))
def prn(*args): print(" ".join(map(lambda exp: pr_str(exp, True), args))) return None
def PRINT(mt): res = printer.pr_str(mt) return res
def println(*args): print(" ".join(map(lambda exp: pr_str(exp, False), args))) return None
def PRINT(line): res = printer.pr_str(line) log(str(res),"PRINT") return res
def PRINT(exp): return printer.pr_str(exp)