From 900ab5c498e8f9e7172179dd0b313e0a82b38ff6 Mon Sep 17 00:00:00 2001 From: Nicolas Boulenguez Date: Wed, 28 Aug 2024 17:40:29 +0200 Subject: [PATCH] nim: mark several variables as immutable Also apply 2dedaa39192ceb4facc5445100803e7dc2558d28 to step0 --- impls/nim/env.nim | 2 +- impls/nim/step0_repl.nim | 2 ++ impls/nim/step3_env.nim | 6 +++--- impls/nim/step4_if_fn_do.nim | 10 ++++------ impls/nim/step5_tco.nim | 8 +++----- impls/nim/step6_file.nim | 10 ++++------ impls/nim/step7_quote.nim | 12 +++++------- impls/nim/step8_macros.nim | 18 ++++++++---------- impls/nim/step9_try.nim | 18 ++++++++---------- impls/nim/stepA_mal.nim | 18 ++++++++---------- 10 files changed, 46 insertions(+), 58 deletions(-) diff --git a/impls/nim/env.nim b/impls/nim/env.nim index 75f481c42b..79f210beb6 100644 --- a/impls/nim/env.nim +++ b/impls/nim/env.nim @@ -11,7 +11,7 @@ proc initEnv*(outer: Env = nil, binds, exprs: MalType = nilObj): Env = else: result.data[e.str] = exprs.list[i] -proc set*(e: var Env, key: string, value: MalType): MalType {.discardable.} = +proc set*(e: Env, key: string, value: MalType): MalType {.discardable.} = e.data[key] = value value diff --git a/impls/nim/step0_repl.nim b/impls/nim/step0_repl.nim index 6ae7d895a0..45c3127aba 100644 --- a/impls/nim/step0_repl.nim +++ b/impls/nim/step0_repl.nim @@ -7,5 +7,7 @@ proc eval(ast: string): string = ast proc print(exp: string): string = exp while true: + try: let line = readLineFromStdin("user> ") echo line.read.eval.print + except IOError: quit() diff --git a/impls/nim/step3_env.nim b/impls/nim/step3_env.nim index 1554d4d57b..0ada25543f 100644 --- a/impls/nim/step3_env.nim +++ b/impls/nim/step3_env.nim @@ -2,7 +2,7 @@ import rdstdin, tables, sequtils, types, reader, printer, env proc read(str: string): MalType = str.read_str -proc eval(ast: MalType, env: var Env): MalType = +proc eval(ast: MalType, env: Env): MalType = let dbgeval = env.get("DEBUG-EVAL") if not (dbgeval.isNil or dbgeval.kind in {Nil, False}): @@ -30,7 +30,7 @@ proc eval(ast: MalType, env: var Env): MalType = of "def!": result = env.set(a1.str, a2.eval(env)) of "let*": - var let_env = initEnv(env) + let let_env = initEnv(env) case a1.kind of List, Vector: for i in countup(0, a1.list.high, 2): @@ -48,7 +48,7 @@ proc print(exp: MalType): string = exp.pr_str template wrapNumberFun(op): untyped = fun proc(xs: varargs[MalType]): MalType = number op(xs[0].number, xs[1].number) -var repl_env = initEnv() +let repl_env = initEnv() repl_env.set("+", wrapNumberFun(`+`)) repl_env.set("-", wrapNumberFun(`-`)) diff --git a/impls/nim/step4_if_fn_do.nim b/impls/nim/step4_if_fn_do.nim index 9986cbf013..72e7bd42ac 100644 --- a/impls/nim/step4_if_fn_do.nim +++ b/impls/nim/step4_if_fn_do.nim @@ -2,7 +2,7 @@ import rdstdin, tables, sequtils, types, reader, printer, env, core proc read(str: string): MalType = str.read_str -proc eval(ast: MalType, env: var Env): MalType = +proc eval(ast: MalType, env: Env): MalType = let dbgeval = env.get("DEBUG-EVAL") if not (dbgeval.isNil or dbgeval.kind in {Nil, False}): @@ -35,7 +35,7 @@ proc eval(ast: MalType, env: var Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var let_env = initEnv(env) + let let_env = initEnv(env) case a1.kind of List, Vector: for i in countup(0, a1.list.high, 2): @@ -62,10 +62,8 @@ proc eval(ast: MalType, env: var Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var env2 = env return fun(proc(a: varargs[MalType]): MalType = - var newEnv = initEnv(env2, a1, list(a)) - a2.eval(newEnv)) + a2.eval(initEnv(env, a1, list(a)))) let el = ast.list.mapIt(it.eval(env)) result = el[0].fun(el[1 .. ^1]) @@ -75,7 +73,7 @@ proc eval(ast: MalType, env: var Env): MalType = proc print(exp: MalType): string = exp.pr_str -var repl_env = initEnv() +let repl_env = initEnv() for k, v in ns.items: repl_env.set(k, v) diff --git a/impls/nim/step5_tco.nim b/impls/nim/step5_tco.nim index aae61c8d26..00f9bfa38d 100644 --- a/impls/nim/step5_tco.nim +++ b/impls/nim/step5_tco.nim @@ -44,7 +44,7 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var let_env = initEnv(env) + let let_env = initEnv(env) case a1.kind of List, Vector: for i in countup(0, a1.list.high, 2): @@ -80,10 +80,8 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var env2 = env let fn = proc(a: varargs[MalType]): MalType = - var newEnv = initEnv(env2, a1, list(a)) - a2.eval(newEnv) + a2.eval(initEnv(env, a1, list(a))) return malfun(fn, a2, a1, env) let f = eval(a0, env) @@ -97,7 +95,7 @@ proc eval(ast: MalType, env: Env): MalType = proc print(exp: MalType): string = exp.pr_str -var repl_env = initEnv() +let repl_env = initEnv() for k, v in ns.items: repl_env.set(k, v) diff --git a/impls/nim/step6_file.nim b/impls/nim/step6_file.nim index 9f228fc2ae..c54de4dc76 100644 --- a/impls/nim/step6_file.nim +++ b/impls/nim/step6_file.nim @@ -44,7 +44,7 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var let_env = initEnv(env) + let let_env = initEnv(env) case a1.kind of List, Vector: for i in countup(0, a1.list.high, 2): @@ -80,10 +80,8 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var env2 = env let fn = proc(a: varargs[MalType]): MalType = - var newEnv = initEnv(env2, a1, list(a)) - a2.eval(newEnv) + a2.eval(initEnv(env, a1, list(a))) return malfun(fn, a2, a1, env) let f = eval(a0, env) @@ -97,12 +95,12 @@ proc eval(ast: MalType, env: Env): MalType = proc print(exp: MalType): string = exp.pr_str -var repl_env = initEnv() +let repl_env = initEnv() for k, v in ns.items: repl_env.set(k, v) repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env))) -var ps = commandLineParams() +let ps = commandLineParams() repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str))) diff --git a/impls/nim/step7_quote.nim b/impls/nim/step7_quote.nim index c09ecfbba1..8ab5418d7f 100644 --- a/impls/nim/step7_quote.nim +++ b/impls/nim/step7_quote.nim @@ -7,7 +7,7 @@ proc quasiquote(ast: MalType): MalType proc quasiquote_loop(xs: seq[MalType]): MalType = result = list() for i in countdown(xs.high, 0): - var elt = xs[i] + let elt = xs[i] if elt.kind == List and 0 < elt.list.len and elt.list[0] == symbol "splice-unquote": result = list(symbol "concat", elt.list[1], result) else: @@ -72,7 +72,7 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var let_env = initEnv(env) + let let_env = initEnv(env) case a1.kind of List, Vector: for i in countup(0, a1.list.high, 2): @@ -115,10 +115,8 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var env2 = env let fn = proc(a: varargs[MalType]): MalType = - var newEnv = initEnv(env2, a1, list(a)) - a2.eval(newEnv) + a2.eval(initEnv(env, a1, list(a))) return malfun(fn, a2, a1, env) let f = eval(a0, env) @@ -132,12 +130,12 @@ proc eval(ast: MalType, env: Env): MalType = proc print(exp: MalType): string = exp.pr_str -var repl_env = initEnv() +let repl_env = initEnv() for k, v in ns.items: repl_env.set(k, v) repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env))) -var ps = commandLineParams() +let ps = commandLineParams() repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str))) diff --git a/impls/nim/step8_macros.nim b/impls/nim/step8_macros.nim index e890900a26..e379c2d82d 100644 --- a/impls/nim/step8_macros.nim +++ b/impls/nim/step8_macros.nim @@ -7,7 +7,7 @@ proc quasiquote(ast: MalType): MalType proc quasiquote_loop(xs: seq[MalType]): MalType = result = list() for i in countdown(xs.high, 0): - var elt = xs[i] + let elt = xs[i] if elt.kind == List and 0 < elt.list.len and elt.list[0] == symbol "splice-unquote": result = list(symbol "concat", elt.list[1], result) else: @@ -72,7 +72,7 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var let_env = initEnv(env) + let let_env = initEnv(env) case a1.kind of List, Vector: for i in countup(0, a1.list.high, 2): @@ -90,9 +90,9 @@ proc eval(ast: MalType, env: Env): MalType = continue # TCO of "defmacro!": - var fun = ast.list[2].eval(env) - fun = malfun(fun.malfun.fn, fun.malfun.ast, fun.malfun.params, fun.malfun.env, true) - return env.set(ast.list[1].str, fun) + let fun = ast.list[2].eval(env) + let mac = malfun(fun.malfun.fn, fun.malfun.ast, fun.malfun.params, fun.malfun.env, true) + return env.set(ast.list[1].str, mac) of "do": let last = ast.list.high @@ -120,10 +120,8 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var env2 = env let fn = proc(a: varargs[MalType]): MalType = - var newEnv = initEnv(env2, a1, list(a)) - a2.eval(newEnv) + a2.eval(initEnv(env, a1, list(a))) return malfun(fn, a2, a1, env) let f = eval(a0, env) @@ -140,12 +138,12 @@ proc eval(ast: MalType, env: Env): MalType = proc print(exp: MalType): string = exp.pr_str -var repl_env = initEnv() +let repl_env = initEnv() for k, v in ns.items: repl_env.set(k, v) repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env))) -var ps = commandLineParams() +let ps = commandLineParams() repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str))) diff --git a/impls/nim/step9_try.nim b/impls/nim/step9_try.nim index 1b25a11488..ca8effaac8 100644 --- a/impls/nim/step9_try.nim +++ b/impls/nim/step9_try.nim @@ -7,7 +7,7 @@ proc quasiquote(ast: MalType): MalType proc quasiquote_loop(xs: seq[MalType]): MalType = result = list() for i in countdown(xs.high, 0): - var elt = xs[i] + let elt = xs[i] if elt.kind == List and 0 < elt.list.len and elt.list[0] == symbol "splice-unquote": result = list(symbol "concat", elt.list[1], result) else: @@ -73,7 +73,7 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var let_env = initEnv(env) + let let_env = initEnv(env) case a1.kind of List, Vector: for i in countup(0, a1.list.high, 2): @@ -91,9 +91,9 @@ proc eval(ast: MalType, env: Env): MalType = continue # TCO of "defmacro!": - var fun = ast.list[2].eval(env) - fun = malfun(fun.malfun.fn, fun.malfun.ast, fun.malfun.params, fun.malfun.env, true) - return env.set(ast.list[1].str, fun) + let fun = ast.list[2].eval(env) + let mac = malfun(fun.malfun.fn, fun.malfun.ast, fun.malfun.params, fun.malfun.env, true) + return env.set(ast.list[1].str, mac) of "try*": let a1 = ast.list[1] @@ -140,10 +140,8 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var env2 = env let fn = proc(a: varargs[MalType]): MalType = - var newEnv = initEnv(env2, a1, list(a)) - a2.eval(newEnv) + a2.eval(initEnv(env, a1, list(a))) return malfun(fn, a2, a1, env) let f = eval(a0, env) @@ -160,12 +158,12 @@ proc eval(ast: MalType, env: Env): MalType = proc print(exp: MalType): string = exp.pr_str -var repl_env = initEnv() +let repl_env = initEnv() for k, v in ns.items: repl_env.set(k, v) repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env))) -var ps = commandLineParams() +let ps = commandLineParams() repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str))) diff --git a/impls/nim/stepA_mal.nim b/impls/nim/stepA_mal.nim index 031d598a49..526e30404b 100644 --- a/impls/nim/stepA_mal.nim +++ b/impls/nim/stepA_mal.nim @@ -7,7 +7,7 @@ proc quasiquote(ast: MalType): MalType proc quasiquote_loop(xs: seq[MalType]): MalType = result = list() for i in countdown(xs.high, 0): - var elt = xs[i] + let elt = xs[i] if elt.kind == List and 0 < elt.list.len and elt.list[0] == symbol "splice-unquote": result = list(symbol "concat", elt.list[1], result) else: @@ -73,7 +73,7 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var let_env = initEnv(env) + let let_env = initEnv(env) case a1.kind of List, Vector: for i in countup(0, a1.list.high, 2): @@ -91,9 +91,9 @@ proc eval(ast: MalType, env: Env): MalType = continue # TCO of "defmacro!": - var fun = ast.list[2].eval(env) - fun = malfun(fun.malfun.fn, fun.malfun.ast, fun.malfun.params, fun.malfun.env, true) - return env.set(ast.list[1].str, fun) + let fun = ast.list[2].eval(env) + let mac = malfun(fun.malfun.fn, fun.malfun.ast, fun.malfun.params, fun.malfun.env, true) + return env.set(ast.list[1].str, mac) of "try*": let a1 = ast.list[1] @@ -140,10 +140,8 @@ proc eval(ast: MalType, env: Env): MalType = let a1 = ast.list[1] a2 = ast.list[2] - var env2 = env let fn = proc(a: varargs[MalType]): MalType = - var newEnv = initEnv(env2, a1, list(a)) - a2.eval(newEnv) + a2.eval(initEnv(env, a1, list(a))) return malfun(fn, a2, a1, env) let f = eval(a0, env) @@ -160,12 +158,12 @@ proc eval(ast: MalType, env: Env): MalType = proc print(exp: MalType): string = exp.pr_str -var repl_env = initEnv() +let repl_env = initEnv() for k, v in ns.items: repl_env.set(k, v) repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env))) -var ps = commandLineParams() +let ps = commandLineParams() repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str)))