blob: 4d65da2d83847aad00bc67db45340bc5fae6f642 [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
24int expect(const char* expr_str, const char* expected, int* errors) {
25 Expr* e;
26 int error;
27 char* result;
28
29 printf(".");
30
31 yy_scan_string(expr_str);
32 error = yyparse(&e);
33 if (error > 0) {
34 fprintf(stderr, "error parsing \"%s\"\n", expr_str);
35 ++*errors;
36 return 0;
37 }
38
39 result = Evaluate(NULL, e);
40 if (result == NULL && expected != NULL) {
41 fprintf(stderr, "error evaluating \"%s\"\n", expr_str);
42 ++*errors;
43 return 0;
44 }
45
46 if (result == NULL && expected == NULL) {
47 return 1;
48 }
49
50 if (strcmp(result, expected) != 0) {
51 fprintf(stderr, "evaluating \"%s\": expected \"%s\", got \"%s\"\n",
52 expr_str, expected, result);
53 ++*errors;
54 free(result);
55 return 0;
56 }
57
58 free(result);
59 return 1;
60}
61
62int test() {
63 int errors = 0;
64
65 expect("a", "a", &errors);
66 expect("\"a\"", "a", &errors);
67 expect("\"\\x61\"", "a", &errors);
68 expect("# this is a comment\n"
69 " a\n"
70 " \n",
71 "a", &errors);
72
73
74 // sequence operator
75 expect("a; b; c", "c", &errors);
76
77 // string concat operator
78 expect("a + b", "ab", &errors);
79 expect("a + \n \"b\"", "ab", &errors);
80 expect("a + b +\nc\n", "abc", &errors);
81
82 // string concat function
83 expect("concat(a, b)", "ab", &errors);
84 expect("concat(a,\n \"b\")", "ab", &errors);
85 expect("concat(a + b,\nc,\"d\")", "abcd", &errors);
86 expect("\"concat\"(a + b,\nc,\"d\")", "abcd", &errors);
87
88 // logical and
89 expect("a && b", "b", &errors);
90 expect("a && \"\"", "", &errors);
91 expect("\"\" && b", "", &errors);
92 expect("\"\" && \"\"", "", &errors);
93 expect("\"\" && abort()", "", &errors); // test short-circuiting
94 expect("t && abort()", NULL, &errors);
95
96 // logical or
97 expect("a || b", "a", &errors);
98 expect("a || \"\"", "a", &errors);
99 expect("\"\" || b", "b", &errors);
100 expect("\"\" || \"\"", "", &errors);
101 expect("a || abort()", "a", &errors); // test short-circuiting
102 expect("\"\" || abort()", NULL, &errors);
103
104 // logical not
105 expect("!a", "", &errors);
106 expect("! \"\"", "t", &errors);
107 expect("!!a", "t", &errors);
108
109 // precedence
110 expect("\"\" == \"\" && b", "b", &errors);
111 expect("a + b == ab", "t", &errors);
112 expect("ab == a + b", "t", &errors);
113 expect("a + (b == ab)", "a", &errors);
114 expect("(ab == a) + b", "b", &errors);
115
116 // substring function
117 expect("is_substring(cad, abracadabra)", "t", &errors);
118 expect("is_substring(abrac, abracadabra)", "t", &errors);
119 expect("is_substring(dabra, abracadabra)", "t", &errors);
120 expect("is_substring(cad, abracxadabra)", "", &errors);
121 expect("is_substring(abrac, axbracadabra)", "", &errors);
122 expect("is_substring(dabra, abracadabrxa)", "", &errors);
123
124 // ifelse function
125 expect("ifelse(t, yes, no)", "yes", &errors);
126 expect("ifelse(!t, yes, no)", "no", &errors);
127 expect("ifelse(t, yes, abort())", "yes", &errors);
128 expect("ifelse(!t, abort(), no)", "no", &errors);
129
130 // if "statements"
131 expect("if t then yes else no endif", "yes", &errors);
132 expect("if \"\" then yes else no endif", "no", &errors);
133 expect("if \"\" then yes endif", "", &errors);
134 expect("if \"\"; t then yes endif", "yes", &errors);
135
136 printf("\n");
137
138 return errors;
139}
140
141int main(int argc, char** argv) {
142 RegisterBuiltins();
143 FinishRegistration();
144
145 if (argc == 1) {
146 return test() != 0;
147 }
148
149 FILE* f = fopen(argv[1], "r");
150 char buffer[8192];
151 int size = fread(buffer, 1, 8191, f);
152 fclose(f);
153 buffer[size] = '\0';
154
155 Expr* root;
156 yy_scan_bytes(buffer, size);
157 int error = yyparse(&root);
158 printf("parse returned %d\n", error);
159 if (error == 0) {
160 char* result = Evaluate(NULL, root);
161 printf("result is [%s]\n", result == NULL ? "(NULL)" : result);
162 }
163 return 0;
164}