aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTao Bao2016-10-11 00:52:18 -0500
committerTao Bao2016-10-13 01:29:59 -0500
commit39119ad8ecd00a9c19fb173c78cb4a8d22a4540a (patch)
treeb7c6d625baefc3521fe46c794d8d82fe99535229 /edify/expr.cpp
parent19bb05dfc7a148dc4fc4ce744e5cfd013949497a (diff)
downloadplatform-bootable-recovery-39119ad8ecd00a9c19fb173c78cb4a8d22a4540a.tar.gz
platform-bootable-recovery-39119ad8ecd00a9c19fb173c78cb4a8d22a4540a.tar.xz
platform-bootable-recovery-39119ad8ecd00a9c19fb173c78cb4a8d22a4540a.zip
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
Diffstat (limited to 'edify/expr.cpp')
-rw-r--r--edify/expr.cpp65
1 files changed, 13 insertions, 52 deletions
diff --git a/edify/expr.cpp b/edify/expr.cpp
index aa3a55a8..4000bc4d 100644
--- a/edify/expr.cpp
+++ b/edify/expr.cpp
@@ -14,20 +14,20 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17#include <string.h> 17#include "expr.h"
18#include <stdbool.h> 18
19#include <stdarg.h>
19#include <stdio.h> 20#include <stdio.h>
20#include <stdlib.h> 21#include <stdlib.h>
21#include <stdarg.h> 22#include <string.h>
22#include <unistd.h> 23#include <unistd.h>
23 24
24#include <string> 25#include <string>
26#include <unordered_map>
25 27
26#include <android-base/stringprintf.h> 28#include <android-base/stringprintf.h>
27#include <android-base/strings.h> 29#include <android-base/strings.h>
28 30
29#include "expr.h"
30
31// Functions should: 31// Functions should:
32// 32//
33// - return a malloc()'d string 33// - return a malloc()'d string
@@ -319,61 +319,22 @@ Value* Literal(const char* name, State* state, int argc, Expr* argv[]) {
319 return StringValue(strdup(name)); 319 return StringValue(strdup(name));
320} 320}
321 321
322Expr* Build(Function fn, YYLTYPE loc, int count, ...) {
323 va_list v;
324 va_start(v, count);
325 Expr* e = reinterpret_cast<Expr*>(malloc(sizeof(Expr)));
326 e->fn = fn;
327 e->name = "(operator)";
328 e->argc = count;
329 e->argv = reinterpret_cast<Expr**>(malloc(count * sizeof(Expr*)));
330 int i;
331 for (i = 0; i < count; ++i) {
332 e->argv[i] = va_arg(v, Expr*);
333 }
334 va_end(v);
335 e->start = loc.start;
336 e->end = loc.end;
337 return e;
338}
339
340// ----------------------------------------------------------------- 322// -----------------------------------------------------------------
341// the function table 323// the function table
342// ----------------------------------------------------------------- 324// -----------------------------------------------------------------
343 325
344static int fn_entries = 0; 326static std::unordered_map<std::string, Function> fn_table;
345static int fn_size = 0;
346NamedFunction* fn_table = NULL;
347 327
348void RegisterFunction(const char* name, Function fn) { 328void RegisterFunction(const std::string& name, Function fn) {
349 if (fn_entries >= fn_size) { 329 fn_table[name] = fn;
350 fn_size = fn_size*2 + 1;
351 fn_table = reinterpret_cast<NamedFunction*>(realloc(fn_table, fn_size * sizeof(NamedFunction)));
352 }
353 fn_table[fn_entries].name = name;
354 fn_table[fn_entries].fn = fn;
355 ++fn_entries;
356} 330}
357 331
358static int fn_entry_compare(const void* a, const void* b) { 332Function FindFunction(const std::string& name) {
359 const char* na = ((const NamedFunction*)a)->name; 333 if (fn_table.find(name) == fn_table.end()) {
360 const char* nb = ((const NamedFunction*)b)->name; 334 return nullptr;
361 return strcmp(na, nb); 335 } else {
362} 336 return fn_table[name];
363
364void FinishRegistration() {
365 qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare);
366}
367
368Function FindFunction(const char* name) {
369 NamedFunction key;
370 key.name = name;
371 NamedFunction* nf = reinterpret_cast<NamedFunction*>(bsearch(&key, fn_table, fn_entries,
372 sizeof(NamedFunction), fn_entry_compare));
373 if (nf == NULL) {
374 return NULL;
375 } 337 }
376 return nf->fn;
377} 338}
378 339
379void RegisterBuiltins() { 340void RegisterBuiltins() {