From 2788df88e6261ac641adf9f39bbfe517a7d77d9c Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 19 Jul 2017 12:25:45 +0000 Subject: Add else if --- Sil/Compile.icl | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) (limited to 'Sil/Compile.icl') 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 -- cgit v1.2.3