Skip to content

Commit

Permalink
elm: merge eval-ast into eval (part of kanaka#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Feb 7, 2022
1 parent 95adfe7 commit a93486c
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 350 deletions.
5 changes: 3 additions & 2 deletions impls/elm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ MAINTAINER Joel Martin <[email protected]>
RUN apt-get -y update

# Required for running tests
RUN apt-get -y install make python
RUN apt-get -y install make python3
RUN ln -s /usr/bin/python3 /usr/local/bin/python

RUN mkdir -p /mal
WORKDIR /mal
Expand All @@ -18,7 +19,7 @@ WORKDIR /mal
# Specific implementation requirements
##########################################################

RUN apt-get -y install libreadline-dev nodejs npm
RUN apt-get -y install g++ libreadline-dev nodejs npm

ENV HOME /mal
ENV NPM_CONFIG_CACHE /mal/.npm
16 changes: 4 additions & 12 deletions impls/elm/Step2_eval.elm
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ read =

eval : ReplEnv -> MalExpr -> ( Result String MalExpr, ReplEnv )
eval env ast =
-- let
-- _ = Debug.log ("EVAL: " ++ printStr env True ast) ()
-- -- The output ends with an ugly ": ()", but that does not hurt.
-- in
case ast of
MalList _ [] ->
( Ok ast, env )
Expand All @@ -145,13 +149,6 @@ eval env ast =
( Err msg, newEnv ) ->
( Err msg, newEnv )

_ ->
evalAst env ast


evalAst : ReplEnv -> MalExpr -> ( Result String MalExpr, ReplEnv )
evalAst env ast =
case ast of
MalSymbol sym ->
-- Lookup symbol in env and return value or raise error if not found.
case Dict.get sym env of
Expand All @@ -161,11 +158,6 @@ evalAst env ast =
Nothing ->
( Err ("symbol '" ++ sym ++ "' not found"), env )

MalList _ list ->
-- Return new list that is result of calling eval on each element of list.
evalList env list []
|> mapFirst (Result.map (MalList Nothing))

MalVector _ vec ->
evalList env (Array.toList vec) []
|> mapFirst (Result.map (Array.fromList >> MalVector Nothing))
Expand Down
20 changes: 8 additions & 12 deletions impls/elm/Step3_env.elm
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ read =

eval : Env -> MalExpr -> ( Result String MalExpr, Env )
eval env ast =
let
_ = case Env.get "DEBUG-EVAL" env of
Err _ -> ()
Ok MalNil -> ()
Ok (MalBool False) -> ()
_ -> Debug.log ("EVAL: " ++ printString env True ast) ()
-- The output ends with an ugly ": ()", but that does not hurt.
in
case ast of
MalList _ [] ->
( Ok ast, env )
Expand Down Expand Up @@ -147,13 +155,6 @@ eval env ast =
( Err msg, newEnv ) ->
( Err msg, newEnv )

_ ->
evalAst env ast


evalAst : Env -> MalExpr -> ( Result String MalExpr, Env )
evalAst env ast =
case ast of
MalSymbol sym ->
-- Lookup symbol in env and return value or raise error if not found.
case Env.get sym env of
Expand All @@ -163,11 +164,6 @@ evalAst env ast =
Err msg ->
( Err msg, env )

MalList _ list ->
-- Return new list that is result of calling eval on each element of list.
evalList env list []
|> mapFirst (Result.map (MalList Nothing))

MalVector _ vec ->
evalList env (Array.toList vec) []
|> mapFirst (Result.map (Array.fromList >> MalVector Nothing))
Expand Down
21 changes: 9 additions & 12 deletions impls/elm/Step4_if_fn_do.elm
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ read =

eval : MalExpr -> Eval MalExpr
eval ast =
Eval.withEnv (\env -> Eval.succeed <|
case Env.get "DEBUG-EVAL" env of
Err _ -> ()
Ok MalNil -> ()
Ok (MalBool False) -> ()
_ -> Debug.log ("EVAL: " ++ printString env True ast) ()
-- The output ends with an ugly ": ()", but that does not hurt.
) |> Eval.andThen (\_ ->
case ast of
MalList _ [] ->
Eval.succeed ast
Expand Down Expand Up @@ -192,13 +200,6 @@ eval ast =
)
)

_ ->
evalAst ast


evalAst : MalExpr -> Eval MalExpr
evalAst ast =
case ast of
MalSymbol sym ->
-- Lookup symbol in env and return value or raise error if not found.
Eval.withEnv
Expand All @@ -211,11 +212,6 @@ evalAst ast =
Eval.fail msg
)

MalList _ list ->
-- Return new list that is result of calling eval on each element of list.
evalList list
|> Eval.map (MalList Nothing)

MalVector _ vec ->
evalList (Array.toList vec)
|> Eval.map (Array.fromList >> MalVector Nothing)
Expand All @@ -230,6 +226,7 @@ evalAst ast =

_ ->
Eval.succeed ast
)


