diff options
| author | Eugen Wissner <belka@caraus.de> | 2024-10-11 16:14:01 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2024-10-11 16:14:01 +0200 |
| commit | 0850f0a8d66af028e32a79063cdad328b70db909 (patch) | |
| tree | 8c1fa49d5476e706e94a7af62ce37b12ef65e32d /lib/Language/Elna/Frontend | |
| parent | 87f183baad01f2b572f5f9051895b5876a56dd4c (diff) | |
| download | elna-0850f0a8d66af028e32a79063cdad328b70db909.tar.gz | |
Implement if statements with equality
Diffstat (limited to 'lib/Language/Elna/Frontend')
| -rw-r--r-- | lib/Language/Elna/Frontend/AST.hs | 33 | ||||
| -rw-r--r-- | lib/Language/Elna/Frontend/NameAnalysis.hs | 52 | ||||
| -rw-r--r-- | lib/Language/Elna/Frontend/Parser.hs | 28 |
3 files changed, 56 insertions, 57 deletions
diff --git a/lib/Language/Elna/Frontend/AST.hs b/lib/Language/Elna/Frontend/AST.hs index b9ed539..68ac581 100644 --- a/lib/Language/Elna/Frontend/AST.hs +++ b/lib/Language/Elna/Frontend/AST.hs @@ -6,8 +6,8 @@ module Language.Elna.Frontend.AST , Statement(..) , TypeExpression(..) , VariableDeclaration(..) - {-, VariableAccess(..) - , Condition(..)-} + --, VariableAccess(..) + , Condition(..) , Expression(..) , Literal(..) ) where @@ -67,8 +67,8 @@ instance Show TypeExpression data Statement = EmptyStatement - {-| AssignmentStatement VariableAccess Expression | IfStatement Condition Statement (Maybe Statement) + {-| AssignmentStatement VariableAccess Expression | WhileStatement Condition Statement -} | CompoundStatement [Statement] | CallStatement Identifier [Expression] @@ -77,13 +77,13 @@ data Statement instance Show Statement where show EmptyStatement = ";" - {-show (AssignmentStatement lhs rhs) = - concat [show lhs, " := ", show rhs, ";"] show (IfStatement condition if' else') = concat [ "if (", show condition, ") " , show if' , maybe "" ((<> " else ") . show) else' ] + {-show (AssignmentStatement lhs rhs) = + concat [show lhs, " := ", show rhs, ";"] show (WhileStatement expression statement) = concat ["while (", show expression, ") ", show statement, ";"]-} show (CompoundStatement statements) = @@ -143,22 +143,21 @@ instance Show VariableAccess show (VariableAccess variableName) = show variableName show (ArrayAccess arrayAccess elementIndex) = concat [show arrayAccess, "[", show elementIndex, "]"] - +-} data Condition = EqualCondition Expression Expression - | NonEqualCondition Expression Expression - | LessCondition Expression Expression - | GreaterCondition Expression Expression - | LessOrEqualCondition Expression Expression - | GreaterOrEqualCondition Expression Expression + -- | NonEqualCondition Expression Expression + -- | LessCondition Expression Expression + -- | GreaterCondition Expression Expression + -- | LessOrEqualCondition Expression Expression + -- | GreaterOrEqualCondition Expression Expression deriving Eq instance Show Condition where show (EqualCondition lhs rhs) = concat [show lhs, " = ", show rhs] - show (NonEqualCondition lhs rhs) = concat [show lhs, " # ", show rhs] - show (LessCondition lhs rhs) = concat [show lhs, " < ", show rhs] - show (GreaterCondition lhs rhs) = concat [show lhs, " > ", show rhs] - show (LessOrEqualCondition lhs rhs) = concat [show lhs, " <= ", show rhs] - show (GreaterOrEqualCondition lhs rhs) = concat [show lhs, " >= ", show rhs] --} + -- show (NonEqualCondition lhs rhs) = concat [show lhs, " # ", show rhs] + -- show (LessCondition lhs rhs) = concat [show lhs, " < ", show rhs] + -- show (GreaterCondition lhs rhs) = concat [show lhs, " > ", show rhs] + -- show (LessOrEqualCondition lhs rhs) = concat [show lhs, " <= ", show rhs] + -- show (GreaterOrEqualCondition lhs rhs) = concat [show lhs, " >= ", show rhs] diff --git a/lib/Language/Elna/Frontend/NameAnalysis.hs b/lib/Language/Elna/Frontend/NameAnalysis.hs index 18ae6dd..12e51f8 100644 --- a/lib/Language/Elna/Frontend/NameAnalysis.hs +++ b/lib/Language/Elna/Frontend/NameAnalysis.hs @@ -160,17 +160,37 @@ statement globalTable (AST.CallStatement name arguments) >> traverse_ (expression globalTable) arguments statement globalTable (AST.CompoundStatement statements) = traverse_ (statement globalTable) statements -{- statement globalTable (AST.AssignmentStatement lvalue rvalue) - = variableAccess globalTable lvalue - >> expression globalTable rvalue statement globalTable (AST.IfStatement ifCondition ifStatement elseStatement) = condition globalTable ifCondition >> statement globalTable ifStatement >> maybe (pure ()) (statement globalTable) elseStatement -statement globalTable (AST.WhileStatement whileCondition loop) - = condition globalTable whileCondition - >> statement globalTable loop +-- statement globalTable (AST.AssignmentStatement lvalue rvalue) +-- = variableAccess globalTable lvalue +-- >> expression globalTable rvalue +--statement globalTable (AST.WhileStatement whileCondition loop) +-- = condition globalTable whileCondition +-- >> statement globalTable loop +condition :: SymbolTable -> AST.Condition -> NameAnalysis () +condition globalTable (AST.EqualCondition lhs rhs) + = expression globalTable lhs + >> expression globalTable rhs +--condition globalTable (AST.NonEqualCondition lhs rhs) +-- = expression globalTable lhs +-- >> expression globalTable rhs +--condition globalTable (AST.LessCondition lhs rhs) +-- = expression globalTable lhs +-- >> expression globalTable rhs +--condition globalTable (AST.GreaterCondition lhs rhs) +-- = expression globalTable lhs +-- >> expression globalTable rhs +--condition globalTable (AST.LessOrEqualCondition lhs rhs) +-- = expression globalTable lhs +-- >> expression globalTable rhs +--condition globalTable (AST.GreaterOrEqualCondition lhs rhs) +-- = expression globalTable lhs +-- >> expression globalTable rhs +{- variableAccess :: SymbolTable -> AST.VariableAccess -> NameAnalysis () variableAccess globalTable (AST.ArrayAccess arrayExpression indexExpression) = variableAccess globalTable arrayExpression @@ -178,26 +198,6 @@ variableAccess globalTable (AST.ArrayAccess arrayExpression indexExpression) variableAccess globalTable (AST.VariableAccess identifier) = checkSymbol globalTable identifier -condition :: SymbolTable -> AST.Condition -> NameAnalysis () -condition globalTable (AST.EqualCondition lhs rhs) - = expression globalTable lhs - >> expression globalTable rhs -condition globalTable (AST.NonEqualCondition lhs rhs) - = expression globalTable lhs - >> expression globalTable rhs -condition globalTable (AST.LessCondition lhs rhs) - = expression globalTable lhs - >> expression globalTable rhs -condition globalTable (AST.GreaterCondition lhs rhs) - = expression globalTable lhs - >> expression globalTable rhs -condition globalTable (AST.LessOrEqualCondition lhs rhs) - = expression globalTable lhs - >> expression globalTable rhs -condition globalTable (AST.GreaterOrEqualCondition lhs rhs) - = expression globalTable lhs - >> expression globalTable rhs - enter :: Identifier -> Info -> SymbolTable -> NameAnalysis SymbolTable enter identifier info table = maybe (identifierAlreadyDefinedError identifier) pure diff --git a/lib/Language/Elna/Frontend/Parser.hs b/lib/Language/Elna/Frontend/Parser.hs index 9d6bb60..9dd3206 100644 --- a/lib/Language/Elna/Frontend/Parser.hs +++ b/lib/Language/Elna/Frontend/Parser.hs @@ -16,8 +16,8 @@ import Language.Elna.Frontend.AST , Statement(..) , TypeExpression(..) , VariableDeclaration(..) - {-, VariableAccess(..) - , Condition(..)-} + --, VariableAccess(..) + , Condition(..) , Expression(..) , Literal(..) ) @@ -97,7 +97,7 @@ variableAccessP = do identifier <- identifierP indices <- many $ bracketsP expressionP pure $ foldr (flip ArrayAccess) (VariableAccess identifier) indices - +-} conditionP :: Parser Condition conditionP = do lhs <- expressionP @@ -105,14 +105,14 @@ conditionP = do conditionCons lhs <$> expressionP where comparisonOperator = - [ symbol "<" >> pure LessCondition - , symbol "<=" >> pure LessOrEqualCondition - , symbol ">" >> pure GreaterCondition - , symbol ">=" >> pure GreaterOrEqualCondition - , symbol "=" >> pure EqualCondition - , symbol "#" >> pure NonEqualCondition + --, symbol "<" >> pure LessCondition + --, symbol "<=" >> pure LessOrEqualCondition + --, symbol ">" >> pure GreaterCondition + --, symbol ">=" >> pure GreaterOrEqualCondition + [ symbol "=" >> pure EqualCondition + --, symbol "#" >> pure NonEqualCondition ] --} + symbol :: Text -> Parser Text symbol = Lexer.symbol space @@ -182,22 +182,22 @@ procedureDeclarationP = procedureCons statementP :: Parser Statement statementP = EmptyStatement <$ semicolonP + <|> ifElseP {-<|> CompoundStatement <$> blockP (many statementP) <|> try assignmentP - <|> try ifElseP <|> try whileP -} - <|> try callP + <|> callP <?> "statement" where callP = CallStatement <$> identifierP <*> parensP (sepBy expressionP commaP) <* semicolonP - {-ifElseP = IfStatement + ifElseP = IfStatement <$> (symbol "if" *> parensP conditionP) <*> statementP <*> optional (symbol "else" *> statementP) - whileP = WhileStatement + {-whileP = WhileStatement <$> (symbol "while" *> parensP conditionP) <*> statementP assignmentP = AssignmentStatement |
