2015-03-26 08:17:46 +03:00
|
|
|
module REPL
|
|
|
|
open System
|
2015-07-02 21:39:54 +03:00
|
|
|
open Node
|
|
|
|
open Types
|
2015-03-26 08:17:46 +03:00
|
|
|
|
2015-07-02 21:39:54 +03:00
|
|
|
let rec iterPairs f = function
|
|
|
|
| Pair(first, second, t) ->
|
|
|
|
f first second
|
|
|
|
iterPairs f t
|
|
|
|
| Empty -> ()
|
2015-07-03 01:55:42 +03:00
|
|
|
| _ -> raise <| Error.errExpectedX "list or vector"
|
2015-07-02 21:39:54 +03:00
|
|
|
|
2022-01-10 02:15:40 +03:00
|
|
|
let rec defBangForm env = function
|
2015-07-02 21:39:54 +03:00
|
|
|
| [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 env = function
|
|
|
|
| [bindings; form] ->
|
|
|
|
let newEnv = Env.makeNew env [] []
|
|
|
|
let binder = setBinding newEnv
|
|
|
|
match bindings with
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, _) | Vector(_, _) -> iterPairs binder bindings
|
2015-07-02 21:39:54 +03:00
|
|
|
| _ -> raise <| Error.errExpectedX "list or vector"
|
|
|
|
eval newEnv 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 -> eval env falseForm
|
|
|
|
| _ -> eval env trueForm
|
|
|
|
|
|
|
|
and doForm env = function
|
|
|
|
| [a] -> eval env 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
|
2015-07-06 00:23:34 +03:00
|
|
|
| [List(_, binds); body] -> makeFunc binds body
|
|
|
|
| [Vector(_, seg); body] -> makeFunc (List.ofSeq seg) body
|
2015-07-02 21:39:54 +03:00
|
|
|
| [_; _] -> raise <| Error.errExpectedX "bindings of list or vector"
|
|
|
|
| _ -> raise <| Error.wrongArity ()
|
|
|
|
|
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
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, Symbol("def!")::rest) -> defBangForm env rest
|
|
|
|
| List(_, Symbol("let*")::rest) -> letStarForm env rest
|
|
|
|
| List(_, Symbol("if")::rest) -> ifForm env rest
|
|
|
|
| List(_, Symbol("do")::rest) -> doForm env rest
|
|
|
|
| List(_, Symbol("fn*")::rest) -> fnStarForm env rest
|
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
|
2015-07-02 21:39:54 +03:00
|
|
|
body |> eval inner
|
2015-07-03 17:57:15 +03:00
|
|
|
| _ -> raise <| Error.errExpectedX "func"
|
2022-01-10 02:15:40 +03:00
|
|
|
| _ -> ast
|
2015-07-02 21:39:54 +03:00
|
|
|
|
|
|
|
let READ input =
|
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-03 22:20:44 +03:00
|
|
|
Reader.read_str input
|
2015-03-26 08:17:46 +03:00
|
|
|
|
2015-07-02 21:39:54 +03:00
|
|
|
let EVAL env ast =
|
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-03 22:20:44 +03:00
|
|
|
Some(eval env ast)
|
2015-03-26 08:17:46 +03:00
|
|
|
|
2015-07-02 21:39:54 +03:00
|
|
|
let PRINT v =
|
2015-03-26 08:17:46 +03:00
|
|
|
v
|
2015-03-26 22:05:02 +03:00
|
|
|
|> Seq.singleton
|
2015-03-26 08:17:46 +03:00
|
|
|
|> Printer.pr_str
|
|
|
|
|> printfn "%s"
|
|
|
|
|
2015-07-02 21:39:54 +03:00
|
|
|
let RE env input =
|
|
|
|
READ input
|
2015-03-26 08:17:46 +03:00
|
|
|
|> Seq.ofList
|
2015-07-02 21:39:54 +03:00
|
|
|
|> Seq.choose (fun form -> EVAL env form)
|
2015-03-27 12:21:56 +03:00
|
|
|
|
2015-07-02 21:39:54 +03:00
|
|
|
let REP env input =
|
2015-03-27 12:21:56 +03:00
|
|
|
input
|
2015-07-02 21:39:54 +03:00
|
|
|
|> RE env
|
|
|
|
|> Seq.iter (fun value -> PRINT value)
|
2015-03-26 08:17:46 +03:00
|
|
|
|
2015-07-03 03:04:33 +03:00
|
|
|
let getReadlineMode args =
|
|
|
|
if args |> Array.exists (fun e -> e = "--raw") then
|
2015-03-26 08:17:46 +03:00
|
|
|
Readline.Mode.Raw
|
|
|
|
else
|
|
|
|
Readline.Mode.Terminal
|
|
|
|
|
|
|
|
[<EntryPoint>]
|
|
|
|
let main args =
|
|
|
|
let mode = getReadlineMode args
|
|
|
|
let env = Env.makeRootEnv ()
|
2015-03-27 12:21:56 +03:00
|
|
|
|
2015-07-02 21:39:54 +03:00
|
|
|
RE env "(def! not (fn* (a) (if a false true)))" |> Seq.iter ignore
|
2015-03-27 12:21:56 +03:00
|
|
|
|
2015-03-26 08:17:46 +03:00
|
|
|
let rec loop () =
|
|
|
|
match Readline.read "user> " mode with
|
|
|
|
| null -> 0
|
|
|
|
| input ->
|
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-03 22:20:44 +03:00
|
|
|
try
|
|
|
|
REP env input
|
|
|
|
with
|
|
|
|
| Error.EvalError(str)
|
|
|
|
| Error.ReaderError(str) ->
|
|
|
|
printfn "Error: %s" str
|
|
|
|
| ex ->
|
|
|
|
printfn "Error: %s" (ex.Message)
|
2015-03-26 08:17:46 +03:00
|
|
|
loop ()
|
|
|
|
loop ()
|