aboutsummaryrefslogtreecommitdiff
path: root/Sil/Compile.icl
diff options
context:
space:
mode:
authorCamil Staps2017-07-19 12:25:45 +0000
committerCamil Staps2017-07-19 12:25:45 +0000
commit2788df88e6261ac641adf9f39bbfe517a7d77d9c (patch)
tree49ee4f4932dccf4b06782bd4879898dfa6fdfe4e /Sil/Compile.icl
parentAdd readme and license (diff)
Add else if
Diffstat (limited to 'Sil/Compile.icl')
-rw-r--r--Sil/Compile.icl38
1 files changed, 17 insertions, 21 deletions
diff --git a/Sil/Compile.icl b/Sil/Compile.icl
index 42fa162..abed973 100644
--- a/Sil/Compile.icl
+++ b/Sil/Compile.icl
@@ -190,28 +190,24 @@ where
gen (Return (Just app)) = comment "Return" *> gen app *> cleanup *> tell ['ABC'.Rtn]
gen (Return Nothing) = comment "Return" *> cleanup *> tell ['ABC'.Rtn]
gen (MachineStm s) = tell ['ABC'.Raw s]
- gen (If c t Nothing) =
+ gen (If blocks else) =
fresh "ifend" >>= \end ->
- comment "condition" *>
- gen c *>
- toBStack 'ABC'.BT_Bool 1 *>
- tell [ 'ABC'.JmpFalse end ] *>
- comment "if-true" *>
- gen t *>
- tell [ 'ABC'.Label end ]
- gen (If c t (Just e)) =
- fresh "else" >>= \else -> fresh "ifend" >>= \end ->
- comment "condition" *>
- gen c *>
- toBStack 'ABC'.BT_Bool 1 *>
- tell [ 'ABC'.JmpFalse else ] *>
- comment "if-true" *>
- gen t *>
- tell [ 'ABC'.Jmp end
- , 'ABC'.Label else ] *>
- comment "if-false" *>
- gen e *>
- tell [ 'ABC'.Label end ]
+ mapM_ (genifblock end) blocks *>
+ genelse end else
+ where
+ genifblock :: 'ABC'.Label (Application, CodeBlock) -> Gen ()
+ genifblock end (cond, cb) =
+ fresh "ifelse" >>= \else ->
+ gen cond *>
+ toBStack 'ABC'.BT_Bool 1 *>
+ tell [ 'ABC'.JmpFalse else ] *>
+ gen cb *>
+ tell [ 'ABC'.Jmp end
+ , 'ABC'.Label else ]
+
+ genelse :: 'ABC'.Label (Maybe CodeBlock) -> Gen ()
+ genelse end Nothing = tell ['ABC'.Label end]
+ genelse end (Just cb) = gen cb *> tell ['ABC'.Label end]
instance gen Application
where