From 147967c04b0efeec4246b8ba0ff9b6961c9fe6cd Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 28 Nov 2024 16:28:19 +0100 Subject: [PATCH] Reverse procedure parameters on the stack --- lib/Language/Elna/Glue.hs | 9 +++++++-- tests/expectations/print_in_proc.txt | 1 + tests/vm/print_in_proc.elna | 5 +++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/Language/Elna/Glue.hs b/lib/Language/Elna/Glue.hs index 79ae461..f5bd005 100644 --- a/lib/Language/Elna/Glue.hs +++ b/lib/Language/Elna/Glue.hs @@ -72,8 +72,9 @@ declaration -> AST.Declaration -> Glue (Maybe (AST.Identifier, Vector (Quadruple Variable))) declaration globalTable (AST.ProcedureDeclaration procedureName parameters variableDeclarations statements) - = traverseWithIndex registerVariable variableDeclarations - >> traverseWithIndex registerParameter parameters + = Glue (modify' resetTemporaryCounter) + >> traverseWithIndex registerVariable variableDeclarations + >> traverseWithIndex registerParameter (reverse parameters) >> nameQuadruplesTuple <$> traverse (statement globalTable) statements where traverseWithIndex f = traverse_ (uncurry f) . zip [0..] @@ -91,6 +92,10 @@ declaration globalTable (AST.ProcedureDeclaration procedureName parameters varia $ flip Vector.snoc StopQuadruple $ fold quadrupleList ) + resetTemporaryCounter paste = paste + { temporaryCounter = 0 + , localMap = mempty + } declaration _ (AST.TypeDefinition _ _) = pure Nothing statement :: SymbolTable -> AST.Statement -> Glue (Vector (Quadruple Variable)) diff --git a/tests/expectations/print_in_proc.txt b/tests/expectations/print_in_proc.txt index 8351c19..bdf281c 100644 --- a/tests/expectations/print_in_proc.txt +++ b/tests/expectations/print_in_proc.txt @@ -1 +1,2 @@ 14 +8 diff --git a/tests/vm/print_in_proc.elna b/tests/vm/print_in_proc.elna index 8c4588b..244c551 100644 --- a/tests/vm/print_in_proc.elna +++ b/tests/vm/print_in_proc.elna @@ -1,7 +1,8 @@ -proc print2(a: int) { +proc print2(a: int, b: int) { printi(a); + printi(b); } proc main() { - print2(14); + print2(14, 8); }