From f7b36cb81d96817d1c69ffe9025b88112de7400c Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 24 Nov 2024 13:05:11 +0100 Subject: Implement the while loop --- lib/Language/Elna/Glue.hs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'lib/Language/Elna/Glue.hs') diff --git a/lib/Language/Elna/Glue.hs b/lib/Language/Elna/Glue.hs index 02a9b9f..79ae461 100644 --- a/lib/Language/Elna/Glue.hs +++ b/lib/Language/Elna/Glue.hs @@ -71,14 +71,16 @@ declaration :: SymbolTable -> AST.Declaration -> Glue (Maybe (AST.Identifier, Vector (Quadruple Variable))) -declaration globalTable (AST.ProcedureDeclaration procedureName _ variableDeclarations statements) - = traverse_ registerVariable variableDeclarations +declaration globalTable (AST.ProcedureDeclaration procedureName parameters variableDeclarations statements) + = traverseWithIndex registerVariable variableDeclarations + >> traverseWithIndex registerParameter parameters >> nameQuadruplesTuple <$> traverse (statement globalTable) statements where - registerVariable (AST.VariableDeclaration identifier _) = do - currentCounter <- fmap (fromIntegral . HashMap.size) - $ Glue $ gets $ getField @"localMap" - Glue $ modify' $ modifier identifier $ LocalVariable currentCounter + traverseWithIndex f = traverse_ (uncurry f) . zip [0..] + registerParameter index (AST.Parameter identifier _ _) = + Glue $ modify' $ modifier identifier $ ParameterVariable index + registerVariable index (AST.VariableDeclaration identifier _) = + Glue $ modify' $ modifier identifier $ LocalVariable index modifier identifier currentCounter generator = generator { localMap = HashMap.insert identifier currentCounter $ getField @"localMap" generator @@ -131,7 +133,7 @@ statement localTable (AST.AssignmentStatement variableAccess' assignee) = do . AssignQuadruple rhsOperand <$> lookupLocal identifier pure $ rhsStatements <> lhsStatements -{- statement localTable (AST.WhileStatement whileCondition whileStatement) = do +statement localTable (AST.WhileStatement whileCondition whileStatement) = do (conditionStatements, jumpConstructor) <- condition localTable whileCondition startLabel <- createLabel endLabel <- createLabel @@ -141,7 +143,7 @@ statement localTable (AST.AssignmentStatement variableAccess' assignee) = do <> conditionStatements <> Vector.fromList [jumpConstructor startLabel, GoToQuadruple endLabel, LabelQuadruple startLabel] <> whileStatements - <> Vector.fromList [GoToQuadruple conditionLabel, LabelQuadruple endLabel] -} + <> Vector.fromList [GoToQuadruple conditionLabel, LabelQuadruple endLabel] createTemporary :: Glue Variable createTemporary = do -- cgit v1.2.3