From 39119ad8ecd00a9c19fb173c78cb4a8d22a4540a Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 10 Oct 2016 22:52:18 -0700 Subject: 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 --- edify/parser.yy | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'edify/parser.yy') diff --git a/edify/parser.yy b/edify/parser.yy index 098a6370a..58a8dec65 100644 --- a/edify/parser.yy +++ b/edify/parser.yy @@ -33,6 +33,25 @@ struct yy_buffer_state; 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(malloc(sizeof(Expr))); + e->fn = fn; + e->name = "(operator)"; + e->argc = count; + e->argv = static_cast(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 @@ input: expr { *root = $1; } ; expr: STRING { - $$ = reinterpret_cast(malloc(sizeof(Expr))); + $$ = static_cast(malloc(sizeof(Expr))); $$->fn = Literal; $$->name = $1; $$->argc = 0; @@ -91,9 +110,9 @@ expr: STRING { | 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(malloc(sizeof(Expr))); + $$ = static_cast(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 @@ arglist: /* empty */ { } | expr { $$.argc = 1; - $$.argv = reinterpret_cast(malloc(sizeof(Expr*))); + $$.argv = static_cast(malloc(sizeof(Expr*))); $$.argv[0] = $1; } | arglist ',' expr { $$.argc = $1.argc + 1; - $$.argv = reinterpret_cast(realloc($$.argv, $$.argc * sizeof(Expr*))); + $$.argv = static_cast(realloc($$.argv, $$.argc * sizeof(Expr*))); $$.argv[$$.argc-1] = $3; } ; -- cgit v1.2.3