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
|
3
|
22 module dmdscript_tango.symbol;
|
0
|
23
|
4
|
24 //import std.stdio;
|
|
25 import tango.io.Stdout;
|
0
|
26
|
3
|
27 import dmdscript_tango.script;
|
|
28 import dmdscript_tango.identifier;
|
|
29 import dmdscript_tango.scopex;
|
|
30 import dmdscript_tango.statement;
|
|
31 import dmdscript_tango.irstate;
|
|
32 import dmdscript_tango.opcodes;
|
|
33 import dmdscript_tango.textgen.errmsgs;
|
0
|
34
|
|
35 /****************************** Symbol ******************************/
|
|
36
|
|
37 class Symbol
|
|
38 {
|
|
39 Identifier *ident;
|
|
40
|
|
41 this()
|
|
42 {
|
|
43 }
|
|
44
|
|
45 this(Identifier *ident)
|
|
46 {
|
|
47 this.ident = ident;
|
|
48 }
|
|
49
|
|
50 int opEquals(Object o)
|
|
51 { Symbol s;
|
|
52
|
|
53 if (this == o)
|
|
54 return true;
|
|
55 s = cast(Symbol)o;
|
|
56 if (s && ident == s.ident)
|
|
57 return true;
|
|
58 return false;
|
|
59 }
|
|
60
|
|
61 tchar[] toString()
|
|
62 {
|
|
63 return ident ? "__ident" : "__anonymous";
|
|
64 }
|
|
65
|
|
66 void semantic(Scope *sc)
|
|
67 {
|
|
68 assert(0);
|
|
69 }
|
|
70
|
|
71 Symbol search(Identifier *ident)
|
|
72 {
|
|
73 assert(0);
|
|
74 //error(DTEXT("%s.%s is undefined"),toString(), ident.toString());
|
|
75 return this;
|
|
76 }
|
|
77
|
|
78 void toBuffer(inout tchar[] buf)
|
|
79 {
|
|
80 buf ~= toString();
|
|
81 }
|
|
82 }
|
|
83
|
|
84
|
|
85 /********************************* ScopeSymbol ****************************/
|
|
86
|
|
87 // Symbol that generates a scope
|
|
88
|
|
89 class ScopeSymbol : Symbol
|
|
90 {
|
|
91 Symbol[] members; // all Symbol's in this scope
|
|
92 SymbolTable *symtab; // member[] sorted into table
|
|
93
|
|
94 this()
|
|
95 {
|
|
96 super();
|
|
97 }
|
|
98
|
|
99 this(Identifier *id)
|
|
100 {
|
|
101 super(id);
|
|
102 }
|
|
103
|
|
104 Symbol search(Identifier *ident)
|
|
105 { Symbol s;
|
|
106
|
|
107 //writef("ScopeSymbol::search(%s, '%s')\n", toString(), ident.toString());
|
|
108 // Look in symbols declared in this module
|
|
109 s = symtab ? symtab.lookup(ident) : null;
|
|
110 if (s)
|
4
|
111 Stdout.format("\ts = '{}.{}'\n",toString(),s.toString());
|
|
112 //writef("\ts = '%s.%s'\n",toString(),s.toString());
|
0
|
113 return s;
|
|
114 }
|
|
115 }
|
|
116
|
|
117
|
|
118 /****************************** SymbolTable ******************************/
|
|
119
|
|
120 // Table of Symbol's
|
|
121
|
|
122 struct SymbolTable
|
|
123 {
|
|
124 Symbol[Identifier*] members;
|
|
125
|
|
126 // Look up Identifier. Return Symbol if found, NULL if not.
|
|
127 Symbol lookup(Identifier *ident)
|
|
128 {
|
|
129 Symbol *ps;
|
|
130
|
|
131 ps = ident in members;
|
|
132 if (ps)
|
|
133 return *ps;
|
|
134 return null;
|
|
135 }
|
|
136
|
|
137 // Insert Symbol in table. Return NULL if already there.
|
|
138 Symbol insert(Symbol s)
|
|
139 {
|
|
140 Symbol *ps;
|
|
141
|
|
142 ps = s.ident in members;
|
|
143 if (ps)
|
|
144 return null; // already in table
|
|
145 members[s.ident] = s;
|
|
146 return s;
|
|
147 }
|
|
148
|
|
149 // Look for Symbol in table. If there, return it.
|
|
150 // If not, insert s and return that.
|
|
151 Symbol update(Symbol s)
|
|
152 {
|
|
153 members[s.ident] = s;
|
|
154 return s;
|
|
155 }
|
|
156 }
|
|
157
|
|
158
|
|
159 /****************************** FunctionSymbol ******************************/
|
|
160
|
|
161 class FunctionSymbol : ScopeSymbol
|
|
162 {
|
|
163 Loc loc;
|
|
164
|
|
165 Identifier*[] parameters; // array of Identifier's
|
|
166 TopStatement[] topstatements; // array of TopStatement's
|
|
167
|
|
168 SymbolTable labtab; // symbol table for LabelSymbol's
|
|
169
|
|
170 IR *code;
|
|
171 uint nlocals;
|
|
172
|
|
173 this(Loc loc, Identifier* ident, Identifier*[] parameters,
|
|
174 TopStatement[] topstatements)
|
|
175 {
|
|
176 super(ident);
|
|
177 this.loc = loc;
|
|
178 this.parameters = parameters;
|
|
179 this.topstatements = topstatements;
|
|
180 }
|
|
181
|
|
182 void semantic(Scope *sc)
|
|
183 {
|
|
184 }
|
|
185 }
|
|
186
|
|
187
|
|
188 /****************************** LabelSymbol ******************************/
|
|
189
|
|
190 class LabelSymbol : Symbol
|
|
191 { Loc loc;
|
|
192 LabelStatement statement;
|
|
193
|
|
194 this(Loc loc, Identifier* ident, LabelStatement statement)
|
|
195 {
|
|
196 super(ident);
|
|
197 this.loc = loc;
|
|
198 this.statement = statement;
|
|
199 }
|
|
200 }
|
|
201
|
|
202
|