blob: bc9e58778be5031f388d5bf6d3912dad9567e2ce [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 <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20#include "ast.h"
21#include "lexer.h"
22#include "parser.h"
23#include "register.h"
24#include "execute.h"
25
26void
27lexTest()
28{
29 int token;
30 do {
31 token = yylex();
32 if (token == 0) {
33 printf(" EOF");
34 fflush(stdout);
35 break;
36 } else {
37 printf(" %s", tokenToString(token));
38 fflush(stdout);
39 if (token == TOK_IDENTIFIER) {
40 if (strcmp(yylval.literalString, "assert") == 0) {
41 setLexerArgumentType(AM_BOOLEAN_ARGS);
42 } else {
43 setLexerArgumentType(AM_WORD_ARGS);
44 }
45 do {
46 token = yylex();
47 printf(" %s", tokenToString(token));
48 fflush(stdout);
49 } while (token != TOK_EOL && token != TOK_EOF && token != 0);
50 } else if (token != TOK_EOL) {
51 fprintf(stderr, "syntax error: expected identifier\n");
52 break;
53 }
54 }
55 } while (token != 0);
56 printf("\n");
57}
58
59void
60usage()
61{
62 printf("usage: amend [--debug-lex|--debug-ast] [<filename>]\n");
63 exit(1);
64}
65
66extern const AmCommandList *gCommands;
67int
68main(int argc, char *argv[])
69{
70 FILE *inputFile = NULL;
71 bool debugLex = false;
72 bool debugAst = false;
73 const char *fileName = NULL;
74 int err;
75
76#if 1
77 extern int test_symtab(void);
78 int ret = test_symtab();
79 if (ret != 0) {
80 fprintf(stderr, "test_symtab() failed: %d\n", ret);
81 exit(ret);
82 }
83 extern int test_cmd_fn(void);
84 ret = test_cmd_fn();
85 if (ret != 0) {
86 fprintf(stderr, "test_cmd_fn() failed: %d\n", ret);
87 exit(ret);
88 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080089#endif
90
91 argc--;
92 argv++;
93 while (argc > 0) {
94 if (strcmp("--debug-lex", argv[0]) == 0) {
95 debugLex = true;
96 } else if (strcmp("--debug-ast", argv[0]) == 0) {
97 debugAst = true;
98 } else if (argv[0][0] == '-') {
99 fprintf(stderr, "amend: Unknown option \"%s\"\n", argv[0]);
100 usage();
101 } else {
102 fileName = argv[0];
103 }
104 argc--;
105 argv++;
106 }
107
108 if (fileName != NULL) {
109 inputFile = fopen(fileName, "r");
110 if (inputFile == NULL) {
111 fprintf(stderr, "amend: Can't open input file '%s'\n", fileName);
112 usage();
113 }
114 }
115
116 commandInit();
117//xxx clean up
118
119 err = registerUpdateCommands();
120 if (err < 0) {
121 fprintf(stderr, "amend: Error registering commands: %d\n", err);
122 exit(-err);
123 }
124 err = registerUpdateFunctions();
125 if (err < 0) {
126 fprintf(stderr, "amend: Error registering functions: %d\n", err);
127 exit(-err);
128 }
129
130#if AMEND_LEXER_BUFFER_INPUT
131 if (inputFile == NULL) {
132 fprintf(stderr, "amend: No input file\n");
133 usage();
134 }
135 char *fileData;
136 int fileDataLen;
137 fseek(inputFile, 0, SEEK_END);
138 fileDataLen = ftell(inputFile);
139 rewind(inputFile);
140 if (fileDataLen < 0) {
141 fprintf(stderr, "amend: Can't get file length\n");
142 exit(2);
143 } else if (fileDataLen == 0) {
144 printf("amend: Empty input file\n");
145 exit(0);
146 }
147 fileData = (char *)malloc(fileDataLen + 1);
148 if (fileData == NULL) {
149 fprintf(stderr, "amend: Can't allocate %d bytes\n", fileDataLen + 1);
150 exit(2);
151 }
152 size_t nread = fread(fileData, 1, fileDataLen, inputFile);
153 if (nread != (size_t)fileDataLen) {
154 fprintf(stderr, "amend: Didn't read %d bytes, only %zd\n", fileDataLen,
155 nread);
156 exit(2);
157 }
158 fileData[fileDataLen] = '\0';
159 setLexerInputBuffer(fileData, fileDataLen);
160#else
161 if (inputFile == NULL) {
162 inputFile = stdin;
163 }
164 yyset_in(inputFile);
165#endif
166
167 if (debugLex) {
168 lexTest();
169 } else {
170 int ret = yyparse();
171 if (ret != 0) {
172 fprintf(stderr, "amend: Parse failed (%d)\n", ret);
173 exit(2);
174 } else {
175 if (debugAst) {
176 dumpCommandList(gCommands);
177 }
178printf("amend: Parse successful.\n");
179 ret = execCommandList((ExecContext *)1, gCommands);
180 if (ret != 0) {
181 fprintf(stderr, "amend: Execution failed (%d)\n", ret);
182 exit(3);
183 }
184printf("amend: Execution successful.\n");
185 }
186 }
187
188 return 0;
189}