Check types
This commit is contained in:
parent
18a299098c
commit
5e7683af32
5
TODO
5
TODO
@ -1,4 +1 @@
|
|||||||
# Type analysis
|
# Register allocation
|
||||||
|
|
||||||
- Iterate the tree and apply the expression function on procedure expressions.
|
|
||||||
- Check statement types.
|
|
||||||
|
@ -5,7 +5,7 @@ module Language.Elna.TypeAnalysis
|
|||||||
|
|
||||||
import Control.Applicative (Alternative(..))
|
import Control.Applicative (Alternative(..))
|
||||||
import Control.Monad.Trans.Except (Except, runExcept, throwE)
|
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 Data.Vector as Vector
|
||||||
import qualified Language.Elna.AST as AST
|
import qualified Language.Elna.AST as AST
|
||||||
import Language.Elna.Location (Identifier(..))
|
import Language.Elna.Location (Identifier(..))
|
||||||
@ -56,7 +56,21 @@ typeAnalysis globalTable = either Just (const Nothing)
|
|||||||
. program
|
. program
|
||||||
|
|
||||||
program :: AST.Program -> TypeAnalysis ()
|
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 :: SymbolTable -> AST.Statement -> TypeAnalysis ()
|
||||||
statement globalTable = \case
|
statement globalTable = \case
|
||||||
|
Loading…
Reference in New Issue
Block a user