File: ucsym.h

package info (click to toggle)
exult 1.2-15.3
  • links: PTS, VCS
  • area: contrib
  • in suites: jessie, jessie-kfreebsd
  • size: 8,656 kB
  • ctags: 10,524
  • sloc: cpp: 99,373; sh: 7,324; ansic: 4,659; makefile: 991; yacc: 769; lex: 313; xml: 19
file content (242 lines) | stat: -rw-r--r-- 6,274 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
 **	Ucsym.h - Usecode compiler symbol table.
 **
 **	Written: 1/2/01 - JSF
 **/

/*
Copyright (C) 2000 The Exult Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifndef INCL_UCSYM
#define INCL_UCSYM

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <string>
#include <map>
#include <vector>
using std::vector;

class Uc_array_expression;
class Uc_expression;
class Uc_function;

/*
 *	For comparing names:
 */
class String_compare
	{
public:
	bool operator()(char * const &x, char * const &y) const;
	};

/*
 *	A formal parameter or local symbol within a function.
 */
class Uc_symbol
	{
protected:
	std::string name;			// This will be the key.
public:
	friend class Uc_scope;
	Uc_symbol(char *nm) : name(nm)
		{  }
	const char *get_name() { return name.c_str(); }
					// Gen. code to put result on stack.
	virtual int gen_value(vector<char>& out);
					// Gen. to assign from stack.
	virtual int gen_assign(vector<char>& out);
					// Generate function/procedure call.
	virtual int gen_call(vector<char>& out, Uc_function *fun, bool orig,
		Uc_expression *item, Uc_array_expression *parms, 
							bool retvalue);
	virtual int get_string_offset()	// Get offset in text_data.
		{ return -1; }
					// Return var/int expression.
	virtual Uc_expression *create_expression();
	};

/*
 *	A variable (untyped) that can be assigned to.
 */
class Uc_var_symbol : public Uc_symbol
	{
protected:
	int offset;			// Within function.  Locals follow
					//   formal parameters.
public:
	friend class Uc_scope;
	Uc_var_symbol(char *nm, int off) : Uc_symbol(nm), offset(off)
		{  }
	int get_offset()
		{ return offset; }
					// Gen. code to put result on stack.
	virtual int gen_value(vector<char>& out);
					// Gen. to assign from stack.
	virtual int gen_assign(vector<char>& out);
					// Return var/int expression.
	virtual Uc_expression *create_expression();
	};

/*
 *	A static (persistent) variable.
 */
class Uc_static_var_symbol : public Uc_var_symbol
	{
public:
	Uc_static_var_symbol(char *nm, int off) : Uc_var_symbol(nm, offset)
		{  }
					// Gen. code to put result on stack.
	virtual int gen_value(vector<char>& out);
					// Gen. to assign from stack.
	virtual int gen_assign(vector<char>& out);
	};

/*
 *	A constant integer variable.
 */
class Uc_const_int_symbol : public Uc_symbol
	{
	int value;
public:
	Uc_const_int_symbol(char *nm, int v) : Uc_symbol(nm), value(v)
		{  }
					// Gen. code to put result on stack.
	virtual int gen_value(vector<char>& out);
					// Return var/int expression.
	virtual Uc_expression *create_expression();
	int get_value() const
		{ return value; }
	};

/*
 *	A (constant) string.  The offset is within the usecode function's
 *	text_data field.
 */
class Uc_string_symbol : public Uc_symbol
	{
	int offset;			// In function's text_data.
public:
	Uc_string_symbol(char *nm, int off) : Uc_symbol(nm), offset(off)
		{  }
					// Gen. code to put result on stack.
	virtual int gen_value(vector<char>& out);
	virtual int get_string_offset()	// Get offset in text_data.
		{ return offset; }
					// Return var/int expression.
	virtual Uc_expression *create_expression();
	};

/*
 *	An intrinsic symbol:
 */
class Uc_intrinsic_symbol : public Uc_symbol
	{
	int intrinsic_num;		// Intrinsic #.
	int num_parms;			// # parms. +++++Not used/set yet.
public:
	Uc_intrinsic_symbol(char *nm, int n) : Uc_symbol(nm), intrinsic_num(n),
		num_parms(0)
		{  }
	int get_intrinsic_num()
		{ return intrinsic_num; }
	int get_num_parms()		// ++++Not valid yet.
		{ return num_parms; }
					// Generate function/procedure call.
	virtual int gen_call(vector<char>& out, Uc_function *fun, bool orig,
		Uc_expression *item, Uc_array_expression *parms, 
							bool retvalue);
	};

/*
 *	A function-prototype symbol:
 */
class Uc_function_symbol : public Uc_symbol
	{
	static int last_num;		// Last 'usecode_num', so we can
					//   assign automatically.
public:
					// Keep track of #'s used.
	typedef std::map<int, Uc_symbol *> Sym_nums;
private:
	static Sym_nums nums_used;
					// Note:  offset = Usecode fun. #.
	std::vector<char *> parms;	// Parameters.
	int usecode_num;		// Usecode function #.
public:
	Uc_function_symbol(char *nm, int num, std::vector<char *>& p);
	const std::vector<char *>& get_parms()
		{ return parms; }
	int get_usecode_num()
		{ return usecode_num; }
	int get_num_parms()
		{ return parms.size(); }
					// Generate function/procedure call.
	virtual int gen_call(vector<char>& out, Uc_function *fun, bool orig,
		Uc_expression *item, Uc_array_expression *parms, 
							bool retvalue);
	};

/*
 *	A 'scope' in the symbol table:
 */
class Uc_scope
	{
	Uc_scope *parent;		// ->parent.
					// For finding syms. by name.
	typedef std::map<char *, Uc_symbol *, String_compare> Sym_map;
	Sym_map symbols;
	std::vector<Uc_scope *> scopes;	// Scopes within.
public:
	Uc_scope(Uc_scope *p) : parent(p)
		{  }
	~Uc_scope();
	Uc_scope *get_parent()
		{ return parent; }
	Uc_symbol *search(const char *nm)	// Look in this scope.
		{
		char *nm1 = (char *) nm;
		Sym_map::const_iterator it = symbols.find(nm1);
		if (it == symbols.end())
			return 0;
		else
			return (*it).second;
		}
					// Search upwards through scopes.
	Uc_symbol *search_up(char *nm);
	void add(Uc_symbol *sym)	// Add (does NOT check for dups.)
		{
		const char *nm = sym->name.c_str();
		char *nm1 = (char *)nm;	// ???Can't figure this out!
		symbols[nm1] = sym; 
		}
	Uc_scope *add_scope()		// Create new scope.
		{
		Uc_scope *newscope = new Uc_scope(this);
		scopes.push_back(newscope);
		return newscope;
		}
					// Add a function decl.
	int add_function_symbol(Uc_function_symbol *fun);
	};

#endif