changeset 26:b4dc2b2c0e38 new_gen

Added a DType class
author Anders Halager <halager@gmail.com>
date Sat, 19 Apr 2008 22:19:14 +0200
parents 14c1abba773f
children 9031487e97d7
files gen/LLVMGen.d sema/DType.d sema/SymbolTable.d sema/SymbolTableBuilder.d
diffstat 4 files changed, 78 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/gen/LLVMGen.d	Sat Apr 19 19:31:28 2008 +0200
+++ b/gen/LLVMGen.d	Sat Apr 19 22:19:14 2008 +0200
@@ -213,8 +213,7 @@
                 return b.buildNeg(target, "neg");
             case ExpType.AssignExp:
                 auto assignExp = cast(AssignExp)exp;
-                buildAssign(exp.env.find(assignExp.identifier), assignExp.exp);
-                return null;
+                return buildAssign(exp.env.find(assignExp.identifier), assignExp.exp);
             case ExpType.CallExp:
                 auto callExp = cast(CallExp)exp;
                 auto func_sym = exp.env.find(cast(Identifier)callExp.exp);
@@ -238,7 +237,7 @@
             case StmtType.Return:
                 auto ret = cast(ReturnStmt)stmt;
                 auto sym = stmt.env.parentFunction();
-                Type t = typeToLLVM[sym.type.get];
+                Type t = typeToLLVM[sym.type.name];
                 Value v = genExpression(ret.exp);
                 if (v.type != t)
                 {
@@ -328,9 +327,9 @@
         }
     }
 
-    private void buildAssign(Symbol sym, Exp exp)
+    private Value buildAssign(Symbol sym, Exp exp)
     {
-        Type t = typeToLLVM[sym.type.get];
+        Type t = typeToLLVM[sym.type.name];
         auto name = sym.id.get;
         auto AI = table.find(name);
         Value v = genExpression(exp);
@@ -347,7 +346,7 @@
             else
                 v = b.buildTrunc(v, t, ".cast");
         }
-        b.buildStore(v, AI);
+        return b.buildStore(v, AI);
     }
 
 private:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sema/DType.d	Sat Apr 19 22:19:14 2008 +0200
@@ -0,0 +1,65 @@
+module sema.DType;
+
+import tango.text.Util : jhash;
+
+import sema.SymbolTable;
+
+class DType
+{
+    Identifier id;
+    Scope sc;
+    DType actual;
+
+    this(Identifier id, Scope sc, DType actual = null)
+    {
+        this.id = id;
+        this.sc = sc;
+        if (actual is null)
+            this.actual = this;
+        _name = id.get;
+    }
+
+    int opEquals(Object o)
+    {
+        if (auto t = cast(DType)o)
+            return this.actual is t.actual;
+        return 0;
+    }
+
+    int opCmp(Object o)
+    {
+        if (auto t = cast(DType)o)
+            return cast(void*)this.actual - cast(void*)t.actual;
+        return 0;
+    }
+
+    hash_t toHash()
+    {
+        return cast(hash_t)(cast(void*)this);
+    }
+
+    char[] name() { return _name; }
+
+private:
+    char[] _name;
+}
+
+class DStruct : DType
+{
+    this(Identifier id, Scope sc, DType actual = null)
+    {
+        super(id, sc, actual);
+    }
+    DType[] members;
+}
+
+class DFunction : DType
+{
+    this(Identifier id, Scope sc, DType actual = null)
+    {
+        super(id, sc, actual);
+    }
+    DType[] params;
+    DType return_type;
+}
+
--- a/sema/SymbolTable.d	Sat Apr 19 19:31:28 2008 +0200
+++ b/sema/SymbolTable.d	Sat Apr 19 22:19:14 2008 +0200
@@ -2,7 +2,8 @@
 
 import tango.io.Stdout;
 
-import lexer.Token;
+import lexer.Token,
+       sema.DType;
 
 import ast.Exp : Identifier;
 
@@ -93,6 +94,6 @@
 class Symbol
 {
     Identifier id;
-    Identifier type;
+    DType type;
 }
 
--- a/sema/SymbolTableBuilder.d	Sat Apr 19 19:31:28 2008 +0200
+++ b/sema/SymbolTableBuilder.d	Sat Apr 19 22:19:14 2008 +0200
@@ -3,7 +3,9 @@
 import tango.io.Stdout,
        tango.core.Array : find;
 
-public import sema.SymbolTable;
+public
+import sema.SymbolTable,
+       sema.DType;
 
 import sema.Visitor;
 
@@ -41,7 +43,7 @@
     override void visitFuncDecl(FuncDecl d)
     {
         auto sym = current().add(d.identifier);
-        sym.type = d.type;
+        sym.type = new DType(d.type, current());
         visitExp(d.type);
         visitExp(d.identifier);
         d.env = current();
@@ -66,7 +68,7 @@
 
         auto sc = current();
         auto sym = sc.add(d.identifier);
-        sym.type = d.type;
+        sym.type = new DType(d.type, sc);
         d.env = sc;
         visitExp(d.type);
         visitExp(d.identifier);