fsharp: step 4: Added prn, str, println, and pr-str
[jackhill/mal.git] / fsharp / step4_if_fn_do.fs
1 module REPL
2 open System
3
4 let read input =
5 try
6 Reader.read_str input
7 with
8 | Types.ReaderError(msg) ->
9 printfn "%s" msg
10 []
11
12 let eval env ast =
13 try
14 Some(Eval.eval env ast)
15 with
16 | Types.EvalError(msg) ->
17 printfn "%s" msg
18 None
19
20 let print v =
21 v
22 |> Seq.singleton
23 |> Printer.pr_str
24 |> printfn "%s"
25
26 let rep env input =
27 read input
28 |> Seq.ofList
29 |> Seq.choose (fun form -> eval env form)
30 |> Seq.iter (fun value -> print value)
31
32 let getReadlineMode (args : string array) =
33 if args.Length > 0 && args.[0] = "--raw" then
34 Readline.Mode.Raw
35 else
36 Readline.Mode.Terminal
37
38 [<EntryPoint>]
39 let main args =
40 let mode = getReadlineMode args
41 let env = Env.makeRootEnv ()
42 let rec loop () =
43 match Readline.read "user> " mode with
44 | null -> 0
45 | input ->
46 rep env input
47 loop ()
48 loop ()