Check types

This commit is contained in:
Eugen Wissner 2024-08-09 20:16:41 +02:00
parent 18a299098c
commit 5e7683af32
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 17 additions and 6 deletions

5
TODO
View File

@ -1,4 +1 @@
# Type analysis
- Iterate the tree and apply the expression function on procedure expressions.
- Check statement types.
# Register allocation

View File

@ -5,7 +5,7 @@ module Language.Elna.TypeAnalysis
import Control.Applicative (Alternative(..))
import Control.Monad.Trans.Except (Except, runExcept, throwE)
import Control.Monad.Trans.Reader (ReaderT, asks, runReaderT)
import Control.Monad.Trans.Reader (ReaderT, asks, runReaderT, withReaderT, ask)
import qualified Data.Vector as Vector
import qualified Language.Elna.AST as AST
import Language.Elna.Location (Identifier(..))
@ -56,7 +56,21 @@ typeAnalysis globalTable = either Just (const Nothing)
. program
program :: AST.Program -> TypeAnalysis ()
program (AST.Program _declarations) = pure ()
program (AST.Program declarations) = traverse_ declaration declarations
declaration :: AST.Declaration -> TypeAnalysis ()
declaration (AST.ProcedureDefinition procedureName _ _ body) = do
globalTable <- TypeAnalysis ask
case SymbolTable.lookup procedureName globalTable of
Just (ProcedureInfo localTable _) -> TypeAnalysis
$ withReaderT (const localTable)
$ runTypeAnalysis
$ traverse_ (statement globalTable) body
Just anotherInfo -> TypeAnalysis $ lift $ throwE
$ UnexpectedProcedureInfoError anotherInfo
Nothing -> TypeAnalysis $ lift $ throwE
$ UndefinedSymbolError procedureName
declaration _ = pure ()
statement :: SymbolTable -> AST.Statement -> TypeAnalysis ()
statement globalTable = \case