-
Notifications
You must be signed in to change notification settings - Fork 0
/
botscript.h
333 lines (267 loc) · 7.92 KB
/
botscript.h
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#ifndef _BOTSCRIPT_H_
#define _BOTSCRIPT_H_
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <stdio.h>
using namespace std;
#define D( ... ) /* printf( __VA_ARGS__ ); printf( "\n" ) */
enum Opcode {
O_PUSH
};
class BSIdentifier;
class BSExpression;
class BSFunctionDefinition;
class SymbolRegistrar {
public:
static string RegisterSymbol( const string& s );
static string RegisterSymbol( BSFunctionDefinition* func );
static string RegisterLabel();
static void codegen( ostream& oss );
private:
static string NewSymbolName() {
ostringstream oss;
oss << "#" << symnum++;
return oss.str();
}
static size_t symnum;
static map<string, string> symbols;
};
class BSNode {
public:
virtual ~BSNode() {
}
virtual void codegen( ostream& oss ) {}
};
class BSExpression : public BSNode {};
class BSStatement : public BSNode {};
typedef vector<BSStatement*> StatementList;
typedef vector<BSExpression*> ExpressionList;
typedef vector<BSIdentifier*> IdentifierList;
class BSExpressionStatement : public BSStatement {
public:
BSExpression* expression;
BSExpressionStatement( BSExpression* expr ) : expression( expr ) {
D( "BSExpressionStatement" );
}
virtual void codegen( ostream& oss ) {
expression->codegen( oss );
}
};
class BSNil : public BSExpression {
public:
BSNil() { D( "BSNil" ); }
virtual void codegen( ostream& oss ) {
oss << " PUSH NIL" << endl;
}
};
class BSInteger : public BSExpression {
public:
long long value;
BSInteger( long long value_ ) : value( value_ ) { D( "BSInteger: %lld", value ); }
virtual void codegen( ostream& oss ) {
oss << " PUSH INT " << value << endl;
}
};
class BSDouble : public BSExpression {
public:
double value;
BSDouble( double value_ ) : value( value_ ) { D( "BSDouble" ); }
};
class BSString : public BSExpression {
public:
string value;
BSString( const string& value_ ) : value( value_ ) { D( "BSString: '%s'", value.c_str() ); }
virtual void codegen( ostream& oss ) {
oss << " PUSH STR " << SymbolRegistrar::RegisterSymbol( value ) << endl;
}
};
class BSReturn : public BSStatement {
public:
BSExpression* expression;
BSReturn() : expression( new BSNil() ) { D( "BSReturn" ); }
BSReturn( BSExpression* expr ) : expression( expr ) { D( "BSReturn expr" ); }
virtual void codegen( ostream& oss ) {
expression->codegen( oss );
oss << " RET" << endl;
}
};
class BSBlock : public BSExpression {
public:
StatementList statements;
BSBlock() { D( "BSBlock" ); }
virtual void codegen( ostream& oss ) {
StatementList::iterator it;
for( it = statements.begin(); it != statements.end(); ++it ) {
if( *it ) {
( *it )->codegen( oss );
} else {
oss << " NOP???" << endl;
}
}
}
void ensure_return() {
// Ensure functions have a return instruction
if( statements.size() ) {
BSReturn* ret = dynamic_cast<BSReturn*>( statements.back() );
if( ret ) {
return;
}
}
statements.push_back( new BSReturn() );
}
};
class BSIdentifier : public BSExpression {
public:
string value;
string subscript;
BSIdentifier( const string& value_ ) : value( value_ ) { D( "BSIdentifier: %s", value.c_str() ); }
BSIdentifier( const string& value_, const string& sub_ )
: value( value_ ), subscript( sub_ ) { D( "BSIdentifier" ); }
virtual void codegen( ostream& oss ) {
oss << " PUSH IDENT " << value;
if( subscript.size() ) {
oss << "." << subscript;
}
oss << endl;
}
bool operator < ( const BSIdentifier& other ) const
{
return ( value == other.value )
? subscript < other.subscript
: value < other.value;
}
};
class BSDictionaryItem : public BSExpression {
public:
BSIdentifier* key;
BSExpression* value;
BSDictionaryItem( BSIdentifier* k, BSExpression* v )
: key( k ), value( v ) { D( "BSDictionaryItem" ); }
};
class BSDictionary : public BSExpression {
public:
map<BSIdentifier*,BSExpression*> dict;
BSDictionary() { D( "BSDictionary" ); }
void addItem( const BSDictionaryItem* item ) {
dict[ item->key ] = item->value;
}
};
class BSFunctionCall : public BSExpression {
public:
BSIdentifier* function_name;
ExpressionList* args;
BSFunctionCall( BSIdentifier* fname_, ExpressionList* args_ )
: function_name( fname_ ), args( args_ ) { D( "BSFunctionCall" ); }
virtual void codegen( ostream& oss ) {
// Evaluate args in reverse order & put on the stack
for( ExpressionList::reverse_iterator it = args->rbegin(); it !=
args->rend(); ++it ) {
if( *it ) {
( *it )->codegen( oss );
} else {
printf( "Null statement\n" );
}
}
oss << " CALL " << function_name->value << " " << args->size() << endl;
}
};
class BSFunctionDefinition : public BSExpression {
public:
IdentifierList* args;
BSBlock* body;
BSFunctionDefinition( IdentifierList* args_,
BSBlock* body_ )
: args( args_ ), body( body_ )
{
body->ensure_return();
}
virtual void codegen( ostream& oss ) {
string symname = SymbolRegistrar::RegisterSymbol( this );
oss << " PUSH FUNC " << symname << endl;
}
};
class BSConditionalBlock : public BSStatement {
public:
BSExpression* expression;
BSBlock* statements;
protected:
BSConditionalBlock( BSExpression* expr, BSBlock* stmts )
: expression( expr ), statements( stmts ) {}
};
class BSIfBlock : public BSConditionalBlock {
public:
BSBlock* else_statements;
BSIfBlock( BSExpression* expr,
BSBlock* stmts )
: BSConditionalBlock( expr, stmts ), else_statements( new BSBlock() ) { D( "BSIfBlock" ); }
BSIfBlock( BSExpression* expr,
BSBlock* stmts,
BSBlock* else_stmts )
: BSConditionalBlock( expr, stmts ), else_statements( else_stmts ) { D( "BSIfBlock with else" ); }
virtual void codegen( ostream& oss ) {
string first = SymbolRegistrar::RegisterLabel();
string second = SymbolRegistrar::RegisterLabel();
expression->codegen( oss );
oss << " JZ " << first << endl;
statements->codegen( oss );
if( else_statements->statements.size() ) {
oss << " JZ " << second << endl;
oss << first << endl;
else_statements->codegen( oss );
oss << second << endl;
} else {
oss << first << endl;
}
}
};
class BSForEachLoop : public BSConditionalBlock {
public:
IdentifierList* identifiers;
BSForEachLoop( IdentifierList* idents,
BSExpression* expr,
BSBlock* stmts )
: BSConditionalBlock( expr, stmts ), identifiers( idents ) { D( "BSForEachLoop" ); }
};
class BSAddAssign : public BSStatement {
public:
BSIdentifier* identifier;
BSExpression* expression;
BSAddAssign( BSIdentifier* ident, BSExpression* expr )
: identifier( ident ), expression( expr ) { D( "BSAdd" ); }
};
class BSAdd : public BSExpression {
public:
BSExpression* a;
BSExpression* b;
BSAdd( BSExpression* a_, BSExpression* b_ ) : a( a_ ), b( b_ ) {}
virtual void codegen( ostream& oss ) {
a->codegen( oss );
b->codegen( oss );
oss << " ADD" << endl;
}
};
class BSAssign : public BSStatement {
public:
BSIdentifier* lhs;
BSExpression* rhs;
BSAssign( BSIdentifier* ident, BSExpression* expr )
: lhs( ident ), rhs( expr ) { D( "BSAssign" ); }
virtual void codegen( ostream& oss )
{
lhs->codegen( oss );
rhs->codegen( oss );
oss << " ASSIGN" << endl;
}
};
class BSBinaryOperation : public BSExpression {
public:
BSExpression* a;
BSExpression* b;
int op;
BSBinaryOperation( BSExpression* a_, BSExpression* b_, int op_ )
: a( a_ ), b( b_ ), op( op_ ) {}
virtual void codegen( ostream& oss );
};
#endif