Mercurial > projects > dang
view sema/SymbolTableBuilder.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 | 642c6a998fd9 |
children | e331e4e816e4 |
line wrap: on
line source
module sema.SymbolTableBuilder; import tango.io.Stdout, tango.core.Array : find; public import sema.SymbolTable; import sema.Visitor; class SymbolTableBuilder : Visitor!(void) { this() { table ~= new Scope; } override void visit(Decl[] decls) { foreach (decl; decls) visitDecl(decl); } override void visitDecl(Decl d) { d.env = current(); super.visitDecl(d); } override void visitStmt(Stmt s) { s.env = current(); super.visitStmt(s); } override void visitExp(Exp e) { e.env = current(); super.visitExp(e); } override void visitFuncDecl(FuncDecl d) { auto sym = current().add(d.identifier); sym.type = d.type; visitExp(d.type); visitExp(d.identifier); d.env = current(); auto sc = push(); sc.parentFunction = sym; foreach (arg; d.funcArgs) visitDecl(arg); foreach (stmt; d.statements) visitStmt(stmt); pop(sc); } override void visitVarDecl(VarDecl d) { auto sc = current(); auto sym = sc.add(d.identifier); sym.type = d.type; super.visitVarDecl(d); } override void visitDeclStmt(DeclStmt d) { super.visitDeclStmt(d); push(); } override void visitIfStmt(IfStmt s) { s.env = current(); visitExp(s.cond); auto sc = push(); foreach (stmt; s.then_body) visitStmt(stmt); pop(sc); sc = push(); foreach (stmt; s.else_body) visitStmt(stmt); pop(sc); } override void visitWhileStmt(WhileStmt s) { s.env = current(); auto sc = push(); super.visitWhileStmt(s); pop(sc); } private: Scope[] table; Scope push() { auto sc = new Scope(current()); table ~= sc; return sc; } 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; } Scope current() { return table[$ - 1]; } }