-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix.cc
176 lines (172 loc) · 5.39 KB
/
postfix.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
167
168
169
170
171
172
173
174
175
176
#include "ast.h"
Tokens Tokens::shuntingYard( int i, int n ) const {
stack<Token> operator_stack;
stack<int> comma_stack;
Tokens output;
for( Tokens::const_iterator t = begin() + i; t != ( begin() + n ); t++ ) {
const Token& token = *t;
if( !operator_stack.empty() && operator_stack.top().type == LEFT_BRACKET && comma_stack.top() == 0 && token.type != RIGHT_BRACKET )
comma_stack.top() = 1;
if( token.type == INTEGER || token.type == FLOAT || token.type == STRING || token.type == VARIABLE )
output.push_back( token );
else if( token.type == FUNCTION )
operator_stack.push( token );
else if( token.type == COMMA ) {
comma_stack.top()++;
if( operator_stack.empty() )
throw compile_exception( "No left-parenthesis before ','", i );
while( operator_stack.top().type != LEFT_BRACKET ) {
output.push_back( operator_stack.top() );
operator_stack.pop();
if( operator_stack.empty() )
throw compile_exception( "No left-parenthesis before ','", i );
}
} else if( token.type == OPERATOR ) {
while( !operator_stack.empty() && operator_stack.top().type == OPERATOR && (
( get<1>( operator_precedence.at(token.str()) ) == LEFT && get<0>(operator_precedence.at(token.str())) >= get<0>(operator_precedence.at(operator_stack.top().str())) ) ||
( get<1>( operator_precedence.at(token.str()) ) == RIGHT && get<0>(operator_precedence.at(token.str())) > get<0>(operator_precedence.at(operator_stack.top().str())) ) ) ) {
output.push_back( operator_stack.top() );
operator_stack.pop();
}
if( token.str() == "~" ) {
bool done = false;
stack<Token> reverse;
while( ++t != begin() + n ) {
if( t->type == TYPENAME ) {
reverse.push( *t );
if( finalTypenames.count( t->str() ) ) { // ending typename
done = true;
break;
}
} else
throw compile_exception( "Incomplete typename in typecast", i );
}
while( !reverse.empty() ) {
output.push_back( reverse.top() );
reverse.pop();
}
output.push_back( token );
if( !done )
throw compile_exception( "Unexpected end of statement", i );
} else
operator_stack.push( token );
} else if( token.type == LEFT_BRACKET ) {
operator_stack.push( token );
comma_stack.push(0);
} else if( token.type == RIGHT_BRACKET ) {
if( operator_stack.empty() )
throw compile_exception( "Missing left-parenthesis", i );
while( operator_stack.top().type != LEFT_BRACKET ) {
output.push_back( operator_stack.top() );
operator_stack.pop();
if( operator_stack.empty() )
throw compile_exception( "Missing left-parenthesis", i );
}
operator_stack.pop();
if( token.str() == "[" ) {
Token t;
t.str() = "init-list " + to_string( comma_stack.top() );
t.type = KEYWORD;
output.push_back( t );
} else if( token.str() == "{" ) {
Token t;
t.str() = "init-set " + to_string( comma_stack.top() );
t.type = KEYWORD;
output.push_back( t );
} else if( !operator_stack.empty() && operator_stack.top().type == FUNCTION ) {
operator_stack.top().str() += " " + to_string( comma_stack.top() );
output.push_back( operator_stack.top() );
operator_stack.pop();
} else if( comma_stack.top() > 1 ) {
Token t;
t.str() = "init-tuple " + to_string( comma_stack.top() );
t.type = KEYWORD;
output.push_back( t );
}
comma_stack.pop();
}
}
while( !operator_stack.empty() ) {
if( operator_stack.top().type == LEFT_BRACKET )
throw compile_exception( "Missing right-parenthesis", i );
output.push_back( operator_stack.top() );
operator_stack.pop();
}
return output;
}
AST* Tokens::postfixTranslate( int& n ) const {
AST* h = new AST;
int parameters;
stack<AST*> reverse;
n--;
if( n < 0 )
throw compile_exception( "Expecting more tokens ( Hint: += / -= / ... are no legal operators )", -1 );
switch( at(n).type ) { /*KEYWORDS NOT YET SUPPORTED*/
case OPERATOR:
if( get<2>( operator_precedence.at(at(n).str()) ) == true ) // is unary
parameters = 1;
else
parameters = 2;
if( operator_function_name.count(at(n)) )
h->val = "@" + operator_function_name.at(at(n));
else
h->val = at(n);
h->type = AT_FUNCTIONCALL;
break;
case FUNCTION: {
stringstream ss( at(n).str(), ios_base::in );
ss >> (h->val) >> parameters;
h->type = AT_FUNCTIONCALL;
break;
}
case INTEGER:
case FLOAT:
h->type = AT_NUMBER;
h->val = at(n);
parameters = 0;
break;
case STRING:
h->type = AT_STRING;
h->val = at(n);
parameters = 0;
break;
case VARIABLE:
h->type = AT_WORD;
h->val = at(n);
parameters = 0;
break;
case KEYWORD: {
stringstream ss( at(n) );
string a;
ss >> a;
if( a.compare( 0, 4, "init" ) == 0 ) {
if( a == "init-list" )
h->type = AT_INLINE_LIST;
else if( a == "init-set" )
h->type = AT_INLINE_SET;
else if( a == "init-tuple" )
h->type = AT_ARRAY;
else
throw compile_exception( "Unknown initaliser list", -1 );
h->val = at(n);
ss >> parameters;
break;
}
}
case TYPENAME:
h->type = AT_DATATYPE;
h->val = at(n);
parameters = nonFinalTypenames.count( at(n) );
break;
default:
throw compile_exception( "Unknown/Unsupported token type " + to_string( at(n).type ), -1 );
h = nullptr;
}
for( int i = 0; i < parameters; i++ )
reverse.push( postfixTranslate( n ) );
while( !reverse.empty() ) {
h->children.push_back( reverse.top() );
reverse.pop();
}
return h;
}