diff --git a/process/step2_eval.txt b/process/step2_eval.txt index d24a365a0e..d3eb5b1a80 100644 --- a/process/step2_eval.txt +++ b/process/step2_eval.txt @@ -4,16 +4,15 @@ import types, reader, printer READ(str): return reader.read_str(str) EVAL(ast, env): - ;; prn('EVAL ast) - match ast: - 'key: return lookup(env, key) - OR raise "'"+k+"' not found" - [form1 ..]: return [EVAL(form1, env) ..] - {key1 value1 ..}: return {key1 EVAL(value1, env) ..} - (callable arg1 ..): f = EVAL(callable, env) - args = [EVAL(arg1, env) ..] - return apply(f, args) - otherwise: return ast + // prn('EVAL ast) + match ast: + 'key: return env[key] or raise "'{key}' not found" + [form1 ..]: return [EVAL(form1, env) ..] + {key1 value1 ..}: return {key1 EVAL(value1, env) ..} + (callable arg1 ..): f = EVAL(callable, env) + args = [EVAL(arg1, env) ..] + return f(args) + otherwise: return ast PRINT(exp): return printer.pr_str(exp) diff --git a/process/step3_env.txt b/process/step3_env.txt index bf2daf7d42..8729698e45 100644 --- a/process/step3_env.txt +++ b/process/step3_env.txt @@ -4,22 +4,21 @@ import types, reader, printer, env READ(str): return reader.read_str(str) EVAL(ast, env): - if env.get('DEBUG-EVAL) then prn('EVAL ast) - match ast: - 'key: return env.get(key) - OR raise "'"+k+"' not found" - [form1 ..]: return [EVAL(form1, env) ..] - {key1 value1 ..}: return {key1 EVAL(value1, env) ..} - ('def! 'key value): return env.set(key, EVAL(value, env)) - ('let* (k1 v1 ..) form): env = new Env(env) - env.set(k1, EVAL(v1, env)) - .. - return EVAL(form, env) - ('let* [k1 v1 ..] form): // idem - (callable arg1 ..): f = EVAL(callable, env) - args = [EVAL(arg1, env) ..] - return apply(f, args) - otherwise: return ast + if env.get('DEBUG-EVAL) exists and not in nil, false then prn('EVAL ast) + match ast: + 'key: return env.get(key) or raise "'{key}' not found" + [form1 ..]: return [EVAL(form1, env) ..] + {key1 value1 ..}: return {key1 EVAL(value1, env) ..} + ('def! 'key value): return env.set(key, EVAL(value, env)) + ('let* (k1 v1 ..) form): env = new Env(env) + env.set(k1, EVAL(v1, env)) + .. + return EVAL(form, env) + ('let* [k1 v1 ..] form): // idem + (callable arg1 ..): f = EVAL(callable, env) + args = [EVAL(arg1, env) ..] + return f(args) + otherwise: return ast PRINT(exp): return printer.pr_str(exp) diff --git a/process/step4_if_fn_do.txt b/process/step4_if_fn_do.txt index 246b1ab721..8085df5c12 100644 --- a/process/step4_if_fn_do.txt +++ b/process/step4_if_fn_do.txt @@ -4,34 +4,33 @@ import types, reader, printer, env, core READ(str): return reader.read_str(str) EVAL(ast, env): - if env.get('DEBUG-EVAL) then prn('EVAL ast) - match ast: - 'key: return env.get(key) - OR raise "'"+k+"' not found" - [form1 ..]: return [EVAL(form1, env) ..] - {key1 value1 ..}: return {key1 EVAL(value1, env) ..} - ('def! 'key value): return env.set(key, EVAL(value, env)) - ('let* (k1 v1 ..) form): env = new Env(env) - env.set(k1, EVAL(v1, env)) - .. - return EVAL(form, env) - ('let* [k1 v1 ..] form): // idem - ('do form1 .. last): EVAL(form1, env) - .. - return EVAL(last, env) - ('if tst yes no): if EVAL(tst, env) then: return EVAL(yes, env) - else: return EVAL(no, env) - ('if tst yes): if EVAL(tst, env) then: return EVAL(yes, env) - else: return nil - ('fn* ('key1 ..) impl): return new MalFunc(env, impl, params=[key1 ..]) - ('fn* ['key1 ..] impl): // idem - (callable arg1 ..): f = EVAL(callable, env) - args = [EVAL(arg1, env) ..] - if malfn?(f): - return EVAL(f.impl, - new Env(f.env, f.parm, args)) - return apply(f, args) - otherwise: return ast + if env.get('DEBUG-EVAL) exists and not in nil, false then prn('EVAL ast) + match ast: + 'key: return env.get(key) or raise "'{key}' not found" + [form1 ..]: return [EVAL(form1, env) ..] + {key1 value1 ..}: return {key1 EVAL(value1, env) ..} + ('def! 'key value): return env.set(key, EVAL(value, env)) + ('let* (k1 v1 ..) form): env = new Env(env) + env.set(k1, EVAL(v1, env)) + .. + return EVAL(form, env) + ('let* [k1 v1 ..] form): // idem + ('do form1 .. last): EVAL(form1, env) + .. + return EVAL(last, env) + ('if cond yes no): if EVAL(cond, env) in nil, false + then return EVAL(yes, env) + else return EVAL(no, env) + ('if cond yes): // idem with return nil in the else branch + ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) + ('fn* ['key1 ..] impl): // idem + (callable arg1 ..): f = EVAL(callable, env) + args = [EVAL(arg1, env) ..] + if malfn?(f) then: + return EVAL(f.impl, + new Env(f.env, f.parm, args)) + return f(args) + otherwise: return ast PRINT(exp): return printer.pr_str(exp) diff --git a/process/step5_tco.txt b/process/step5_tco.txt index 2132c0c6e6..1fd6d0540c 100644 --- a/process/step5_tco.txt +++ b/process/step5_tco.txt @@ -4,33 +4,34 @@ import types, reader, printer, env, core READ(str): return reader.read_str(str) EVAL(ast, env): - loop: - if env.get('DEBUG-EVAL) then prn('EVAL ast) - match ast: - 'key: return env.get(key) - OR raise "'"+k+"' not found" - [form1 ..]: return [EVAL(form1, env) ..] - {key1 value1 ..}: return {key1 EVAL(value1, env) ..} - ('def! 'key value): return env.set(key, EVAL(value, env)) - ('let* (k1 v1 ..) form): env = new Env(env) - env.set(k1, EVAL(v1, env)) - .. - ast = form - ('let* [k1 v1 ..] form): // idem - ('do form1 .. last): EVAL(form1, env) - .. - ast = last - ('if tst yes no): if EVAL(tst, env) then ast = yes else ast = no - ('if tst yes): if EVAL(tst, env) then ast = yes else return nil - ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) - ('fn* ['key1 ..] impl): // idem - (callable arg1 ..): f = EVAL(callable, env) - args = [EVAL(arg1, env) ..] - if malfn?(f): env = new Env(f.env, f.parm, args) - ast = f.impl - continue - return apply(f, args) - otherwise: return ast + loop: + if env.get('DEBUG-EVAL) exists and not in nil, false then prn('EVAL ast) + match ast: + 'key: return env.get(key) or raise "'{key}' not found" + [form1 ..]: return [EVAL(form1, env) ..] + {key1 value1 ..}: return {key1 EVAL(value1, env) ..} + ('def! 'key value): return env.set(key, EVAL(value, env)) + ('let* (k1 v1 ..) form): env = new Env(env) + env.set(k1, EVAL(v1, env)) + .. + ast = form; continue + ('let* [k1 v1 ..] form): // idem + ('do form1 .. last): EVAL(form1, env) + .. + ast = last; continue + ('if cond yes no): if EVAL(cond, env) in nil, false + then ast = yes; continue + else ast = no; continue + ('if cond yes): // idem with return nil in the else branch + ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) + ('fn* ['key1 ..] impl): // idem + (callable arg1 ..): f = EVAL(callable, env) + args = [EVAL(arg1, env) ..] + if malfn?(f) then: + env = new Env(f.env, f.parm, args) + ast = f.impl; continue + return f(args) + otherwise: return ast PRINT(exp): return printer.pr_str(exp) diff --git a/process/step6_file.txt b/process/step6_file.txt index 4ffd32a7e6..221f4ba7e5 100644 --- a/process/step6_file.txt +++ b/process/step6_file.txt @@ -4,33 +4,34 @@ import types, reader, printer, env, core READ(str): return reader.read_str(str) EVAL(ast, env): - loop: - if env.get('DEBUG-EVAL) then prn('EVAL ast) - match ast: - 'key: return env.get(key) - OR raise "'"+k+"' not found" - [form1 ..]: return [EVAL(form1, env) ..] - {key1 value1 ..}: return {key1 EVAL(value1, env) ..} - ('def! 'key value): return env.set(key, EVAL(value, env)) - ('let* (k1 v1 ..) form): env = new Env(env) - env.set(k1, EVAL(v1, env)) - .. - ast = form - ('let* [k1 v1 ..] form): // idem - ('do form1 .. last): EVAL(form1, env) - .. - ast = last - ('if tst yes no): if EVAL(tst, env) then ast = yes else ast = no - ('if tst yes): if EVAL(tst, env) then ast = yes else return nil - ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) - ('fn* ['key1 ..] impl): // idem - (callable arg1 ..): f = EVAL(callable, env) - args = [EVAL(arg1, env) ..] - if malfn?(f): env = new Env(f.env, f.parm, args) - ast = f.impl - continue - return apply(f, args) - otherwise: return ast + loop: + if env.get('DEBUG-EVAL) exists and not in nil, false then prn('EVAL ast) + match ast: + 'key: return env.get(key) or raise "'{key}' not found" + [form1 ..]: return [EVAL(form1, env) ..] + {key1 value1 ..}: return {key1 EVAL(value1, env) ..} + ('def! 'key value): return env.set(key, EVAL(value, env)) + ('let* (k1 v1 ..) form): env = new Env(env) + env.set(k1, EVAL(v1, env)) + .. + ast = form; continue + ('let* [k1 v1 ..] form): // idem + ('do form1 .. last): EVAL(form1, env) + .. + ast = last; continue + ('if cond yes no): if EVAL(cond, env) in nil, false + then ast = yes; continue + else ast = no; continue + ('if cond yes): // idem with return nil in the else branch + ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) + ('fn* ['key1 ..] impl): // idem + (callable arg1 ..): f = EVAL(callable, env) + args = [EVAL(arg1, env) ..] + if malfn?(f) then: + env = new Env(f.env, f.parm, args) + ast = f.impl; continue + return f(args) + otherwise: return ast PRINT(exp): return printer.pr_str(exp) diff --git a/process/step7_quote.txt b/process/step7_quote.txt index e84f77af1c..fb103551fc 100644 --- a/process/step7_quote.txt +++ b/process/step7_quote.txt @@ -6,35 +6,36 @@ READ(str): return reader.read_str(str) quasiquote(ast): return ... // quasiquote EVAL(ast, env): - loop: - if env.get('DEBUG-EVAL) then prn('EVAL ast) - match ast: - 'key: return env.get(key) - OR raise "'"+k+"' not found" - [form1 ..]: return [EVAL(form1, env) ..] - {key1 value1 ..}: return {key1 EVAL(value1, env) ..} - ('def! 'key value): return env.set(key, EVAL(value, env)) - ('let* (k1 v1 ..) form): env = new Env(env) - env.set(k1, EVAL(v1, env)) - .. - ast = form - ('let* [k1 v1 ..] form): // idem - ('do form1 .. last): EVAL(form1, env) - .. - ast = last - ('if tst yes no): if EVAL(tst, env) then ast = yes else ast = no - ('if tst yes): if EVAL(tst, env) then ast = yes else return nil - ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) - ('fn* ['key1 ..] impl): // idem - ('quote form): return form - ('quasiquote form): ast = quasiquote(form) - (callable arg1 ..): f = EVAL(callable, env) - args = [EVAL(arg1, env) ..] - if malfn?(f): env = new Env(f.env, f.parm, args) - ast = f.impl - continue - return apply(f, args) - otherwise: return ast + loop: + if env.get('DEBUG-EVAL) exists and not in nil, false then prn('EVAL ast) + match ast: + 'key: return env.get(key) or raise "'{key}' not found" + [form1 ..]: return [EVAL(form1, env) ..] + {key1 value1 ..}: return {key1 EVAL(value1, env) ..} + ('def! 'key value): return env.set(key, EVAL(value, env)) + ('let* (k1 v1 ..) form): env = new Env(env) + env.set(k1, EVAL(v1, env)) + .. + ast = form; continue + ('let* [k1 v1 ..] form): // idem + ('do form1 .. last): EVAL(form1, env) + .. + ast = last; continue + ('if cond yes no): if EVAL(cond, env) in nil, false + then ast = yes; continue + else ast = no; continue + ('if cond yes): // idem with return nil in the else branch + ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) + ('fn* ['key1 ..] impl): // idem + ('quote form): return form + ('quasiquote form): ast = quasiquote(form); continue + (callable arg1 ..): f = EVAL(callable, env) + args = [EVAL(arg1, env) ..] + if malfn?(f) then: + env = new Env(f.env, f.parm, args) + ast = f.impl; continue + return f(args) + otherwise: return ast PRINT(exp): return printer.pr_str(exp) diff --git a/process/step8_macros.txt b/process/step8_macros.txt index ef7a75c8e0..87cabd7e62 100644 --- a/process/step8_macros.txt +++ b/process/step8_macros.txt @@ -6,38 +6,39 @@ READ(str): return reader.read_str(str) quasiquote(ast): return ... // quasiquote EVAL(ast, env): - loop: - if env.get('DEBUG-EVAL) then prn('EVAL ast) - match ast: - 'key: return env.get(key) - OR raise "'"+k+"' not found" - [form1 ..]: return [EVAL(form1, env) ..] - {key1 value1 ..}: return {key1 EVAL(value1, env) ..} - ('def! 'key value): return env.set(key, EVAL(value, env)) - ('let* (k1 v1 ..) form): env = new Env(env) - env.set(k1, EVAL(v1, env)) - .. - ast = form - ('let* [k1 v1 ..] form): // idem - ('do form1 .. last): EVAL(form1, env) - .. - ast = last - ('if tst yes no): if EVAL(tst, env) then ast = yes else ast = no - ('if tst yes): if EVAL(tst, env) then ast = yes else return nil - ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) - ('fn* ['key1 ..] impl): // idem - ('quote form): return form - ('quasiquote form): ast = quasiquote(form) - ('defmacro! 'key value): return env.set(key, as_macro(EVAL(value, env))) - (callable arg1 ..): f = EVAL(callable, env) - if macro?(f): ast = apply(f, [arg1 ..]) - continue - args = [EVAL(arg1, env) ..] - if malfn?(f): env = new Env(f.env, f.parm, args) - ast = f.impl - continue - return apply(f, args) - otherwise: return ast + loop: + if env.get('DEBUG-EVAL) exists and not in nil, false then prn('EVAL ast) + match ast: + 'key: return env.get(key) or raise "'{key}' not found" + [form1 ..]: return [EVAL(form1, env) ..] + {key1 value1 ..}: return {key1 EVAL(value1, env) ..} + ('def! 'key value): return env.set(key, EVAL(value, env)) + ('let* (k1 v1 ..) form): env = new Env(env) + env.set(k1, EVAL(v1, env)) + .. + ast = form; continue + ('let* [k1 v1 ..] form): // idem + ('do form1 .. last): EVAL(form1, env) + .. + ast = last; continue + ('if cond yes no): if EVAL(cond, env) in nil, false + then ast = yes; continue + else ast = no; continue + ('if cond yes): // idem with return nil in the else branch + ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) + ('fn* ['key1 ..] impl): // idem + ('quote form): return form + ('quasiquote form): ast = quasiquote(form); continue + ('defmacro! 'key value): return env.set(key, as_macro(EVAL(value, env))) + (callable arg1 ..): f = EVAL(callable, env) + if macro?(f) then: + ast = f(arg1, ..); continue + args = [EVAL(arg1, env) ..] + if malfn?(f) then: + env = new Env(f.env, f.parm, args) + ast = f.impl; continue + return f(args) + otherwise: return ast PRINT(exp): return printer.pr_str(exp) diff --git a/process/step9_try.txt b/process/step9_try.txt index b2054c7d4a..a27c785a36 100644 --- a/process/step9_try.txt +++ b/process/step9_try.txt @@ -6,43 +6,45 @@ READ(str): return reader.read_str(str) quasiquote(ast): return ... // quasiquote EVAL(ast, env): - loop: - if env.get('DEBUG-EVAL) then prn('EVAL ast) - match ast: - 'key: return env.get(key) - OR raise "'"+k+"' not found" - [form1 ..]: return [EVAL(form1, env) ..] - {key1 value1 ..}: return {key1 EVAL(value1, env) ..} - ('def! 'key value): return env.set(key, EVAL(value, env)) - ('let* (k1 v1 ..) form): env = new Env(env) - env.set(k1, EVAL(v1, env)) - .. - ast = form - ('let* [k1 v1 ..] form): // idem - ('do form1 .. last): EVAL(form1, env) - .. - ast = last - ('if tst yes no): if EVAL(tst, env) then ast = yes else ast = no - ('if tst yes): if EVAL(tst, env) then ast = yes else return nil - ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) - ('fn* ['key1 ..] impl): // idem - ('quote form): return form - ('quasiquote form): ast = quasiquote(form) - ('defmacro! 'key value): return env.set(key, as_macro(EVAL(value, env))) - ('try* f ('catch* 'k h)): try returning EVAL(f, env) - if native or malval exception then: - env = new Env(env, [k], [exception]) - ast = h - ('try* f): ast = f - (callable arg1 ..): f = EVAL(callable, env) - if macro?(f): ast = apply(f, [arg1 ..]) - continue - args = [EVAL(arg1, env) ..] - if malfn?(f): env = new Env(f.env, f.parm, args) - ast = f.impl - continue - return apply(f, args) - otherwise: return ast + loop: + if env.get('DEBUG-EVAL) exists and not in nil, false then prn('EVAL ast) + match ast: + 'key: return env.get(key) or raise "'{key}' not found" + [form1 ..]: return [EVAL(form1, env) ..] + {key1 value1 ..}: return {key1 EVAL(value1, env) ..} + ('def! 'key value): return env.set(key, EVAL(value, env)) + ('let* (k1 v1 ..) form): env = new Env(env) + env.set(k1, EVAL(v1, env)) + .. + ast = form; continue + ('let* [k1 v1 ..] form): // idem + ('do form1 .. last): EVAL(form1, env) + .. + ast = last; continue + ('if cond yes no): if EVAL(cond, env) in nil, false + then ast = yes; continue + else ast = no; continue + ('if cond yes): // idem with return nil in the else branch + ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) + ('fn* ['key1 ..] impl): // idem + ('quote form): return form + ('quasiquote form): ast = quasiquote(form); continue + ('defmacro! 'key value): return env.set(key, as_macro(EVAL(value, env))) + ('try* f ('catch* 'k h)): try returning EVAL(f, env) + if native or malval exception then: + env = new Env(env) + env.set(k, exception) + ast = h; continue + ('try* form): ast = form; continue + (callable arg1 ..): f = EVAL(callable, env) + if macro?(f) then: + ast = f(arg1, ..); continue + args = [EVAL(arg1, env) ..] + if malfn?(f) then: + env = new Env(f.env, f.parm, args) + ast = f.impl; continue + return f(args) + otherwise: return ast PRINT(exp): return printer.pr_str(exp) diff --git a/process/stepA_mal.txt b/process/stepA_mal.txt index e5e184cdfd..432bdef706 100644 --- a/process/stepA_mal.txt +++ b/process/stepA_mal.txt @@ -6,43 +6,45 @@ READ(str): return reader.read_str(str) quasiquote(ast): return ... // quasiquote EVAL(ast, env): - loop: - if env.get('DEBUG-EVAL) then prn('EVAL ast) - match ast: - 'key: return env.get(key) - OR raise "'"+k+"' not found" - [form1 ..]: return [EVAL(form1, env) ..] - {key1 value1 ..}: return {key1 EVAL(value1, env) ..} - ('def! 'key value): return env.set(key, EVAL(value, env)) - ('let* (k1 v1 ..) form): env = new Env(env) - env.set(k1, EVAL(v1, env)) - .. - ast = form - ('let* [k1 v1 ..] form): // idem - ('do form1 .. last): EVAL(form1, env) - .. - ast = last - ('if tst yes no): if EVAL(tst, env) then ast = yes else ast = no - ('if tst yes): if EVAL(tst, env) then ast = yes else return nil - ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) - ('fn* ['key1 ..] impl): // idem - ('quote form): return form - ('quasiquote form): ast = quasiquote(form) - ('defmacro! 'key value): return env.set(key, as_macro(EVAL(value, env))) - ('try* f ('catch* 'k h)): try returning EVAL(f, env) - if native or malval exception then: - env = new Env(env, [k], [exception]) - ast = h - ('try* f): ast = f - (callable arg1 ..): f = EVAL(callable, env) - if macro?(f): ast = apply(f, [arg1 ..]) - continue - args = [EVAL(arg1, env) ..] - if malfn?(f): env = new Env(f.env, f.parm, args) - ast = f.impl - continue - return apply(f, args) - otherwise: return ast + loop: + if env.get('DEBUG-EVAL) exists and not in nil, false then prn('EVAL ast) + match ast: + 'key: return env.get(key) or raise "'{key}' not found" + [form1 ..]: return [EVAL(form1, env) ..] + {key1 value1 ..}: return {key1 EVAL(value1, env) ..} + ('def! 'key value): return env.set(key, EVAL(value, env)) + ('let* (k1 v1 ..) form): env = new Env(env) + env.set(k1, EVAL(v1, env)) + .. + ast = form; continue + ('let* [k1 v1 ..] form): // idem + ('do form1 .. last): EVAL(form1, env) + .. + ast = last; continue + ('if cond yes no): if EVAL(cond, env) in nil, false + then ast = yes; continue + else ast = no; continue + ('if cond yes): // idem with return nil in the else branch + ('fn* ('key1 ..) impl): return new MalFn(env, impl, parm=[key1 ..]) + ('fn* ['key1 ..] impl): // idem + ('quote form): return form + ('quasiquote form): ast = quasiquote(form); continue + ('defmacro! 'key value): return env.set(key, as_macro(EVAL(value, env))) + ('try* f ('catch* 'k h)): try returning EVAL(f, env) + if native or malval exception then: + env = new Env(env) + env.set(k, exception) + ast = h; continue + ('try* form): ast = form; continue + (callable arg1 ..): f = EVAL(callable, env) + if macro?(f) then: + ast = f(arg1, ..); continue + args = [EVAL(arg1, env) ..] + if malfn?(f) then: + env = new Env(f.env, f.parm, args) + ast = f.impl; continue + return f(args) + otherwise: return ast PRINT(exp): return printer.pr_str(exp)