summaryrefslogtreecommitdiffstats
path: root/edify/expr.cpp
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2016-10-11 07:52:18 +0200
committerTao Bao <tbao@google.com>2016-10-13 08:29:59 +0200
commit39119ad8ecd00a9c19fb173c78cb4a8d22a4540a (patch)
treeb7c6d625baefc3521fe46c794d8d82fe99535229 /edify/expr.cpp
parentMerge "updater: Kill the duplicate PrintSha1() in install.cpp." (diff)
downloadandroid_bootable_recovery-39119ad8ecd00a9c19fb173c78cb4a8d22a4540a.tar
android_bootable_recovery-39119ad8ecd00a9c19fb173c78cb4a8d22a4540a.tar.gz
android_bootable_recovery-39119ad8ecd00a9c19fb173c78cb4a8d22a4540a.tar.bz2
android_bootable_recovery-39119ad8ecd00a9c19fb173c78cb4a8d22a4540a.tar.lz
android_bootable_recovery-39119ad8ecd00a9c19fb173c78cb4a8d22a4540a.tar.xz
android_bootable_recovery-39119ad8ecd00a9c19fb173c78cb4a8d22a4540a.tar.zst
android_bootable_recovery-39119ad8ecd00a9c19fb173c78cb4a8d22a4540a.zip
Diffstat (limited to '')
-rw-r--r--edify/expr.cpp65
1 files changed, 13 insertions, 52 deletions
diff --git a/edify/expr.cpp b/edify/expr.cpp
index aa3a55a87..4000bc4db 100644
--- a/edify/expr.cpp
+++ b/edify/expr.cpp
@@ -14,20 +14,20 @@
* limitations under the License.
*/
-#include <string.h>
-#include <stdbool.h>
+#include "expr.h"
+
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
-#include <stdarg.h>
+#include <string.h>
#include <unistd.h>
#include <string>
+#include <unordered_map>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
-#include "expr.h"
-
// Functions should:
//
// - return a malloc()'d string
@@ -319,61 +319,22 @@ Value* Literal(const char* name, State* state, int argc, Expr* argv[]) {
return StringValue(strdup(name));
}
-Expr* Build(Function fn, YYLTYPE loc, int count, ...) {
- va_list v;
- va_start(v, count);
- Expr* e = reinterpret_cast<Expr*>(malloc(sizeof(Expr)));
- e->fn = fn;
- e->name = "(operator)";
- e->argc = count;
- e->argv = reinterpret_cast<Expr**>(malloc(count * sizeof(Expr*)));
- int i;
- for (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;
-}
-
// -----------------------------------------------------------------
// the function table
// -----------------------------------------------------------------
-static int fn_entries = 0;
-static int fn_size = 0;
-NamedFunction* fn_table = NULL;
+static std::unordered_map<std::string, Function> fn_table;
-void RegisterFunction(const char* name, Function fn) {
- if (fn_entries >= fn_size) {
- fn_size = fn_size*2 + 1;
- fn_table = reinterpret_cast<NamedFunction*>(realloc(fn_table, fn_size * sizeof(NamedFunction)));
- }
- fn_table[fn_entries].name = name;
- fn_table[fn_entries].fn = fn;
- ++fn_entries;
+void RegisterFunction(const std::string& name, Function fn) {
+ fn_table[name] = fn;
}
-static int fn_entry_compare(const void* a, const void* b) {
- const char* na = ((const NamedFunction*)a)->name;
- const char* nb = ((const NamedFunction*)b)->name;
- return strcmp(na, nb);
-}
-
-void FinishRegistration() {
- qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare);
-}
-
-Function FindFunction(const char* name) {
- NamedFunction key;
- key.name = name;
- NamedFunction* nf = reinterpret_cast<NamedFunction*>(bsearch(&key, fn_table, fn_entries,
- sizeof(NamedFunction), fn_entry_compare));
- if (nf == NULL) {
- return NULL;
+Function FindFunction(const std::string& name) {
+ if (fn_table.find(name) == fn_table.end()) {
+ return nullptr;
+ } else {
+ return fn_table[name];
}
- return nf->fn;
}
void RegisterBuiltins() {