From 5e7683af32ecb769e986554e12465cbe9acb4a05 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 9 Aug 2024 20:16:41 +0200 Subject: [PATCH] Check types --- TODO | 5 +---- lib/Language/Elna/TypeAnalysis.hs | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/TODO b/TODO index 6a2f85b..46877d5 100644 --- a/TODO +++ b/TODO @@ -1,4 +1 @@ -# Type analysis - -- Iterate the tree and apply the expression function on procedure expressions. -- Check statement types. +# Register allocation diff --git a/lib/Language/Elna/TypeAnalysis.hs b/lib/Language/Elna/TypeAnalysis.hs index 2544947..040a022 100644 --- a/lib/Language/Elna/TypeAnalysis.hs +++ b/lib/Language/Elna/TypeAnalysis.hs @@ -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