Skip to content

Commit

Permalink
Add list
Browse files Browse the repository at this point in the history
  • Loading branch information
cruessler committed Feb 18, 2024
1 parent 5ee2c48 commit 5e5d059
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/elm/Compiler/Ast/Primitive.elm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ all =
, Primitive2 { name = "lessp", f = P.lessp }
, Primitive2 { name = "less?", f = P.lessp }
, Primitive2 { name = "remainder", f = P.remainder }
, PrimitiveN { name = "list", f = P.list, numberOfDefaultArguments = 2 }
, Primitive2 { name = "fput", f = P.fput }
, Primitive2 { name = "lput", f = P.lput }
, PrimitiveN { name = "array", f = P.array, numberOfDefaultArguments = 1 }
Expand Down
28 changes: 20 additions & 8 deletions app/elm/Vm/Primitive.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Vm.Primitive exposing
, greaterp
, integerp
, lessp
, list
, listp
, lput
, lshift
Expand Down Expand Up @@ -181,8 +182,8 @@ count value =
Type.Float float ->
float |> String.fromFloat |> String.length

Type.List list ->
List.length list
Type.List list_ ->
List.length list_

Type.Array { items } ->
Array.length items
Expand Down Expand Up @@ -568,6 +569,17 @@ word value1 value2 =
Ok <| Type.Word <| (Type.toString value1 ++ Type.toString value2)


{-|
> outputs a list whose members are its inputs, which can be any Logo datum
> (word, list, or array).
-}
list : List Type.Value -> Result Error Type.Value
list =
Ok << Type.List


{-| Join a list of values into a list. One level of nesting will be flattened.
sentence [ Word "a", Word "b ] == Ok (List [ Word "a", Word "b" ])
Expand All @@ -580,8 +592,8 @@ sentence =
toList : Type.Value -> List Type.Value
toList value =
case value of
Type.List list ->
list
Type.List list_ ->
list_

_ ->
[ value ]
Expand All @@ -599,8 +611,8 @@ sentence =
fput : Type.Value -> Type.Value -> Result Error Type.Value
fput value1 value2 =
case ( value1, value2 ) of
( _, Type.List list ) ->
Ok <| Type.List <| value1 :: list
( _, Type.List list_ ) ->
Ok <| Type.List <| value1 :: list_

_ ->
let
Expand All @@ -625,8 +637,8 @@ fput value1 value2 =
lput : Type.Value -> Type.Value -> Result Error Type.Value
lput value1 value2 =
case ( value1, value2 ) of
( _, Type.List list ) ->
Ok <| Type.List <| list ++ [ value1 ]
( _, Type.List list_ ) ->
Ok <| Type.List <| list_ ++ [ value1 ]

_ ->
let
Expand Down
4 changes: 4 additions & 0 deletions tests/Test/Run/Builtin.elm
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ primitives =
]
, describe "remainder" <|
[ printsLine "print remainder 1 1" "0" ]
, describe "list" <|
[ printsLine "print (list 1 2)" "1 2"
, printsLine "print (list (list 3 4) 1 2)" "[3 4] 1 2"
]
, describe "sentence" <|
[ printsLine "print sentence 1 1" "1 1"
, printsLine "print se 1 1" "1 1"
Expand Down

0 comments on commit 5e5d059

Please sign in to comment.