summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-01-30 09:34:05 +0100
committertomsmeding <tom.smeding@gmail.com>2017-01-30 09:34:05 +0100
commit9b71b9206cd6854877262de2aa1c76421aa899be (patch)
tree0669bcea396f97ab5456051b6a695f994ad330b5
parent1cb1af774cdf0bd8c3f8da257c789184d349bc8c (diff)
Implement >= and <=
-rw-r--r--codegen.hs18
-rw-r--r--fibo.nl35
-rw-r--r--parser.hs8
3 files changed, 57 insertions, 4 deletions
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,