Mercurial > projects > dang
view sema/SymbolTableBuilder.d @ 59:1d6f4ad38a91 new_gen
Make most of the tests pass again
Still have two bugs, one is that type conversion has been diabled for
return statements, the other is at line ~540 of CodeGen.d, were the
following error is emitted:
Can't find an implicit conversion between { i32 } and i32
author | Anders Halager <halager@gmail.com> |
---|---|
date | Tue, 29 Apr 2008 00:31:56 +0200 |
parents | 4ae365eff712 |
children | 9f8131676242 |
line wrap: on
line source
module sema.SymbolTableBuilder; import tango.io.Stdout, tango.core.Array : find; public import sema.SymbolTable; import sema.Visitor, basic.SmallArray; class SymbolTableBuilder : Visitor!(void) { override void visit(Decl[] decls) { auto sb = new ScopeBuilder(); sb.visit(decls); foreach (decl; decls) visitDecl(decl); } override void visitFuncDecl(FuncDecl d) { visitExp(d.type); visitExp(d.identifier); SmallArray!(DType, 8) arg_types; foreach (arg; d.funcArgs) { visitDecl(arg); arg_types ~= d.env.find(arg.identifier).type; } foreach (stmt; d.statements) visitStmt(stmt); auto func_t = new DFunction(d.identifier); func_t.return_type = d.env.findType(d.type); func_t.params = arg_types.safe(); auto sym = d.env.find(d.identifier); sym.type = func_t; } override void visitVarDecl(VarDecl d) { if (d.init) visitExp(d.init); d.env.find(d.identifier).type = typeOf(d.type, d.env); visitExp(d.type); visitExp(d.identifier); } override void visitStructDecl(StructDecl s) { DType[char[]] types; auto st = (cast(DStruct)s.env.types[s.identifier.get]); foreach(varDecl ; s.vars) { st.addMember(typeOf(varDecl.type, varDecl.env), varDecl.identifier.get); } super.visitStructDecl(s); } DType typeOf(Identifier id, Scope sc) { return sc.findType(id); /* if (auto type = id.get in types) return *type; DType res = new DType(id); types[id.get] = res; return res;*/ } } class ScopeBuilder : Visitor!(void) { this() { table ~= new Scope; table[0].types["void"] = DType.Void; table[0].types["bool"] = DType.Bool; table[0].types["byte"] = DType.Byte; table[0].types["ubyte"] = DType.UByte; table[0].types["short"] = DType.Short; table[0].types["ushort"] = DType.UShort; table[0].types["int"] = DType.Int; table[0].types["uint"] = DType.UInt; table[0].types["long"] = DType.Long; table[0].types["ulong"] = DType.ULong; } 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(); s.stmtIndex = s.env.stmtIndex; super.visitStmt(s); } override void visitExp(Exp e) { e.env = current(); e.stmtIndex = e.env.stmtIndex; super.visitExp(e); } override void visitFuncDecl(FuncDecl d) { auto sym = current().add(d.identifier); auto sc = push(); visitExp(d.type); visitExp(d.identifier); d.env = current(); sc.parentFunction = d; foreach (arg; d.funcArgs) visitDecl(arg); foreach (stmt; d.statements) { sc.currentStmtIndex++; visitStmt(stmt); } pop(sc); } override void visitVarDecl(VarDecl d) { if (d.init) visitExp(d.init); if (need_push > 0 && current().parentFunction !is null) { push(); --need_push; } auto sc = current(); auto sym = sc.add(d.identifier); d.env = sc; visitExp(d.type); visitExp(d.identifier); } override void visitStructDecl(StructDecl s) { auto sc = current(); auto sym = sc.add(s.identifier); s.env = sc; auto type = new DStruct(s.identifier); sc.types[s.identifier.get] = type; sc = push(); super.visitStructDecl(s); pop(sc); } 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(); visitStmt(s.then_body); pop(sc); if (s.else_body !is null) { sc = push(); visitStmt(s.else_body); pop(sc); } } override void visitWhileStmt(WhileStmt s) { s.env = current(); auto sc = push(); super.visitWhileStmt(s); pop(sc); } override void visitCompoundStmt(CompoundStatement s) { s.env = current(); auto sc = push(); super.visitCompoundStmt(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]; } }