edify: Some clean-ups to libedify.
- Remove dead declarations in expr.h: SetError(), GetError(),
ClearError().
- Remove the declaration of Build() out of expr.h.
- Use std::unordered_map to implement RegisterFunction() and
FindFunction(); kill FinishRegistration().
- Add a testcase for calling unknown functions.
Test: mmma bootable/recovery; recovery_component_test passes.
Change-Id: I9af6825ae677f92b22d716a4a5682f58522af03b
diff --git a/edify/parser.yy b/edify/parser.yy
index 098a637..58a8dec 100644
--- a/edify/parser.yy
+++ b/edify/parser.yy
@@ -33,6 +33,25 @@
void yy_switch_to_buffer(struct yy_buffer_state* new_buffer);
struct yy_buffer_state* yy_scan_string(const char* yystr);
+// Convenience function for building expressions with a fixed number
+// of arguments.
+static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) {
+ va_list v;
+ va_start(v, count);
+ Expr* e = static_cast<Expr*>(malloc(sizeof(Expr)));
+ e->fn = fn;
+ e->name = "(operator)";
+ e->argc = count;
+ e->argv = static_cast<Expr**>(malloc(count * sizeof(Expr*)));
+ for (size_t i = 0; i < count; ++i) {
+ e->argv[i] = va_arg(v, Expr*);
+ }
+ va_end(v);
+ e->start = loc.start;
+ e->end = loc.end;
+ return e;
+}
+
%}
%locations
@@ -70,7 +89,7 @@
;
expr: STRING {
- $$ = reinterpret_cast<Expr*>(malloc(sizeof(Expr)));
+ $$ = static_cast<Expr*>(malloc(sizeof(Expr)));
$$->fn = Literal;
$$->name = $1;
$$->argc = 0;
@@ -91,9 +110,9 @@
| IF expr THEN expr ENDIF { $$ = Build(IfElseFn, @$, 2, $2, $4); }
| IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, @$, 3, $2, $4, $6); }
| STRING '(' arglist ')' {
- $$ = reinterpret_cast<Expr*>(malloc(sizeof(Expr)));
+ $$ = static_cast<Expr*>(malloc(sizeof(Expr)));
$$->fn = FindFunction($1);
- if ($$->fn == NULL) {
+ if ($$->fn == nullptr) {
char buffer[256];
snprintf(buffer, sizeof(buffer), "unknown function \"%s\"", $1);
yyerror(root, error_count, buffer);
@@ -113,12 +132,12 @@
}
| expr {
$$.argc = 1;
- $$.argv = reinterpret_cast<Expr**>(malloc(sizeof(Expr*)));
+ $$.argv = static_cast<Expr**>(malloc(sizeof(Expr*)));
$$.argv[0] = $1;
}
| arglist ',' expr {
$$.argc = $1.argc + 1;
- $$.argv = reinterpret_cast<Expr**>(realloc($$.argv, $$.argc * sizeof(Expr*)));
+ $$.argv = static_cast<Expr**>(realloc($$.argv, $$.argc * sizeof(Expr*)));
$$.argv[$$.argc-1] = $3;
}
;