-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.cpp
More file actions
459 lines (434 loc) · 22 KB
/
main.cpp
File metadata and controls
459 lines (434 loc) · 22 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "compilation/await_condition.h"
#include "compilation/await_routine.h"
#include "compilation/expression.h"
#include "compilation/expressions.h"
#include "compilation/method_call.h"
#include "compilation/property_assignment.h"
#include "compilation/routine.h"
#include "compilation/routine_call.h"
#include "compilation/rule.h"
#include "compilation/variable.h"
#include "compilation/variable_assignment.h"
#include "global.h"
#include "modules/bluetooth.h"
#include "modules/core.h"
#include "modules/expander.h"
#include "modules/module.h"
#include "proxy.h"
#include "rom/gpio.h"
#include "rom/uart.h"
#include "storage.h"
#include "utils/tictoc.h"
#include "utils/timing.h"
#include "utils/uart.h"
#include <chrono>
#include <functional>
#include <memory>
#include <stdexcept>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define BUFFER_SIZE 1024
Core_ptr core_module;
extern "C" {
#include "parser.h"
void app_main();
}
void process_lizard(const char *line, bool trigger_keep_alive = true, bool from_expander = false);
std::string identifier_to_string(const struct owl_ref ref) {
const struct parsed_identifier identifier = parsed_identifier_get(ref);
return std::string(identifier.identifier, identifier.length);
}
Expression_ptr compile_expression(const struct owl_ref ref);
std::vector<ConstExpression_ptr> compile_arguments(const struct owl_ref ref) {
std::vector<ConstExpression_ptr> arguments;
for (struct owl_ref r = ref; !r.empty; r = owl_next(r)) {
arguments.push_back(compile_expression(r));
}
return arguments;
}
Expression_ptr compile_expression(const struct owl_ref ref) {
const struct parsed_expression expression = parsed_expression_get(ref);
switch (expression.type) {
case PARSED_TRUE:
return std::make_shared<BooleanExpression>(true);
case PARSED_FALSE:
return std::make_shared<BooleanExpression>(false);
case PARSED_STRING: {
const struct parsed_string string = parsed_string_get(expression.string);
return std::make_shared<StringExpression>(std::string(string.string, string.length));
}
case PARSED_INTEGER:
return std::make_shared<IntegerExpression>(parsed_integer_get(expression.integer).integer);
case PARSED_NUMBER:
return std::make_shared<NumberExpression>(parsed_number_get(expression.number).number);
case PARSED_VARIABLE:
return std::make_shared<VariableExpression>(Global::get_variable(identifier_to_string(expression.identifier)));
case PARSED_PROPERTY:
return std::make_shared<PropertyExpression>(Global::get_module(identifier_to_string(expression.module_name)),
identifier_to_string(expression.property_name));
case PARSED_PARENTHESES:
return compile_expression(expression.expression);
case PARSED_POWER:
return std::make_shared<PowerExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_NEGATE:
return std::make_shared<NegateExpression>(compile_expression(expression.operand));
case PARSED_MULTIPLY:
return std::make_shared<MultiplyExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_DIVIDE:
return std::make_shared<DivideExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_MODULO:
return std::make_shared<ModuloExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_FLOOR_DIVIDE:
return std::make_shared<FloorDivideExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_ADD:
return std::make_shared<AddExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_SUBTRACT:
return std::make_shared<SubtractExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_SHIFT_LEFT:
return std::make_shared<ShiftLeftExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_SHIFT_RIGHT:
return std::make_shared<ShiftRightExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_BIT_AND:
return std::make_shared<BitAndExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_BIT_XOR:
return std::make_shared<BitXorExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_BIT_OR:
return std::make_shared<BitOrExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_GREATER:
return std::make_shared<GreaterExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_LESS:
return std::make_shared<LessExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_GREATER_EQUAL:
return std::make_shared<GreaterEqualExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_LESS_EQUAL:
return std::make_shared<LessEqualExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_EQUAL:
return std::make_shared<EqualExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_UNEQUAL:
return std::make_shared<UnequalExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_NOT:
return std::make_shared<NotExpression>(compile_expression(expression.operand));
case PARSED_AND:
return std::make_shared<AndExpression>(compile_expression(expression.left), compile_expression(expression.right));
case PARSED_OR:
return std::make_shared<OrExpression>(compile_expression(expression.left), compile_expression(expression.right));
default:
throw std::runtime_error("invalid expression");
}
}
std::vector<Action_ptr> compile_actions(const struct owl_ref ref) {
std::vector<Action_ptr> actions;
for (struct owl_ref r = ref; !r.empty; r = owl_next(r)) {
const struct parsed_action action = parsed_action_get(r);
if (!action.noop.empty) {
} else if (!action.method_call.empty) {
const struct parsed_method_call method_call = parsed_method_call_get(action.method_call);
const std::string module_name = identifier_to_string(method_call.module_name);
const Module_ptr module = Global::get_module(module_name);
const std::string method_name = identifier_to_string(method_call.method_name);
const std::vector<ConstExpression_ptr> arguments = compile_arguments(method_call.argument);
actions.push_back(std::make_shared<MethodCall>(module, method_name, arguments));
} else if (!action.routine_call.empty) {
const struct parsed_routine_call routine_call = parsed_routine_call_get(action.routine_call);
const std::string routine_name = identifier_to_string(routine_call.routine_name);
const Routine_ptr routine = Global::get_routine(routine_name);
actions.push_back(std::make_shared<RoutineCall>(routine));
} else if (!action.property_assignment.empty) {
const struct parsed_property_assignment property_assignment = parsed_property_assignment_get(action.property_assignment);
const std::string module_name = identifier_to_string(property_assignment.module_name);
const Module_ptr module = Global::get_module(module_name);
const std::string property_name = identifier_to_string(property_assignment.property_name);
const ConstExpression_ptr expression = compile_expression(property_assignment.expression);
actions.push_back(std::make_shared<PropertyAssignment>(module, property_name, expression));
} else if (!action.variable_assignment.empty) {
const struct parsed_variable_assignment variable_assignment = parsed_variable_assignment_get(action.variable_assignment);
const std::string variable_name = identifier_to_string(variable_assignment.variable_name);
const Variable_ptr variable = Global::get_variable(variable_name);
const ConstExpression_ptr expression = compile_expression(variable_assignment.expression);
if (variable->type != expression->type) {
throw std::runtime_error("type mismatch for variable assignment");
}
if (variable->type == identifier) {
throw std::runtime_error("assignment of identifiers is forbidden");
}
actions.push_back(std::make_shared<VariableAssignment>(variable, expression));
} else if (!action.await_condition.empty) {
struct parsed_await_condition await_condition = parsed_await_condition_get(action.await_condition);
const ConstExpression_ptr condition = compile_expression(await_condition.condition);
actions.push_back(std::make_shared<AwaitCondition>(condition));
} else if (!action.await_routine.empty) {
struct parsed_await_routine await_routine = parsed_await_routine_get(action.await_routine);
const std::string routine_name = identifier_to_string(await_routine.routine_name);
const Routine_ptr routine = Global::get_routine(routine_name);
actions.push_back(std::make_shared<AwaitRoutine>(routine));
} else {
throw std::runtime_error("unknown action type");
}
}
return actions;
}
void process_tree(owl_tree *const tree, bool from_expander) {
const struct parsed_statements statements = owl_tree_get_parsed_statements(tree);
for (struct owl_ref r = statements.statement; !r.empty; r = owl_next(r)) {
const struct parsed_statement statement = parsed_statement_get(r);
if (!statement.noop.empty) {
} else if (!statement.expression.empty) {
const ConstExpression_ptr expression = compile_expression(statement.expression);
static char buffer[256];
expression->print_to_buffer(buffer, sizeof(buffer));
echo(buffer);
} else if (!statement.constructor.empty) {
const struct parsed_constructor constructor = parsed_constructor_get(statement.constructor);
if (constructor.expander_name.empty) {
const std::string module_name = identifier_to_string(constructor.module_name);
if (Global::has_module(module_name)) {
throw std::runtime_error("module \"" + module_name + "\" already exists");
}
const std::string module_type = identifier_to_string(constructor.module_type);
const std::vector<ConstExpression_ptr> arguments = compile_arguments(constructor.argument);
const Module_ptr module = Module::create(module_type, module_name, arguments, process_lizard);
Global::add_module(module_name, module);
} else {
const std::string module_name = identifier_to_string(constructor.module_name);
const std::string module_type = identifier_to_string(constructor.module_type);
const std::string expander_name = identifier_to_string(constructor.expander_name);
const Module_ptr expander_module = Global::get_module(expander_name);
if (expander_module->type != expander) {
throw std::runtime_error("module \"" + expander_name + "\" is not an expander");
}
const Expander_ptr expander = std::static_pointer_cast<Expander>(expander_module);
const std::vector<ConstExpression_ptr> arguments = compile_arguments(constructor.argument);
const Module_ptr proxy = std::make_shared<Proxy>(module_name, expander_name, module_type, expander, arguments);
Global::add_module(module_name, proxy);
}
} else if (!statement.method_call.empty) {
const struct parsed_method_call method_call = parsed_method_call_get(statement.method_call);
const std::string module_name = identifier_to_string(method_call.module_name);
const Module_ptr module = Global::get_module(module_name);
const std::string method_name = identifier_to_string(method_call.method_name);
const std::vector<ConstExpression_ptr> arguments = compile_arguments(method_call.argument);
module->call_with_shadows(method_name, arguments);
} else if (!statement.routine_call.empty) {
const struct parsed_routine_call routine_call = parsed_routine_call_get(statement.routine_call);
const std::string routine_name = identifier_to_string(routine_call.routine_name);
const Routine_ptr routine = Global::get_routine(routine_name);
if (routine->is_running()) {
throw std::runtime_error("routine \"" + routine_name + "\" is already running");
}
routine->start();
} else if (!statement.property_assignment.empty) {
const struct parsed_property_assignment property_assignment = parsed_property_assignment_get(statement.property_assignment);
const std::string module_name = identifier_to_string(property_assignment.module_name);
const Module_ptr module = Global::get_module(module_name);
const std::string property_name = identifier_to_string(property_assignment.property_name);
const ConstExpression_ptr expression = compile_expression(property_assignment.expression);
module->write_property(property_name, expression, from_expander);
} else if (!statement.variable_assignment.empty) {
const struct parsed_variable_assignment variable_assignment = parsed_variable_assignment_get(statement.variable_assignment);
const std::string variable_name = identifier_to_string(variable_assignment.variable_name);
const Variable_ptr variable = Global::get_variable(variable_name);
const ConstExpression_ptr expression = compile_expression(variable_assignment.expression);
variable->assign(expression);
} else if (!statement.variable_declaration.empty) {
const struct parsed_variable_declaration variable_declaration = parsed_variable_declaration_get(statement.variable_declaration);
const struct parsed_datatype datatype = parsed_datatype_get(variable_declaration.datatype);
const std::string variable_name = identifier_to_string(variable_declaration.variable_name);
switch (datatype.type) {
case PARSED_BOOLEAN:
Global::add_variable(variable_name, std::make_shared<BooleanVariable>());
break;
case PARSED_INTEGER:
Global::add_variable(variable_name, std::make_shared<IntegerVariable>());
break;
case PARSED_NUMBER:
Global::add_variable(variable_name, std::make_shared<NumberVariable>());
break;
case PARSED_STRING:
Global::add_variable(variable_name, std::make_shared<StringVariable>());
break;
default:
throw std::runtime_error("invalid data type for variable declaration");
}
if (!variable_declaration.expression.empty) {
const ConstExpression_ptr expression = compile_expression(variable_declaration.expression);
Global::get_variable(variable_name)->assign(expression);
}
} else if (!statement.routine_definition.empty) {
const struct parsed_routine_definition routine_definition = parsed_routine_definition_get(statement.routine_definition);
const std::string routine_name = identifier_to_string(routine_definition.routine_name);
if (Global::has_routine(routine_name)) {
throw std::runtime_error("routine \"" + routine_name + "\" already exists");
}
const struct parsed_actions actions = parsed_actions_get(routine_definition.actions);
Global::add_routine(routine_name, std::make_shared<Routine>(compile_actions(actions.action)));
} else if (!statement.rule_definition.empty) {
const struct parsed_rule_definition rule_definition = parsed_rule_definition_get(statement.rule_definition);
const struct parsed_actions actions = parsed_actions_get(rule_definition.actions);
const Routine_ptr routine = std::make_shared<Routine>(compile_actions(actions.action));
const ConstExpression_ptr condition = compile_expression(rule_definition.condition);
Global::add_rule(std::make_shared<Rule>(condition, routine));
} else {
throw std::runtime_error("unknown statement type");
}
}
}
void process_lizard(const char *line, bool trigger_keep_alive, bool from_expander) {
if (trigger_keep_alive) {
core_module->keep_alive();
}
const bool debug = core_module->get_property("debug")->boolean_value;
if (debug) {
echo(">> %s", line);
tic();
}
auto const tree = std::unique_ptr<owl_tree, std::function<void(owl_tree *)>>(owl_tree_create_from_string(line), owl_tree_destroy);
if (debug) {
toc("Tree creation");
}
struct source_range range;
switch (owl_tree_get_error(tree.get(), &range)) {
case ERROR_INVALID_FILE:
echo("error: invalid file");
break;
case ERROR_INVALID_OPTIONS:
echo("error: invalid options");
break;
case ERROR_INVALID_TOKEN:
echo("error: invalid token at range %zu %zu \"%s\"", range.start, range.end,
std::string(line, range.start, range.end - range.start).c_str());
break;
case ERROR_UNEXPECTED_TOKEN:
echo("error: unexpected token at range %zu %zu \"%s\"", range.start, range.end,
std::string(line, range.start, range.end - range.start).c_str());
break;
case ERROR_MORE_INPUT_NEEDED:
echo("error: more input needed at range %zu %zu", range.start, range.end);
break;
default:
if (debug) {
owl_tree_print(tree.get());
tic();
}
process_tree(tree.get(), from_expander);
if (debug) {
toc("Tree traversal");
}
}
}
void process_line(const char *line, const int len) {
if (len >= 2 && line[0] == '!') {
switch (line[1]) {
case '+':
Storage::append_to_startup(line + 2);
break;
case '-':
Storage::remove_from_startup(line + 2);
break;
case '?':
Storage::print_startup(line + 2);
break;
case '.':
Storage::save_startup();
break;
case '!':
process_lizard(line + 2);
break;
case '"':
echo(line + 2);
break;
default:
throw std::runtime_error("unrecognized control command");
}
} else {
process_lizard(line);
}
}
void process_uart() {
static char input[BUFFER_SIZE];
while (true) {
const int pos = uart_pattern_pop_pos(UART_NUM_0);
if (pos < 0) {
break;
}
int len = uart_read_bytes(UART_NUM_0, (uint8_t *)input, pos + 1, 0);
bool checksum_ok = true;
len = check(input, len, &checksum_ok);
if (!checksum_ok) {
echo("warning: Checksum mismatch while processing UART0");
continue;
}
process_line(input, len);
}
}
void run_step(Module_ptr module) {
try {
module->step();
} catch (const std::runtime_error &e) {
echo("error in module \"%s\": %s", module->name.c_str(), e.what());
}
}
void app_main() {
vTaskDelay(1500 / portTICK_PERIOD_MS); // ensure that all log messages are sent out completely before proceeding
const uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 0,
.source_clk = UART_SCLK_DEFAULT,
.flags = {},
};
uart_param_config(UART_NUM_0, &uart_config);
QueueHandle_t uart_queue;
uart_driver_install(UART_NUM_0, BUFFER_SIZE * 2, 0, 20, &uart_queue, 0);
uart_enable_pattern_det_baud_intr(UART_NUM_0, '\n', 1, 9, 0, 0);
uart_pattern_queue_reset(UART_NUM_0, 100);
try {
Global::add_module("core", core_module = std::make_shared<Core>("core"));
} catch (const std::runtime_error &e) {
echo("error while initializing core module: %s", e.what());
exit(1);
}
try {
Storage::init();
process_lizard(Storage::startup.c_str());
} catch (const std::runtime_error &e) {
echo("error while loading startup script: %s", e.what());
}
printf("\nReady.\n");
while (true) {
try {
process_uart();
} catch (const std::runtime_error &e) {
echo("error processing uart0: %s", e.what());
}
for (auto const &[module_name, module] : Global::modules) {
if (module != core_module) {
run_step(module);
}
}
run_step(core_module);
for (auto const &rule : Global::rules) {
try {
if (rule->condition->evaluate_boolean() && !rule->routine->is_running()) {
rule->routine->start();
}
rule->routine->step();
} catch (const std::runtime_error &e) {
echo("error in rule: %s", e.what());
}
}
for (auto const &[routine_name, routine] : Global::routines) {
try {
routine->step();
} catch (const std::runtime_error &e) {
echo("error in routine \"%s\": %s", routine_name.c_str(), e.what());
}
}
delay(10);
}
}