Add array assignment to the IR

This commit is contained in:
2024-12-02 13:57:03 +01:00
parent 147967c04b
commit 0c40bca60b
11 changed files with 82 additions and 55 deletions

View File

@ -176,16 +176,16 @@ instance Show Expression
show (DivisionExpression lhs rhs) = concat [show lhs, " / ", show rhs]
show (VariableExpression variable) = show variable
newtype VariableAccess
data VariableAccess
= VariableAccess Identifier
-- | ArrayAccess VariableAccess Expression
| ArrayAccess VariableAccess Expression
deriving Eq
instance Show VariableAccess
where
show (VariableAccess variableName) = show variableName
{- show (ArrayAccess arrayAccess elementIndex) =
concat [show arrayAccess, "[", show elementIndex, "]"] -}
show (ArrayAccess arrayAccess elementIndex) =
concat [show arrayAccess, "[", show elementIndex, "]"]
data Condition
= EqualCondition Expression Expression

View File

@ -202,18 +202,6 @@ condition globalTable (AST.GreaterOrEqualCondition lhs rhs)
variableAccess :: SymbolTable -> AST.VariableAccess -> NameAnalysis ()
variableAccess globalTable (AST.VariableAccess identifier) =
checkSymbol globalTable identifier
{- variableAccess globalTable (AST.ArrayAccess arrayExpression indexExpression)
variableAccess globalTable (AST.ArrayAccess arrayExpression indexExpression)
= variableAccess globalTable arrayExpression
>> expression globalTable indexExpression
enter :: Identifier -> Info -> SymbolTable -> NameAnalysis SymbolTable
enter identifier info table
= maybe (identifierAlreadyDefinedError identifier) pure
$ SymbolTable.enter identifier info table
identifierAlreadyDefinedError :: Identifier -> NameAnalysis a
identifierAlreadyDefinedError = NameAnalysis
. lift
. throwE
. IdentifierAlreadyDefinedError
-}

View File

@ -93,10 +93,10 @@ expressionP :: Parser Expression
expressionP = makeExprParser termP operatorTable
variableAccessP :: Parser VariableAccess
variableAccessP = VariableAccess <$> identifierP {- do
variableAccessP = do
identifier <- identifierP
indices <- many $ bracketsP expressionP
pure $ foldr (flip ArrayAccess) (VariableAccess identifier) indices -}
pure $ foldr (flip ArrayAccess) (VariableAccess identifier) indices
conditionP :: Parser Condition
conditionP = do

View File

@ -11,7 +11,7 @@ import Control.Monad.Trans.Reader (ReaderT, runReaderT, withReaderT, ask, asks)
import Data.Foldable (traverse_)
import qualified Data.Vector as Vector
import qualified Language.Elna.Frontend.AST as AST
import Language.Elna.Frontend.SymbolTable (Info(..), {-ParameterInfo(..), -}SymbolTable)
import Language.Elna.Frontend.SymbolTable (Info(..), ParameterInfo(..), SymbolTable)
import qualified Language.Elna.Frontend.SymbolTable as SymbolTable
import Language.Elna.Frontend.Types (Type(..), booleanType, intType)
import Language.Elna.Location (Identifier(..))
@ -31,11 +31,10 @@ data Error
| ArithmeticExpressionError Type
| ComparisonExpressionError Type Type
| InvalidConditionTypeError Type
{- | InvalidAssignmentError Type
| ExpectedLvalueError AST.Expression
| ArgumentTypeMismatchError Type Type
{- | ExpectedLvalueError AST.Expression
| ArgumentTypeMismatchError Type Type -}
| ArrayIndexError Type
| ArrayAccessError Type -}
| ArrayAccessError Type
deriving Eq
instance Show Error
@ -56,6 +55,10 @@ instance Show Error
<> show lhs <> "\" and \"" <> show rhs <> "\""
show (InvalidConditionTypeError got) =
"Expected a condition to be a boolean, got: " <> show got
show (ArrayIndexError got) =
"Expected an array index expression to be an integer, got: " <> show got
show (ArrayAccessError got) =
"Expected to encounter an array, got: " <> show got
newtype TypeAnalysis a = TypeAnalysis
{ runTypeAnalysis :: ReaderT SymbolTable (Except Error) a
@ -127,7 +130,7 @@ statement globalTable = \case
Nothing -> TypeAnalysis $ lift $ throwE
$ UndefinedSymbolError procedureName
where
checkArgument SymbolTable.ParameterInfo{} _argument = pure () {-
checkArgument ParameterInfo{} _argument = pure () {-
argumentType <- expression globalTable argument
unless (argumentType == type')
$ TypeAnalysis $ lift $ throwE $ ArgumentTypeMismatchError type' argumentType
@ -145,7 +148,7 @@ variableAccess globalTable (AST.VariableAccess identifier) = do
$ UnexpectedVariableInfoError anotherInfo
Nothing -> TypeAnalysis $ lift $ throwE
$ UndefinedSymbolError identifier
{-variableAccess globalTable (AST.ArrayAccess arrayExpression indexExpression) = do
variableAccess globalTable (AST.ArrayAccess arrayExpression indexExpression) = do
arrayType <- variableAccess globalTable arrayExpression
indexType <- expression globalTable indexExpression
unless (indexType == intType)
@ -154,7 +157,7 @@ variableAccess globalTable (AST.VariableAccess identifier) = do
ArrayType _ baseType -> pure baseType
nonArrayType -> TypeAnalysis $ lift $ throwE
$ ArrayAccessError nonArrayType
-}
expression :: SymbolTable -> AST.Expression -> TypeAnalysis Type
expression globalTable = \case
AST.VariableExpression variableExpression ->