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