From 9b71b9206cd6854877262de2aa1c76421aa899be Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Mon, 30 Jan 2017 09:34:05 +0100 Subject: Implement >= and <= --- codegen.hs | 18 ++++++++++++++++++ fibo.nl | 35 +++++++++++++++++++++++++++++++++++ parser.hs | 8 ++++---- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 fibo.nl diff --git a/codegen.hs b/codegen.hs index bf620e4..b8d8c54 100644 --- a/codegen.hs +++ b/codegen.hs @@ -441,6 +441,24 @@ genExpression (ExBinOp bo e1 e2 (Just t)) = do (TypeUInt _) -> addInstr $ A.ICmp A.ULT e1op' e2op' [] _ -> undefined return $ A.LocalReference (A.IntegerType 1) (A.Name label) + GEqual -> do + sharedType <- commonTypeM (fromJust (exTypeOf e1)) (fromJust (exTypeOf e2)) + e1op' <- castOperand e1op sharedType + e2op' <- castOperand e2op sharedType + label <- case sharedType of + (TypeInt _) -> addInstr $ A.ICmp A.SGE e1op' e2op' [] + (TypeUInt _) -> addInstr $ A.ICmp A.UGE e1op' e2op' [] + _ -> undefined + return $ A.LocalReference (A.IntegerType 1) (A.Name label) + LEqual -> do + sharedType <- commonTypeM (fromJust (exTypeOf e1)) (fromJust (exTypeOf e2)) + e1op' <- castOperand e1op sharedType + e2op' <- castOperand e2op sharedType + label <- case sharedType of + (TypeInt _) -> addInstr $ A.ICmp A.SLE e1op' e2op' [] + (TypeUInt _) -> addInstr $ A.ICmp A.ULE e1op' e2op' [] + _ -> undefined + return $ A.LocalReference (A.IntegerType 1) (A.Name label) BoolOr -> do e1op' <- castToBool e1op e2op' <- castToBool e2op diff --git a/fibo.nl b/fibo.nl new file mode 100644 index 0000000..ac12c2d --- /dev/null +++ b/fibo.nl @@ -0,0 +1,35 @@ +type int = i32; +type char = i8; + +extern func void(int) putchar; + +void printnum(int n){ + if(n==0){ + putchar('0'); + putchar('\n'); + return; + } + if(n<0){ + putchar('-'); + n=-n; + } + while(n>0){ + putchar('0'+n%10); + n=n/10; + } + putchar('\n'); + return; +} + +int main(){ + int a=0; + int b=1; + int i=0; + while(i<=10){ + printnum(b); + b=a+b; + a=b-a; + i=i+1; + } + return 0; +} diff --git a/parser.hs b/parser.hs index 81cbbd3..211c044 100644 --- a/parser.hs +++ b/parser.hs @@ -86,10 +86,10 @@ exprTable = binary "%" Modulo E.AssocLeft], [binary "+" Plus E.AssocLeft, binary "-" Minus E.AssocLeft], - [binary ">" Greater E.AssocNone, - binary "<" Less E.AssocNone, - binary ">=" GEqual E.AssocNone, - binary "<=" LEqual E.AssocNone], + [binary ">=" GEqual E.AssocNone, + binary "<=" LEqual E.AssocNone, + binary ">" Greater E.AssocNone, + binary "<" Less E.AssocNone], [binary "==" Equal E.AssocNone, binary "!=" Unequal E.AssocNone], [binary "&&" BoolAnd E.AssocLeft, -- cgit v1.2.3-54-g00ecf