Skip to content

Commit

Permalink
Add current progress
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeAbel committed Jan 7, 2024
1 parent 2b5c4c3 commit a3a0beb
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 38 deletions.
4 changes: 2 additions & 2 deletions lvtc/src/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module AST
, VarDeclaration
, VarAssignation
, Condition
) where
) where

import Data.Int (Int32)

Expand Down Expand Up @@ -51,5 +51,5 @@ type VarDeclaration = (Var, Value)
type VarAssignation = (Symbol, Value)

data Instruction =
Function FuncCall | Return Value | Declaration VarDeclaration |
Call FuncCall | Return Value | Declaration VarDeclaration |
Assignation VarAssignation | Cond Condition
126 changes: 90 additions & 36 deletions lvtc/src/TypeCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,93 @@ module Lib

import AST

functions :: [FuncDeclaration],

checkFuncCall

checkCondition :: Condition -> TypeEnv -> Maybe TypeEnv
checkCondition (Symbol s, xs, ys) env =
checkCondition (Type t, xs, ys) env =
checkCondition (FuncCall (symbol, values), xs, ys) env = checkBoolFunc (checkFuncCall (FuncCall (symbol, values)) env) xs ys
checkCondition _ _ = Nothing

checkInstructions :: [Instruction] -> TypeEnv -> Maybe TypeEnv
checkInstructions ((Symbol s):xs) env =
checkInstructions ((Type t):xs) env =
checkInstructions ((FuncCall f):xs) env =
checkInstructions ((Return r):xs) env =
checkInstructions ((VarDeclaration v):xs) env =
checkInstructions ((Condition c):xs) env = checkCondition c env
checkInstructions _ _ = Nothing

checkFunctionInstructions :: Maybe TypeEnv -> [ASTExpression] -> Maybe TypeEnv
checkFunctionInstructions (Just env) xs = typeCheck xs env
checkFunctionInstructions _ _ _ = Nothing

checkFunc :: FuncDeclaration -> TypeEnv -> [ASTExpression] -> Maybe TypeEnv
checkFunc (FuncDeclaration (Prototype (symbol, vars, (Void))) []) env xs = Just env
checkFunc (FuncDeclaration _ []) env xs = Nothing
checkFunc (FuncDeclaration prototype instructions) env xs = checkFuncInstructions (checkInstructions instructions env) xs
checkFunc _ _ _ = Nothing

typeCheck :: [FuncDeclaration] -> TypeEnv -> Maybe TypeEnv
typeCheck ((FuncDeclaration func):xs) (Env functions a) = checkFunc func (Env functions ++ [func] a) xs
typeCheck ((Alias symbol value):xs) env = checkAlias (Alias symbol value) env xs
typeCheck _ _ = Nothing

handleTypeCheck :: [FuncDeclaration] -> Maybe TypeEnv
handleTypeCheck expressions = typeCheck expressions (TypeEnv [] [])
--checkCondition :: Condition -> TypeEnv -> Maybe TypeEnv
--checkCondition (Symbol s, xs, ys) env =
--checkCondition (Type t, xs, ys) env =
--checkCondition (FuncCall (symbol, values), xs, ys) env = checkBoolFunc (checkFuncCall (FuncCall (symbol, values)) env) xs ys
--checkCondition _ _ = Nothing

findVarInParams :: Value -> [Var] -> Maybe Type
findVarInParams (Var s) ((Var s, t):xs)
| s == s = Just t
| otherwise = findVarInParams (Var s) xs
findVarInParams _ [] = Nothing

findVarIn

findValueType :: Value -> [Var] -> [VarAssignation] -> [FuncDeclaration] -> Maybe Type
findSymbol (Var s) params vars env = (findVarParams (Var s) params) <|> ()

