blob: 0e361084728492799d21ffc316a42ea9b8b9b9d7 [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 Zongkerd9c9d102009-06-12 12:24:39 -070033 yy_scan_string(expr_str);
34 int error_count = 0;
35 error = yyparse(&e, &error_count);
36 if (error > 0 || error_count > 0) {
37 fprintf(stderr, "error parsing \"%s\" (%d errors)\n",
38 expr_str, error_count);
39 ++*errors;
40 return 0;
41 }
Doug Zongker37bee622009-06-08 17:35:39 -070042
Doug Zongkerd9c9d102009-06-12 12:24:39 -070043 State state;
44 state.cookie = NULL;
45 state.script = expr_str;
46 state.errmsg = NULL;
Doug Zongker37bee622009-06-08 17:35:39 -070047
Doug Zongkerd9c9d102009-06-12 12:24:39 -070048 result = Evaluate(&state, e);
49 free(state.errmsg);
50 if (result == NULL && expected != NULL) {
51 fprintf(stderr, "error evaluating \"%s\"\n", expr_str);
52 ++*errors;
53 return 0;
54 }
Doug Zongker37bee622009-06-08 17:35:39 -070055
Doug Zongkerd9c9d102009-06-12 12:24:39 -070056 if (result == NULL && expected == NULL) {
57 return 1;
58 }
59
60 if (strcmp(result, expected) != 0) {
61 fprintf(stderr, "evaluating \"%s\": expected \"%s\", got \"%s\"\n",
62 expr_str, expected, result);
63 ++*errors;
64 free(result);
65 return 0;
66 }
67
Doug Zongker37bee622009-06-08 17:35:39 -070068 free(result);
Doug Zongkerd9c9d102009-06-12 12:24:39 -070069 return 1;
Doug Zongker37bee622009-06-08 17:35:39 -070070}
71
72int test() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070073 int errors = 0;
Doug Zongker37bee622009-06-08 17:35:39 -070074
Doug Zongkerd9c9d102009-06-12 12:24:39 -070075 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 Zongker37bee622009-06-08 17:35:39 -070082
83
Doug Zongkerd9c9d102009-06-12 12:24:39 -070084 // sequence operator
85 expect("a; b; c", "c", &errors);
Doug Zongker37bee622009-06-08 17:35:39 -070086
Doug Zongkerd9c9d102009-06-12 12:24:39 -070087 // 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 Zongker37bee622009-06-08 17:35:39 -070091
Doug Zongkerd9c9d102009-06-12 12:24:39 -070092 // 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 Zongker37bee622009-06-08 17:35:39 -070097
Doug Zongkerd9c9d102009-06-12 12:24:39 -070098 // 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 Zongker37bee622009-06-08 17:35:39 -0700105
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700106 // 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 Zongker37bee622009-06-08 17:35:39 -0700113
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700114 // logical not
115 expect("!a", "", &errors);
116 expect("! \"\"", "t", &errors);
117 expect("!!a", "t", &errors);
Doug Zongker37bee622009-06-08 17:35:39 -0700118
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700119 // 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 Zongker37bee622009-06-08 17:35:39 -0700125
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700126 // 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 Zongker37bee622009-06-08 17:35:39 -0700133
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700134 // 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 Zongker37bee622009-06-08 17:35:39 -0700139
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700140 // 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 Zongker37bee622009-06-08 17:35:39 -0700145
Doug Zongkere3da02e2009-06-12 16:13:52 -0700146 // 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
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700156 printf("\n");
Doug Zongker37bee622009-06-08 17:35:39 -0700157
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700158 return errors;
159}
160
161void ExprDump(int depth, Expr* n, char* script) {
162 printf("%*s", depth*2, "");
163 char temp = script[n->end];
164 script[n->end] = '\0';
165 printf("%s %p (%d-%d) \"%s\"\n",
166 n->name == NULL ? "(NULL)" : n->name, n->fn, n->start, n->end,
167 script+n->start);
168 script[n->end] = temp;
169 int i;
170 for (i = 0; i < n->argc; ++i) {
171 ExprDump(depth+1, n->argv[i], script);
172 }
Doug Zongker37bee622009-06-08 17:35:39 -0700173}
174
175int main(int argc, char** argv) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700176 RegisterBuiltins();
177 FinishRegistration();
Doug Zongker37bee622009-06-08 17:35:39 -0700178
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700179 if (argc == 1) {
180 return test() != 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700181 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700182
183 FILE* f = fopen(argv[1], "r");
184 char buffer[8192];
185 int size = fread(buffer, 1, 8191, f);
186 fclose(f);
187 buffer[size] = '\0';
188
189 Expr* root;
190 int error_count = 0;
191 yy_scan_bytes(buffer, size);
192 int error = yyparse(&root, &error_count);
193 printf("parse returned %d; %d errors encountered\n", error, error_count);
194 if (error == 0 || error_count > 0) {
195
196 ExprDump(0, root, buffer);
197
198 State state;
199 state.cookie = NULL;
200 state.script = buffer;
201 state.errmsg = NULL;
202
203 char* result = Evaluate(&state, root);
204 if (result == NULL) {
205 printf("result was NULL, message is: %s\n",
206 (state.errmsg == NULL ? "(NULL)" : state.errmsg));
207 free(state.errmsg);
208 } else {
209 printf("result is [%s]\n", result);
210 }
211 }
212 return 0;
Doug Zongker37bee622009-06-08 17:35:39 -0700213}