changeset 7:2ce5209f1954

Starting to work on bool support, for now == works
author Anders Halager <halager@gmail.com>
date Fri, 18 Apr 2008 12:50:54 +0200
parents 606a57c90a0b
children a0a3223a76c3
files ast/Exp.d gen/LLVMGen.d parser/Parser.d test.td
diffstat 4 files changed, 44 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Exp.d	Fri Apr 18 12:24:14 2008 +0200
+++ b/ast/Exp.d	Fri Apr 18 12:50:54 2008 +0200
@@ -55,10 +55,11 @@
 
 class BinaryExp : Exp
 {
-    public enum Operator : char
+    public enum Operator
     {
-        Mul = '*', Div = '/',
-        Add = '+', Sub = '-'
+        Eq, Ne,
+        Mul, Div,
+        Add, Sub,
     }
 
     this(Operator op, Exp left, Exp right)
@@ -69,6 +70,13 @@
         this.right = right;
     }
 
+    char[] resultType()
+    {
+        if (op >= Operator.Eq && op <= Operator.Ne)
+            return "bool";
+        return null;
+    }
+
     Operator op;
     Exp left, right;
 }
--- a/gen/LLVMGen.d	Fri Apr 18 12:24:14 2008 +0200
+++ b/gen/LLVMGen.d	Fri Apr 18 12:50:54 2008 +0200
@@ -23,14 +23,18 @@
             "byte"   : "i8",
             "short"  : "i16",
             "long"   : "i64",
-            "bool"   : "i8",
+            "bool"   : "i1",
             "float"  : "float",
             "double" : "double",
-            "void"   : "void",
-            "+"      : "add",
-            "-"      : "sub",
-            "*"      : "mul",
-            "/"      : "div"
+            "void"   : "void"
+        ];
+        alias BinaryExp.Operator op;
+        opToLLVM = [
+            op.Add      : "add"[],
+            op.Sub      : "sub",
+            op.Mul      : "mul",
+            op.Div      : "div",
+            op.Eq       : "icmp eq"
         ];
         table = new SimpleSymbolTable();
     }
@@ -191,10 +195,16 @@
 
                 auto res = Ref(left.type, table.find);
                 printBeginLine(res.name);
-                print(" = "~typeToLLVM[[binaryExp.op]]~" ");
+                print(" = "~opToLLVM[binaryExp.op]~" ");
                 print(left);
                 print(", ");
                 printEndLine(right.name);
+
+                // exp always returns known type (== returns bool no matter
+                // what the params are)
+                if (binaryExp.resultType)
+                    res.type = typeToLLVM[binaryExp.resultType];
+
                 return res;
             case ExpType.IntegerLit:
                 auto integetLit = cast(IntegerLit)exp;
@@ -417,8 +427,9 @@
     SimpleSymbolTable table;
     SymbolTable symbolTable;
     static char[][char[]] typeToLLVM;
+    static char[][BinaryExp.Operator] opToLLVM;
 
-    static char[][] intTypes = [ "i8", "i16", "i32", "i64" ];
+    static char[][] intTypes = [ "i1", "i8", "i16", "i32", "i64" ];
 }
 
 struct Ref
--- a/parser/Parser.d	Fri Apr 18 12:24:14 2008 +0200
+++ b/parser/Parser.d	Fri Apr 18 12:50:54 2008 +0200
@@ -294,10 +294,12 @@
 
     static BinOp[] _binary =
     [
-        {Tok.Add, 3, true, BinaryExp.Operator.Add},
-        {Tok.Sub, 3, true, BinaryExp.Operator.Sub},
-        {Tok.Mul, 5, true, BinaryExp.Operator.Mul},
-        {Tok.Div, 5, true, BinaryExp.Operator.Div}
+        {Tok.Equals,    2, true, BinaryExp.Operator.Eq},
+        {Tok.NotEquals, 2, true, BinaryExp.Operator.Ne},
+        {Tok.Add,       3, true, BinaryExp.Operator.Add},
+        {Tok.Sub,       3, true, BinaryExp.Operator.Sub},
+        {Tok.Mul,       5, true, BinaryExp.Operator.Mul},
+        {Tok.Div,       5, true, BinaryExp.Operator.Div}
     ];
     BinOp* binary(Tok t)
     {
--- a/test.td	Fri Apr 18 12:24:14 2008 +0200
+++ b/test.td	Fri Apr 18 12:50:54 2008 +0200
@@ -8,12 +8,18 @@
     return nice(var1, var2);
 }
 
+int fac(int n)
+{
+    if (n - 1)
+        return n * fac(n - 1);
+    return 1;
+}
+
 int nice(long s, short t)
 {
     byte x = 5 + t;
     if (x == 0)
-        if (s)
-            t = 5 + 1 * 5 * s + t;
+        t = 5 + 1 * 5 * s + t;
     return 2 * (t + -1) - x;
 }