Skip to content

Commit

Permalink
elm: add metadata to containers
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon authored and kanaka committed Aug 6, 2024
1 parent 77be585 commit 97d06eb
Show file tree
Hide file tree
Showing 15 changed files with 375 additions and 351 deletions.
170 changes: 97 additions & 73 deletions impls/elm/Core.elm

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions impls/elm/Env.elm
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,13 @@ gc expr env =
recur frameId acc
|> countBound bound

MalList list ->
MalList _ list ->
countList acc list

MalVector vec ->
MalVector _ vec ->
countList acc (Array.toList vec)

MalMap map ->
MalMap _ map ->
countList acc (Dict.values map)

MalAtom atomId ->
Expand Down
6 changes: 3 additions & 3 deletions impls/elm/Printer.elm
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ printString env readably ast =
MalKeyword kw ->
":" ++ kw

MalList list ->
MalList _ list ->
printList env readably list

MalVector vec ->
MalVector _ vec ->
printVector env readably vec

MalMap map ->
MalMap _ map ->
printMap env readably map

MalFunction _ ->
Expand Down
6 changes: 3 additions & 3 deletions impls/elm/Reader.elm
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ keyword =

list : Parser MalExpr
list =
Parser.map MalList <| Parser.sequence
Parser.map (MalList Nothing) <| Parser.sequence
{ start = "("
, separator = ""
, end = ")"
Expand All @@ -103,7 +103,7 @@ list =

vector : Parser MalExpr
vector =
Parser.map (MalVector << Array.fromList) <| Parser.sequence
Parser.map (MalVector Nothing << Array.fromList) <| Parser.sequence
{ start = "["
, separator = ""
, end = "]"
Expand All @@ -128,7 +128,7 @@ mapEntry =

map : Parser MalExpr
map =
Parser.map (MalMap << Dict.fromList) <| Parser.sequence
Parser.map (MalMap Nothing << Dict.fromList) <| Parser.sequence
{ start = "{"
, separator = ""
, end = "}"
Expand Down
20 changes: 10 additions & 10 deletions impls/elm/Step2_eval.elm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ initReplEnv : ReplEnv
initReplEnv =
let
makeFn =
CoreFunc >> MalFunction
CoreFunc Nothing >> MalFunction

binaryOp fn args =
case args of
Expand Down Expand Up @@ -121,17 +121,17 @@ read =
eval : ReplEnv -> MalExpr -> ( Result String MalExpr, ReplEnv )
eval env ast =
case ast of
MalList [] ->
MalList _ [] ->
( Ok ast, env )

MalList list ->
MalList _ list ->
case evalList env list [] of
( Ok newList, newEnv ) ->
case newList of
[] ->
( Err "can't happen", newEnv )

(MalFunction (CoreFunc fn)) :: args ->
(MalFunction (CoreFunc _ fn)) :: args ->
case Eval.runSimple (fn args) of
Ok res ->
( Ok res, newEnv )
Expand Down Expand Up @@ -161,22 +161,22 @@ evalAst env ast =
Nothing ->
( Err ("symbol '" ++ sym ++ "' not found"), env )

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

MalVector vec ->
MalVector _ vec ->
evalList env (Array.toList vec) []
|> mapFirst (Result.map (Array.fromList >> MalVector))
|> mapFirst (Result.map (Array.fromList >> MalVector Nothing))

MalMap map ->
MalMap _ map ->
evalList env (Dict.values map) []
|> mapFirst
(Result.map
(zip (Dict.keys map)
>> Dict.fromList
>> MalMap
>> MalMap Nothing
)
)

Expand Down
28 changes: 14 additions & 14 deletions impls/elm/Step3_env.elm
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ initReplEnv : Env
initReplEnv =
let
makeFn =
CoreFunc >> MalFunction
CoreFunc Nothing >> MalFunction

binaryOp fn args =
case args of
Expand Down Expand Up @@ -117,23 +117,23 @@ read =
eval : Env -> MalExpr -> ( Result String MalExpr, Env )
eval env ast =
case ast of
MalList [] ->
MalList _ [] ->
( Ok ast, env )

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

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

MalList list ->
MalList _ list ->
case evalList env list [] of
( Ok newList, newEnv ) ->
case newList of
[] ->
( Err "can't happen", newEnv )

(MalFunction (CoreFunc fn)) :: args ->
(MalFunction (CoreFunc _ fn)) :: args ->
case Eval.runSimple (fn args) of
Ok res ->
( Ok res, newEnv )
Expand Down Expand Up @@ -163,22 +163,22 @@ evalAst env ast =
Err msg ->
( Err msg, env )

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

MalVector vec ->
MalVector _ vec ->
evalList env (Array.toList vec) []
|> mapFirst (Result.map (Array.fromList >> MalVector))
|> mapFirst (Result.map (Array.fromList >> MalVector Nothing))

MalMap map ->
MalMap _ map ->
evalList env (Dict.values map) []
|> mapFirst
(Result.map
(zip (Dict.keys map)
>> Dict.fromList
>> MalMap
>> MalMap Nothing
)
)

Expand Down Expand Up @@ -249,10 +249,10 @@ evalLet env args =
( Err msg, env )
in
case args of
[ MalList binds, body ] ->
[ MalList _ binds, body ] ->
go binds body

[ MalVector bindsVec, body ] ->
[ MalVector _ bindsVec, body ] ->
go (Array.toList bindsVec) body

_ ->
Expand Down
52 changes: 26 additions & 26 deletions impls/elm/Step4_if_fn_do.elm
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ update msg model =


runInit : Env -> Eval MalExpr -> ( Model, Cmd Msg )
runInit env0 expr =
case Eval.run env0 expr of
( env, EvalOk _ ) ->
runInit env0 expr0 =
case Eval.run env0 expr0 of
( env, EvalOk expr ) ->
-- Init went okay, start REPL.
( ReplActive env, readLine prompt )

Expand All @@ -121,10 +121,10 @@ runInit env0 expr =


run : Env -> Eval MalExpr -> ( Model, Cmd Msg )
run env0 expr =
case Eval.run env0 expr of
( env, EvalOk expr1 ) ->
( ReplActive env, writeLine (print env expr1) )
run env0 expr0 =
case Eval.run env0 expr0 of
( env, EvalOk expr ) ->
( ReplActive env, writeLine (print env expr) )

( env, EvalErr msg ) ->
( ReplActive env, writeLine (printError env msg) )
Expand Down Expand Up @@ -153,33 +153,33 @@ read =
eval : MalExpr -> Eval MalExpr
eval ast =
case ast of
MalList [] ->
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 ->
case newList of
[] ->
Eval.fail "can't happen"

(MalFunction (CoreFunc fn)) :: args ->
(MalFunction (CoreFunc _ fn)) :: args ->
fn args

(MalFunction (UserFunc { eagerFn })) :: args ->
Expand Down Expand Up @@ -211,21 +211,21 @@ evalAst ast =
Eval.fail msg
)

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

MalVector vec ->
MalVector _ vec ->
evalList (Array.toList vec)
|> Eval.map (Array.fromList >> MalVector)
|> Eval.map (Array.fromList >> MalVector Nothing)

MalMap map ->
MalMap _ map ->
evalList (Dict.values map)
|> Eval.map
(zip (Dict.keys map)
>> Dict.fromList
>> MalMap
>> MalMap Nothing
)

_ ->
Expand Down Expand Up @@ -298,10 +298,10 @@ evalLet args =
)
in
case args of
[ MalList binds, body ] ->
[ MalList _ binds, body ] ->
go binds body

[ MalVector bindsVec, body ] ->
[ MalVector _ bindsVec, body ] ->
go (Array.toList bindsVec) body

_ ->
Expand Down Expand Up @@ -404,7 +404,7 @@ evalFn parms =
List.length binds

varArgs =
MalList (List.drop minArgs args)
MalList Nothing (List.drop minArgs args)
in
if List.length args < minArgs then
Err <|
Expand Down Expand Up @@ -456,10 +456,10 @@ evalFn parms =
Eval.fail msg
in
case parms of
[ MalList bindsList, body ] ->
[ MalList _ bindsList, body ] ->
go bindsList body

[ MalVector bindsVec, body ] ->
[ MalVector _ bindsVec, body ] ->
go (Array.toList bindsVec) body

_ ->
Expand Down
Loading

0 comments on commit 97d06eb

Please sign in to comment.