evalList : List MalExpr -> Eval (List MalExpr)
Expand Down
40 changes: 17 additions & 23 deletions impls/elm/Step5_tco.elm
Original file line number Diff line number Diff line change
Expand Up @@ -191,28 +191,34 @@ evalApply { frameId, bound, body } =

evalNoApply : MalExpr -> Eval MalExpr
evalNoApply ast =
debug "evalNoApply"
(\env -> printString env True ast)
(case ast of
MalList _ [] ->
Eval.withEnv (\env -> Eval.succeed <|
case Env.get "DEBUG-EVAL" env of
Err _ -> ()
Ok MalNil -> ()
Ok (MalBool False) -> ()
_ -> Debug.log ("EVAL: " ++ printString env True ast) ()
-- The output ends with an ugly ": ()", but that does not hurt.
) |> Eval.andThen (\_ ->
case ast of
MalList _ [] ->
Eval.succeed ast

MalList _ ((MalSymbol "def!") :: args) ->
MalList _ ((MalSymbol "def!") :: args) ->
evalDef args

MalList _ ((MalSymbol "let*") :: args) ->
MalList _ ((MalSymbol "let*") :: args) ->
evalLet args

MalList _ ((MalSymbol "do") :: args) ->
MalList _ ((MalSymbol "do") :: args) ->
evalDo args

MalList _ ((MalSymbol "if") :: args) ->
MalList _ ((MalSymbol "if") :: args) ->
evalIf args

MalList _ ((MalSymbol "fn*") :: args) ->
MalList _ ((MalSymbol "fn*") :: args) ->
evalFn args

MalList _ list ->
MalList _ list ->
evalList list
|> Eval.andThen
(\newList ->
Expand All @@ -233,14 +239,6 @@ evalNoApply ast =
)
)

_ ->
evalAst ast
)


evalAst : MalExpr -> Eval MalExpr
evalAst ast =
case ast of
MalSymbol sym ->
-- Lookup symbol in env and return value or raise error if not found.
Eval.withEnv
Expand All @@ -253,11 +251,6 @@ evalAst ast =
Eval.fail msg
)

MalList _ list ->
-- Return new list that is result of calling eval on each element of list.
evalList list
|> Eval.map (MalList Nothing)

MalVector _ vec ->
evalList (Array.toList vec)
|> Eval.map (Array.fromList >> MalVector Nothing)
Expand All @@ -272,6 +265,7 @@ evalAst ast =

_ ->
Eval.succeed ast
)


evalList : List MalExpr -> Eval (List MalExpr)
Expand Down
40 changes: 17 additions & 23 deletions impls/elm/Step6_file.elm
Original file line number Diff line number Diff line change
Expand Up @@ -261,28 +261,34 @@ evalApply { frameId, bound, body } =

evalNoApply : MalExpr -> Eval MalExpr
evalNoApply ast =
debug "evalNoApply"
(\env -> printString env True ast)
(case ast of
MalList _ [] ->
Eval.withEnv (\env -> Eval.succeed <|
case Env.get "DEBUG-EVAL" env of
Err _ -> ()
Ok MalNil -> ()
Ok (MalBool False) -> ()
_ -> Debug.log ("EVAL: " ++ printString env True ast) ()
-- The output ends with an ugly ": ()", but that does not hurt.
) |> Eval.andThen (\_ ->
case ast of
MalList _ [] ->
Eval.succeed ast

MalList _ ((MalSymbol "def!") :: args) ->
MalList _ ((MalSymbol "def!") :: args) ->
evalDef args

MalList _ ((MalSymbol "let*") :: args) ->
MalList _ ((MalSymbol "let*") :: args) ->
evalLet args

MalList _ ((MalSymbol "do") :: args) ->
MalList _ ((MalSymbol "do") :: args) ->
evalDo args

MalList _ ((MalSymbol "if") :: args) ->
MalList _ ((MalSymbol "if") :: args) ->
evalIf args

MalList _ ((MalSymbol "fn*") :: args) ->
MalList _ ((MalSymbol "fn*") :: args) ->
evalFn args

MalList _ list ->
MalList _ list ->
evalList list
|> Eval.andThen
(\newList ->
Expand All @@ -303,14 +309,6 @@ evalNoApply ast =
)
)

_ ->
evalAst ast
)


evalAst : MalExpr -> Eval MalExpr
evalAst ast =
case ast of
MalSymbol sym ->
-- Lookup symbol in env and return value or raise error if not found.
Eval.withEnv
Expand All @@ -323,11 +321,6 @@ evalAst ast =
Eval.fail msg
)

MalList _ list ->
-- Return new list that is result of calling eval on each element of list.
evalList list
|> Eval.map (MalList Nothing)

MalVector _ vec ->
evalList (Array.toList vec)
|> Eval.map (Array.fromList >> MalVector Nothing)
Expand All @@ -342,6 +335,7 @@ evalAst ast =

_ ->
Eval.succeed ast
)


evalList : List MalExpr -> Eval (List MalExpr)
Expand Down
Loading

0 comments on commit a93486c

Please sign in to comment.