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