1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-05 18:08:55 +03:00
mal/impls/skew/step7_quote.sk
Nicolas Boulenguez 033892777a 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) .
2024-08-05 11:40:49 -05:00

161 lines
4.6 KiB
Plaintext

def READ(str string) MalVal {
return read_str(str)
}
def starts_with(lst MalList, sym string) bool {
return lst.count == 2 && lst[0].isSymbol(sym)
}
def qq_loop(elt MalVal, acc MalList) MalList {
if elt is MalList && starts_with(elt as MalList, "splice-unquote") {
return MalList.new([MalSymbol.new("concat"), (elt as MalList)[1], acc])
} else {
return MalList.new([MalSymbol.new("cons"), quasiquote(elt), acc])
}
}
def qq_foldr(xs List<MalVal>) MalList {
var acc = MalList.new([])
for i = xs.count-1; 0 <= i; i -= 1 {
acc = qq_loop(xs[i], acc)
}
return acc
}
def quasiquote(ast MalVal) MalVal {
if ast is MalVector {
return MalList.new([MalSymbol.new("vec"), qq_foldr((ast as MalVector).val)])
} else if ast is MalSymbol || ast is MalHashMap {
return MalList.new([MalSymbol.new("quote"), ast])
} else if !(ast is MalList) {
return ast
} else if starts_with(ast as MalList, "unquote") {
return (ast as MalList)[1]
} else {
return qq_foldr((ast as MalList).val)
}
}
def EVAL(ast MalVal, env Env) MalVal {
while true {
const dbgeval = env.get("DEBUG-EVAL")
if dbgeval != null && !(dbgeval is MalNil) && !(dbgeval is MalFalse) {
printLn("EVAL: " + PRINT(ast))
}
if ast is MalSymbol {
const key = (ast as MalSymbol).val
const val = env.get(key)
if val == null { throw MalError.new("'" + key + "' not found") }
return val
} else if ast is MalList {
# proceed further after this conditional
} else if ast is MalVector {
return MalVector.new((ast as MalVector).val.map<MalVal>(e => EVAL(e, env)))
} else if ast is MalHashMap {
var result List<MalVal> = []
(ast as MalHashMap).val.each((k string, v MalVal) => {
result.append(MalVal.fromHashKey(k))
result.append(EVAL(v, env))
})
return MalHashMap.fromList(result)
} else {
return ast
}
const astList = ast as MalList
if astList.isEmpty { return ast }
const a0sym = astList[0] as MalSymbol
if a0sym.val == "def!" {
return env.set(astList[1] as MalSymbol, EVAL(astList[2], env))
} else if a0sym.val == "let*" {
var letenv = Env.new(env)
const assigns = astList[1] as MalSequential
for i = 0; i < assigns.count; i += 2 {
letenv.set(assigns[i] as MalSymbol, EVAL(assigns[i + 1], letenv))
}
ast = astList[2]
env = letenv
continue # TCO
} else if a0sym.val == "quote" {
return astList[1]
} else if a0sym.val == "quasiquote" {
ast = quasiquote(astList[1])
continue # TCO
} else if a0sym.val == "do" {
for i = 1; i < astList.count - 1; i += 1 {
EVAL(astList[i], env)
}
ast = astList[astList.count - 1]
continue # TCO
} else if a0sym.val == "if" {
const condRes = EVAL(astList[1], env)
if condRes is MalNil || condRes is MalFalse {
ast = astList.count > 3 ? astList[3] : gNil
} else {
ast = astList[2]
}
continue # TCO
} else if a0sym.val == "fn*" {
const argsNames = astList[1] as MalSequential
return MalFunc.new(astList[2], argsNames, env, (args List<MalVal>) => EVAL(astList[2], Env.new(env, argsNames.val, args)))
} else {
const evaledList = astList.val.map<MalVal>(e => EVAL(e, env))
const fn = evaledList[0]
const callArgs = evaledList.slice(1)
if fn is MalNativeFunc {
return (fn as MalNativeFunc).call(callArgs)
} else if fn is MalFunc {
const f = fn as MalFunc
ast = f.ast
env = Env.new(f.env, f.params.val, callArgs)
continue # TCO
} else {
throw MalError.new("Expected function as head of list")
}
}
}
}
def PRINT(exp MalVal) string {
return exp?.print(true)
}
var repl_env = Env.new(null)
def RE(str string) MalVal {
return EVAL(READ(str), repl_env)
}
def REP(str string) string {
return PRINT(RE(str))
}
@entry
def main {
# core.sk: defined using Skew
ns.each((name, func) => repl_env.set(MalSymbol.new(name), MalNativeFunc.new(func)))
repl_env.set(MalSymbol.new("*ARGV*"), MalList.new(argv.isEmpty ? [] : argv.slice(1).map<MalVal>(e => MalString.new(e))))
# core.mal: defined using the language itself
RE("(def! not (fn* (a) (if a false true)))")
RE("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))")
if argv.count > 0 {
RE("(load-file \"" + argv[0] + "\")")
return
}
var line string
while (line = readLine("user> ")) != null {
if line == "" { continue }
try {
printLn(REP(line))
}
catch e MalError {
printLn("Error: \(e.message)")
}
catch e Error {
printLn("Error: \(e.message)")
}
}
}