Check types
This commit is contained in:
parent
18a299098c
commit
5e7683af32
5
TODO
5
TODO
@ -1,4 +1 @@
|
||||
# Type analysis
|
||||
|
||||
- Iterate the tree and apply the expression function on procedure expressions.
|
||||
- Check statement types.
|
||||
# Register allocation
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user