Skip to content

Commit

Permalink
nim: mark several variables as immutable
Browse files Browse the repository at this point in the history
Also apply 2dedaa3 to step0
  • Loading branch information
asarhaddon committed Aug 28, 2024
1 parent 2765085 commit 900ab5c
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 58 deletions.
2 changes: 1 addition & 1 deletion impls/nim/env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions impls/nim/step0_repl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
6 changes: 3 additions & 3 deletions impls/nim/step3_env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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}):
Expand Down Expand Up @@ -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):
Expand All @@ -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(`-`))
Expand Down
10 changes: 4 additions & 6 deletions impls/nim/step4_if_fn_do.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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}):
Expand Down Expand Up @@ -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):
Expand All @@ -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])
Expand All @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions impls/nim/step5_tco.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions impls/nim/step6_file.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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)))


Expand Down
12 changes: 5 additions & 7 deletions impls/nim/step7_quote.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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)))


Expand Down
18 changes: 8 additions & 10 deletions impls/nim/step8_macros.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)))


Expand Down
18 changes: 8 additions & 10 deletions impls/nim/step9_try.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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)))


Expand Down
18 changes: 8 additions & 10 deletions impls/nim/stepA_mal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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)))


Expand Down

0 comments on commit 900ab5c

Please sign in to comment.