blob: f53efdc9caf069e0046f520b71c2514de1588679 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 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 "ast.h"
19
20static const char gSpaces[] =
21 " "
22 " "
23 " "
24 " "
25 " "
26 " "
27 " ";
28const int gSpacesMax = sizeof(gSpaces) - 1;
29
30static const char *
31pad(int level)
32{
33 level *= 4;
34 if (level > gSpacesMax) {
35 level = gSpacesMax;
36 }
37 return gSpaces + gSpacesMax - level;
38}
39
40void dumpBooleanValue(int level, const AmBooleanValue *booleanValue);
41void dumpStringValue(int level, const AmStringValue *stringValue);
42
43void
44dumpBooleanExpression(int level, const AmBooleanExpression *booleanExpression)
45{
46 const char *op;
47 bool unary = false;
48
49 switch (booleanExpression->op) {
50 case AM_BOP_NOT:
51 op = "NOT";
52 unary = true;
53 break;
54 case AM_BOP_EQ:
55 op = "EQ";
56 break;
57 case AM_BOP_NE:
58 op = "NE";
59 break;
60 case AM_BOP_AND:
61 op = "AND";
62 break;
63 case AM_BOP_OR:
64 op = "OR";
65 break;
66 default:
67 op = "??";
68 break;
69 }
70
71 printf("%sBOOLEAN %s {\n", pad(level), op);
72 dumpBooleanValue(level + 1, booleanExpression->arg1);
73 if (!unary) {
74 dumpBooleanValue(level + 1, booleanExpression->arg2);
75 }
76 printf("%s}\n", pad(level));
77}
78
79void
80dumpFunctionArguments(int level, const AmFunctionArguments *functionArguments)
81{
82 int i;
83 for (i = 0; i < functionArguments->argc; i++) {
84 dumpStringValue(level, &functionArguments->argv[i]);
85 }
86}
87
88void
89dumpFunctionCall(int level, const AmFunctionCall *functionCall)
90{
91 printf("%sFUNCTION %s (\n", pad(level), functionCall->name);
92 dumpFunctionArguments(level + 1, functionCall->args);
93 printf("%s)\n", pad(level));
94}
95
96void
97dumpStringValue(int level, const AmStringValue *stringValue)
98{
99 switch (stringValue->type) {
100 case AM_SVAL_LITERAL:
101 printf("%s\"%s\"\n", pad(level), stringValue->u.literal);
102 break;
103 case AM_SVAL_FUNCTION:
104 dumpFunctionCall(level, stringValue->u.function);
105 break;
106 default:
107 printf("%s<UNKNOWN SVAL TYPE %d>\n", pad(level), stringValue->type);
108 break;
109 }
110}
111
112void
113dumpStringComparisonExpression(int level,
114 const AmStringComparisonExpression *stringComparisonExpression)
115{
116 const char *op;
117
118 switch (stringComparisonExpression->op) {
119 case AM_SOP_LT:
120 op = "LT";
121 break;
122 case AM_SOP_LE:
123 op = "LE";
124 break;
125 case AM_SOP_GT:
126 op = "GT";
127 break;
128 case AM_SOP_GE:
129 op = "GE";
130 break;
131 case AM_SOP_EQ:
132 op = "EQ";
133 break;
134 case AM_SOP_NE:
135 op = "NE";
136 break;
137 default:
138 op = "??";
139 break;
140 }
141 printf("%sSTRING %s {\n", pad(level), op);
142 dumpStringValue(level + 1, stringComparisonExpression->arg1);
143 dumpStringValue(level + 1, stringComparisonExpression->arg2);
144 printf("%s}\n", pad(level));
145}
146
147void
148dumpBooleanValue(int level, const AmBooleanValue *booleanValue)
149{
150 switch (booleanValue->type) {
151 case AM_BVAL_EXPRESSION:
152 dumpBooleanExpression(level, &booleanValue->u.expression);
153 break;
154 case AM_BVAL_STRING_COMPARISON:
155 dumpStringComparisonExpression(level,
156 &booleanValue->u.stringComparison);
157 break;
158 default:
159 printf("%s<UNKNOWN BVAL TYPE %d>\n", pad(1), booleanValue->type);
160 break;
161 }
162}
163
164void
165dumpWordList(const AmWordList *wordList)
166{
167 int i;
168 for (i = 0; i < wordList->argc; i++) {
169 printf("%s\"%s\"\n", pad(1), wordList->argv[i]);
170 }
171}
172
173void
174dumpCommandArguments(const AmCommandArguments *commandArguments)
175{
176 if (commandArguments->booleanArgs) {
177 dumpBooleanValue(1, commandArguments->u.b);
178 } else {
179 dumpWordList(commandArguments->u.w);
180 }
181}
182
183void
184dumpCommand(const AmCommand *command)
185{
186 printf("command \"%s\" {\n", command->name);
187 dumpCommandArguments(command->args);
188 printf("}\n");
189}
190
191void
192dumpCommandList(const AmCommandList *commandList)
193{
194 int i;
195 for (i = 0; i < commandList->commandCount; i++) {
196 dumpCommand(commandList->commands[i]);
197 }
198}