1 port module Main exposing (..)
4 import Dict exposing (Dict)
5 import IO exposing (..)
6 import Json.Decode exposing (decodeValue)
7 import Platform exposing (programWithFlags)
8 import Types exposing (..)
9 import Reader exposing (readString)
10 import Printer exposing (printString)
11 import Utils exposing (maybeToList, zip, last, justValues)
17 main : Program Flags Model Msg
23 \model -> input (decodeValue decodeIO >> Input)
33 = InitIO Env (IO -> Eval MalExpr)
36 | ReplIO Env (IO -> Eval MalExpr)
39 init : Flags -> ( Model, Cmd Msg )
50 (\b a -> a |> Eval.andThen (\_ -> b))
53 runInit initEnv evalMalInit
56 update : Msg -> Model -> ( Model, Cmd Msg )
73 Input (Ok (LineRead (Just line))) ->
79 ( model, readLine prompt )
81 Input (Ok LineWritten) ->
82 ( model, readLine prompt )
84 Input (Ok (LineRead Nothing)) ->
89 Debug.crash "unexpected IO received: " io
100 Debug.crash msg ( model, Cmd.none )
103 runInit : Env -> Eval MalExpr -> ( Model, Cmd Msg )
105 case Eval.run env expr of
106 ( env, EvalOk expr ) ->
107 -- Init went okay, start REPL.
108 ( ReplActive env, readLine prompt )
110 ( env, EvalErr msg ) ->
111 -- Init failed, don't start REPL.
112 ( InitError msg, writeLine ("ERR:" ++ msg) )
114 ( env, EvalIO cmd cont ) ->
116 ( InitIO env cont, cmd )
119 run : Env -> Eval MalExpr -> ( Model, Cmd Msg )
121 case Eval.run env expr of
122 ( env, EvalOk expr ) ->
123 ( ReplActive env, writeLine (print expr) )
125 ( env, EvalErr msg ) ->
126 ( ReplActive env, writeLine ("ERR:" ++ msg) )
128 ( env, EvalIO cmd cont ) ->
129 ( ReplIO env cont, cmd )
137 {-| read can return three things:
139 Ok (Just expr) -> parsed okay
140 Ok Nothing -> empty string (only whitespace and/or comments)
141 Err msg -> parse error
144 read : String -> Result String (Maybe MalExpr)
149 debug : String -> a -> Eval b -> Eval b
153 Env.debug env msg value
158 eval : MalExpr -> Eval MalExpr
166 (printString True expr)
174 |> Eval.andThen (Eval.runLoop apply)
177 evalApply : ApplyRec -> Eval MalExpr
178 evalApply { frameId, bound, body } =
181 Eval.modifyEnv (Env.enter frameId bound)
182 |> Eval.andThen (\_ -> evalNoApply body)
185 Eval.modifyEnv (Env.leave env.currentFrameId)
186 |> Eval.map (\_ -> res)
191 evalNoApply : MalExpr -> Eval MalExpr
194 (printString True ast)
199 MalList ((MalSymbol "def!") :: args) ->
202 MalList ((MalSymbol "let*") :: args) ->
205 MalList ((MalSymbol "do") :: args) ->
208 MalList ((MalSymbol "if") :: args) ->
211 MalList ((MalSymbol "fn*") :: args) ->
220 Eval.fail "can't happen"
222 (MalFunction (CoreFunc fn)) :: args ->
225 (MalFunction (UserFunc { fn })) :: args ->
229 Eval.fail ((printString True fn) ++ " is not a function")
237 evalAst : MalExpr -> Eval MalExpr
241 -- Lookup symbol in env and return value or raise error if not found.
244 case Env.get sym env of
253 -- Return new list that is result of calling eval on each element of list.
258 evalList (Array.toList vec)
259 |> Eval.map (Array.fromList >> MalVector)
262 evalList (Dict.values map)
273 evalList : List MalExpr -> Eval (List MalExpr)
279 Eval.succeed (List.reverse acc)
291 evalDef : List MalExpr -> Eval MalExpr
294 [ MalSymbol name, uneValue ] ->
298 Eval.modifyEnv (Env.def name value)
299 |> Eval.andThen (\_ -> Eval.succeed value)
303 Eval.fail "def! expected two args: name and value"
306 evalLet : List MalExpr -> Eval MalExpr
311 (MalSymbol name) :: expr :: rest ->
315 Eval.modifyEnv (Env.set name value)
318 if List.isEmpty rest then
326 Eval.fail "let* expected an even number of binds (symbol expr ..)"
329 Eval.modifyEnv Env.push
330 |> Eval.andThen (\_ -> evalBinds binds)
331 |> Eval.andThen (\_ -> evalNoApply body)
334 Eval.modifyEnv Env.pop
335 |> Eval.map (\_ -> res)
339 [ MalList binds, body ] ->
342 [ MalVector bindsVec, body ] ->
343 go (Array.toList bindsVec) body
346 Eval.fail "let* expected two args: binds and a body"
349 evalDo : List MalExpr -> Eval MalExpr
351 case List.reverse args of
353 evalList (List.reverse rest)
354 |> Eval.andThen (\_ -> evalNoApply last)
357 Eval.fail "do expected at least one arg"
360 evalIf : List MalExpr -> Eval MalExpr
364 expr /= MalNil && expr /= (MalBool False)
366 go condition trueExpr falseExpr =
368 |> Eval.map isThruthy
380 [ condition, trueExpr ] ->
381 go condition trueExpr MalNil
383 [ condition, trueExpr, falseExpr ] ->
384 go condition trueExpr falseExpr
387 Eval.fail "if expected at least two args"
390 evalFn : List MalExpr -> Eval MalExpr
393 {- Extract symbols from the binds list and verify their uniqueness -}
394 extractSymbols acc list =
397 Ok (List.reverse acc)
399 (MalSymbol name) :: rest ->
400 if List.member name acc then
401 Err "all binds must have unique names"
403 extractSymbols (name :: acc) rest
406 Err "all binds in fn* must be a symbol"
409 case List.reverse list of
410 var :: "&" :: rest ->
411 Ok <| bindVarArgs (List.reverse rest) var
414 if List.member "&" list then
415 Err "varargs separator '&' is used incorrectly"
420 extractSymbols [] >> Result.andThen parseBinds
422 bindArgs binds args =
427 if List.length args /= numBinds then
430 ++ (toString numBinds)
435 bindVarArgs binds var args =
441 MalList (List.drop minArgs args)
443 if List.length args < minArgs then
445 "function expected at least "
446 ++ (toString minArgs)
449 Ok <| zip binds args ++ [ ( var, varArgs ) ]
451 makeFn frameId binder body =
460 -- TODO : choice Env.enter prematurely?
461 -- I think it is needed by the garbage collect..
473 case extractAndParse bindsList of
475 Eval.modifyEnv Env.ref
476 -- reference the current frame.
482 (makeFn env.currentFrameId binder body)
490 [ MalList bindsList, body ] ->
493 [ MalVector bindsVec, body ] ->
494 go (Array.toList bindsVec) body
497 Eval.fail "fn* expected two args: binds list and body"
500 print : MalExpr -> String
507 Doesn't actually run the Eval but returns the monad.
510 rep : String -> Maybe (Eval MalExpr)
512 case readString input of