From 57521d476ff59f758a896ebb120cfde0d4be2767 Mon Sep 17 00:00:00 2001 From: Nicolas Boulenguez Date: Fri, 11 Oct 2024 00:34:22 +0200 Subject: [PATCH] io: merge eval_ast into eval, add DEBUG-EVAL --- impls/io/step2_eval.io | 19 ++++++------ impls/io/step3_env.io | 24 ++++++++++------ impls/io/step4_if_fn_do.io | 24 ++++++++++------ impls/io/step5_tco.io | 26 +++++++++++------ impls/io/step6_file.io | 26 +++++++++++------ impls/io/step7_quote.io | 28 +++++++++++------- impls/io/step8_macros.io | 59 +++++++++++++++----------------------- impls/io/step9_try.io | 57 ++++++++++++++---------------------- impls/io/stepA_mal.io | 57 ++++++++++++++---------------------- 9 files changed, 161 insertions(+), 159 deletions(-) diff --git a/impls/io/step2_eval.io b/impls/io/step2_eval.io index fd85f1ce12..7f039e06dd 100644 --- a/impls/io/step2_eval.io +++ b/impls/io/step2_eval.io @@ -4,22 +4,25 @@ MalReader READ := method(str, MalReader read_str(str)) eval_ast := method(ast, env, + MalList with(ast map(a, EVAL(a, env)))) + +EVAL := method(ast, env, + + // "EVAL: " .. PRINT(ast) println + (ast type) switch( - "MalSymbol", env at(ast val) ifNil(Exception raise("'" .. (ast val) "' not found")), - "MalList", MalList with(ast map(a, EVAL(a, env))), - "MalVector", MalVector with(ast map(a, EVAL(a, env))), + "MalSymbol", return(env at(ast val) ifNil(Exception raise("'" .. (ast val) "' not found"))), + "MalList",, + "MalVector", return(MalVector with(ast map(a, EVAL(a, env)))), "MalMap", m := MalMap clone ast foreach(k, v, m atPut(k, EVAL(v, env)) ) - m, - ast + return(m), + return(ast) ) -) -EVAL := method(ast, env, - if(ast type != "MalList", return(eval_ast(ast, env))) if(ast isEmpty, return ast) el := eval_ast(ast, env) f := el at(0) diff --git a/impls/io/step3_env.io b/impls/io/step3_env.io index 87d1195182..b11f3287a2 100644 --- a/impls/io/step3_env.io +++ b/impls/io/step3_env.io @@ -4,22 +4,30 @@ MalReader READ := method(str, MalReader read_str(str)) eval_ast := method(ast, env, + MalList with(ast map(a, EVAL(a, env)))) + +debugEvalSymbol := MalSymbol with("DEBUG-EVAL") + +EVAL := method(ast, env, + + debugEvalEnv := env find(debugEvalSymbol) + if(debugEvalEnv isNil,, + if(debugEvalEnv.get(debugEvalSymbol), + ("EVAL: " .. PRINT(ast)) println)) + (ast type) switch( - "MalSymbol", env get(ast), - "MalList", MalList with(ast map(a, EVAL(a, env))), - "MalVector", MalVector with(ast map(a, EVAL(a, env))), + "MalSymbol", return(env get(ast)), + "MalList",, + "MalVector", return(MalVector with(ast map(a, EVAL(a, env)))), "MalMap", m := MalMap clone ast foreach(k, v, m atPut(k, EVAL(v, env)) ) - m, - ast + return(m), + return(ast) ) -) -EVAL := method(ast, env, - if(ast type != "MalList", return(eval_ast(ast, env))) if(ast isEmpty, return ast) if(ast at(0) type == "MalSymbol", ast at(0) val switch( diff --git a/impls/io/step4_if_fn_do.io b/impls/io/step4_if_fn_do.io index 885f1753a2..1ddc9430be 100644 --- a/impls/io/step4_if_fn_do.io +++ b/impls/io/step4_if_fn_do.io @@ -4,22 +4,30 @@ MalReader READ := method(str, MalReader read_str(str)) eval_ast := method(ast, env, + MalList with(ast map(a, EVAL(a, env)))) + +debugEvalSymbol := MalSymbol with("DEBUG-EVAL") + +EVAL := method(ast, env, + + debugEvalEnv := env find(debugEvalSymbol) + if(debugEvalEnv isNil,, + if(debugEvalEnv.get(debugEvalSymbol), + ("EVAL: " .. PRINT(ast)) println)) + (ast type) switch( - "MalSymbol", env get(ast), - "MalList", MalList with(ast map(a, EVAL(a, env))), - "MalVector", MalVector with(ast map(a, EVAL(a, env))), + "MalSymbol", return(env get(ast)), + "MalList",, + "MalVector", return(MalVector with(ast map(a, EVAL(a, env)))), "MalMap", m := MalMap clone ast foreach(k, v, m atPut(k, EVAL(v, env)) ) - m, - ast + return(m), + return(ast) ) -) -EVAL := method(ast, env, - if(ast type != "MalList", return(eval_ast(ast, env))) if(ast isEmpty, return ast) if(ast at(0) type == "MalSymbol", ast at(0) val switch( diff --git a/impls/io/step5_tco.io b/impls/io/step5_tco.io index db5ba26ae9..0904b2e4e7 100644 --- a/impls/io/step5_tco.io +++ b/impls/io/step5_tco.io @@ -4,23 +4,31 @@ MalReader READ := method(str, MalReader read_str(str)) eval_ast := method(ast, env, + MalList with(ast map(a, EVAL(a, env)))) + +debugEvalSymbol := MalSymbol with("DEBUG-EVAL") + +EVAL := method(ast, env, + loop( + + debugEvalEnv := env find(debugEvalSymbol) + if(debugEvalEnv isNil,, + if(debugEvalEnv.get(debugEvalSymbol), + ("EVAL: " .. PRINT(ast)) println)) + (ast type) switch( - "MalSymbol", env get(ast), - "MalList", MalList with(ast map(a, EVAL(a, env))), - "MalVector", MalVector with(ast map(a, EVAL(a, env))), + "MalSymbol", return(env get(ast)), + "MalList",, + "MalVector", return(MalVector with(ast map(a, EVAL(a, env)))), "MalMap", m := MalMap clone ast foreach(k, v, m atPut(k, EVAL(v, env)) ) - m, - ast + return(m), + return(ast) ) -) -EVAL := method(ast, env, - loop( - if(ast type != "MalList", return(eval_ast(ast, env))) if(ast isEmpty, return ast) if(ast at(0) type == "MalSymbol", ast at(0) val switch( diff --git a/impls/io/step6_file.io b/impls/io/step6_file.io index 2f70faee15..de2ef103bc 100644 --- a/impls/io/step6_file.io +++ b/impls/io/step6_file.io @@ -4,23 +4,31 @@ MalReader READ := method(str, MalReader read_str(str)) eval_ast := method(ast, env, + MalList with(ast map(a, EVAL(a, env)))) + +debugEvalSymbol := MalSymbol with("DEBUG-EVAL") + +EVAL := method(ast, env, + loop( + + debugEvalEnv := env find(debugEvalSymbol) + if(debugEvalEnv isNil,, + if(debugEvalEnv.get(debugEvalSymbol), + ("EVAL: " .. PRINT(ast)) println)) + (ast type) switch( - "MalSymbol", env get(ast), - "MalList", MalList with(ast map(a, EVAL(a, env))), - "MalVector", MalVector with(ast map(a, EVAL(a, env))), + "MalSymbol", return(env get(ast)), + "MalList",, + "MalVector", return(MalVector with(ast map(a, EVAL(a, env)))), "MalMap", m := MalMap clone ast foreach(k, v, m atPut(k, EVAL(v, env)) ) - m, - ast + return(m), + return(ast) ) -) -EVAL := method(ast, env, - loop( - if(ast type != "MalList", return(eval_ast(ast, env))) if(ast isEmpty, return ast) if(ast at(0) type == "MalSymbol", ast at(0) val switch( diff --git a/impls/io/step7_quote.io b/impls/io/step7_quote.io index 1ac52462fd..ee92363396 100644 --- a/impls/io/step7_quote.io +++ b/impls/io/step7_quote.io @@ -21,23 +21,31 @@ quasiquote := method(ast, ast)) eval_ast := method(ast, env, + MalList with(ast map(a, EVAL(a, env)))) + +debugEvalSymbol := MalSymbol with("DEBUG-EVAL") + +EVAL := method(ast, env, + loop( + + debugEvalEnv := env find(debugEvalSymbol) + if(debugEvalEnv isNil,, + if(debugEvalEnv.get(debugEvalSymbol), + ("EVAL: " .. PRINT(ast)) println)) + (ast type) switch( - "MalSymbol", env get(ast), - "MalList", MalList with(ast map(a, EVAL(a, env))), - "MalVector", MalVector with(ast map(a, EVAL(a, env))), + "MalSymbol", return(env get(ast)), + "MalList",, + "MalVector", return(MalVector with(ast map(a, EVAL(a, env)))), "MalMap", m := MalMap clone ast foreach(k, v, m atPut(k, EVAL(v, env)) ) - m, - ast + return(m), + return(ast) ) -) -EVAL := method(ast, env, - loop( - if(ast type != "MalList", return(eval_ast(ast, env))) if(ast isEmpty, return ast) if(ast at(0) type == "MalSymbol", ast at(0) val switch( @@ -66,8 +74,6 @@ EVAL := method(ast, env, continue, // TCO "quote", return(ast at(1)), - "quasiquoteexpand", - return quasiquote(ast at(1)), "quasiquote", ast = quasiquote(ast at(1)) continue // TCO diff --git a/impls/io/step8_macros.io b/impls/io/step8_macros.io index 102b1e31f1..e944d7a500 100644 --- a/impls/io/step8_macros.io +++ b/impls/io/step8_macros.io @@ -20,44 +20,32 @@ quasiquote := method(ast, qq_foldr(ast)), ast)) -isMacroCall := method(ast, env, - if(ast type != "MalList", return false) - a0 := ast first - if(a0 type != "MalSymbol", return false) - if(env find(a0) isNil, return false) - f := env get(a0) - (f type == "MalFunc") and (f isMacro) -) +eval_ast := method(ast, env, + MalList with(ast map(a, EVAL(a, env)))) -macroexpand := method(ast, env, - while(isMacroCall(ast, env), - macro := env get(ast at(0)) - ast = macro blk call(ast rest) - ) - ast -) +debugEvalSymbol := MalSymbol with("DEBUG-EVAL") + +EVAL := method(ast, env, + loop( + + debugEvalEnv := env find(debugEvalSymbol) + if(debugEvalEnv isNil,, + if(debugEvalEnv.get(debugEvalSymbol), + ("EVAL: " .. PRINT(ast)) println)) -eval_ast := method(ast, env, (ast type) switch( - "MalSymbol", env get(ast), - "MalList", MalList with(ast map(a, EVAL(a, env))), - "MalVector", MalVector with(ast map(a, EVAL(a, env))), + "MalSymbol", return(env get(ast)), + "MalList",, + "MalVector", return(MalVector with(ast map(a, EVAL(a, env)))), "MalMap", m := MalMap clone ast foreach(k, v, m atPut(k, EVAL(v, env)) ) - m, - ast + return(m), + return(ast) ) -) - -EVAL := method(ast, env, - loop( - if(ast type != "MalList", return(eval_ast(ast, env))) - ast = macroexpand(ast, env) - if(ast type != "MalList", return(eval_ast(ast, env))) if(ast isEmpty, return ast) if(ast at(0) type == "MalSymbol", @@ -87,29 +75,28 @@ EVAL := method(ast, env, continue, // TCO "quote", return(ast at(1)), - "quasiquoteexpand", - return quasiquote(ast at(1)), "quasiquote", ast = quasiquote(ast at(1)) continue, // TCO "defmacro!", - return(env set(ast at(1), EVAL(ast at(2), env) clone setIsMacro(true))), - "macroexpand", - return(macroexpand(ast at(1), env)) + return(env set(ast at(1), EVAL(ast at(2), env) clone setIsMacro(true))) ) ) // Apply - el := eval_ast(ast, env) - f := el at(0) - args := el rest + f := EVAL(ast at(0), env) + raw_args := ast rest f type switch( "Block", + args := eval_ast(raw_args, env) return(f call(args)), "MalFunc", + if(f isMacro, ast = f blk call(raw_args), + args := eval_ast(raw_args, env) ast = f ast env = Env with(f env, f params, args) continue, // TCO + ) Exception raise("Unknown function type") ) ) diff --git a/impls/io/step9_try.io b/impls/io/step9_try.io index b2d966e413..1a2cf490e0 100644 --- a/impls/io/step9_try.io +++ b/impls/io/step9_try.io @@ -20,44 +20,32 @@ quasiquote := method(ast, qq_foldr(ast)), ast)) -isMacroCall := method(ast, env, - if(ast type != "MalList", return false) - a0 := ast first - if(a0 type != "MalSymbol", return false) - if(env find(a0) isNil, return false) - f := env get(a0) - (f type == "MalFunc") and (f isMacro) -) +eval_ast := method(ast, env, + MalList with(ast map(a, EVAL(a, env)))) -macroexpand := method(ast, env, - while(isMacroCall(ast, env), - macro := env get(ast at(0)) - ast = macro blk call(ast rest) - ) - ast -) +debugEvalSymbol := MalSymbol with("DEBUG-EVAL") + +EVAL := method(ast, env, + loop( + + debugEvalEnv := env find(debugEvalSymbol) + if(debugEvalEnv isNil,, + if(debugEvalEnv.get(debugEvalSymbol), + ("EVAL: " .. PRINT(ast)) println)) -eval_ast := method(ast, env, (ast type) switch( - "MalSymbol", env get(ast), - "MalList", MalList with(ast map(a, EVAL(a, env))), - "MalVector", MalVector with(ast map(a, EVAL(a, env))), + "MalSymbol", return(env get(ast)), + "MalList",, + "MalVector", return(MalVector with(ast map(a, EVAL(a, env)))), "MalMap", m := MalMap clone ast foreach(k, v, m atPut(k, EVAL(v, env)) ) - m, - ast + return(m), + return(ast) ) -) - -EVAL := method(ast, env, - loop( - if(ast type != "MalList", return(eval_ast(ast, env))) - ast = macroexpand(ast, env) - if(ast type != "MalList", return(eval_ast(ast, env))) if(ast isEmpty, return ast) if(ast at(0) type == "MalSymbol", @@ -87,15 +75,11 @@ EVAL := method(ast, env, continue, // TCO "quote", return(ast at(1)), - "quasiquoteexpand", - return quasiquote(ast at(1)), "quasiquote", ast = quasiquote(ast at(1)) continue, // TCO "defmacro!", return(env set(ast at(1), EVAL(ast at(2), env) clone setIsMacro(true))), - "macroexpand", - return(macroexpand(ast at(1), env)), "try*", if(ast at(2) == nil, return(EVAL(ast at(1), env))) e := try(result := EVAL(ast at(1), env)) @@ -111,16 +95,19 @@ EVAL := method(ast, env, ) // Apply - el := eval_ast(ast, env) - f := el at(0) - args := el rest + f := EVAL(ast at(0), env) + raw_args := ast rest f type switch( "Block", + args := eval_ast(raw_args, env) return(f call(args)), "MalFunc", + if(f isMacro, ast = f blk call(raw_args), + args := eval_ast(raw_args, env) ast = f ast env = Env with(f env, f params, args) continue, // TCO + ) Exception raise("Unknown function type") ) ) diff --git a/impls/io/stepA_mal.io b/impls/io/stepA_mal.io index ee0fbe4fa7..18fada7f36 100644 --- a/impls/io/stepA_mal.io +++ b/impls/io/stepA_mal.io @@ -20,44 +20,32 @@ quasiquote := method(ast, qq_foldr(ast)), ast)) -isMacroCall := method(ast, env, - if(ast type != "MalList", return false) - a0 := ast first - if(a0 type != "MalSymbol", return false) - if(env find(a0) isNil, return false) - f := env get(a0) - (f type == "MalFunc") and (f isMacro) -) +eval_ast := method(ast, env, + MalList with(ast map(a, EVAL(a, env)))) -macroexpand := method(ast, env, - while(isMacroCall(ast, env), - macro := env get(ast at(0)) - ast = macro blk call(ast rest) - ) - ast -) +debugEvalSymbol := MalSymbol with("DEBUG-EVAL") + +EVAL := method(ast, env, + loop( + + debugEvalEnv := env find(debugEvalSymbol) + if(debugEvalEnv isNil,, + if(debugEvalEnv.get(debugEvalSymbol), + ("EVAL: " .. PRINT(ast)) println)) -eval_ast := method(ast, env, (ast type) switch( - "MalSymbol", env get(ast), - "MalList", MalList with(ast map(a, EVAL(a, env))), - "MalVector", MalVector with(ast map(a, EVAL(a, env))), + "MalSymbol", return(env get(ast)), + "MalList",, + "MalVector", return(MalVector with(ast map(a, EVAL(a, env)))), "MalMap", m := MalMap clone ast foreach(k, v, m atPut(k, EVAL(v, env)) ) - m, - ast + return(m), + return(ast) ) -) - -EVAL := method(ast, env, - loop( - if(ast type != "MalList", return(eval_ast(ast, env))) - ast = macroexpand(ast, env) - if(ast type != "MalList", return(eval_ast(ast, env))) if(ast isEmpty, return ast) if(ast at(0) type == "MalSymbol", @@ -87,15 +75,11 @@ EVAL := method(ast, env, continue, // TCO "quote", return(ast at(1)), - "quasiquoteexpand", - return quasiquote(ast at(1)), "quasiquote", ast = quasiquote(ast at(1)) continue, // TCO "defmacro!", return(env set(ast at(1), EVAL(ast at(2), env) clone setIsMacro(true))), - "macroexpand", - return(macroexpand(ast at(1), env)), "try*", if(ast at(2) == nil, return(EVAL(ast at(1), env))) e := try(result := EVAL(ast at(1), env)) @@ -111,16 +95,19 @@ EVAL := method(ast, env, ) // Apply - el := eval_ast(ast, env) - f := el at(0) - args := el rest + f := EVAL(ast at(0), env) + raw_args := ast rest f type switch( "Block", + args := eval_ast(raw_args, env) return(f call(args)), "MalFunc", + if(f isMacro, ast = f blk call(raw_args), + args := eval_ast(raw_args, env) ast = f ast env = Env with(f env, f params, args) continue, // TCO + ) Exception raise("Unknown function type") ) )