diff sema/DType.d @ 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 189c049cbfcc
children 54585ad7e426
line wrap: on
line diff
--- 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)