2015-03-16 15:09:10 +03:00
|
|
|
module REPL
|
|
|
|
open System
|
2015-07-02 21:39:54 +03:00
|
|
|
open Node
|
2015-06-30 06:57:47 +03:00
|
|
|
open Types
|
2015-03-16 15:09:10 +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-06-30 06:57:47 +03:00
|
|
|
|
|
|
|
let rec eval_ast env = function
|
|
|
|
| Symbol(sym) -> Env.get env sym
|
2015-07-06 00:23:34 +03:00
|
|
|
| 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
|
2015-06-30 06:57:47 +03:00
|
|
|
| node -> node
|
|
|
|
|
|
|
|
and defBang env = function
|
2015-07-06 00:23:34 +03:00
|
|
|
| [sym; node] ->
|
2015-06-30 06:57:47 +03:00
|
|
|
match sym with
|
|
|
|
| Symbol(sym) ->
|
|
|
|
let node = eval env node
|
|
|
|
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 letStar env = function
|
2015-07-06 00:23:34 +03:00
|
|
|
| [bindings; form] ->
|
2015-06-30 06:57:47 +03:00
|
|
|
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-06-30 06:57:47 +03:00
|
|
|
| _ -> raise <| Error.errExpectedX "list or vector"
|
|
|
|
eval newEnv form
|
|
|
|
| _ -> raise <| Error.wrongArity ()
|
|
|
|
|
|
|
|
and eval env = function
|
2016-03-28 23:56:23 +03:00
|
|
|
| List(_, []) as emptyList -> emptyList
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, Symbol("def!")::rest) -> defBang env rest
|
|
|
|
| List(_, Symbol("let*")::rest) -> letStar env rest
|
|
|
|
| List(_, _) as node ->
|
2015-06-30 06:57:47 +03:00
|
|
|
let resolved = node |> eval_ast env
|
|
|
|
match resolved with
|
2015-07-06 00:23:34 +03:00
|
|
|
| List(_, BuiltInFunc(_, _, f)::rest) -> f rest
|
2015-07-03 17:57:15 +03:00
|
|
|
| _ -> raise <| Error.errExpectedX "func"
|
2015-06-30 06:57:47 +03:00
|
|
|
| node -> node |> eval_ast env
|
|
|
|
|
|
|
|
let READ input =
|
2015-03-16 15:09:10 +03:00
|
|
|
try
|
|
|
|
Reader.read_str input
|
|
|
|
with
|
2015-05-02 09:50:09 +03:00
|
|
|
| Error.ReaderError(msg) ->
|
2015-03-16 15:09:10 +03:00
|
|
|
printfn "%s" msg
|
|
|
|
[]
|
|
|
|
|
2015-06-30 06:57:47 +03:00
|
|
|
let EVAL env ast =
|
2015-03-16 15:09:10 +03:00
|
|
|
try
|
2015-06-30 06:57:47 +03:00
|
|
|
Some(eval env ast)
|
2015-03-16 15:09:10 +03:00
|
|
|
with
|
2016-12-27 02:05:32 +03:00
|
|
|
| Error.EvalError(str)
|
|
|
|
| Error.ReaderError(str) ->
|
|
|
|
printfn "%s" str
|
|
|
|
None
|
|
|
|
| Error.MalError(node) ->
|
|
|
|
printfn "%s" (Printer.pr_str [node])
|
|
|
|
None
|
|
|
|
| ex ->
|
|
|
|
printfn "%s" (ex.Message)
|
2015-03-16 15:09:10 +03:00
|
|
|
None
|
|
|
|
|
2015-06-30 06:57:47 +03:00
|
|
|
let PRINT v =
|
2015-03-16 15:09:10 +03:00
|
|
|
v
|
2015-03-26 22:05:02 +03:00
|
|
|
|> Seq.singleton
|
2015-03-16 15:09:10 +03:00
|
|
|
|> Printer.pr_str
|
|
|
|
|> printfn "%s"
|
|
|
|
|
2015-06-30 06:57:47 +03:00
|
|
|
let REP env input =
|
|
|
|
READ input
|
2015-03-16 15:09:10 +03:00
|
|
|
|> Seq.ofList
|
2015-06-30 06:57:47 +03:00
|
|
|
|> Seq.choose (fun form -> EVAL env form)
|
|
|
|
|> Seq.iter (fun value -> PRINT value)
|
2015-03-16 15:09:10 +03:00
|
|
|
|
2015-07-03 03:04:33 +03:00
|
|
|
let getReadlineMode args =
|
|
|
|
if args |> Array.exists (fun e -> e = "--raw") then
|
2015-03-16 15:09:10 +03:00
|
|
|
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 ->
|
2015-06-30 06:57:47 +03:00
|
|
|
REP env input
|
2015-03-16 15:09:10 +03:00
|
|
|
loop ()
|
|
|
|
loop ()
|