import Dict exposing (Dict, get, insert)
import Result exposing (andThen)
import String exposing (lines, replace, words)
+import Integer exposing (Integer)
type JoyType
= JoySymbol String
- | JoyInt Int
+ | JoyInt Integer
| JoyList (List JoyType)
| JoyTrue
| JoyFalse
joy_loop stack expression
"+" ->
- joy_binary_math_op (+) stack expression
+ joy_binary_math_op (Integer.add) stack expression
"-" ->
- joy_binary_math_op (-) stack expression
+ joy_binary_math_op (Integer.sub) stack expression
"*" ->
- joy_binary_math_op (*) stack expression
+ joy_binary_math_op (Integer.mul) stack expression
"/" ->
- joy_binary_math_op (//) stack expression
+ joy_binary_math_op (Integer.add) stack expression
"%" ->
- joy_binary_math_op (swap_args remainderBy) stack expression
+ joy_binary_math_op (swap_args Integer.add) stack expression
"add" ->
- joy_binary_math_op (+) stack expression
+ joy_binary_math_op (Integer.add) stack expression
"sub" ->
- joy_binary_math_op (-) stack expression
+ joy_binary_math_op (Integer.sub) stack expression
"mul" ->
- joy_binary_math_op (*) stack expression
+ joy_binary_math_op (Integer.mul) stack expression
"div" ->
- joy_binary_math_op (//) stack expression
+ joy_binary_math_op (Integer.add) stack expression
"mod" ->
- joy_binary_math_op (swap_args remainderBy) stack expression
+ joy_binary_math_op (swap_args Integer.add) stack expression
"<" ->
- joy_comparison_op (<) stack expression
+ joy_comparison_op (Integer.lt) stack expression
">" ->
- joy_comparison_op (>) stack expression
+ joy_comparison_op (Integer.gt) stack expression
"<=" ->
- joy_comparison_op (<=) stack expression
+ joy_comparison_op (Integer.lt) stack expression
">=" ->
- joy_comparison_op (>=) stack expression
+ joy_comparison_op (Integer.gte) stack expression
"<>" ->
joy_comparison_op (/=) stack expression
joy_comparison_op (/=) stack expression
"=" ->
- joy_comparison_op (==) stack expression
-
- "&&" ->
- joy_binary_math_op Bitwise.and stack expression
-
- "||" ->
- joy_binary_math_op Bitwise.or stack expression
-
- "xor" ->
- joy_binary_math_op Bitwise.xor stack expression
-
- "lshift" ->
- joy_binary_math_op (swap_args Bitwise.shiftLeftBy) stack expression
-
- "<<" ->
- joy_binary_math_op (swap_args Bitwise.shiftLeftBy) stack expression
-
- "rshift" ->
- joy_binary_math_op (swap_args Bitwise.shiftRightBy) stack expression
-
- ">>" ->
- joy_binary_math_op (swap_args Bitwise.shiftRightBy) stack expression
+ joy_comparison_op (Integer.eq) stack expression
"/\\" ->
joy_logical_op (&&) stack expression
Err msg
-joy_binary_math_op : (Int -> Int -> Int) -> JoyFunction
+joy_binary_math_op : (Integer -> Integer -> Integer) -> JoyFunction
joy_binary_math_op op stack expression =
case pop_int stack of
Ok ( a, s0 ) ->
Err msg
-swap_args : (Int -> Int -> Int) -> (Int -> Int -> Int)
+swap_args : (Integer -> Integer -> Integer) -> (Integer -> Integer -> Integer)
swap_args op =
\a b -> op b a
-joy_comparison_op : (Int -> Int -> Bool) -> JoyFunction
+joy_comparison_op : (Integer -> Integer -> Bool) -> JoyFunction
joy_comparison_op op stack expression =
case pop_int stack of
Ok ( a, s0 ) ->
Ok ( stack, expression )
JoyInt i ->
- if 0 == i then
+ if Integer.eq Integer.zero i then
Ok ( JoyFalse :: s0, expression )
else
JoyFalse :: stack
-push_int : Int -> JList -> JList
+push_int : Integer -> JList -> JList
push_int i stack =
JoyInt i :: stack
j :: stack
-pop_int : JList -> Result JoyErr ( Int, JList )
+pop_int : JList -> Result JoyErr ( Integer, JList )
pop_int stack =
pop_any stack |> andThen isnt_int
Ok ( item, rest )
-isnt_int : ( JoyType, JList ) -> Result JoyErr ( Int, JList )
+isnt_int : ( JoyType, JList ) -> Result JoyErr ( Integer, JList )
isnt_int ( item, stack ) =
case item of
JoyInt i ->
name
JoyInt n ->
- String.fromInt n
+ Integer.toString n
JoyTrue ->
"true"
JoyFalse
_ ->
- case String.toInt tok of
+ case Integer.fromString tok of
Just i ->
JoyInt i