Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
Yabin Cui | 12f499e | 2016-01-27 12:26:18 -0800 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 23 | #include "expr.h" |
| 24 | #include "parser.h" |
| 25 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 26 | extern int yyparse(Expr** root, int* error_count); |
| 27 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 28 | int expect(const char* expr_str, const char* expected, int* errors) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 29 | Expr* e; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 30 | char* result; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 31 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 32 | printf("."); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 33 | |
Doug Zongker | 0d32f25 | 2014-02-13 15:07:56 -0800 | [diff] [blame] | 34 | int error_count = parse_string(expr_str, &e, &error_count); |
Dan Albert | 7279f97 | 2014-12-18 11:50:23 -0800 | [diff] [blame] | 35 | if (error_count > 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 36 | printf("error parsing \"%s\" (%d errors)\n", |
| 37 | expr_str, error_count); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 38 | ++*errors; |
| 39 | return 0; |
| 40 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 41 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 42 | State state; |
| 43 | state.cookie = NULL; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 44 | state.script = strdup(expr_str); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 45 | state.errmsg = NULL; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 46 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 47 | result = Evaluate(&state, e); |
| 48 | free(state.errmsg); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 49 | free(state.script); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 50 | if (result == NULL && expected != NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 51 | printf("error evaluating \"%s\"\n", expr_str); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 52 | ++*errors; |
| 53 | return 0; |
| 54 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 55 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 56 | if (result == NULL && expected == NULL) { |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | if (strcmp(result, expected) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 61 | printf("evaluating \"%s\": expected \"%s\", got \"%s\"\n", |
| 62 | expr_str, expected, result); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 63 | ++*errors; |
| 64 | free(result); |
| 65 | return 0; |
| 66 | } |
| 67 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 68 | free(result); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 69 | return 1; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | int test() { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 73 | int errors = 0; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 74 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 75 | expect("a", "a", &errors); |
| 76 | expect("\"a\"", "a", &errors); |
| 77 | expect("\"\\x61\"", "a", &errors); |
| 78 | expect("# this is a comment\n" |
| 79 | " a\n" |
| 80 | " \n", |
| 81 | "a", &errors); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 82 | |
| 83 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 84 | // sequence operator |
| 85 | expect("a; b; c", "c", &errors); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 86 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 87 | // string concat operator |
| 88 | expect("a + b", "ab", &errors); |
| 89 | expect("a + \n \"b\"", "ab", &errors); |
| 90 | expect("a + b +\nc\n", "abc", &errors); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 91 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 92 | // string concat function |
| 93 | expect("concat(a, b)", "ab", &errors); |
| 94 | expect("concat(a,\n \"b\")", "ab", &errors); |
| 95 | expect("concat(a + b,\nc,\"d\")", "abcd", &errors); |
| 96 | expect("\"concat\"(a + b,\nc,\"d\")", "abcd", &errors); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 97 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 98 | // logical and |
| 99 | expect("a && b", "b", &errors); |
| 100 | expect("a && \"\"", "", &errors); |
| 101 | expect("\"\" && b", "", &errors); |
| 102 | expect("\"\" && \"\"", "", &errors); |
| 103 | expect("\"\" && abort()", "", &errors); // test short-circuiting |
| 104 | expect("t && abort()", NULL, &errors); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 105 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 106 | // logical or |
| 107 | expect("a || b", "a", &errors); |
| 108 | expect("a || \"\"", "a", &errors); |
| 109 | expect("\"\" || b", "b", &errors); |
| 110 | expect("\"\" || \"\"", "", &errors); |
| 111 | expect("a || abort()", "a", &errors); // test short-circuiting |
| 112 | expect("\"\" || abort()", NULL, &errors); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 113 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 114 | // logical not |
| 115 | expect("!a", "", &errors); |
| 116 | expect("! \"\"", "t", &errors); |
| 117 | expect("!!a", "t", &errors); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 118 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 119 | // precedence |
| 120 | expect("\"\" == \"\" && b", "b", &errors); |
| 121 | expect("a + b == ab", "t", &errors); |
| 122 | expect("ab == a + b", "t", &errors); |
| 123 | expect("a + (b == ab)", "a", &errors); |
| 124 | expect("(ab == a) + b", "b", &errors); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 125 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 126 | // substring function |
| 127 | expect("is_substring(cad, abracadabra)", "t", &errors); |
| 128 | expect("is_substring(abrac, abracadabra)", "t", &errors); |
| 129 | expect("is_substring(dabra, abracadabra)", "t", &errors); |
| 130 | expect("is_substring(cad, abracxadabra)", "", &errors); |
| 131 | expect("is_substring(abrac, axbracadabra)", "", &errors); |
| 132 | expect("is_substring(dabra, abracadabrxa)", "", &errors); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 133 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 134 | // ifelse function |
| 135 | expect("ifelse(t, yes, no)", "yes", &errors); |
| 136 | expect("ifelse(!t, yes, no)", "no", &errors); |
| 137 | expect("ifelse(t, yes, abort())", "yes", &errors); |
| 138 | expect("ifelse(!t, abort(), no)", "no", &errors); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 139 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 140 | // if "statements" |
| 141 | expect("if t then yes else no endif", "yes", &errors); |
| 142 | expect("if \"\" then yes else no endif", "no", &errors); |
| 143 | expect("if \"\" then yes endif", "", &errors); |
| 144 | expect("if \"\"; t then yes endif", "yes", &errors); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 145 | |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 146 | // numeric comparisons |
| 147 | expect("less_than_int(3, 14)", "t", &errors); |
| 148 | expect("less_than_int(14, 3)", "", &errors); |
| 149 | expect("less_than_int(x, 3)", "", &errors); |
| 150 | expect("less_than_int(3, x)", "", &errors); |
| 151 | expect("greater_than_int(3, 14)", "", &errors); |
| 152 | expect("greater_than_int(14, 3)", "t", &errors); |
| 153 | expect("greater_than_int(x, 3)", "", &errors); |
| 154 | expect("greater_than_int(3, x)", "", &errors); |
| 155 | |
Yabin Cui | 12f499e | 2016-01-27 12:26:18 -0800 | [diff] [blame] | 156 | // big string |
| 157 | expect(std::string(8192, 's').c_str(), std::string(8192, 's').c_str(), &errors); |
| 158 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 159 | printf("\n"); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 160 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 161 | return errors; |
| 162 | } |
| 163 | |
| 164 | void ExprDump(int depth, Expr* n, char* script) { |
| 165 | printf("%*s", depth*2, ""); |
| 166 | char temp = script[n->end]; |
| 167 | script[n->end] = '\0'; |
| 168 | printf("%s %p (%d-%d) \"%s\"\n", |
| 169 | n->name == NULL ? "(NULL)" : n->name, n->fn, n->start, n->end, |
| 170 | script+n->start); |
| 171 | script[n->end] = temp; |
| 172 | int i; |
| 173 | for (i = 0; i < n->argc; ++i) { |
| 174 | ExprDump(depth+1, n->argv[i], script); |
| 175 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int main(int argc, char** argv) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 179 | RegisterBuiltins(); |
| 180 | FinishRegistration(); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 181 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 182 | if (argc == 1) { |
| 183 | return test() != 0; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 184 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 185 | |
| 186 | FILE* f = fopen(argv[1], "r"); |
Kenny Root | 21854cc | 2010-02-17 18:31:48 -0800 | [diff] [blame] | 187 | if (f == NULL) { |
| 188 | printf("%s: %s: No such file or directory\n", argv[0], argv[1]); |
| 189 | return 1; |
| 190 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 191 | char buffer[8192]; |
| 192 | int size = fread(buffer, 1, 8191, f); |
| 193 | fclose(f); |
| 194 | buffer[size] = '\0'; |
| 195 | |
| 196 | Expr* root; |
| 197 | int error_count = 0; |
Doug Zongker | 0d32f25 | 2014-02-13 15:07:56 -0800 | [diff] [blame] | 198 | int error = parse_string(buffer, &root, &error_count); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 199 | printf("parse returned %d; %d errors encountered\n", error, error_count); |
| 200 | if (error == 0 || error_count > 0) { |
| 201 | |
| 202 | ExprDump(0, root, buffer); |
| 203 | |
| 204 | State state; |
| 205 | state.cookie = NULL; |
| 206 | state.script = buffer; |
| 207 | state.errmsg = NULL; |
| 208 | |
| 209 | char* result = Evaluate(&state, root); |
| 210 | if (result == NULL) { |
| 211 | printf("result was NULL, message is: %s\n", |
| 212 | (state.errmsg == NULL ? "(NULL)" : state.errmsg)); |
| 213 | free(state.errmsg); |
| 214 | } else { |
| 215 | printf("result is [%s]\n", result); |
| 216 | } |
| 217 | } |
| 218 | return 0; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 219 | } |