Thread the dictionary through the call stack.
This commit is contained in:
parent
4d6230e01d
commit
4acdec71ed
|
|
@ -18,25 +18,38 @@ type alias JList = List JoyType
|
||||||
type alias JoyDict = Dict String JList
|
type alias JoyDict = Dict String JList
|
||||||
|
|
||||||
|
|
||||||
joy : JList -> JList -> Result String JList
|
joy : JList -> JList -> JoyDict -> Result String (JList, JoyDict)
|
||||||
joy stack expression =
|
joy stack expression dict =
|
||||||
case expression of
|
case expression of
|
||||||
[] ->
|
[] ->
|
||||||
Ok stack
|
Ok (stack, dict)
|
||||||
term :: rest_of_expression ->
|
term :: rest_of_expression ->
|
||||||
case term of
|
case term of
|
||||||
JoySymbol symbol ->
|
JoySymbol symbol ->
|
||||||
case joy_eval symbol stack rest_of_expression of
|
case joy_eval symbol stack rest_of_expression dict of
|
||||||
Err msg -> Err msg
|
Err msg -> Err msg
|
||||||
Ok (s, e) -> joy s e
|
Ok (s, e, dict0) -> joy s e dict0
|
||||||
_ ->
|
_ ->
|
||||||
joy (term :: stack) rest_of_expression
|
joy (term :: stack) rest_of_expression dict
|
||||||
|
|
||||||
|
|
||||||
joy_eval : String -> JList -> JList -> Result String (JList, JList)
|
joy_eval : String -> JList -> JList -> JoyDict -> Result String (JList, JList, JoyDict)
|
||||||
joy_eval symbol stack expression =
|
joy_eval symbol stack expression dict =
|
||||||
|
if symbol == "" then
|
||||||
|
Ok (stack, expression, dict)
|
||||||
|
else
|
||||||
|
case joy_function_eval symbol stack expression of
|
||||||
|
Err msg ->
|
||||||
|
if "Unknown word." == msg then
|
||||||
|
-- Look up word in dictionary.
|
||||||
|
Err ("Unknown word: " ++ symbol)
|
||||||
|
else
|
||||||
|
Err msg
|
||||||
|
Ok (stack0, expression0) -> Ok (stack0, expression0, dict)
|
||||||
|
|
||||||
|
|
||||||
|
joy_function_eval symbol stack expression =
|
||||||
case symbol of
|
case symbol of
|
||||||
"" -> Ok (stack, expression)
|
|
||||||
|
|
||||||
"branch" -> joy_branch stack expression
|
"branch" -> joy_branch stack expression
|
||||||
"i" -> joy_i stack expression
|
"i" -> joy_i stack expression
|
||||||
|
|
@ -81,7 +94,10 @@ joy_eval symbol stack expression =
|
||||||
"swap" -> joy_swap stack expression
|
"swap" -> joy_swap stack expression
|
||||||
"truthy" -> joy_truthy stack expression
|
"truthy" -> joy_truthy stack expression
|
||||||
|
|
||||||
_ -> Err ("Unknown word: " ++ symbol)
|
_ -> Err ("Unknown word.")
|
||||||
|
|
||||||
|
|
||||||
|
-- _ -> Err ("Unknown word: " ++ symbol)
|
||||||
|
|
||||||
|
|
||||||
joy_branch : JList -> JList -> Result String (JList, JList)
|
joy_branch : JList -> JList -> Result String (JList, JList)
|
||||||
|
|
@ -428,12 +444,12 @@ parse tokens = parse0 tokens []
|
||||||
text_to_expression text = parse (tokenize text)
|
text_to_expression text = parse (tokenize text)
|
||||||
|
|
||||||
|
|
||||||
|
doit : String -> JoyDict -> Result String (String, JoyDict)
|
||||||
doit text =
|
doit text dict =
|
||||||
case text_to_expression text of
|
case text_to_expression text of
|
||||||
Ok ast ->
|
Ok ast ->
|
||||||
case joy [] ast of
|
case joy [] ast dict of
|
||||||
Ok expr -> Ok (joyExpressionToString expr)
|
Ok (expr, dict0) -> Ok (joyExpressionToString expr, dict0)
|
||||||
Err msg -> Err msg
|
Err msg -> Err msg
|
||||||
Err msg -> Err msg
|
Err msg -> Err msg
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,13 +52,13 @@ update msg model =
|
||||||
|
|
||||||
view : Model -> Html Msg
|
view : Model -> Html Msg
|
||||||
view model =
|
view model =
|
||||||
case doit model.content of
|
case doit model.content model.dictionary of
|
||||||
Err msg ->
|
Err msg ->
|
||||||
div []
|
div []
|
||||||
[ input [ placeholder "Text to reverse", value model.content, onInput Change ] []
|
[ input [ placeholder "Text to reverse", value model.content, onInput Change ] []
|
||||||
, div [] [ text msg ]
|
, div [] [ text msg ]
|
||||||
]
|
]
|
||||||
Ok message ->
|
Ok (message, dict0) ->
|
||||||
div []
|
div []
|
||||||
[ input [ placeholder "Text to reverse", value model.content, onInput Change ] []
|
[ input [ placeholder "Text to reverse", value model.content, onInput Change ] []
|
||||||
, div [] [ text message ]
|
, div [] [ text message ]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue