Mercurial > projects > dang
view sema/Scope.d @ 101:fea8d61a2451 new_gen
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Wed, 07 May 2008 19:58:13 +0200 |
parents | 857f0d530789 |
children | 189c049cbfcc |
line wrap: on
line source
module sema.Scope; import tango.io.Stdout; import lexer.Token, ast.Module, ast.Decl, ast.Exp; public import sema.DType; class Scope { this() {} this(Scope enclosing) { this.enclosing = enclosing; this.func = enclosing.func; this.inModule = enclosing.inModule; this.mHandle = enclosing.mHandle; } Scope enclosing; ModuleHandler mHandle; Module inModule; ImportDecl[] imports; void add(Identifier id) { symbols[id] = id; } Identifier find(Identifier id) { if(!id) return null; if (auto sym = id in symbols) return *sym; if (enclosing !is null) { auto type = enclosing.find(id); if(type is null) return mHandle.find(getImports, id); return type; } return null; } ImportDecl[] getImports() { if(enclosing) return enclosing.getImports ~ imports; return imports; } DType findType(Identifier id) { if (auto type = id.get in types) return *type; if (enclosing !is null) { auto type = enclosing.findType(id); if(type is null) return mHandle.findType(getImports, id); return type; } return null; } char[][] names() { char[][] res; if (parentFunction() !is null) res ~= "pf: " ~ parentFunction().identifier.get; if (enclosing) res = enclosing.names; foreach (id, sym; symbols) res ~= sym.name ~ " : " ~ (sym.type is null? "?" : sym.type.name); return res; } FuncDecl parentFunction() { if (func !is null) return func; else if (enclosing !is null) return enclosing.parentFunction(); else return null; } int stmtIndex() { if (currentStmtIndex != -1) return currentStmtIndex; else if (enclosing !is null) return enclosing.stmtIndex(); else return -1; } int opEquals(Object o) { return this is o; } char[] toString() { if (func) return Stdout.layout.convert("{}: {}", func.identifier.get, symbols.length); return Stdout.layout.convert("root: {}", symbols.length); } FuncDecl parentFunction(FuncDecl f) { func = f; return f; } DType[char[]] types; int currentStmtIndex = -1; private: Identifier[Identifier] symbols; FuncDecl func; } class ModuleHandler { void add(Module m) { modules[m.moduleName] = m; } void add(Module m, char[] file) { fileToModule[file] = m.moduleName; add(m); } DType findType(ImportDecl[] imports, Identifier type) { foreach(i ; imports) if(i.get in modules) { auto t = modules[i.get].env.findType(type); if(t !is null) return t; } return null; } Identifier find(ImportDecl[] imports, Identifier id) { foreach(i ; imports) if(i.get in modules) { auto t = modules[i.get].env.find(id); if(t !is null) return t; } return null; } char[][char[]] fileToModule; Module[char[]] modules; }