1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-11 21:57:38 +03:00
mal/impls/fsharp/step6_file.fs

172 lines
5.6 KiB
Forth
Raw Normal View History

2015-04-05 08:20:42 +03:00
module REPL
open System
open Node
open Types
2015-04-05 08:20:42 +03:00
let rec iterPairs f = function
| Pair(first, second, t) ->
f first second
iterPairs f t
| Empty -> ()
| _ -> raise <| Error.errExpectedX "list or vector"
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 02:15:40 +03:00
let rec defBangForm env = function
| [sym; form] ->
match sym with
| Symbol(sym) ->
let node = eval env form
Env.set env sym node
node
| _ -> raise <| Error.errExpectedX "symbol"
| _ -> raise <| Error.wrongArity ()
and setBinding env first second =
let s = match first with
| Symbol(s) -> s
| _ -> raise <| Error.errExpectedX "symbol"
let form = eval env second
Env.set env s form
and letStarForm outer = function
| [bindings; form] ->
let inner = Env.makeNew outer [] []
let binder = setBinding inner
match bindings with
| List(_, _) | Vector(_, _)-> iterPairs binder bindings
| _ -> raise <| Error.errExpectedX "list or vector"
inner, form
| _ -> raise <| Error.wrongArity ()
and ifForm env = function
| [condForm; trueForm; falseForm] -> ifForm3 env condForm trueForm falseForm
| [condForm; trueForm] -> ifForm3 env condForm trueForm Nil
| _ -> raise <| Error.wrongArity ()
and ifForm3 env condForm trueForm falseForm =
match eval env condForm with
| Bool(false) | Nil -> falseForm
| _ -> trueForm
and doForm env = function
| [a] -> a
| a::rest ->
eval env a |> ignore
doForm env rest
| _ -> raise <| Error.wrongArity ()
and fnStarForm outer nodes =
let makeFunc binds body =
let f = fun nodes ->
let inner = Env.makeNew outer binds nodes
eval inner body
Env.makeFunc f body binds outer
match nodes with
| [List(_, binds); body] -> makeFunc binds body
| [Vector(_, seg); body] -> makeFunc (List.ofSeq seg) body
| [_; _] -> raise <| Error.errExpectedX "bindings of list or vector"
| _ -> raise <| Error.wrongArity ()
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 02:15:40 +03:00
and eval env ast =
ignore <| match Env.get env "DEBUG-EVAL" with
| None | Some(Bool(false)) | Some(Nil) -> ()
| _ -> Printer.pr_str [ast] |> printfn "EVAL: %s"
match ast with
| Symbol(sym) -> match Env.get env sym with
| Some(value) -> value
| None -> Error.symbolNotFound sym |> raise
| Vector(_, seg) -> seg |> Seq.map (eval env) |> Array.ofSeq |> Node.ofArray
| Map(_, map) -> map |> Map.map (fun k v -> eval env v) |> makeMap
| List(_, Symbol("def!")::rest) -> defBangForm env rest
| List(_, Symbol("let*")::rest) ->
let inner, form = letStarForm env rest
form |> eval inner
| List(_, Symbol("if")::rest) -> ifForm env rest |> eval env
| List(_, Symbol("do")::rest) -> doForm env rest |> eval env
| List(_, Symbol("fn*")::rest) -> fnStarForm env rest
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 02:15:40 +03:00
| List(_, (a0 :: rest)) ->
let args = List.map (eval env) rest
match eval env a0 with
| BuiltInFunc(_, _, f) -> f args
| Func(_, _, _, body, binds, outer) ->
let inner = Env.makeNew outer binds args
body |> eval inner
| _ -> raise <| Error.errExpectedX "func"
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 02:15:40 +03:00
| _ -> ast
let READ input =
Reader.read_str input
2015-04-05 08:20:42 +03:00
let EVAL env ast =
Some(eval env ast)
2015-04-05 08:20:42 +03:00
let PRINT v =
2015-04-05 08:20:42 +03:00
v
|> Seq.singleton
|> Printer.pr_str
|> printfn "%s"
let RE env input =
READ input
2015-04-05 08:20:42 +03:00
|> Seq.ofList
|> Seq.choose (fun form -> EVAL env form)
2015-04-05 08:20:42 +03:00
let REP env input =
2015-04-05 08:20:42 +03:00
input
|> RE env
|> Seq.iter (fun value -> PRINT value)
2015-04-05 08:20:42 +03:00
let getReadlineMode args =
if args |> Array.exists (fun e -> e = "--raw") then
Readline.Mode.Raw
else
Readline.Mode.Terminal
let eval_func env = function
| [ast] -> eval env ast
| _ -> raise <| Error.wrongArity ()
2015-04-05 08:20:42 +03:00
let argv_func = function
| file::rest -> rest |> List.map Types.String |> makeList
| [] -> EmptyLIST
2015-04-05 08:20:42 +03:00
let configureEnv args =
let env = Env.makeRootEnv ()
Env.set env "eval" <| Env.makeBuiltInFunc (fun nodes -> eval_func env nodes)
Env.set env "*ARGV*" <| argv_func args
RE env """
2015-04-05 08:20:42 +03:00
(def! not (fn* (a) (if a false true)))
(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) "\nnil)")))))
2015-04-05 08:20:42 +03:00
""" |> Seq.iter ignore
env
[<EntryPoint>]
let main args =
let mode = getReadlineMode args
let args = Seq.ofArray args |> Seq.filter (fun e -> e <> "--raw") |> List.ofSeq
let env = configureEnv args
match args with
| file::_ ->
System.IO.File.ReadAllText file
|> RE env |> Seq.iter ignore
2015-04-05 08:20:42 +03:00
0
| _ ->
let rec loop () =
match Readline.read "user> " mode with
| null -> 0
| input ->
try
REP env input
with
| Error.EvalError(str)
| Error.ReaderError(str) ->
printfn "Error: %s" str
| ex ->
printfn "Error: %s" (ex.Message)
2015-04-05 08:20:42 +03:00
loop ()
loop ()