summaryrefslogtreecommitdiffstats
path: root/edify
diff options
context:
space:
mode:
authorEthan Yonker <dees_troy@teamw.in>2017-09-08 13:50:54 +0200
committerEthan Yonker <dees_troy@teamw.in>2017-09-08 14:14:59 +0200
commit8373cfe28cf1b5ad758faa1d502e21787c3665e4 (patch)
tree2f567c52cd6f89e1052481497d236b58d230b7e2 /edify
parentDO NOT MERGE Android 8.0 stuff (diff)
parentrelease-request-f4ecf242-5d1c-45e0-8c7c-ede48d1a9e82-for-git_oc-release-4111650 snap-temp-L02200000075283731 (diff)
downloadandroid_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar.gz
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar.bz2
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar.lz
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar.xz
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar.zst
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.zip
Diffstat (limited to 'edify')
-rw-r--r--edify/Android.mk30
-rw-r--r--edify/README.md (renamed from edify/README)3
-rw-r--r--edify/edify_parser.cpp79
-rw-r--r--edify/expr.cpp585
-rw-r--r--edify/expr.h172
-rw-r--r--edify/main.cpp219
-rw-r--r--edify/parser.yy74
7 files changed, 457 insertions, 705 deletions
diff --git a/edify/Android.mk b/edify/Android.mk
index 71cf7652a..d8058c16f 100644
--- a/edify/Android.mk
+++ b/edify/Android.mk
@@ -1,23 +1,36 @@
# Copyright 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
LOCAL_PATH := $(call my-dir)
edify_src_files := \
- lexer.ll \
- parser.yy \
- expr.cpp
+ lexer.ll \
+ parser.yy \
+ expr.cpp
#
-# Build the host-side command line tool
+# Build the host-side command line tool (host executable)
#
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
- $(edify_src_files) \
- main.cpp
+ $(edify_src_files) \
+ edify_parser.cpp
+LOCAL_CFLAGS := -Werror
LOCAL_CPPFLAGS := -g -O0
-LOCAL_MODULE := edify
+LOCAL_MODULE := edify_parser
LOCAL_YACCFLAGS := -v
LOCAL_CPPFLAGS += -Wno-unused-parameter
LOCAL_CPPFLAGS += -Wno-deprecated-register
@@ -28,12 +41,13 @@ LOCAL_STATIC_LIBRARIES += libbase
include $(BUILD_HOST_EXECUTABLE)
#
-# Build the device-side library
+# Build the device-side library (static library)
#
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(edify_src_files)
+LOCAL_CFLAGS := -Werror
LOCAL_CPPFLAGS := -Wno-unused-parameter
LOCAL_CPPFLAGS += -Wno-deprecated-register
LOCAL_MODULE := libedify
diff --git a/edify/README b/edify/README.md
index 810455cca..b3330e23a 100644
--- a/edify/README
+++ b/edify/README.md
@@ -1,3 +1,6 @@
+edify
+=====
+
Update scripts (from donut onwards) are written in a new little
scripting language ("edify") that is superficially somewhat similar to
the old one ("amend"). This is a brief overview of the new language.
diff --git a/edify/edify_parser.cpp b/edify/edify_parser.cpp
new file mode 100644
index 000000000..f1b56284c
--- /dev/null
+++ b/edify/edify_parser.cpp
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * This is a host-side tool for validating a given edify script file.
+ *
+ * We used to have edify test cases here, which have been moved to
+ * tests/component/edify_test.cpp.
+ *
+ * Caveat: It doesn't recognize functions defined through updater, which
+ * makes the tool less useful. We should either extend the tool or remove it.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+
+#include <memory>
+#include <string>
+
+#include <android-base/file.h>
+
+#include "expr.h"
+
+static void ExprDump(int depth, const std::unique_ptr<Expr>& n, const std::string& script) {
+ printf("%*s", depth*2, "");
+ printf("%s %p (%d-%d) \"%s\"\n",
+ n->name.c_str(), n->fn, n->start, n->end,
+ script.substr(n->start, n->end - n->start).c_str());
+ for (size_t i = 0; i < n->argv.size(); ++i) {
+ ExprDump(depth+1, n->argv[i], script);
+ }
+}
+
+int main(int argc, char** argv) {
+ RegisterBuiltins();
+
+ if (argc != 2) {
+ printf("Usage: %s <edify script>\n", argv[0]);
+ return 1;
+ }
+
+ std::string buffer;
+ if (!android::base::ReadFileToString(argv[1], &buffer)) {
+ printf("%s: failed to read %s: %s\n", argv[0], argv[1], strerror(errno));
+ return 1;
+ }
+
+ std::unique_ptr<Expr> root;
+ int error_count = 0;
+ int error = parse_string(buffer.data(), &root, &error_count);
+ printf("parse returned %d; %d errors encountered\n", error, error_count);
+ if (error == 0 || error_count > 0) {
+
+ ExprDump(0, root, buffer);
+
+ State state(buffer, nullptr);
+ std::string result;
+ if (!Evaluate(&state, root, &result)) {
+ printf("result was NULL, message is: %s\n",
+ (state.errmsg.empty() ? "(NULL)" : state.errmsg.c_str()));
+ } else {
+ printf("result is [%s]\n", result.c_str());
+ }
+ }
+ return 0;
+}
diff --git a/edify/expr.cpp b/edify/expr.cpp
index cc14fbe93..54ab3325c 100644
--- a/edify/expr.cpp
+++ b/edify/expr.cpp
@@ -14,201 +14,172 @@
* 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 <memory>
#include <string>
+#include <unordered_map>
+#include <vector>
+#include <android-base/parseint.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
-#include "expr.h"
-
// Functions should:
//
// - return a malloc()'d string
-// - if Evaluate() on any argument returns NULL, return NULL.
+// - if Evaluate() on any argument returns nullptr, return nullptr.
-int BooleanString(const char* s) {
- return s[0] != '\0';
+static bool BooleanString(const std::string& s) {
+ return !s.empty();
}
-char* Evaluate(State* state, Expr* expr) {
- Value* v = expr->fn(expr->name, state, expr->argc, expr->argv);
- if (v == NULL) return NULL;
+bool Evaluate(State* state, const std::unique_ptr<Expr>& expr, std::string* result) {
+ if (result == nullptr) {
+ return false;
+ }
+
+ std::unique_ptr<Value> v(expr->fn(expr->name.c_str(), state, expr->argv));
+ if (!v) {
+ return false;
+ }
if (v->type != VAL_STRING) {
ErrorAbort(state, kArgsParsingFailure, "expecting string, got value type %d", v->type);
- FreeValue(v);
- return NULL;
+ return false;
}
- char* result = v->data;
- free(v);
- return result;
+
+ *result = v->data;
+ return true;
}
-Value* EvaluateValue(State* state, Expr* expr) {
- return expr->fn(expr->name, state, expr->argc, expr->argv);
+Value* EvaluateValue(State* state, const std::unique_ptr<Expr>& expr) {
+ return expr->fn(expr->name.c_str(), state, expr->argv);
}
-Value* StringValue(char* str) {
- if (str == NULL) return NULL;
- Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
- v->type = VAL_STRING;
- v->size = strlen(str);
- v->data = str;
- return v;
+Value* StringValue(const char* str) {
+ if (str == nullptr) {
+ return nullptr;
+ }
+ return new Value(VAL_STRING, str);
}
-void FreeValue(Value* v) {
- if (v == NULL) return;
- free(v->data);
- free(v);
+Value* StringValue(const std::string& str) {
+ return StringValue(str.c_str());
}
-Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) {
- if (argc == 0) {
- return StringValue(strdup(""));
- }
- char** strings = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
- int i;
- for (i = 0; i < argc; ++i) {
- strings[i] = NULL;
- }
- char* result = NULL;
- int length = 0;
- for (i = 0; i < argc; ++i) {
- strings[i] = Evaluate(state, argv[i]);
- if (strings[i] == NULL) {
- goto done;
- }
- length += strlen(strings[i]);
+Value* ConcatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
+ if (argv.empty()) {
+ return StringValue("");
}
-
- result = reinterpret_cast<char*>(malloc(length+1));
- int p;
- p = 0;
- for (i = 0; i < argc; ++i) {
- strcpy(result+p, strings[i]);
- p += strlen(strings[i]);
+ std::string result;
+ for (size_t i = 0; i < argv.size(); ++i) {
+ std::string str;
+ if (!Evaluate(state, argv[i], &str)) {
+ return nullptr;
+ }
+ result += str;
}
- result[p] = '\0';
- done:
- for (i = 0; i < argc; ++i) {
- free(strings[i]);
- }
- free(strings);
return StringValue(result);
}
-Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) {
- if (argc != 2 && argc != 3) {
- free(state->errmsg);
- state->errmsg = strdup("ifelse expects 2 or 3 arguments");
- return NULL;
+Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
+ if (argv.size() != 2 && argv.size() != 3) {
+ state->errmsg = "ifelse expects 2 or 3 arguments";
+ return nullptr;
}
- char* cond = Evaluate(state, argv[0]);
- if (cond == NULL) {
- return NULL;
+
+ std::string cond;
+ if (!Evaluate(state, argv[0], &cond)) {
+ return nullptr;
}
- if (BooleanString(cond) == true) {
- free(cond);
+ if (!cond.empty()) {
return EvaluateValue(state, argv[1]);
- } else {
- if (argc == 3) {
- free(cond);
- return EvaluateValue(state, argv[2]);
- } else {
- return StringValue(cond);
- }
+ } else if (argv.size() == 3) {
+ return EvaluateValue(state, argv[2]);
}
+
+ return StringValue("");
}
-Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]) {
- char* msg = NULL;
- if (argc > 0) {
- msg = Evaluate(state, argv[0]);
- }
- free(state->errmsg);
- if (msg) {
+Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
+ std::string msg;
+ if (!argv.empty() && Evaluate(state, argv[0], &msg)) {
state->errmsg = msg;
} else {
- state->errmsg = strdup("called abort()");
+ state->errmsg = "called abort()";
}
- return NULL;
+ return nullptr;
}
-Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]) {
- int i;
- for (i = 0; i < argc; ++i) {
- char* v = Evaluate(state, argv[i]);
- if (v == NULL) {
- return NULL;
+Value* AssertFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
+ for (size_t i = 0; i < argv.size(); ++i) {
+ std::string result;
+ if (!Evaluate(state, argv[i], &result)) {
+ return nullptr;
}
- int b = BooleanString(v);
- free(v);
- if (!b) {
- int prefix_len;
+ if (result.empty()) {
int len = argv[i]->end - argv[i]->start;
- char* err_src = reinterpret_cast<char*>(malloc(len + 20));
- strcpy(err_src, "assert failed: ");
- prefix_len = strlen(err_src);
- memcpy(err_src + prefix_len, state->script + argv[i]->start, len);
- err_src[prefix_len + len] = '\0';
- free(state->errmsg);
- state->errmsg = err_src;
- return NULL;
+ state->errmsg = "assert failed: " + state->script.substr(argv[i]->start, len);
+ return nullptr;
}
}
- return StringValue(strdup(""));
+ return StringValue("");
}
-Value* SleepFn(const char* name, State* state, int argc, Expr* argv[]) {
- char* val = Evaluate(state, argv[0]);
- if (val == NULL) {
- return NULL;
+Value* SleepFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
+ std::string val;
+ if (!Evaluate(state, argv[0], &val)) {
+ return nullptr;
+ }
+
+ int v;
+ if (!android::base::ParseInt(val.c_str(), &v, 0)) {
+ return nullptr;
}
- int v = strtol(val, NULL, 10);
sleep(v);
+
return StringValue(val);
}
-Value* StdoutFn(const char* name, State* state, int argc, Expr* argv[]) {
- int i;
- for (i = 0; i < argc; ++i) {
- char* v = Evaluate(state, argv[i]);
- if (v == NULL) {
- return NULL;
+Value* StdoutFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
+ for (size_t i = 0; i < argv.size(); ++i) {
+ std::string v;
+ if (!Evaluate(state, argv[i], &v)) {
+ return nullptr;
}
- fputs(v, stdout);
- free(v);
+ fputs(v.c_str(), stdout);
}
- return StringValue(strdup(""));
+ return StringValue("");
}
Value* LogicalAndFn(const char* name, State* state,
- int argc, Expr* argv[]) {
- char* left = Evaluate(state, argv[0]);
- if (left == NULL) return NULL;
- if (BooleanString(left) == true) {
- free(left);
+ const std::vector<std::unique_ptr<Expr>>& argv) {
+ std::string left;
+ if (!Evaluate(state, argv[0], &left)) {
+ return nullptr;
+ }
+ if (BooleanString(left)) {
return EvaluateValue(state, argv[1]);
} else {
- return StringValue(left);
+ return StringValue("");
}
}
Value* LogicalOrFn(const char* name, State* state,
- int argc, Expr* argv[]) {
- char* left = Evaluate(state, argv[0]);
- if (left == NULL) return NULL;
- if (BooleanString(left) == false) {
- free(left);
+ const std::vector<std::unique_ptr<Expr>>& argv) {
+ std::string left;
+ if (!Evaluate(state, argv[0], &left)) {
+ return nullptr;
+ }
+ if (!BooleanString(left)) {
return EvaluateValue(state, argv[1]);
} else {
return StringValue(left);
@@ -216,174 +187,144 @@ Value* LogicalOrFn(const char* name, State* state,
}
Value* LogicalNotFn(const char* name, State* state,
- int argc, Expr* argv[]) {
- char* val = Evaluate(state, argv[0]);
- if (val == NULL) return NULL;
- bool bv = BooleanString(val);
- free(val);
- return StringValue(strdup(bv ? "" : "t"));
+ const std::vector<std::unique_ptr<Expr>>& argv) {
+ std::string val;
+ if (!Evaluate(state, argv[0], &val)) {
+ return nullptr;
+ }
+
+ return StringValue(BooleanString(val) ? "" : "t");
}
Value* SubstringFn(const char* name, State* state,
- int argc, Expr* argv[]) {
- char* needle = Evaluate(state, argv[0]);
- if (needle == NULL) return NULL;
- char* haystack = Evaluate(state, argv[1]);
- if (haystack == NULL) {
- free(needle);
- return NULL;
- }
-
- char* result = strdup(strstr(haystack, needle) ? "t" : "");
- free(needle);
- free(haystack);
+ const std::vector<std::unique_ptr<Expr>>& argv) {
+ std::string needle;
+ if (!Evaluate(state, argv[0], &needle)) {
+ return nullptr;
+ }
+
+ std::string haystack;
+ if (!Evaluate(state, argv[1], &haystack)) {
+ return nullptr;
+ }
+
+ std::string result = (haystack.find(needle) != std::string::npos) ? "t" : "";
return StringValue(result);
}
-Value* EqualityFn(const char* name, State* state, int argc, Expr* argv[]) {
- char* left = Evaluate(state, argv[0]);
- if (left == NULL) return NULL;
- char* right = Evaluate(state, argv[1]);
- if (right == NULL) {
- free(left);
- return NULL;
+Value* EqualityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
+ std::string left;
+ if (!Evaluate(state, argv[0], &left)) {
+ return nullptr;
+ }
+ std::string right;
+ if (!Evaluate(state, argv[1], &right)) {
+ return nullptr;
}
- char* result = strdup(strcmp(left, right) == 0 ? "t" : "");
- free(left);
- free(right);
+ const char* result = (left == right) ? "t" : "";
return StringValue(result);
}
-Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]) {
- char* left = Evaluate(state, argv[0]);
- if (left == NULL) return NULL;
- char* right = Evaluate(state, argv[1]);
- if (right == NULL) {
- free(left);
- return NULL;
+Value* InequalityFn(const char* name, State* state,
+ const std::vector<std::unique_ptr<Expr>>& argv) {
+ std::string left;
+ if (!Evaluate(state, argv[0], &left)) {
+ return nullptr;
+ }
+ std::string right;
+ if (!Evaluate(state, argv[1], &right)) {
+ return nullptr;
}
- char* result = strdup(strcmp(left, right) != 0 ? "t" : "");
- free(left);
- free(right);
+ const char* result = (left != right) ? "t" : "";
return StringValue(result);
}
-Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]) {
- Value* left = EvaluateValue(state, argv[0]);
- if (left == NULL) return NULL;
- FreeValue(left);
+Value* SequenceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
+ std::unique_ptr<Value> left(EvaluateValue(state, argv[0]));
+ if (!left) {
+ return nullptr;
+ }
return EvaluateValue(state, argv[1]);
}
-Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
- if (argc != 2) {
- free(state->errmsg);
- state->errmsg = strdup("less_than_int expects 2 arguments");
- return NULL;
+Value* LessThanIntFn(const char* name, State* state,
+ const std::vector<std::unique_ptr<Expr>>& argv) {
+ if (argv.size() != 2) {
+ state->errmsg = "less_than_int expects 2 arguments";
+ return nullptr;
}
- char* left;
- char* right;
- if (ReadArgs(state, argv, 2, &left, &right) < 0) return NULL;
-
- bool result = false;
- char* end;
-
- long l_int = strtol(left, &end, 10);
- if (left[0] == '\0' || *end != '\0') {
- goto done;
+ std::vector<std::string> args;
+ if (!ReadArgs(state, argv, &args)) {
+ return nullptr;
}
- long r_int;
- r_int = strtol(right, &end, 10);
- if (right[0] == '\0' || *end != '\0') {
- goto done;
+ // Parse up to at least long long or 64-bit integers.
+ int64_t l_int;
+ if (!android::base::ParseInt(args[0].c_str(), &l_int)) {
+ state->errmsg = "failed to parse int in " + args[0];
+ return nullptr;
}
- result = l_int < r_int;
+ int64_t r_int;
+ if (!android::base::ParseInt(args[1].c_str(), &r_int)) {
+ state->errmsg = "failed to parse int in " + args[1];
+ return nullptr;
+ }
- done:
- free(left);
- free(right);
- return StringValue(strdup(result ? "t" : ""));
+ return StringValue(l_int < r_int ? "t" : "");
}
Value* GreaterThanIntFn(const char* name, State* state,
- int argc, Expr* argv[]) {
- if (argc != 2) {
- free(state->errmsg);
- state->errmsg = strdup("greater_than_int expects 2 arguments");
- return NULL;
+ const std::vector<std::unique_ptr<Expr>>& argv) {
+ if (argv.size() != 2) {
+ state->errmsg = "greater_than_int expects 2 arguments";
+ return nullptr;
}
- Expr* temp[2];
- temp[0] = argv[1];
- temp[1] = argv[0];
+ std::vector<std::string> args;
+ if (!ReadArgs(state, argv, &args)) {
+ return nullptr;
+ }
- return LessThanIntFn(name, state, 2, temp);
-}
+ // Parse up to at least long long or 64-bit integers.
+ int64_t l_int;
+ if (!android::base::ParseInt(args[0].c_str(), &l_int)) {
+ state->errmsg = "failed to parse int in " + args[0];
+ return nullptr;
+ }
+
+ int64_t r_int;
+ if (!android::base::ParseInt(args[1].c_str(), &r_int)) {
+ state->errmsg = "failed to parse int in " + args[1];
+ return nullptr;
+ }
-Value* Literal(const char* name, State* state, int argc, Expr* argv[]) {
- return StringValue(strdup(name));
+ return StringValue(l_int > r_int ? "t" : "");
}
-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;
+Value* Literal(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
+ return StringValue(name);
}
// -----------------------------------------------------------------
// the function table
// -----------------------------------------------------------------
-static int fn_entries = 0;
-static int fn_size = 0;
-NamedFunction* fn_table = NULL;
-
-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;
-}
-
-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);
-}
+static std::unordered_map<std::string, Function> fn_table;
-void FinishRegistration() {
- qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare);
+void RegisterFunction(const std::string& name, Function fn) {
+ fn_table[name] = fn;
}
-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() {
@@ -404,106 +345,56 @@ void RegisterBuiltins() {
// convenience methods for functions
// -----------------------------------------------------------------
-// Evaluate the expressions in argv, giving 'count' char* (the ... is
-// zero or more char** to put them in). If any expression evaluates
-// to NULL, free the rest and return -1. Return 0 on success.
-int ReadArgs(State* state, Expr* argv[], int count, ...) {
- char** args = reinterpret_cast<char**>(malloc(count * sizeof(char*)));
- va_list v;
- va_start(v, count);
- int i;
- for (i = 0; i < count; ++i) {
- args[i] = Evaluate(state, argv[i]);
- if (args[i] == NULL) {
- va_end(v);
- int j;
- for (j = 0; j < i; ++j) {
- free(args[j]);
- }
- free(args);
- return -1;
- }
- *(va_arg(v, char**)) = args[i];
- }
- va_end(v);
- free(args);
- return 0;
+// Evaluate the expressions in argv, and put the results of strings in args. If any expression
+// evaluates to nullptr, return false. Return true on success.
+bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
+ std::vector<std::string>* args) {
+ return ReadArgs(state, argv, args, 0, argv.size());
}
-// Evaluate the expressions in argv, giving 'count' Value* (the ... is
-// zero or more Value** to put them in). If any expression evaluates
-// to NULL, free the rest and return -1. Return 0 on success.
-int ReadValueArgs(State* state, Expr* argv[], int count, ...) {
- Value** args = reinterpret_cast<Value**>(malloc(count * sizeof(Value*)));
- va_list v;
- va_start(v, count);
- int i;
- for (i = 0; i < count; ++i) {
- args[i] = EvaluateValue(state, argv[i]);
- if (args[i] == NULL) {
- va_end(v);
- int j;
- for (j = 0; j < i; ++j) {
- FreeValue(args[j]);
- }
- free(args);
- return -1;
+bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
+ std::vector<std::string>* args, size_t start, size_t len) {
+ if (args == nullptr) {
+ return false;
+ }
+ if (start + len > argv.size()) {
+ return false;
+ }
+ for (size_t i = start; i < start + len; ++i) {
+ std::string var;
+ if (!Evaluate(state, argv[i], &var)) {
+ args->clear();
+ return false;
}
- *(va_arg(v, Value**)) = args[i];
+ args->push_back(var);
}
- va_end(v);
- free(args);
- return 0;
+ return true;
}
-// Evaluate the expressions in argv, returning an array of char*
-// results. If any evaluate to NULL, free the rest and return NULL.
-// The caller is responsible for freeing the returned array and the
-// strings it contains.
-char** ReadVarArgs(State* state, int argc, Expr* argv[]) {
- char** args = (char**)malloc(argc * sizeof(char*));
- int i = 0;
- for (i = 0; i < argc; ++i) {
- args[i] = Evaluate(state, argv[i]);
- if (args[i] == NULL) {
- int j;
- for (j = 0; j < i; ++j) {
- free(args[j]);
- }
- free(args);
- return NULL;
- }
- }
- return args;
+// Evaluate the expressions in argv, and put the results of Value* in args. If any expression
+// evaluate to nullptr, return false. Return true on success.
+bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
+ std::vector<std::unique_ptr<Value>>* args) {
+ return ReadValueArgs(state, argv, args, 0, argv.size());
}
-// Evaluate the expressions in argv, returning an array of Value*
-// results. If any evaluate to NULL, free the rest and return NULL.
-// The caller is responsible for freeing the returned array and the
-// Values it contains.
-Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) {
- Value** args = (Value**)malloc(argc * sizeof(Value*));
- int i = 0;
- for (i = 0; i < argc; ++i) {
- args[i] = EvaluateValue(state, argv[i]);
- if (args[i] == NULL) {
- int j;
- for (j = 0; j < i; ++j) {
- FreeValue(args[j]);
- }
- free(args);
- return NULL;
+bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
+ std::vector<std::unique_ptr<Value>>* args, size_t start, size_t len) {
+ if (args == nullptr) {
+ return false;
+ }
+ if (len == 0 || start + len > argv.size()) {
+ return false;
+ }
+ for (size_t i = start; i < start + len; ++i) {
+ std::unique_ptr<Value> v(EvaluateValue(state, argv[i]));
+ if (!v) {
+ args->clear();
+ return false;
}
+ args->push_back(std::move(v));
}
- return args;
-}
-
-static void ErrorAbortV(State* state, const char* format, va_list ap) {
- std::string buffer;
- android::base::StringAppendV(&buffer, format, ap);
- free(state->errmsg);
- state->errmsg = strdup(buffer.c_str());
- return;
+ return true;
}
// Use printf-style arguments to compose an error message to put into
@@ -511,7 +402,7 @@ static void ErrorAbortV(State* state, const char* format, va_list ap) {
Value* ErrorAbort(State* state, const char* format, ...) {
va_list ap;
va_start(ap, format);
- ErrorAbortV(state, format, ap);
+ android::base::StringAppendV(&state->errmsg, format, ap);
va_end(ap);
return nullptr;
}
@@ -519,8 +410,14 @@ Value* ErrorAbort(State* state, const char* format, ...) {
Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) {
va_list ap;
va_start(ap, format);
- ErrorAbortV(state, format, ap);
+ android::base::StringAppendV(&state->errmsg, format, ap);
va_end(ap);
state->cause_code = cause_code;
return nullptr;
}
+
+State::State(const std::string& script, void* cookie) :
+ script(script),
+ cookie(cookie) {
+}
+
diff --git a/edify/expr.h b/edify/expr.h
index 886347991..4838d20c0 100644
--- a/edify/expr.h
+++ b/edify/expr.h
@@ -19,27 +19,26 @@
#include <unistd.h>
+#include <memory>
+#include <string>
+#include <vector>
+
#include "error_code.h"
-#include "yydefs.h"
-#define MAX_STRING_LEN 1024
+struct State {
+ State(const std::string& script, void* cookie);
-typedef struct Expr Expr;
+ // The source of the original script.
+ const std::string& script;
-typedef struct {
// Optional pointer to app-specific data; the core of edify never
// uses this value.
void* cookie;
- // The source of the original script. Must be NULL-terminated,
- // and in writable memory (Evaluate may make temporary changes to
- // it but will restore it when done).
- char* script;
-
// The error message (if any) returned if the evaluation aborts.
- // Should be NULL initially, will be either NULL or a malloc'd
- // pointer after Evaluate() returns.
- char* errmsg;
+ // Should be empty initially, will be either empty or a string that
+ // Evaluate() returns.
+ std::string errmsg;
// error code indicates the type of failure (e.g. failure to update system image)
// during the OTA process.
@@ -50,117 +49,95 @@ typedef struct {
CauseCode cause_code = kNoCause;
bool is_retry = false;
+};
+
+enum ValueType {
+ VAL_INVALID = -1,
+ VAL_STRING = 1,
+ VAL_BLOB = 2,
+};
-} State;
+struct Value {
+ ValueType type;
+ std::string data;
-#define VAL_STRING 1 // data will be NULL-terminated; size doesn't count null
-#define VAL_BLOB 2
+ Value(ValueType type, const std::string& str) :
+ type(type),
+ data(str) {}
+};
-typedef struct {
- int type;
- ssize_t size;
- char* data;
-} Value;
+struct Expr;
-typedef Value* (*Function)(const char* name, State* state,
- int argc, Expr* argv[]);
+using Function = Value* (*)(const char* name, State* state,
+ const std::vector<std::unique_ptr<Expr>>& argv);
struct Expr {
- Function fn;
- const char* name;
- int argc;
- Expr** argv;
- int start, end;
+ Function fn;
+ std::string name;
+ std::vector<std::unique_ptr<Expr>> argv;
+ int start, end;
+
+ Expr(Function fn, const std::string& name, int start, int end) :
+ fn(fn),
+ name(name),
+ start(start),
+ end(end) {}
};
-// Take one of the Expr*s passed to the function as an argument,
-// evaluate it, return the resulting Value. The caller takes
-// ownership of the returned Value.
-Value* EvaluateValue(State* state, Expr* expr);
+// Evaluate the input expr, return the resulting Value.
+Value* EvaluateValue(State* state, const std::unique_ptr<Expr>& expr);
-// Take one of the Expr*s passed to the function as an argument,
-// evaluate it, assert that it is a string, and return the resulting
-// char*. The caller takes ownership of the returned char*. This is
-// a convenience function for older functions that want to deal only
-// with strings.
-char* Evaluate(State* state, Expr* expr);
+// Evaluate the input expr, assert that it is a string, and update the result parameter. This
+// function returns true if the evaluation succeeds. This is a convenience function for older
+// functions that want to deal only with strings.
+bool Evaluate(State* state, const std::unique_ptr<Expr>& expr, std::string* result);
// Glue to make an Expr out of a literal.
-Value* Literal(const char* name, State* state, int argc, Expr* argv[]);
+Value* Literal(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
// Functions corresponding to various syntactic sugar operators.
// ("concat" is also available as a builtin function, to concatenate
// more than two strings.)
-Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]);
-Value* LogicalAndFn(const char* name, State* state, int argc, Expr* argv[]);
-Value* LogicalOrFn(const char* name, State* state, int argc, Expr* argv[]);
-Value* LogicalNotFn(const char* name, State* state, int argc, Expr* argv[]);
-Value* SubstringFn(const char* name, State* state, int argc, Expr* argv[]);
-Value* EqualityFn(const char* name, State* state, int argc, Expr* argv[]);
-Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]);
-Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]);
-
-// Convenience function for building expressions with a fixed number
-// of arguments.
-Expr* Build(Function fn, YYLTYPE loc, int count, ...);
+Value* ConcatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
+Value* LogicalAndFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
+Value* LogicalOrFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
+Value* LogicalNotFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
+Value* SubstringFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
+Value* EqualityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
+Value* InequalityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
+Value* SequenceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
// Global builtins, registered by RegisterBuiltins().
-Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]);
-Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]);
-Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]);
-
-
-// For setting and getting the global error string (when returning
-// NULL from a function).
-void SetError(const char* message); // makes a copy
-const char* GetError(); // retains ownership
-void ClearError();
-
-
-typedef struct {
- const char* name;
- Function fn;
-} NamedFunction;
+Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
+Value* AssertFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
+Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
// Register a new function. The same Function may be registered under
// multiple names, but a given name should only be used once.
-void RegisterFunction(const char* name, Function fn);
+void RegisterFunction(const std::string& name, Function fn);
// Register all the builtins.
void RegisterBuiltins();
-// Call this after all calls to RegisterFunction() but before parsing
-// any scripts to finish building the function table.
-void FinishRegistration();
-
// Find the Function for a given name; return NULL if no such function
// exists.
-Function FindFunction(const char* name);
-
+Function FindFunction(const std::string& name);
// --- convenience functions for use in functions ---
-// Evaluate the expressions in argv, giving 'count' char* (the ... is
-// zero or more char** to put them in). If any expression evaluates
-// to NULL, free the rest and return -1. Return 0 on success.
-int ReadArgs(State* state, Expr* argv[], int count, ...);
-
-// Evaluate the expressions in argv, giving 'count' Value* (the ... is
-// zero or more Value** to put them in). If any expression evaluates
-// to NULL, free the rest and return -1. Return 0 on success.
-int ReadValueArgs(State* state, Expr* argv[], int count, ...);
-
-// Evaluate the expressions in argv, returning an array of char*
-// results. If any evaluate to NULL, free the rest and return NULL.
-// The caller is responsible for freeing the returned array and the
-// strings it contains.
-char** ReadVarArgs(State* state, int argc, Expr* argv[]);
+// Evaluate the expressions in argv, and put the results of strings in args. If any expression
+// evaluates to nullptr, return false. Return true on success.
+bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
+ std::vector<std::string>* args);
+bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
+ std::vector<std::string>* args, size_t start, size_t len);
-// Evaluate the expressions in argv, returning an array of Value*
-// results. If any evaluate to NULL, free the rest and return NULL.
-// The caller is responsible for freeing the returned array and the
-// Values it contains.
-Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]);
+// Evaluate the expressions in argv, and put the results of Value* in args. If any
+// expression evaluate to nullptr, return false. Return true on success.
+bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
+ std::vector<std::unique_ptr<Value>>* args);
+bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
+ std::vector<std::unique_ptr<Value>>* args, size_t start, size_t len);
// Use printf-style arguments to compose an error message to put into
// *state. Returns NULL.
@@ -172,12 +149,11 @@ Value* ErrorAbort(State* state, const char* format, ...)
Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...)
__attribute__((format(printf, 3, 4)));
-// Wrap a string into a Value, taking ownership of the string.
-Value* StringValue(char* str);
+// Copying the string into a Value.
+Value* StringValue(const char* str);
-// Free a Value object.
-void FreeValue(Value* v);
+Value* StringValue(const std::string& str);
-int parse_string(const char* str, Expr** root, int* error_count);
+int parse_string(const char* str, std::unique_ptr<Expr>* root, int* error_count);
#endif // _EXPRESSION_H
diff --git a/edify/main.cpp b/edify/main.cpp
deleted file mode 100644
index 6007a3d58..000000000
--- a/edify/main.cpp
+++ /dev/null
@@ -1,219 +0,0 @@
-/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <string>
-
-#include "expr.h"
-#include "parser.h"
-
-extern int yyparse(Expr** root, int* error_count);
-
-int expect(const char* expr_str, const char* expected, int* errors) {
- Expr* e;
- char* result;
-
- printf(".");
-
- int error_count = parse_string(expr_str, &e, &error_count);
- if (error_count > 0) {
- printf("error parsing \"%s\" (%d errors)\n",
- expr_str, error_count);
- ++*errors;
- return 0;
- }
-
- State state;
- state.cookie = NULL;
- state.script = strdup(expr_str);
- state.errmsg = NULL;
-
- result = Evaluate(&state, e);
- free(state.errmsg);
- free(state.script);
- if (result == NULL && expected != NULL) {
- printf("error evaluating \"%s\"\n", expr_str);
- ++*errors;
- return 0;
- }
-
- if (result == NULL && expected == NULL) {
- return 1;
- }
-
- if (strcmp(result, expected) != 0) {
- printf("evaluating \"%s\": expected \"%s\", got \"%s\"\n",
- expr_str, expected, result);
- ++*errors;
- free(result);
- return 0;
- }
-
- free(result);
- return 1;
-}
-
-int test() {
- int errors = 0;
-
- expect("a", "a", &errors);
- expect("\"a\"", "a", &errors);
- expect("\"\\x61\"", "a", &errors);
- expect("# this is a comment\n"
- " a\n"
- " \n",
- "a", &errors);
-
-
- // sequence operator
- expect("a; b; c", "c", &errors);
-
- // string concat operator
- expect("a + b", "ab", &errors);
- expect("a + \n \"b\"", "ab", &errors);
- expect("a + b +\nc\n", "abc", &errors);
-
- // string concat function
- expect("concat(a, b)", "ab", &errors);
- expect("concat(a,\n \"b\")", "ab", &errors);
- expect("concat(a + b,\nc,\"d\")", "abcd", &errors);
- expect("\"concat\"(a + b,\nc,\"d\")", "abcd", &errors);
-
- // logical and
- expect("a && b", "b", &errors);
- expect("a && \"\"", "", &errors);
- expect("\"\" && b", "", &errors);
- expect("\"\" && \"\"", "", &errors);
- expect("\"\" && abort()", "", &errors); // test short-circuiting
- expect("t && abort()", NULL, &errors);
-
- // logical or
- expect("a || b", "a", &errors);
- expect("a || \"\"", "a", &errors);
- expect("\"\" || b", "b", &errors);
- expect("\"\" || \"\"", "", &errors);
- expect("a || abort()", "a", &errors); // test short-circuiting
- expect("\"\" || abort()", NULL, &errors);
-
- // logical not
- expect("!a", "", &errors);
- expect("! \"\"", "t", &errors);
- expect("!!a", "t", &errors);
-
- // precedence
- expect("\"\" == \"\" && b", "b", &errors);
- expect("a + b == ab", "t", &errors);
- expect("ab == a + b", "t", &errors);
- expect("a + (b == ab)", "a", &errors);
- expect("(ab == a) + b", "b", &errors);
-
- // substring function
- expect("is_substring(cad, abracadabra)", "t", &errors);
- expect("is_substring(abrac, abracadabra)", "t", &errors);
- expect("is_substring(dabra, abracadabra)", "t", &errors);
- expect("is_substring(cad, abracxadabra)", "", &errors);
- expect("is_substring(abrac, axbracadabra)", "", &errors);
- expect("is_substring(dabra, abracadabrxa)", "", &errors);
-
- // ifelse function
- expect("ifelse(t, yes, no)", "yes", &errors);
- expect("ifelse(!t, yes, no)", "no", &errors);
- expect("ifelse(t, yes, abort())", "yes", &errors);
- expect("ifelse(!t, abort(), no)", "no", &errors);
-
- // if "statements"
- expect("if t then yes else no endif", "yes", &errors);
- expect("if \"\" then yes else no endif", "no", &errors);
- expect("if \"\" then yes endif", "", &errors);
- expect("if \"\"; t then yes endif", "yes", &errors);
-
- // numeric comparisons
- expect("less_than_int(3, 14)", "t", &errors);
- expect("less_than_int(14, 3)", "", &errors);
- expect("less_than_int(x, 3)", "", &errors);
- expect("less_than_int(3, x)", "", &errors);
- expect("greater_than_int(3, 14)", "", &errors);
- expect("greater_than_int(14, 3)", "t", &errors);
- expect("greater_than_int(x, 3)", "", &errors);
- expect("greater_than_int(3, x)", "", &errors);
-
- // big string
- expect(std::string(8192, 's').c_str(), std::string(8192, 's').c_str(), &errors);
-
- printf("\n");
-
- return errors;
-}
-
-void ExprDump(int depth, Expr* n, char* script) {
- printf("%*s", depth*2, "");
- char temp = script[n->end];
- script[n->end] = '\0';
- printf("%s %p (%d-%d) \"%s\"\n",
- n->name == NULL ? "(NULL)" : n->name, n->fn, n->start, n->end,
- script+n->start);
- script[n->end] = temp;
- int i;
- for (i = 0; i < n->argc; ++i) {
- ExprDump(depth+1, n->argv[i], script);
- }
-}
-
-int main(int argc, char** argv) {
- RegisterBuiltins();
- FinishRegistration();
-
- if (argc == 1) {
- return test() != 0;
- }
-
- FILE* f = fopen(argv[1], "r");
- if (f == NULL) {
- printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
- return 1;
- }
- char buffer[8192];
- int size = fread(buffer, 1, 8191, f);
- fclose(f);
- buffer[size] = '\0';
-
- Expr* root;
- int error_count = 0;
- int error = parse_string(buffer, &root, &error_count);
- printf("parse returned %d; %d errors encountered\n", error, error_count);
- if (error == 0 || error_count > 0) {
-
- ExprDump(0, root, buffer);
-
- State state;
- state.cookie = NULL;
- state.script = buffer;
- state.errmsg = NULL;
-
- char* result = Evaluate(&state, root);
- if (result == NULL) {
- printf("result was NULL, message is: %s\n",
- (state.errmsg == NULL ? "(NULL)" : state.errmsg));
- free(state.errmsg);
- } else {
- printf("result is [%s]\n", result);
- }
- }
- return 0;
-}
diff --git a/edify/parser.yy b/edify/parser.yy
index 098a6370a..97205fe3b 100644
--- a/edify/parser.yy
+++ b/edify/parser.yy
@@ -19,6 +19,10 @@
#include <stdlib.h>
#include <string.h>
+#include <memory>
+#include <string>
+#include <vector>
+
#include "expr.h"
#include "yydefs.h"
#include "parser.h"
@@ -26,13 +30,26 @@
extern int gLine;
extern int gColumn;
-void yyerror(Expr** root, int* error_count, const char* s);
-int yyparse(Expr** root, int* error_count);
+void yyerror(std::unique_ptr<Expr>* root, int* error_count, const char* s);
+int yyparse(std::unique_ptr<Expr>* root, int* error_count);
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 = new Expr(fn, "(operator)", loc.start, loc.end);
+ for (size_t i = 0; i < count; ++i) {
+ e->argv.emplace_back(va_arg(v, Expr*));
+ }
+ va_end(v);
+ return e;
+}
+
%}
%locations
@@ -40,10 +57,7 @@ struct yy_buffer_state* yy_scan_string(const char* yystr);
%union {
char* str;
Expr* expr;
- struct {
- int argc;
- Expr** argv;
- } args;
+ std::vector<std::unique_ptr<Expr>>* args;
}
%token AND OR SUBSTR SUPERSTR EQ NE IF THEN ELSE ENDIF
@@ -51,7 +65,10 @@ struct yy_buffer_state* yy_scan_string(const char* yystr);
%type <expr> expr
%type <args> arglist
-%parse-param {Expr** root}
+%destructor { delete $$; } expr
+%destructor { delete $$; } arglist
+
+%parse-param {std::unique_ptr<Expr>* root}
%parse-param {int* error_count}
%error-verbose
@@ -66,17 +83,11 @@ struct yy_buffer_state* yy_scan_string(const char* yystr);
%%
-input: expr { *root = $1; }
+input: expr { root->reset($1); }
;
expr: STRING {
- $$ = reinterpret_cast<Expr*>(malloc(sizeof(Expr)));
- $$->fn = Literal;
- $$->name = $1;
- $$->argc = 0;
- $$->argv = NULL;
- $$->start = @$.start;
- $$->end = @$.end;
+ $$ = new Expr(Literal, $1, @$.start, @$.end);
}
| '(' expr ')' { $$ = $2; $$->start=@$.start; $$->end=@$.end; }
| expr ';' { $$ = $1; $$->start=@1.start; $$->end=@1.end; }
@@ -91,41 +102,32 @@ 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<Expr*>(malloc(sizeof(Expr)));
- $$->fn = FindFunction($1);
- if ($$->fn == NULL) {
- char buffer[256];
- snprintf(buffer, sizeof(buffer), "unknown function \"%s\"", $1);
- yyerror(root, error_count, buffer);
+ Function fn = FindFunction($1);
+ if (fn == nullptr) {
+ std::string msg = "unknown function \"" + std::string($1) + "\"";
+ yyerror(root, error_count, msg.c_str());
YYERROR;
}
- $$->name = $1;
- $$->argc = $3.argc;
- $$->argv = $3.argv;
- $$->start = @$.start;
- $$->end = @$.end;
+ $$ = new Expr(fn, $1, @$.start, @$.end);
+ $$->argv = std::move(*$3);
}
;
arglist: /* empty */ {
- $$.argc = 0;
- $$.argv = NULL;
+ $$ = new std::vector<std::unique_ptr<Expr>>;
}
| expr {
- $$.argc = 1;
- $$.argv = reinterpret_cast<Expr**>(malloc(sizeof(Expr*)));
- $$.argv[0] = $1;
+ $$ = new std::vector<std::unique_ptr<Expr>>;
+ $$->emplace_back($1);
}
| arglist ',' expr {
- $$.argc = $1.argc + 1;
- $$.argv = reinterpret_cast<Expr**>(realloc($$.argv, $$.argc * sizeof(Expr*)));
- $$.argv[$$.argc-1] = $3;
+ $$->push_back(std::unique_ptr<Expr>($3));
}
;
%%
-void yyerror(Expr** root, int* error_count, const char* s) {
+void yyerror(std::unique_ptr<Expr>* root, int* error_count, const char* s) {
if (strlen(s) == 0) {
s = "syntax error";
}
@@ -133,7 +135,7 @@ void yyerror(Expr** root, int* error_count, const char* s) {
++*error_count;
}
-int parse_string(const char* str, Expr** root, int* error_count) {
+int parse_string(const char* str, std::unique_ptr<Expr>* root, int* error_count) {
yy_switch_to_buffer(yy_scan_string(str));
return yyparse(root, error_count);
}