1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-13 01:43:50 +03:00
mal/fsharp/step2_eval.fs
Joel Martin dd7a4f55f3 Test uncaught throw, catchless try* . Fix 46 impls.
Fixes made to: ada, c, chuck, clojure, coffee, common-lisp, cpp,
crystal, d, dart, elm, erlang, es6, factor, fsharp, gnu-smalltalk,
groovy, guile, haxe, hy, js, livescript, matlab, miniMAL, nasm, nim,
objc, objpascal, ocaml, perl, perl6, php, plsql, ps, python, r,
rpython, ruby, scheme, swift3, tcl, ts, vb, vimscript, wasm, yorick.

Catchless try* test is an optional test. Not all implementations
support catchless try* but a number were fixed so they at least don't
crash on catchless try*.
2018-12-12 14:18:26 -06:00

64 lines
1.8 KiB
Forth

module REPL
open System
open Node
open Types
let rec eval_ast env = function
| Symbol(sym) -> Env.get env sym
| List(_, lst) -> lst |> List.map (eval env) |> makeList
| Vector(_, seg) -> seg |> Seq.map (eval env) |> Array.ofSeq |> Node.ofArray
| Map(_, map) -> map |> Map.map (fun k v -> eval env v) |> makeMap
| node -> node
and eval env = function
| List(_, []) as emptyList -> emptyList
| List(_, _) as node ->
let resolved = node |> eval_ast env
match resolved with
| List(_, BuiltInFunc(_, _, f)::rest) -> f rest
| _ -> raise <| Error.errExpectedX "func"
| node -> node |> eval_ast env
let READ input =
Reader.read_str input
let EVAL env ast =
Some(eval env ast)
let PRINT v =
v
|> Seq.singleton
|> Printer.pr_str
|> printfn "%s"
let REP env input =
READ input
|> Seq.ofList
|> Seq.choose (fun form -> EVAL env form)
|> Seq.iter (fun value -> PRINT value)
let getReadlineMode args =
if args |> Array.exists (fun e -> e = "--raw") then
Readline.Mode.Raw
else
Readline.Mode.Terminal
[<EntryPoint>]
let main args =
let mode = getReadlineMode args
let env = Env.makeRootEnv ()
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)
loop ()
loop ()