Skip to content

Commit

Permalink
Improve the formatting of process/step*.txt
Browse files Browse the repository at this point in the history
Indent the TCO less in order to reduce the line width and/or add
details, for example implicit `continue` statements.
  • Loading branch information
asarhaddon committed Dec 22, 2021
1 parent 3f11291 commit e8245a8
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 243 deletions.
19 changes: 9 additions & 10 deletions process/step2_eval.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
31 changes: 15 additions & 16 deletions process/step3_env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
55 changes: 27 additions & 28 deletions process/step4_if_fn_do.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
55 changes: 28 additions & 27 deletions process/step5_tco.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
55 changes: 28 additions & 27 deletions process/step6_file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
59 changes: 30 additions & 29 deletions process/step7_quote.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
65 changes: 33 additions & 32 deletions process/step8_macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading

0 comments on commit e8245a8

Please sign in to comment.