-
Notifications
You must be signed in to change notification settings - Fork 0
/
botscript.cpp
70 lines (61 loc) · 1.67 KB
/
botscript.cpp
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
#include "botscript.h"
#include "compiler-parser.tab.h"
#include <sstream>
#include <stdlib.h>
size_t SymbolRegistrar::symnum = 0;
map<string, string> SymbolRegistrar::symbols;
string
SymbolRegistrar::RegisterSymbol( const string& s )
{
string symname = NewSymbolName();
ostringstream oss;
oss << "STR " << s.size() << " " << s << endl;
symbols[ symname ] = oss.str();
return symname;
}
string
SymbolRegistrar::RegisterLabel()
{
return NewSymbolName();
}
string
SymbolRegistrar::RegisterSymbol( BSFunctionDefinition* func )
{
ostringstream oss;
oss << "FUNC " << func->args->size() << endl;
for( IdentifierList::iterator it = func->args->begin(); it !=
func->args->end(); ++it ) {
oss << " POP " << ( *it )->value << endl;
}
func->body->codegen( oss );
string symname = NewSymbolName();
symbols[ symname ] = oss.str();
return symname;
}
void
SymbolRegistrar::codegen( ostream& oss )
{
map<string, string>::iterator it;
for( it = symbols.begin(); it != symbols.end(); ++it ) {
oss << it->first << " " << it->second;
}
}
void
BSBinaryOperation::codegen( ostream& oss )
{
b->codegen( oss );
a->codegen( oss );
switch( op ) {
case TEQUAL: oss << " CMP" << endl; break;
case TNEQUAL: oss << " CMPNE" << endl; break;
case TAND: oss << " AND" << endl; break;
case TOR: oss << " OR" << endl; break;
case TGT: oss << " CMPGT" << endl; break;
case TGTE: oss << " CMPGTE" << endl; break;
case TLT: oss << " CMPLT" << endl; break;
case TLTE: oss << " CMPLTE" << endl; break;
default:
fprintf( stderr, "Unknown operator: %d\n", op );
exit( -1 );
}
}