diff sema/SymbolTableBuilder.d @ 53:da551f90e03f new_gen

Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
author Anders Johnsen <skabet@gmail.com>
date Sat, 26 Apr 2008 18:52:27 +0200
parents 9bc660cbdbec
children 79cb0afafabe
line wrap: on
line diff
--- a/sema/SymbolTableBuilder.d	Sat Apr 26 16:12:36 2008 +0200
+++ b/sema/SymbolTableBuilder.d	Sat Apr 26 18:52:27 2008 +0200
@@ -10,6 +10,62 @@
 
 class SymbolTableBuilder : Visitor!(void)
 {
+    override void visit(Decl[] decls)
+    {
+        auto sb = new ScopeBuilder();
+        sb.visit(decls);
+        foreach (decl; decls)
+            visitDecl(decl);
+    }
+
+    override void visitFuncDecl(FuncDecl d)
+    {
+        d.env.find(d.identifier).type = typeOf(d.type, d.env);
+        visitExp(d.type);
+        visitExp(d.identifier);
+        foreach (arg; d.funcArgs)
+            visitDecl(arg);
+        foreach (stmt; d.statements)
+            visitStmt(stmt);
+    }
+
+    override void visitVarDecl(VarDecl d)
+    {
+        if (d.init)
+            visitExp(d.init);
+
+        d.env.find(d.identifier).type = typeOf(d.type, d.env);
+        visitExp(d.type);
+        visitExp(d.identifier);
+    }
+
+    override void visitStructDecl(StructDecl s)
+    {
+        DType[char[]] types;
+        foreach(varDecl ; s.vars)
+        {
+            types[varDecl.identifier.get] = typeOf(varDecl.type, s.env);
+        }
+
+        (cast(DStruct)s.env.types[s.identifier.get]).setMembers(types);
+        super.visitStructDecl(s);
+    }
+
+    DType typeOf(Identifier id, Scope sc)
+    {
+        return sc.findType(id);
+
+        /*
+        if (auto type = id.get in types)
+            return *type;
+        DType res = new DType(id);
+        types[id.get] = res;
+        return res;*/
+    }
+}
+
+class ScopeBuilder : Visitor!(void)
+{
     this()
     {
         table ~= new Scope;
@@ -53,7 +109,7 @@
     {
         auto sym = current().add(d.identifier);
         auto sc = push();
-        sym.type = typeOf(d.type, sc);
+
         visitExp(d.type);
         visitExp(d.identifier);
         d.env = current();
@@ -70,14 +126,13 @@
         if (d.init)
             visitExp(d.init);
 
-        if (need_push > 0) {
+        if (need_push > 0 && current().parentFunction !is null) {
             push();
             --need_push;
         }
 
         auto sc = current();
         auto sym = sc.add(d.identifier);
-        sym.type = typeOf(d.type, sc);
         d.env = sc;
         visitExp(d.type);
         visitExp(d.identifier);
@@ -87,18 +142,10 @@
     {
         auto sc = current();
         auto sym = sc.add(s.identifier);
-        DType[char[]] types;
-        foreach(varDecl ; s.vars)
-        {
-            types[varDecl.identifier.get] = typeOf(varDecl.type, sc);
-        }
 
         auto type = new DStruct(s.identifier);
         
-        type.setMembers(types);
-
         sc.types[s.identifier.get] = type;
-        sym.type = type;
         s.env = sc;
         super.visitStructDecl(s);
     }
@@ -169,18 +216,5 @@
     {
         return table[$ - 1];
     }
-
-    DType typeOf(Identifier id, Scope sc)
-    {
-        return sc.findType(id);
-
-        /*
-        if (auto type = id.get in types)
-            return *type;
-        DType res = new DType(id);
-        types[id.get] = res;
-        return res;*/
-    }
-    DType[char[]] types;
 }