-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.cc
166 lines (148 loc) · 4.09 KB
/
ast.cc
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
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <map>
#include <tuple>
#include <climits>
#include <sstream>
#include <exception>
#include <set>
#include "ast.h"
const set<string> finalTypenames {
"int",
"string",
"rat",
"real",
"bool"
};
const set<string> nonFinalTypenames { "set", "list" };
const map< string, tuple< int, associativity, bool > > operator_precedence {
{"->",make_tuple(-1,LEFT,false)}, // member variable by name
{".",make_tuple(-1,LEFT,false)}, // VSO to SVO
{"!",make_tuple(0,RIGHT,true)}, // sort list
{"?",make_tuple(0,RIGHT,true)}, // shuffle list
{"#",make_tuple(0,RIGHT,true)}, // count elements list/set
{"@",make_tuple(1,LEFT,false)}, // list indexing
{"\u220A",make_tuple(1,LEFT,false)}, // set contains
{"<->",make_tuple(2,LEFT,false)}, // swap elements
{"<~>",make_tuple(2,RIGHT,false)}, //
{"^",make_tuple(3,RIGHT,false)}, // exponentiation
{"-u",make_tuple(4,RIGHT,true)}, // unary minus
{"*",make_tuple(5,LEFT,false)}, // multiplication
{"/",make_tuple(5,LEFT,false)}, // division
{"\u00AC",make_tuple(6,RIGHT,true)}, // not
{"+",make_tuple(6,LEFT,false)}, // addition
{"-",make_tuple(6,LEFT,false)}, // subtraction
{":",make_tuple(7,LEFT,false)}, // range construction
{"~",make_tuple(12,LEFT,false)}, // cast (temp)
{"<",make_tuple(13,LEFT,false)}, // less-than
{">",make_tuple(13,LEFT,false)}, // more-than
{"<=",make_tuple(13,LEFT,false)}, // at most
{"≤",make_tuple(13,LEFT,false)}, // at most
{">=",make_tuple(13,LEFT,false)}, // at least
{"≥",make_tuple(13,LEFT,false)}, // at least
{"==",make_tuple(14,LEFT,false)}, // equal to
{"!=",make_tuple(14,LEFT,false)}, // less-than or more-than ( not equal to )
{"\u2260",make_tuple(14,LEFT,false)}, // less-than or more-than ( not equal to )
{"\u2227",make_tuple(15,LEFT,false)}, // and
{"\u22BB",make_tuple(16,LEFT,false)}, // xor
{"\u2228",make_tuple(17,LEFT,false)}, // or
{"=",make_tuple(18,RIGHT,false)} // asignment
};
const set<string> overloadable_operators {
"!", "?", "#",
"-","+","*","/","^",
"<"
};
const map<string,string> operator_function_name {
{"!","sort"},
{"?","shuffle"},
{"#","size_of"},
{"\u220A","contains"},
{"<->","std::swap"},
{"^","pow"}, // temp
{"-u","operator-"},
{"*","operator*"},
{"/","operator/"},
{"\u00AC","ynot"},
{"+","operator+"},
{"-","operator-"},
{"<","operator<"},
{">","operator>"},
{"<=","operator<="},
{"\u2264","operator<="},
{">=","operator>="},
{"\u2265","operator>="},
{"==","operator=="},
{"!=","operator!="},
{"\u2260","operator!="},
{"\u2227","yand"},
{"\u22BB","yxor"},
{"\u2228","yor"}
};
const set<string> keywords {
"import",
"space",
"if",
"else",
"while",
"for",
"in",
"break",
"continue",
"function",
"return"
};
vector<AST*> blockRegister;
bool AST::compare( const AST* other ) const {
if( type != other->type || val != other->val || children.size() != other->children.size() )
return false;
for( size_t i = 0; i < children.size(); i++ )
if( !children.at(i)->compare( other->children.at(i) ) )
return false;
return true;
}
AST::AST( ast_type_t t ) {
type = t;
}
AST::AST( ast_type_t t, string v, vector<AST*> c ) {
type = t;
val = move( v );
children = move( c );
}
AST::AST( const Tokens& tokens ) {
type = AT_SYNCBLOCK;
val = "0";
try {
children = tokens.blockTranslate( 0, tokens.size(), finalTypenames );
} catch( compile_exception& e ) {
if( e.token_id >= 0 )
cerr << "(" << tokens.at(e.token_id).str() << ")";
cerr << e.what() << endl;
throw e;
}
}
AST::~AST() { // non-recursive delete
}
AST* AST::cascade() {
for( auto child: children )
delete child->cascade();
return this;
}
const char* compile_exception::what() const noexcept {
string* s = new string( "(" + to_string( token_id ) + ") Error: " + err_str + "!" );
return s->c_str();
}
compile_exception::compile_exception( string err, int i ) : err_str(err), token_id(i) {
}
ostream& operator<<( ostream& os, const AST& ast ) {
os << "(" << ast.type << "," << ast.val << ",";
for( const AST* c: ast.children ) {
if( c == nullptr )
os << "INTERNAL ERROR";
else
os << *c;
}
return os << ")";
}