Generate IR for while and calls

This commit is contained in:
Eugen Wissner 2024-08-18 20:13:59 +02:00
parent 6a54b66421
commit 2bd965bd5c
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0

View File

@ -6,6 +6,7 @@ module Language.Elna.Intermediate
, intermediate
) where
import Data.Bifunctor (Bifunctor(..))
import Data.Int (Int32)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
@ -17,7 +18,6 @@ import qualified Language.Elna.AST as AST
import Language.Elna.Types (Type(..))
import Language.Elna.SymbolTable (SymbolTable, Info(..))
import qualified Language.Elna.SymbolTable as SymbolTable
import Data.Maybe (fromMaybe)
data Operand
= VariableOperand Variable
@ -91,6 +91,26 @@ statement localTable (AST.IfStatement ifCondition ifStatement elseStatement) =
<> Vector.snoc ifStatements (LabelQuadruple endLabel)
Nothing -> Vector.fromList [jumpConstructor ifLabel, GoToQuadruple endLabel, LabelQuadruple ifLabel]
<> Vector.snoc ifStatements (LabelQuadruple endLabel)
statement localTable (AST.WhileStatement whileCondition whileStatement) =
let (conditionStatements, jumpConstructor) = condition localTable whileCondition
whileStatements = statement localTable whileStatement
startLabel = Label "L3"
endLabel = Label "L4"
conditionLabel = Label "L5"
in Vector.fromList [LabelQuadruple conditionLabel]
<> conditionStatements
<> Vector.fromList [jumpConstructor startLabel, GoToQuadruple endLabel, LabelQuadruple startLabel]
<> whileStatements
<> Vector.fromList [GoToQuadruple conditionLabel, LabelQuadruple endLabel]
statement localTable (AST.CallStatement (AST.Identifier callName) arguments) =
let (parameterStatements, argumentStatements)
= bimap (Vector.fromList . fmap ParameterQuadruple) Vector.concat
$ unzip
$ expression localTable <$> arguments
in Vector.snoc (argumentStatements <> parameterStatements)
$ CallQuadruple (Variable callName)
$ fromIntegral
$ Vector.length argumentStatements
statement localTable (AST.CompoundStatement statements) =
foldMap (statement localTable) statements