Mercurial > projects > dang
view sema/SymbolTableBuilder.d @ 24:2d28b21faad6 new_gen
New codegen!
Rewritten codegen to use the llvm bindings
Everything except struct are back to normal, and there a a few additions.
1. Correct code in more cases, return at the end of a while/if wont
generate a "ret" followed by a "br".
2. Better scope, "int x = x" now illegal
3. Probably more
author | Anders Halager <halager@gmail.com> |
---|---|
date | Sat, 19 Apr 2008 18:29:42 +0200 |
parents | e331e4e816e4 |
children | b4dc2b2c0e38 |
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) { if (d.init) visitExp(d.init); if (need_push > 0) { push(); --need_push; } auto sc = current(); auto sym = sc.add(d.identifier); sym.type = d.type; d.env = sc; visitExp(d.type); visitExp(d.identifier); } override void visitStructDecl(StructDecl s) { auto sc = current(); auto sym = sc.add(s.identifier); // sym.type = Tok.Struct; super.visitStructDecl(s); } override void visitDeclStmt(DeclStmt d) { ++need_push; super.visitDeclStmt(d); } private uint need_push = 0; 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]; } }