comparison sema/Scope.d @ 129:ed815b31479b

Added a Symbol
author Anders Halager <halager@gmail.com>
date Sat, 21 Jun 2008 20:41:18 +0200
parents 189c049cbfcc
children a14ac9e5c858
comparison
equal deleted inserted replaced
128:7264c61088c4 129:ed815b31479b
6 ast.Module, 6 ast.Module,
7 ast.Decl, 7 ast.Decl,
8 ast.Exp; 8 ast.Exp;
9 9
10 public 10 public
11 import sema.DType; 11 import sema.DType,
12 sema.Symbol;
12 13
13 class Scope 14 class Scope
14 { 15 {
15 this() {} 16 this() {}
16 this(Scope enclosing) 17 this(Scope enclosing)
26 Module inModule; 27 Module inModule;
27 Scope[] imported; 28 Scope[] imported;
28 29
29 ImportDecl[] imports; 30 ImportDecl[] imports;
30 31
31 32 void put(Identifier id, Decl d)
32 void add(Identifier id)
33 { 33 {
34 symbols[id] = id; 34 symbols[id] = d;
35 } 35 }
36 36
37 Identifier find(Identifier id) 37 Decl find(Identifier id)
38 { 38 {
39 if(id is null) 39 if(id is null)
40 return null; 40 return null;
41 if (auto sym = id in symbols) 41 else if (auto sym = id in symbols)
42 return *sym; 42 return *sym;
43 if (enclosing !is null) 43 else if (enclosing !is null)
44 { 44 {
45 auto type = enclosing.find(id); 45 auto res = enclosing.find(id);
46 if(type is null) 46 if (res is null)
47 return mHandle.find(getImports, id); 47 return mHandle.find(getImports, id);
48 return type; 48 return res;
49 } 49 }
50 return null; 50 return null;
51 } 51 }
52 52
53 ImportDecl[] getImports() 53 ImportDecl[] getImports()
58 } 58 }
59 59
60 DType findType(Identifier id) 60 DType findType(Identifier id)
61 { 61 {
62 if (auto type = id.get in types) 62 if (auto type = id.get in types)
63 return *type; 63 return *type;
64 if (enclosing !is null) 64 if (enclosing !is null)
65 { 65 {
66 auto type = enclosing.findType(id); 66 auto type = enclosing.findType(id);
67 if(type is null) 67 if(type is null)
68 return mHandle.findType(getImports, id); 68 return mHandle.findType(getImports, id);
69 return type; 69 return type;
70 } 70 }
71 return null; 71 return null;
72 } 72 }
73 73
74 /*
74 char[][] names() 75 char[][] names()
75 { 76 {
76 char[][] res; 77 char[][] res;
77 if (parentFunction() !is null) 78 if (parentFunction() !is null)
78 res ~= "pf: " ~ parentFunction().identifier.get; 79 res ~= "pf: " ~ parentFunction().identifier.get;
80 res = enclosing.names; 81 res = enclosing.names;
81 foreach (id, sym; symbols) 82 foreach (id, sym; symbols)
82 res ~= sym.name ~ " : " ~ (sym.type is null? "?" : sym.type.name); 83 res ~= sym.name ~ " : " ~ (sym.type is null? "?" : sym.type.name);
83 return res; 84 return res;
84 } 85 }
86 */
85 87
86 FuncDecl parentFunction() 88 FuncDecl parentFunction()
87 { 89 {
88 if (func !is null) 90 if (func !is null)
89 return func; 91 return func;
121 return f; 123 return f;
122 } 124 }
123 DType[char[]] types; 125 DType[char[]] types;
124 int currentStmtIndex = -1; 126 int currentStmtIndex = -1;
125 private: 127 private:
126 Identifier[Identifier] symbols; 128 Decl[Identifier] symbols;
127 FuncDecl func; 129 FuncDecl func;
128 } 130 }
129 131
130 class ModuleHandler 132 class ModuleHandler
131 { 133 {
149 return t; 151 return t;
150 } 152 }
151 return null; 153 return null;
152 } 154 }
153 155
154 Identifier find(ImportDecl[] imports, Identifier id) 156 Decl find(ImportDecl[] imports, Identifier id)
155 { 157 {
156 foreach(i ; imports) 158 foreach(i ; imports)
157 if(i.get in modules) 159 if(i.get in modules)
158 { 160 {
159 auto t = modules[i.get].env.find(id); 161 auto t = modules[i.get].env.find(id);