Mercurial > projects > dang
view sema/SymbolTable.d @ 22:e331e4e816e4
now handling structs to some extend
author | johnsen@johnsen-laptop |
---|---|
date | Fri, 18 Apr 2008 23:45:45 +0200 |
parents | a51bdf15a33d |
children | 2d28b21faad6 |
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; } Symbol findType(Identifier id) { if (auto sym = id in symbols) if(symbols[id].type == null) 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; }