diff sema/Scope.d @ 92:771ac63898e2 new_gen

A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
author Anders Johnsen <skabet@gmail.com>
date Mon, 05 May 2008 18:44:20 +0200
parents sema/SymbolTable.d@06dda301ea61
children 621cedba53ea
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sema/Scope.d	Mon May 05 18:44:20 2008 +0200
@@ -0,0 +1,108 @@
+module sema.Scope;
+
+import tango.io.Stdout;
+
+import lexer.Token,
+       ast.Decl,
+       ast.Exp;
+
+import sema.DType;
+
+public
+import sema.Symbol;
+
+class Scope
+{
+    this() {}
+    this(Scope enclosing)
+    {
+        this.enclosing = enclosing;
+        this.func = enclosing.func;
+    }
+
+    Scope enclosing;
+
+    Symbol add(Identifier id)
+    {
+        auto s = new Symbol;
+        s.id = id;
+        symbols[id] = s;
+        return s;
+    }
+
+    Symbol find(Identifier id)
+    {
+        if(!id)
+            return null;
+        if (auto sym = id in symbols)
+            return *sym;
+        if (enclosing !is null)
+            return enclosing.find(id);
+        return null;
+    }
+
+    DType findType(Identifier id)
+    {
+        if (auto type = id.get in types)
+                return *type;
+        if (enclosing !is null)
+            return enclosing.findType(id);
+        return null;
+    }
+
+    char[][] names()
+    {
+        char[][] res;
+        if (parentFunction() !is null)
+            res ~= "pf: " ~ parentFunction().identifier.get;
+        if (enclosing)
+            res = enclosing.names;
+        foreach (id, sym; symbols)
+            res ~= sym.id.name ~ " : " ~ (sym.type is null? "?" : sym.type.name);
+        return res;
+    }
+
+    FuncDecl parentFunction()
+    {
+        if (func !is null)
+            return func;
+        else if (enclosing !is null)
+            return enclosing.parentFunction();
+        else
+            return null;
+    }
+
+    int stmtIndex()
+    {
+        if (currentStmtIndex != -1)
+            return currentStmtIndex;
+        else if (enclosing !is null)
+            return enclosing.stmtIndex();
+        else
+            return -1;
+    }
+
+    int opEquals(Object o)
+    {
+        return this is o;
+    }
+
+    char[] toString()
+    {
+        if (func)
+            return Stdout.layout.convert("{}: {}", func.identifier.get, symbols.length);
+        return Stdout.layout.convert("root: {}", symbols.length);
+    }
+
+    FuncDecl parentFunction(FuncDecl f)
+    {
+        func = f;
+        return f;
+    }
+    DType[char[]] types;
+    int currentStmtIndex = -1;
+private:
+    Symbol[Identifier] symbols;
+    FuncDecl func;
+}
+