-
Notifications
You must be signed in to change notification settings - Fork 1
/
cparse-custom.cpp
183 lines (139 loc) · 4.35 KB
/
cparse-custom.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
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
#include <sstream>
#include "./cparse/shunting-yard.h"
#include "./cparse/shunting-yard-exceptions.h"
#include "./statements.h"
#include "./range.h"
namespace custom_features {
std::string parseName(const char** source, char end_char = '\0') {
std::stringstream ss;
const char* code = *source;
// Parse the function name:
if (isalpha(*code) || *code == '_') {
ss << *code;
++code;
while (isalnum(*code) || *code == '_') {
ss << *code;
++code;
}
// Find the beggining of the non space character:
while (isspace(*code) && *code != end_char) ++code;
} else {
throw syntax_error("Expected variable name!");
}
*source = code;
return ss.str();
}
packToken new_operator(const packToken& left, const packToken& right, evaluationData* data) {
TokenMap& parent = left.asMap();
Tuple args;
if (right->type == TUPLE) {
args = right.asTuple();
} else {
args = Tuple(right);
}
// Create a new Map:
packToken child = TokenMap(&parent);
// Call its init function if it exists:
packToken* init = parent.find("__init__");
if (init) {
Function* func = init->asFunc();
Function::call(child, func, &args, data->scope);
}
// Return it:
return child;
}
void new_parser(const char* expr, const char** rest, rpnBuilder* data) {
// Ignore white spaces:
while (isspace(*expr)) ++expr;
const char* name_start = expr;
if (*name_start == '(') {
throw syntax_error("Expected a class name after `new` operator!");
}
// Parse the class reference as an expression:
TokenQueue_t name_rpn = calculator::toRPN(expr, data->scope, "(", &expr);
const char* name_end = expr;
// Move `class_rpn` to the top of `data->rpn`:
while (!name_rpn.empty()) {
TokenBase* token = name_rpn.front();
name_rpn.pop();
data->handle_token(token);
}
// Add the new operator to the queue:
data->handle_op("new_op");
// Find the open bracket:
while (*expr && *expr != '(') ++expr;
if (*expr != '(') {
// Expected '(', throw error:
while (isspace(name_end[-1])) --name_end;
std::string name(name_start, name_end - name_start);
throw syntax_error("Expected '(' after `new " + name + "`");
}
// Save the '(' position:
*rest = expr;
}
packToken in_operator(const packToken& left, const packToken& right, evaluationData* data) {
Iterator* it;
if (right->type == IT) {
it = static_cast<Iterator*>(right->clone());
} else if (right->type & IT) {
it = static_cast<const Iterable*>(right.token())->getIterator();
} else {
throw syntax_error("Invalid use of `in` operator on non-iterable token!");
}
for (packToken* value = it->next(); value; value = it->next()) {
if (left == *value) return true;
}
delete it;
return false;
}
void in_parser(const char* expr, const char** rest, rpnBuilder* data) {
data->handle_op("in_op");
}
class CompiledFunc : public Function {
FuncDeclaration func;
public:
CompiledFunc() : Function() {}
CompiledFunc(const char* code, const char** rest = 0,
TokenMap parent_scope = &TokenMap::empty)
: Function(), func("", code, rest, parent_scope) {}
virtual ~CompiledFunc() {}
public:
const std::string name() const { return ""; }
const args_t args() const { return {}; }
packToken exec(TokenMap scope) const {
return func.asFunc();
}
public:
TokenBase* clone() const {
return new CompiledFunc(*this);
}
};
void function_parser(const char* expr, const char** rest, rpnBuilder* data) {
// Ignore white spaces:
while (isspace(*expr)) ++expr;
if (isalpha(*expr) || *expr == '_') {
// Ignore the name:
while (isalnum(*expr) || *expr == '_') ++expr;
// Ignore white spaces:
while (isspace(*expr)) ++expr;
}
data->handle_token(new CompiledFunc(expr, &expr, data->scope));
data->handle_op("()");
data->handle_token(new Tuple());
*rest = expr;
}
struct Startup {
Startup() {
parserMap_t& parser = calculator::Default().parserMap;
parser.add("new", &new_parser);
parser.add("function", &function_parser);
parser.add("in", &in_parser);
OppMap_t& opp = calculator::Default().opPrecedence;
opp.add("new_op", 14);
opp.add("in_op", 8);
opMap_t& opMap = calculator::Default().opMap;
opMap.add({MAP, "new_op", TUPLE}, &new_operator);
opMap.add({ANY_TYPE, "in_op", IT}, &in_operator);
}
} Startup;
} // namespace custom_features