Skip to content

Commit c2bfffa

Browse files
committed
[feat:Analyser] also analyse the conditions separately in Conditional
1 parent 1db4b69 commit c2bfffa

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

factorial.axl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
(defun factorial [(num: int)] {
2-
(if (== num 1) 1 (*i num (factorial (-i num 1))))
1+
(defun (factorial: int) [(num: int)] {
2+
(if (== num 1) 1 (* num (factorial (- num 1))))
33
})
44

55
(print (factorial 5))

src/Analyser/Analyser.hs

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import Data.Either.Combinators (fromLeft', fromRight, fromRight', isLeft, maybeT
2828
import qualified Data.HashTable.IO as H
2929
import Data.Maybe (fromJust, fromMaybe, isJust)
3030
import Data.Text as T (Text, empty, pack, toLower, unpack)
31+
import Debug.Trace (trace)
3132
import Parser.Ast
3233
( Expr (ArbitraryBlock, Array, Conditional, FunctionCall, FunctionDef, Nil, Root, VariableDef, VariableUsage),
3334
VDataType (Bool, Function, Inferred, NilType),
@@ -80,7 +81,12 @@ analyseExprs acc' curr = do
8081
-- cond :: Expr -> the condition to evaluate, must return bool
8182
-- ift :: Expr -> the Expr to return if cond is **true**
8283
-- iff :: Expr -> the Expr to return if cond is **false**
83-
Conditional cond ift iff -> analyseConditional acc cond ift iff
84+
Conditional cond ift iff -> do
85+
env <- get
86+
v <- sequence . fst <$> liftIO (runStateT (foldl analyseExprs (pure []) [cond, ift, iff]) env)
87+
case v of
88+
Left err -> pure $ makeLeft err
89+
Right _ -> analyseConditional acc cond ift iff
8490
-- body :: [Expr] -> the set of exprs that the block is formed by
8591
ArbitraryBlock body -> analyseArbitraryBlock acc body analyseExprs
8692
-- name :: Text -> the name of the function

src/Analyser/Util.hs

+6-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ getTypeOfExpr ex gd = case ex of
9797
x -> pure $ Left $ "Variable of type '" <> pack (show x) <> "' is not callable"
9898
Analyser.Util.Function vdt _ _ _ -> pure $ Right vdt
9999
Analyser.Util.Argument vdt -> undefined -- TODO
100-
Analyser.Util.IncompleteFunction args vdt native -> pure $ Right vdt
100+
Analyser.Util.IncompleteFunction args vdt native -> do
101+
case vdt of
102+
Inferred -> do
103+
putStrLn "You're trying to call a recursive function without the manual definition of it's return type!"
104+
pure $ Right vdt
105+
_ -> pure $ Right vdt
101106
ArbitraryBlock exprs -> getTypeOfExpr (Prelude.last exprs) gd
102107
-- ideally we want type of ift to be the same as the type of iff
103108
-- but analyseExprs will bail out if type of ift /= type of iff

src/Parser/Ast.hs

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ data Expr
3030
| VariableDef {name :: Text, vtype :: VDataType, val :: Expr} -- like (def username 20)
3131
| Unary UnaryOp Expr -- only - (for now)
3232
-- like (defun (function-name: VDataType) (arg1: DataType, arg2: DataType) { ... })
33-
-- the bool is true if the function is a native function
3433
| FunctionDef
3534
{ name :: Text,
3635
returnType :: VDataType,

0 commit comments

Comments
 (0)