changeset 14:a51bdf15a33d

Better scopes. --- int x = 1 + y; // <- should not see y in scope yet int y; --- This is done py pushing the scope when a DeclStmt is encountered, and making sure things that containts statements pop the correct number of times.
author Anders Halager <halager@gmail.com>
date Fri, 18 Apr 2008 15:25:10 +0200
parents e5caf9971207
children 59bfbaf8847f bd5f9f81c24b
files sema/SymbolTable.d sema/SymbolTableBuilder.d
diffstat 2 files changed, 42 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/sema/SymbolTable.d	Fri Apr 18 14:47:23 2008 +0200
+++ b/sema/SymbolTable.d	Fri Apr 18 15:25:10 2008 +0200
@@ -16,6 +16,7 @@
     this(Scope enclosing)
     {
         this.enclosing = enclosing;
+        this.func = enclosing.func;
     }
 
     Scope enclosing;
@@ -54,6 +55,19 @@
         else
             return null;
     }
+
+    int opEquals(Object o)
+    {
+        return this is o;
+    }
+
+    char[] toString()
+    {
+        if (func)
+            return Stdout.layout.convert("{}: {}", func.id.get, symbols.length);
+        return "root";
+    }
+
     Symbol parentFunction(Symbol f)
     {
         func = f;
--- a/sema/SymbolTableBuilder.d	Fri Apr 18 14:47:23 2008 +0200
+++ b/sema/SymbolTableBuilder.d	Fri Apr 18 15:25:10 2008 +0200
@@ -1,6 +1,7 @@
 module sema.SymbolTableBuilder;
 
-import tango.io.Stdout;
+import tango.io.Stdout,
+       tango.core.Array : find;
 
 public import sema.SymbolTable;
 
@@ -44,13 +45,13 @@
         visitExp(d.type);
         visitExp(d.identifier);
         d.env = current();
-        push();
-        current().parentFunction = sym;
+        auto sc = push();
+        sc.parentFunction = sym;
         foreach (arg; d.funcArgs)
             visitDecl(arg);
         foreach (stmt; d.statements)
             visitStmt(stmt);
-        pop();
+        pop(sc);
     }
 
     override void visitVarDecl(VarDecl d)
@@ -61,39 +62,53 @@
         super.visitVarDecl(d);
     }
 
+    override void visitDeclStmt(DeclStmt d)
+    {
+        super.visitDeclStmt(d);
+        push();
+    }
+
     override void visitIfStmt(IfStmt s)
     {
         s.env = current();
         visitExp(s.cond);
-        push();
+        auto sc = push();
         foreach (stmt; s.then_body)
             visitStmt(stmt);
-        pop();
+        pop(sc);
 
-        push();
+        sc = push();
         foreach (stmt; s.else_body)
             visitStmt(stmt);
-        pop();
+        pop(sc);
     }
 
     override void visitWhileStmt(WhileStmt s)
     {
         s.env = current();
-        push();
+        auto sc = push();
         super.visitWhileStmt(s);
-        pop();
+        pop(sc);
     }
 
 private:
     Scope[] table;
 
-    void push()
+    Scope push()
     {
-        table ~= new Scope(current());
+        auto sc = new Scope(current());
+        table ~= sc;
+        return sc;
     }
 
-    Scope pop()
+    Scope pop(Scope sc = null)
     {
+        if (sc !is null)
+        {
+            table.length = table.find(sc);
+            return sc;
+        }
+
         auto res = table[$ - 1];
         table.length = table.length - 1;
         return res;