checkValueType :: Type -> Maybe Type -> Bool
checkValueType _ Nothing = False
checkValueType t (Just t') = t == t'

assertTypeAndValue :: Type -> Value -> [Var] -> [VarAssignation] -> [FuncDeclaration] -> Bool
assertTypeAndValue _ (Var s) [] [] [] = False
assertTypeAndValue "Int" (Integer _) _ = True
assertTypeAndValue "Bool" (Boolean _) _ = True
assertTypeAndValue t v params vars env = checkValueType $ t (findValueType v params vars env)
assertTypeAndValue _ _ _ = False

isTypeValid :: Type -> Bool
isTypeValid "Int" = True
isTypeValid "Bool" = True
isTypeValid _ = False

checkVars :: [Value] -> Maybe [Var] -> [Var] -> [VarAssignation] -> [FuncDeclaration] -> Bool
checkVarsTypes [] _ _ _ _ = False
checkVarsTypes _ [] _ _ _ = False
checkVarsTypes values param _ _ _ | length fst /= length scd = False
checkVarsTypes [(fs v)] [(ss t)] params vars env = assertTypeAndValue t v params vars env
checkVarsTypes ((fs v):xs) ((ss t):ys) params vars env
| assertTypeAndValue t v params vars env = checkVars xs ys params vars env
| otherwise = False

findFunc :: Symbol -> [FuncDeclaration] -> Maybe [Var]
findFunc s [] = Nothing
findFunc s ((FuncDeclaration (FuncPrototype s params _) _):xs) | s == s = Just params
findFunc s (_:xs) = findFunc s xs

checkCall :: FuncPrototype -> FuncCall -> [VarAssignation] -> [FuncDeclaration] -> [Instructions] -> Maybe [FuncDeclaration]
checkCall (FuncPrototype s params t) (symbol, values) vars env xs
| s == symbol && checkVars values (Just params) params vars env =
checkInstructions (FuncPrototype s param t) xs env vars
| otherwise = Nothing
checkCall (FuncPrototype _ params t) (s, values) vars env xs
| checkVars values (findFunc s env) params vars env =
checkInstructions (FuncPrototype s param t) xs env vars

checkInstructions :: FuncPrototype -> [Instructions] -> [FuncDeclaration] -> [VarAssignation] -> Maybe [FuncDeclaration]
checkInstructions prototype ((Call func):xs) env vars = checkCall prototype func vars env xs
checkInstructions prototype ((Return ret):xs) env vars = checkReturn prototype ret vars env xs
checkInstructions prototype ((Declaration declaration):xs) env vars = checkDeclaration prototype declaration vars env xs
checkInstructions prototype ((Assignation assignation):xs) env vars = checkAssignation prototype declaration vars env xs
checkInstructions prototype ((Condition assignation):xs) env vars = checkAssignation prototype declaration vars env xs
checkInstructions _ _ _ = Nothing

checkVarTypes :: [Var] -> Bool
checkVarTypes [] = False
checkVarTypes [x] = isTypeValid (snd x)
checkVarTypes (x:xs) | isTypeValid (snd x) = checkVarTypes xs

checkFunction :: FuncDeclaration -> [FuncDeclaration] -> Maybe [FuncDeclaration]
checkFunction (FuncDeclaration (FuncPrototype _ args _) _) | not (checkVarTypes args) = Nothing
checkFunction (FuncDeclaration (FuncPrototype _ _ t) _) | not (isTypeValid t) = Nothing
checkFunction (FuncDeclaration prototype instructions) env = checkInstructions prototype instructions env []

checkNotExisting :: FuncDeclaration -> [FuncDeclaration] -> Bool
checkNotExisting _ [] = True
checkNotExisting (FuncDeclaration (FuncPrototype s _ _) _) ((FuncDeclaration (FuncPrototype ls _ _) _):xs)
| s == ls = False
| otherwise = checkNotExisting (FuncDeclaration (FuncPrototype s _ _) _) xs

checkDeclarations :: [FuncDeclaration] -> Maybe [FuncDeclaration] -> Maybe [FuncDeclaration]
checkDeclarations _ Nothing = Nothing
checkDeclarations ((FuncDeclaration func):xs) (Just env)
| checkNotExisting func env =
checkDeclarations xs (checkFunction func env)
| otherwise = Nothing
checkDeclarations _ _ = Nothing

handleTypeCheck :: [FuncDeclaration] -> Maybe [FuncDeclaration]
handleTypeCheck expressions = checkDeclarations expressions (Just [])

0 comments on commit a3a0beb

Please sign in to comment.