mirror of
https://github.com/kanaka/mal.git
synced 2024-08-18 02:00:40 +03:00
033892777a
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) .
120 lines
4.0 KiB
Forth
120 lines
4.0 KiB
Forth
module Env
|
|
|
|
open Types
|
|
|
|
let makeEmpty () = Env()
|
|
|
|
let ofList lst =
|
|
let env = makeEmpty ()
|
|
let accumulate (e : Env) (k, v) = e.Add(k, v); e
|
|
List.fold accumulate env lst
|
|
|
|
let set (env : EnvChain) key node =
|
|
match env with
|
|
| head::_ -> head.[key] <- node
|
|
| _ -> raise <| Error.noEnvironment ()
|
|
|
|
let rec get (chain : EnvChain) key =
|
|
match chain with
|
|
| [] -> None
|
|
| env::rest ->
|
|
match env.TryGetValue(key) with
|
|
| true, v -> Some(v)
|
|
| false, _ -> get rest key
|
|
|
|
let private getNextValue =
|
|
let counter = ref 0
|
|
fun () -> System.Threading.Interlocked.Increment(counter)
|
|
|
|
let makeBuiltInFunc f =
|
|
BuiltInFunc(Node.NIL, getNextValue (), f)
|
|
|
|
let makeFunc f body binds env =
|
|
Func(Node.NIL, getNextValue (), f, body, binds, env)
|
|
|
|
let makeMacro f body binds env =
|
|
Macro(Node.NIL, getNextValue (), f, body, binds, env)
|
|
|
|
let makeRootEnv () =
|
|
let wrap name f = name, makeBuiltInFunc f
|
|
let env =
|
|
[ wrap "+" Core.add
|
|
wrap "-" Core.subtract
|
|
wrap "*" Core.multiply
|
|
wrap "/" Core.divide
|
|
wrap "list" Core.list
|
|
wrap "list?" Core.isList
|
|
wrap "empty?" Core.isEmpty
|
|
wrap "count" Core.count
|
|
wrap "=" Core.eq
|
|
wrap "<" Core.lt
|
|
wrap "<=" Core.le
|
|
wrap ">=" Core.ge
|
|
wrap ">" Core.gt
|
|
wrap "time-ms" Core.time_ms
|
|
wrap "pr-str" Core.pr_str
|
|
wrap "str" Core.str
|
|
wrap "prn" Core.prn
|
|
wrap "println" Core.println
|
|
wrap "read-string" Core.read_str
|
|
wrap "slurp" Core.slurp
|
|
wrap "cons" Core.cons
|
|
wrap "concat" Core.concat
|
|
wrap "vec" Core.vec
|
|
wrap "nth" Core.nth
|
|
wrap "first" Core.first
|
|
wrap "rest" Core.rest
|
|
wrap "throw" Core.throw
|
|
wrap "map" Core.map
|
|
wrap "apply" Core.apply
|
|
wrap "nil?" (Core.isConst Node.NIL)
|
|
wrap "true?" (Core.isConst Node.TRUE)
|
|
wrap "false?" (Core.isConst Node.FALSE)
|
|
wrap "symbol?" Core.isSymbol
|
|
wrap "symbol" Core.symbol
|
|
wrap "string?" Core.isString
|
|
wrap "keyword?" Core.isKeyword
|
|
wrap "keyword" Core.keyword
|
|
wrap "number?" Core.isNumber
|
|
wrap "fn?" Core.isFn
|
|
wrap "macro?" Core.isMacro
|
|
wrap "sequential?" Core.isSequential
|
|
wrap "vector?" Core.isVector
|
|
wrap "vector" Core.vector
|
|
wrap "map?" Core.isMap
|
|
wrap "hash-map" Core.hashMap
|
|
wrap "assoc" Core.assoc
|
|
wrap "dissoc" Core.dissoc
|
|
wrap "get" Core.get
|
|
wrap "contains?" Core.contains
|
|
wrap "keys" Core.keys
|
|
wrap "vals" Core.vals
|
|
wrap "atom" (Core.atom getNextValue)
|
|
wrap "atom?" Core.isAtom
|
|
wrap "deref" Core.deref
|
|
wrap "reset!" Core.reset
|
|
wrap "swap!" Core.swap
|
|
wrap "conj" Core.conj
|
|
wrap "seq" Core.seq
|
|
wrap "meta" Core.meta
|
|
wrap "with-meta" Core.withMeta ]
|
|
|> ofList
|
|
[ env ]
|
|
|
|
let makeNew outer symbols nodes =
|
|
let env = (makeEmpty ())::outer
|
|
let rec loop symbols nodes =
|
|
match symbols, nodes with
|
|
| [Symbol("&"); Symbol(s)], nodes ->
|
|
set env s (Node.makeList nodes)
|
|
env
|
|
| Symbol("&")::_, _ -> raise <| Error.onlyOneSymbolAfterAmp ()
|
|
| Symbol(s)::symbols, n::nodes ->
|
|
set env s n
|
|
loop symbols nodes
|
|
| [], [] -> env
|
|
| _, [] -> raise <| Error.notEnoughValues ()
|
|
| [], _ -> raise <| Error.tooManyValues ()
|
|
| _, _ -> raise <| Error.errExpectedX "symbol"
|
|
loop symbols nodes
|