Elm step 7-9
[jackhill/mal.git] / elm / step0_repl.elm
1 port module Main exposing (..)
2
3 import IO exposing (..)
4 import Json.Decode exposing (decodeValue)
5 import Platform exposing (programWithFlags)
6
7
8 main : Program Flags Model Msg
9 main =
10 programWithFlags
11 { init = init
12 , update = update
13 , subscriptions =
14 \model -> input (decodeValue decodeIO >> Input)
15 }
16
17
18 type alias Flags =
19 { args : List String
20 }
21
22
23 type alias Model =
24 { args : List String
25 }
26
27
28 type Msg
29 = Input (Result String IO)
30
31
32 init : Flags -> ( Model, Cmd Msg )
33 init flags =
34 ( flags, readLine prompt )
35
36
37 update : Msg -> Model -> ( Model, Cmd Msg )
38 update msg model =
39 case msg of
40 Input (Ok (LineRead (Just line))) ->
41 ( model, writeLine (rep line) )
42
43 Input (Ok LineWritten) ->
44 ( model, readLine prompt )
45
46 Input (Ok (LineRead Nothing)) ->
47 ( model, Cmd.none )
48
49 Input (Ok _) ->
50 ( model, Cmd.none )
51
52 Input (Err msg) ->
53 Debug.crash msg ( model, Cmd.none )
54
55
56 prompt : String
57 prompt =
58 "user> "
59
60
61 read : String -> String
62 read ast =
63 ast
64
65
66 eval : String -> String
67 eval ast =
68 ast
69
70
71 print : String -> String
72 print ast =
73 ast
74
75
76 rep : String -> String
77 rep =
78 read >> eval >> print