Mercurial > projects > dang
view sema/SymbolTable.d @ 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 | ae5bbe4e7fd6 |
children | e331e4e816e4 |
line wrap: on
line source
module sema.SymbolTable; import tango.io.Stdout; import lexer.Token; import ast.Exp : Identifier; class SymbolTable { } 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 (auto sym = id in symbols) return *sym; if (enclosing !is null) return enclosing.find(id); return null; } char[][] names() { char[][] res; foreach (id, sym; symbols) res ~= sym.id.name ~ " : " ~ sym.type.name; return res; } Symbol parentFunction() { if (func !is null) return func; else if (enclosing !is null) return enclosing.parentFunction(); 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; return f; } private: Symbol[Identifier] symbols; Symbol func; } class Symbol { Identifier id; Identifier type; }