changeset 116:0cd8d6ab3f89

Add in the types for float and co.
author Anders Halager <halager@gmail.com>
date Sun, 25 May 2008 18:19:09 +0200
parents 244142a21cbc
children 54955003765b
files gen/CodeGen.d parser/Parser.d sema/DType.d tests/sema/deref_2.d tests/sema/deref_3.d
diffstat 5 files changed, 95 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/gen/CodeGen.d	Sun May 25 15:49:25 2008 +0200
+++ b/gen/CodeGen.d	Sun May 25 18:19:09 2008 +0200
@@ -386,7 +386,7 @@
         Value res;
         // TODO: do usual type promotions on a and b
         // TODO: support floats
-        if (t_a.isInteger() && t_b.isInteger())
+        if (t_a.isArithmetic() && t_b.isArithmetic())
         {
             Operation op = t_a.getOperationWith(op2op(e.op), t_b);
             assert(op.isBuiltin(),
@@ -626,11 +626,11 @@
                 auto id = exp.env.find(identifier);
                 return LValue(table.find(id.get));
             case ExpType.Deref:
-                // LValue(*x): x
-                // RValue(*x): load(x)
+                // LValue(*x): load(x)
+                // RValue(*x): load(load(x))
                 // This way *x = *x + 1 will work
-                // We get an i32** rather than i32* because it's alloc'd
-                // so there needs to be a load
+                // We need an ekstra load, because we get an i32** rather than
+                // i32* since stuff is alloc'd
                 auto DE = cast(DerefExp)exp;
                 return LValue(genExpression(DE.exp).value);
             case ExpType.Index:
--- a/parser/Parser.d	Sun May 25 15:49:25 2008 +0200
+++ b/parser/Parser.d	Sun May 25 18:19:09 2008 +0200
@@ -678,6 +678,21 @@
     [
         {Tok.Assign,    1, false, Operator.Assign},
 
+        // =, += etc. 1
+        // (need special-case for the ternary operator at this level)
+        // ||, 2
+        // &&, 3
+        // |, 4
+        // &, 5
+        // ^, 6
+        // ==, !=, is, !is, 7
+        // <, <= etc, 7
+        // in, 7
+        // <<, >>, >>>, 8
+        // +, -, ~, 9
+        // *, /, %, 10
+        // unary operators here
+
         {Tok.Eq,        2, true, Operator.Eq},
         {Tok.Ne,        2, true, Operator.Ne},
 
--- a/sema/DType.d	Sun May 25 15:49:25 2008 +0200
+++ b/sema/DType.d	Sun May 25 18:19:09 2008 +0200
@@ -45,11 +45,19 @@
     /// Return a DFunction if this is one, otherwise return null
     DFunction asFunction() { return null; }
 
+    /// Returns true for integers, reals and complex numbers
+    bool isArithmetic() { return false; }
+
     /// Is this type a DInteger
     bool isInteger() { return false; }
     /// Return a DInteger if this is one, otherwise return null
     DInteger asInteger() { return null; }
 
+    /// Is this type a DReal
+    bool isReal() { return false; }
+    /// Return a DReal if this is one, otherwise return null
+    DReal asReal() { return null; }
+
     int opEquals(Object o)
     {
         if (auto t = cast(DType)o)
@@ -135,6 +143,11 @@
         Int, UInt, Long, ULong,
         Char, WChar, DChar;
 
+    static DReal Float, Double, Real;
+
+    // Ignore - we dont support complex numbers yet
+    static DReal CFloat, CDouble, CReal;
+
     static DType Void;
 
     static this()
@@ -150,6 +163,11 @@
         UInt   = new DInteger("uint",   32, true);
         Long   = new DInteger("long",   64, false);
         ULong  = new DInteger("ulong",  64, true);
+
+        Float  = new DReal("float", 32);
+        Double = new DReal("double", 64);
+        Real   = Double;
+
         Char   = new DInteger("char",    8, true);
         WChar  = new DInteger("wchar",  16, true);
         DChar  = new DInteger("dchar",  32, true);
@@ -157,16 +175,20 @@
 }
 
 /**
-  Class to represent the built-in integer types, from byte to long.
+  Class to represent the built-in numerical types, from byte to long, reals and
+  complex numbers.
+
+  Should not actually use this, but DInteger, DReal or DComplex.
  */
-class DInteger : DType
+class DArithmetic : DType
 {
-    private static char[][DInteger] mangle_types;
+    private static char[][DArithmetic] mangle_types;
 
     static this()
     {
         mangle_types = 
         [
+            cast(DArithmetic)
             Bool    : "b",
             Byte    : "g",
             UByte   : "h",
@@ -175,7 +197,21 @@
             Int     : "i",
             UInt    : "k",
             Long    : "l",
-            ULong   : "m"
+            ULong   : "m",
+
+            Float   : "f",
+            Double  : "d",
+            Real    : "e",
+
+            /*
+            CFloat  : "q",
+            CDouble : "r",
+            CReal   : "c",
+            */
+
+            Char    : "a",
+            WChar   : "u",
+            DChar   : "w"
         ];
     }
 
@@ -188,22 +224,13 @@
 
     override int byteSize() { return bits / 8; }
 
-    override bool hasImplicitConversionTo(DType that)
-    {
-        if (auto o = cast(DInteger)that)
-            return true;
-//            return this.bits >= o.bits;
-        return false;
-    }
-
-    override bool isInteger() { return true; }
-    override DInteger asInteger() { return this; }
+    bool isArithmetic() { return true; }
 
     override Operation getOperationWith(Operator op, DType that)
     {
         Operation operation;
         if (this is that)
-            operation = Operation.builtin(op, unsigned, false);
+            operation = Operation.builtin(op, unsigned, isReal());
         return operation;
     }
 
@@ -216,6 +243,35 @@
     bool unsigned;
 }
 
+class DInteger : DArithmetic
+{
+    this(char[] name, int bits, bool unsigned)
+    {
+        super(name, bits, unsigned);
+    }
+
+    override bool hasImplicitConversionTo(DType that)
+    {
+        if (auto o = cast(DInteger)that)
+            return true;
+        return false;
+    }
+
+    override bool isInteger() { return true; }
+    override DInteger asInteger() { return this; }
+}
+
+class DReal : DArithmetic
+{
+    this(char[] name, int bits)
+    {
+        super(name, bits, false);
+    }
+
+    override bool isReal() { return true; }
+    override DReal asReal() { return this; }
+}
+
 class DStruct : DType
 {
     this(Identifier id, DType actual = null)
--- a/tests/sema/deref_2.d	Sun May 25 15:49:25 2008 +0200
+++ b/tests/sema/deref_2.d	Sun May 25 18:19:09 2008 +0200
@@ -1,7 +1,7 @@
 int main()
 {
-    int *a;
-    int *b;
+    int* a;
+    int* b;
     a = b;
     *a = 1;
 
--- a/tests/sema/deref_3.d	Sun May 25 15:49:25 2008 +0200
+++ b/tests/sema/deref_3.d	Sun May 25 18:19:09 2008 +0200
@@ -1,7 +1,7 @@
 int main()
 {
-    int *a;
-    int **b;
+    int* a;
+    int** b;
     *a = 1;
     *b = a;