0
|
1
|
|
2 /* Digital Mars DMDScript source code.
|
|
3 * Copyright (c) 2000-2002 by Chromium Communications
|
|
4 * D version Copyright (c) 2004-2005 by Digital Mars
|
|
5 * All Rights Reserved
|
|
6 * written by Walter Bright
|
|
7 * www.digitalmars.com
|
|
8 * Use at your own risk. There is no warranty, express or implied.
|
|
9 * License for redistribution is by the GNU General Public License in gpl.txt.
|
|
10 *
|
|
11 * A binary, non-exclusive license for commercial use can be
|
|
12 * purchased from www.digitalmars.com/dscript/buy.html.
|
|
13 *
|
|
14 * DMDScript is implemented in the D Programming Language,
|
|
15 * www.digitalmars.com/d/
|
|
16 *
|
|
17 * For a C++ implementation of DMDScript, including COM support,
|
|
18 * see www.digitalmars.com/dscript/cppscript.html.
|
|
19 */
|
|
20
|
|
21
|
|
22 module dmdscript.symbol;
|
|
23
|
|
24 import std.stdio;
|
|
25
|
|
26 import dmdscript.script;
|
|
27 import dmdscript.identifier;
|
|
28 import dmdscript.scopex;
|
|
29 import dmdscript.statement;
|
|
30 import dmdscript.irstate;
|
|
31 import dmdscript.opcodes;
|
|
32 import dmdscript.errmsgs;
|
|
33
|
|
34 /****************************** Symbol ******************************/
|
|
35
|
|
36 class Symbol
|
|
37 {
|
|
38 Identifier *ident;
|
|
39
|
|
40 this()
|
|
41 {
|
|
42 }
|
|
43
|
|
44 this(Identifier *ident)
|
|
45 {
|
|
46 this.ident = ident;
|
|
47 }
|
|
48
|
|
49 int opEquals(Object o)
|
|
50 { Symbol s;
|
|
51
|
|
52 if (this == o)
|
|
53 return true;
|
|
54 s = cast(Symbol)o;
|
|
55 if (s && ident == s.ident)
|
|
56 return true;
|
|
57 return false;
|
|
58 }
|
|
59
|
|
60 tchar[] toString()
|
|
61 {
|
|
62 return ident ? "__ident" : "__anonymous";
|
|
63 }
|
|
64
|
|
65 void semantic(Scope *sc)
|
|
66 {
|
|
67 assert(0);
|
|
68 }
|
|
69
|
|
70 Symbol search(Identifier *ident)
|
|
71 {
|
|
72 assert(0);
|
|
73 //error(DTEXT("%s.%s is undefined"),toString(), ident.toString());
|
|
74 return this;
|
|
75 }
|
|
76
|
|
77 void toBuffer(inout tchar[] buf)
|
|
78 {
|
|
79 buf ~= toString();
|
|
80 }
|
|
81 }
|
|
82
|
|
83
|
|
84 /********************************* ScopeSymbol ****************************/
|
|
85
|
|
86 // Symbol that generates a scope
|
|
87
|
|
88 class ScopeSymbol : Symbol
|
|
89 {
|
|
90 Symbol[] members; // all Symbol's in this scope
|
|
91 SymbolTable *symtab; // member[] sorted into table
|
|
92
|
|
93 this()
|
|
94 {
|
|
95 super();
|
|
96 }
|
|
97
|
|
98 this(Identifier *id)
|
|
99 {
|
|
100 super(id);
|
|
101 }
|
|
102
|
|
103 Symbol search(Identifier *ident)
|
|
104 { Symbol s;
|
|
105
|
|
106 //writef("ScopeSymbol::search(%s, '%s')\n", toString(), ident.toString());
|
|
107 // Look in symbols declared in this module
|
|
108 s = symtab ? symtab.lookup(ident) : null;
|
|
109 if (s)
|
|
110 writef("\ts = '%s.%s'\n",toString(),s.toString());
|
|
111 return s;
|
|
112 }
|
|
113 }
|
|
114
|
|
115
|
|
116 /****************************** SymbolTable ******************************/
|
|
117
|
|
118 // Table of Symbol's
|
|
119
|
|
120 struct SymbolTable
|
|
121 {
|
|
122 Symbol[Identifier*] members;
|
|
123
|
|
124 // Look up Identifier. Return Symbol if found, NULL if not.
|
|
125 Symbol lookup(Identifier *ident)
|
|
126 {
|
|
127 Symbol *ps;
|
|
128
|
|
129 ps = ident in members;
|
|
130 if (ps)
|
|
131 return *ps;
|
|
132 return null;
|
|
133 }
|
|
134
|
|
135 // Insert Symbol in table. Return NULL if already there.
|
|
136 Symbol insert(Symbol s)
|
|
137 {
|
|
138 Symbol *ps;
|
|
139
|
|
140 ps = s.ident in members;
|
|
141 if (ps)
|
|
142 return null; // already in table
|
|
143 members[s.ident] = s;
|
|
144 return s;
|
|
145 }
|
|
146
|
|
147 // Look for Symbol in table. If there, return it.
|
|
148 // If not, insert s and return that.
|
|
149 Symbol update(Symbol s)
|
|
150 {
|
|
151 members[s.ident] = s;
|
|
152 return s;
|
|
153 }
|
|
154 }
|
|
155
|
|
156
|
|
157 /****************************** FunctionSymbol ******************************/
|
|
158
|
|
159 class FunctionSymbol : ScopeSymbol
|
|
160 {
|
|
161 Loc loc;
|
|
162
|
|
163 Identifier*[] parameters; // array of Identifier's
|
|
164 TopStatement[] topstatements; // array of TopStatement's
|
|
165
|
|
166 SymbolTable labtab; // symbol table for LabelSymbol's
|
|
167
|
|
168 IR *code;
|
|
169 uint nlocals;
|
|
170
|
|
171 this(Loc loc, Identifier* ident, Identifier*[] parameters,
|
|
172 TopStatement[] topstatements)
|
|
173 {
|
|
174 super(ident);
|
|
175 this.loc = loc;
|
|
176 this.parameters = parameters;
|
|
177 this.topstatements = topstatements;
|
|
178 }
|
|
179
|
|
180 void semantic(Scope *sc)
|
|
181 {
|
|
182 }
|
|
183 }
|
|
184
|
|
185
|
|
186 /****************************** LabelSymbol ******************************/
|
|
187
|
|
188 class LabelSymbol : Symbol
|
|
189 { Loc loc;
|
|
190 LabelStatement statement;
|
|
191
|
|
192 this(Loc loc, Identifier* ident, LabelStatement statement)
|
|
193 {
|
|
194 super(ident);
|
|
195 this.loc = loc;
|
|
196 this.statement = statement;
|
|
197 }
|
|
198 }
|
|
199
|
|
200
|