SIGN IN SIGN UP
2015-10-20 15:02:00 -04:00
source readline.vim
source types.vim
source reader.vim
source printer.vim
source env.vim
source core.vim
function READ(str)
return ReadStr(a:str)
endfunction
function EVAL(ast, env)
let ast = a:ast
let env = a:env
while 1
Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
2022-01-10 00:15:40 +01:00
let dbgeval = env.get("DEBUG-EVAL")
if !(empty(dbgeval) || FalseQ(dbgeval) || NilQ(dbgeval))
call PrintLn("EVAL: " . PrStr(ast, 1))
endif
if SymbolQ(ast)
let varname = ast.val
let val = env.get(varname)
if empty(val)
throw "'" . varname . "' not found"
endif
return val
elseif VectorQ(ast)
return VectorNew(map(copy(ast.val), {_, e -> EVAL(e, env)}))
elseif HashQ(ast)
let ret = {}
for [k,v] in items(ast.val)
let newval = EVAL(v, env)
let ret[k] = newval
endfor
return HashNew(ret)
endif
2015-10-20 15:02:00 -04:00
if !ListQ(ast)
Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
2022-01-10 00:15:40 +01:00
return ast
2015-10-20 15:02:00 -04:00
end
if EmptyQ(ast)
return ast
endif
2015-10-20 15:02:00 -04:00
let first = ListFirst(ast)
2016-10-26 06:46:20 +00:00
let first_symbol = SymbolQ(first) ? first.val : ""
2015-10-20 15:02:00 -04:00
if first_symbol == "def!"
2016-10-26 06:46:20 +00:00
let a1 = ast.val[1]
let a2 = ast.val[2]
let ret = env.set(a1.val, EVAL(a2, env))
2015-10-20 15:02:00 -04:00
return ret
elseif first_symbol == "let*"
2016-10-26 06:46:20 +00:00
let a1 = ast.val[1]
let a2 = ast.val[2]
2015-10-20 15:02:00 -04:00
let env = NewEnv(env)
2016-10-26 06:46:20 +00:00
let let_binds = a1.val
2015-10-20 15:02:00 -04:00
let i = 0
while i < len(let_binds)
2016-10-26 06:46:20 +00:00
call env.set(let_binds[i].val, EVAL(let_binds[i+1], env))
2015-10-20 15:02:00 -04:00
let i = i + 2
endwhile
let ast = a2
" TCO
elseif first_symbol == "if"
2016-10-26 06:46:20 +00:00
let condvalue = EVAL(ast.val[1], env)
2015-10-20 15:02:00 -04:00
if FalseQ(condvalue) || NilQ(condvalue)
2016-10-26 06:46:20 +00:00
if len(ast.val) < 4
2015-10-20 15:02:00 -04:00
return g:MalNil
else
2016-10-26 06:46:20 +00:00
let ast = ast.val[3]
2015-10-20 15:02:00 -04:00
endif
else
2016-10-26 06:46:20 +00:00
let ast = ast.val[2]
2015-10-20 15:02:00 -04:00
endif
" TCO
elseif first_symbol == "do"
2016-10-26 06:46:20 +00:00
let astlist = ast.val
Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
2022-01-10 00:15:40 +01:00
for elt in astlist[1:-2]
let ignored = EVAL(elt, env)
endfor
2015-10-20 15:02:00 -04:00
let ast = astlist[-1]
" TCO
elseif first_symbol == "fn*"
let fn = NewFn(ListNth(ast, 2), env, ListNth(ast, 1))
return fn
elseif first_symbol == "eval"
let ast = EVAL(ListNth(ast, 1), env)
let env = env.root()
" TCO
else
" apply list
Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
2022-01-10 00:15:40 +01:00
let el = ListNew(map(copy(ast.val), {_, e -> EVAL(e, env)}))
2015-10-20 15:02:00 -04:00
let funcobj = ListFirst(el)
let args = ListRest(el)
if NativeFunctionQ(funcobj)
return NativeFuncInvoke(funcobj, args)
elseif FunctionQ(funcobj)
2016-10-26 06:46:20 +00:00
let fn = funcobj.val
2015-10-20 15:02:00 -04:00
let ast = fn.ast
let env = NewEnvWithBinds(fn.env, fn.params, args)
" TCO
else
throw "Not a function"
endif
endif
endwhile
endfunction
function PRINT(exp)
return PrStr(a:exp, 1)
endfunction
function RE(str, env)
return EVAL(READ(a:str), a:env)
endfunction
function REP(str, env)
return PRINT(EVAL(READ(a:str), a:env))
endfunction
function GetArgvList()
return ListNew(map(copy(argv()[1:]), {_, arg -> StringNew(arg)}))
2015-10-20 15:02:00 -04:00
endfunction
set maxfuncdepth=10000
let repl_env = NewEnv("")
for [k, v] in items(CoreNs)
call repl_env.set(k, v)
endfor
call repl_env.set("*ARGV*", GetArgvList())
call RE("(def! not (fn* (a) (if a false true)))", repl_env)
call RE("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))", repl_env)
2015-10-20 15:02:00 -04:00
if !empty(argv())
call RE('(load-file "' . argv(0) . '")', repl_env)
qall!
endif
while 1
let [eof, line] = Readline("user> ")
if eof
break
endif
if line == ""
continue
endif
try
call PrintLn(REP(line, repl_env))
catch
call PrintLn("Error: " . v:exception)
endtry
endwhile
qall!