Skip to content

Commit

Permalink
Add lput
Browse files Browse the repository at this point in the history
  • Loading branch information
cruessler committed Feb 6, 2024
1 parent 5aa351a commit d23b09a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 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 @@ -43,6 +43,7 @@ all =
, Primitive2 { name = "less?", f = P.lessp }
, Primitive2 { name = "remainder", f = P.remainder }
, Primitive2 { name = "fput", f = P.fput }
, Primitive2 { name = "lput", f = P.lput }
, PrimitiveN { name = "array", f = P.array, numberOfDefaultArguments = 1 }
, PrimitiveN { name = "sentence", f = P.sentence, numberOfDefaultArguments = 2 }
, PrimitiveN { name = "se", f = P.sentence, numberOfDefaultArguments = 2 }
Expand Down
27 changes: 27 additions & 0 deletions app/elm/Vm/Primitive.elm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Vm.Primitive exposing
, integerp
, lessp
, listp
, lput
, lshift
, minus
, notequalp
Expand Down Expand Up @@ -613,6 +614,32 @@ fput value1 value2 =
Err <| WrongInput "fput" word_


{-|
> outputs a list equal to its second input with one extra member, the first
> input, at the end. If the second input is a word, then the first input must
> be a one-letter word, and LPUT is equivalent to WORD with its inputs in the
> other order.
-}
lput : Type.Value -> Type.Value -> Result Error Type.Value
lput value1 value2 =
case ( value1, value2 ) of
( _, Type.List list ) ->
Ok <| Type.List <| list ++ [ value1 ]

_ ->
let
word_ =
Type.toString value1
in
if String.length word_ == 1 then
Ok <| Type.Word <| Type.toString value2 ++ word_

else
Err <| WrongInput "lput" word_


{-|
> outputs an array of "size" members (must be a positive integer), each of which
Expand Down
12 changes: 12 additions & 0 deletions tests/Test/Primitive.elm
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ primitives =
(P.fput (T.Word "wo") (T.Word "rd"))
(Err <| Error.WrongInput "fput" "wo")
]
, describe "lput"
[ test "works with 2 strings" <|
\_ ->
Expect.equal
(P.lput (T.Word "w") (T.List [ T.Word "o" ]))
(Ok <| T.List [ T.Word "o", T.Word "w" ])
, test "fails with 2 words if first word has more than one character" <|
\_ ->
Expect.equal
(P.lput (T.Word "wo") (T.Word "rd"))
(Err <| Error.WrongInput "lput" "wo")
]
, describe "bitand"
[ test "works with 2 integers" <|
\_ ->
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 @@ -142,6 +142,10 @@ primitives =
[ printsLine "print fput \"w \"ord" "word"
, printsLine "print fput \"w [ o r d ]" "w o r d"
]
, describe "lput" <|
[ printsLine "print lput \"w \"ord" "ordw"
, printsLine "print lput \"w [ o r d ]" "o r d w"
]
]


Expand Down

0 comments on commit d23b09a

Please sign in to comment.