aboutsummaryrefslogtreecommitdiff
path: root/Sil/Compile.icl
diff options
context:
space:
mode:
Diffstat (limited to 'Sil/Compile.icl')
-rw-r--r--Sil/Compile.icl16
1 files changed, 9 insertions, 7 deletions
diff --git a/Sil/Compile.icl b/Sil/Compile.icl
index 4313c0e..495f277 100644
--- a/Sil/Compile.icl
+++ b/Sil/Compile.icl
@@ -17,6 +17,7 @@ from Text import <+
import qualified ABC.Assembler as ABC
import Sil.Syntax
+import Sil.Util
instance toString CompileError
where
@@ -62,7 +63,7 @@ fresh = gets labels
*> pure (hd labs)
reserveVar :: Int Name -> Gen Int
-reserveVar i n = modify (\cs -> {cs & addresses='M'.put n (StackAddr i) cs.addresses}) *> pure (i + 1)
+reserveVar i n = modify (\cs -> {cs & addresses='M'.put n (StackAddr i) cs.addresses}) *> pure (i+1)
class gen a :: a -> Gen ()
@@ -72,7 +73,7 @@ where
instance gen CodeBlock
where
- gen cb = foldM reserveVar 1 [i.init_name \\ i <- cb.cb_init] *>
+ gen cb = foldM reserveVar 0 [i.init_name \\ i <- cb.cb_init] *>
mapM_ gen cb.cb_init *>
mapM_ gen cb.cb_content
@@ -82,8 +83,9 @@ where
instance gen Statement
where
- gen (Declaration n app) = gets addresses >>= \addrs -> case 'M'.get n addrs of
- Just (StackAddr i) -> comment "Declaration" *> gen app *> tell ['ABC'.Fill_a 0 i]
+ gen st=:(Declaration n app) = gets addresses >>= \addrs -> case 'M'.get n addrs of
+ Just (StackAddr i) -> comment (toString st) *> gen app *>
+ tell ['ABC'.Update_a 0 $ i+1, 'ABC'.Pop_a 1]
Just (LabelAddr _) -> liftT $ Error VariableLabel
_ -> liftT $ Error $ UndefinedName n
gen (Application app) = comment "Application" *> gen app
@@ -93,11 +95,11 @@ where
instance gen Application
where
gen (Name n) = gets addresses >>= \addrs -> case 'M'.get n addrs of
- Just (StackAddr i) -> comment "Retrieve name" *> tell ['ABC'.Push_a i]
+ Just (StackAddr i) -> tell ['ABC'.Push_a i]
Just (LabelAddr _) -> liftT $ Error VariableLabel
_ -> liftT $ Error $ UndefinedName n
- gen (Literal (BLit b)) = comment "Literal" *> tell ['ABC'.Create, 'ABC'.FillB b 0]
- gen (Literal (ILit i)) = comment "Literal" *> tell ['ABC'.Create, 'ABC'.FillI i 0]
+ gen (Literal (BLit b)) = tell ['ABC'.Create, 'ABC'.FillB b 0]
+ gen (Literal (ILit i)) = tell ['ABC'.Create, 'ABC'.FillI i 0]
gen (App n args) = gets addresses >>= \addrs -> case 'M'.get n addrs of
Just (LabelAddr l) ->
comment "Retrieve arguments" *> mapM gen args *>