@@ -38,21 +38,43 @@ getStr x = case x of
3838 FunctionDef txt vdt x1 exs b -> " nil"
3939 y -> error $ " could not get string from " <> show y
4040
41- evalNative :: T. Text -> VDataType -> [Expr ] -> IO Expr
42- evalNative " +i" Int args = pure $ IntLiteral $ round (sum (map getNumber args))
43- evalNative " +f" Float args = pure $ FloatLiteral (sum (map getNumber args))
44- evalNative " -i" Int args = pure $ IntLiteral $ round (foldl1 (-) (map getNumber args))
45- evalNative " -f" Float args = pure $ FloatLiteral (foldl1 (-) (map getNumber args))
46- evalNative " *i" Int args = pure $ IntLiteral $ round (product (map getNumber args))
47- evalNative " *f" Float args = pure $ FloatLiteral (product (map getNumber args))
48- evalNative " /i" Int args = pure $ IntLiteral $ round (foldl1 (/) (map getNumber args))
49- evalNative " /f" Float args = pure $ FloatLiteral (foldl1 (/) (map getNumber args))
50- evalNative " str" String args = pure $ StrLiteral (foldl1 (<>) (map getStr args))
51- evalNative " print" NilType args = (putStrLn . T. unpack) (foldl1 (<>) (map getStr args)) >> pure Nil
52- evalNative " >" Bool exprs =
53- if length exprs == 2
54- then case (head exprs, exprs !! 1 ) of
55- (IntLiteral iv, IntLiteral iv') -> pure $ BoolLiteral (iv > iv')
56- _ -> undefined -- TODO
57- else error " can only equality check two arguments"
58- evalNative name _ _ = error $ " evaluator does not know how to execute the native function '" <> T. unpack name <> " '"
41+ evalNative :: (T. Text , VDataType ) -> [Expr ] -> IO Expr
42+ evalNative nt args
43+ | nt == (" +i" , Int ) = pure $ IntLiteral $ round (sum (map getNumber args))
44+ | nt == (" +f" , Float ) = pure $ FloatLiteral (sum (map getNumber args))
45+ | nt == (" -i" , Int ) = pure $ IntLiteral $ round (foldl1 (-) (map getNumber args))
46+ | nt == (" -f" , Float ) = pure $ FloatLiteral (foldl1 (-) (map getNumber args))
47+ | nt == (" *i" , Int ) = pure $ IntLiteral $ round (product (map getNumber args))
48+ | nt == (" *f" , Float ) = pure $ FloatLiteral (product (map getNumber args))
49+ | nt == (" /i" , Int ) = pure $ IntLiteral $ round (foldl1 (/) (map getNumber args))
50+ | nt == (" /f" , Float ) = pure $ FloatLiteral (foldl1 (/) (map getNumber args))
51+ | nt == (" str" , String ) = pure $ StrLiteral (foldl1 (<>) (map getStr args))
52+ | nt == (" print" , NilType ) = (putStrLn . T. unpack) (foldl1 (<>) (map getStr args)) >> pure Nil
53+ | nt == (" >" , Bool )
54+ || nt == (" >=" , Bool )
55+ || nt == (" <" , Bool )
56+ || nt == (" <=" , Bool )
57+ || nt == (" /=" , Bool )
58+ || nt == (" ==" , Bool ) =
59+ if length args == 2
60+ then case (head args, args !! 1 ) of
61+ (IntLiteral iv, IntLiteral iv') ->
62+ pure $
63+ BoolLiteral
64+ ( ( case fst nt of
65+ " >" -> (>)
66+ " >=" -> (>=)
67+ " <" -> (<)
68+ " <=" -> (<=)
69+ " ==" -> (==)
70+ " !=" -> (/=)
71+ )
72+ iv
73+ iv'
74+ )
75+ (FloatLiteral iv, IntLiteral iv') -> pure $ BoolLiteral (iv > fromIntegral iv')
76+ (IntLiteral iv, FloatLiteral iv') -> pure $ BoolLiteral (fromIntegral iv > iv')
77+ (FloatLiteral iv, FloatLiteral iv') -> pure $ BoolLiteral (iv > iv')
78+ (x, y) -> error $ " Can't compare " <> show x <> " with " <> show y
79+ else error " can only equality check two arguments"
80+ | otherwise = error $ " evaluator does not know how to execute the native function '" <> T. unpack (fst nt) <> " '"
0 commit comments