blob: b3fad53b860e437ad8b027236a7a47098a6247b9 [file] [log] [blame]
Doug Zongker37bee622009-06-08 17:35:39 -07001/*
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 Zongkerd9c9d102009-06-12 12:24:39 -070024extern int yyparse(Expr** root, int* error_count);
25
Doug Zongker37bee622009-06-08 17:35:39 -070026int expect(const char* expr_str, const char* expected, int* errors) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070027 Expr* e;
28 int error;
29 char* result;
Doug Zongker37bee622009-06-08 17:35:39 -070030
Doug Zongkerd9c9d102009-06-12 12:24:39 -070031 printf(".");
Doug Zongker37bee622009-06-08 17:35:39 -070032
Doug Zongker0d32f252014-02-13 15:07:56 -080033 int error_count = parse_string(expr_str, &e, &error_count);
Doug Zongkerd9c9d102009-06-12 12:24:39 -070034 if (error > 0 || error_count > 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -070035 printf("error parsing \"%s\" (%d errors)\n",
36 expr_str, error_count);
Doug Zongkerd9c9d102009-06-12 12:24:39 -070037 ++*errors;
38 return 0;
39 }
Doug Zongker37bee622009-06-08 17:35:39 -070040
Doug Zongkerd9c9d102009-06-12 12:24:39 -070041 State state;
42 state.cookie = NULL;
Doug Zongkerc4351c72010-02-22 14:46:32 -080043 state.script = strdup(expr_str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -070044 state.errmsg = NULL;
Doug Zongker37bee622009-06-08 17:35:39 -070045
Doug Zongkerd9c9d102009-06-12 12:24:39 -070046 result = Evaluate(&state, e);
47 free(state.errmsg);
Doug Zongkerc4351c72010-02-22 14:46:32 -080048 free(state.script);
Doug Zongkerd9c9d102009-06-12 12:24:39 -070049 if (result == NULL && expected != NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -070050 printf("error evaluating \"%s\"\n", expr_str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -070051 ++*errors;
52 return 0;
53 }
Doug Zongker37bee622009-06-08 17:35:39 -070054
Doug Zongkerd9c9d102009-06-12 12:24:39 -070055 if (result == NULL && expected == NULL) {
56 return 1;
57 }
58
59 if (strcmp(result, expected) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -070060 printf("evaluating \"%s\": expected \"%s\", got \"%s\"\n",
61 expr_str, expected, result);
Doug Zongkerd9c9d102009-06-12 12:24:39 -070062 ++*errors;
63 free(result);
64 return 0;
65 }
66
Doug Zongker37bee622009-06-08 17:35:39 -070067 free(result);
Doug Zongkerd9c9d102009-06-12 12:24:39 -070068 return 1;
Doug Zongker37bee622009-06-08 17:35:39 -070069}
70
71int test() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070072 int errors = 0;
Doug Zongker37bee622009-06-08 17:35:39 -070073
Doug Zongkerd9c9d102009-06-12 12:24:39 -070074 expect("a", "a", &errors);
75 expect("\"a\"", "a", &errors);
76 expect("\"\\x61\"", "a", &errors);
77 expect("# this is a comment\n"
78 " a\n"
79 " \n",
80 "a", &errors);
Doug Zongker37bee622009-06-08 17:35:39 -070081
82
Doug Zongkerd9c9d102009-06-12 12:24:39 -070083 // sequence operator
84 expect("a; b; c", "c", &errors);
Doug Zongker37bee622009-06-08 17:35:39 -070085
Doug Zongkerd9c9d102009-06-12 12:24:39 -070086 // string concat operator
87 expect("a + b", "ab", &errors);
88 expect("a + \n \"b\"", "ab", &errors);
89 expect("a + b +\nc\n", "abc", &errors);
Doug Zongker37bee622009-06-08 17:35:39 -070090
Doug Zongkerd9c9d102009-06-12 12:24:39 -070091 // string concat function
92 expect("concat(a, b)", "ab", &errors);
93 expect("concat(a,\n \"b\")", "ab", &errors);
94 expect("concat(a + b,\nc,\"d\")", "abcd", &errors);
95 expect("\"concat\"(a + b,\nc,\"d\")", "abcd", &errors);
Doug Zongker37bee622009-06-08 17:35:39 -070096
Doug Zongkerd9c9d102009-06-12 12:24:39 -070097 // logical and
98 expect("a && b", "b", &errors);
99 expect("a && \"\"", "", &errors);
100 expect("\"\" && b", "", &errors);
101 expect("\"\" && \"\"", "", &errors);
102 expect("\"\" && abort()", "", &errors); // test short-circuiting
103 expect("t && abort()", NULL, &errors);
Doug Zongker37bee622009-06-08 17:35:39 -0700104
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700105 // logical or
106 expect("a || b", "a", &errors);
107 expect("a || \"\"", "a", &errors);
108 expect("\"\" || b", "b", &errors);
109 expect("\"\" || \"\"", "", &errors);
110 expect("a || abort()", "a", &errors); // test short-circuiting
111 expect("\"\" || abort()", NULL, &errors);
Doug Zongker37bee622009-06-08 17:35:39 -0700112
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700113 // logical not
114 expect("!a", "", &errors);
115 expect("! \"\"", "t", &errors);
116 expect("!!a", "t", &errors);
Doug Zongker37bee622009-06-08 17:35:39 -0700117
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700118 // precedence
119 expect("\"\" == \"\" && b", "b", &errors);
120 expect("a + b == ab", "t", &errors);
121 expect("ab == a + b", "t", &errors);
122 expect("a + (b == ab)", "a", &errors);
123 expect("(ab == a) + b", "b", &errors);
Doug Zongker37bee622009-06-08 17:35:39 -0700124
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700125 // substring function
126 expect("is_substring(cad, abracadabra)", "t", &errors);
127 expect("is_substring(abrac, abracadabra)", "t", &errors);
128 expect("is_substring(dabra, abracadabra)", "t", &errors);
129 expect("is_substring(cad, abracxadabra)", "", &errors);
130 expect("is_substring(abrac, axbracadabra)", "", &errors);
131 expect("is_substring(dabra, abracadabrxa)", "", &errors);
Doug Zongker37bee622009-06-08 17:35:39 -0700132
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700133 // ifelse function
134 expect("ifelse(t, yes, no)", "yes", &errors);
135 expect("ifelse(!t, yes, no)", "no", &errors);
136 expect("ifelse(t, yes, abort())", "yes", &errors);
137 expect("ifelse(!t, abort(), no)", "no", &errors);
Doug Zongker37bee622009-06-08 17:35:39 -0700138
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700139 // if "statements"
140 expect("if t then yes else no endif", "yes", &errors);
141 expect("if \"\" then yes else no endif", "no", &errors);
142 expect("if \"\" then yes endif", "", &errors);
143 expect("if \"\"; t then yes endif", "yes", &errors);
Doug Zongker37bee622009-06-08 17:35:39 -0700144
Doug Zongkere3da02e2009-06-12 16:13:52 -0700145 // numeric comparisons
146 expect("less_than_int(3, 14)", "t", &errors);
147 expect("less_than_int(14, 3)", "", &errors);
148 expect("less_than_int(x, 3)", "", &errors);
149 expect("less_than_int(3, x)", "", &errors);
150 expect("greater_than_int(3, 14)", "", &errors);
151 expect("greater_than_int(14, 3)", "t", &errors);
152 expect("greater_than_int(x, 3)", "", &errors);
153 expect("greater_than_int(3, x)", "", &errors);
154
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700155 printf("\n");
Doug Zongker37bee622009-06-08 17:35:39 -0700156
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700157 return errors;
158}
159
160void ExprDump(int depth, Expr* n, char* script) {
161 printf("%*s", depth*2, "");
162 char temp = script[n->end];
163 script[n->end] = '\0';
164 printf("%s %p (%d-%d) \"%s\"\n",
165 n->name == NULL ? "(NULL)" : n->name, n->fn, n->start, n->end,
166 script+n->start);
167 script[n->end] = temp;
168 int i;
169 for (i = 0; i < n->argc; ++i) {
170 ExprDump(depth+1, n->argv[i], script);
171 }
Doug Zongker37bee622009-06-08 17:35:39 -0700172}
173
174int main(int argc, char** argv) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700175 RegisterBuiltins();
176 FinishRegistration();
Doug Zongker37bee622009-06-08 17:35:39 -0700177
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700178 if (argc == 1) {
179 return test() != 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700181
182 FILE* f = fopen(argv[1], "r");
Kenny Root21854cc2010-02-17 18:31:48 -0800183 if (f == NULL) {
184 printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
185 return 1;
186 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700187 char buffer[8192];
188 int size = fread(buffer, 1, 8191, f);
189 fclose(f);
190 buffer[size] = '\0';
191
192 Expr* root;
193 int error_count = 0;
Doug Zongker0d32f252014-02-13 15:07:56 -0800194 int error = parse_string(buffer, &root, &error_count);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700195 printf("parse returned %d; %d errors encountered\n", error, error_count);
196 if (error == 0 || error_count > 0) {
197
198 ExprDump(0, root, buffer);
199
200 State state;
201 state.cookie = NULL;
202 state.script = buffer;
203 state.errmsg = NULL;
204
205 char* result = Evaluate(&state, root);
206 if (result == NULL) {
207 printf("result was NULL, message is: %s\n",
208 (state.errmsg == NULL ? "(NULL)" : state.errmsg));
209 free(state.errmsg);
210 } else {
211 printf("result is [%s]\n", result);
212 }
213 }
214 return 0;
Doug Zongker37bee622009-06-08 17:35:39 -0700215}