You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
1.4 KiB
93 lines
1.4 KiB
#include <stdio.h>
|
|
#include "ast.h"
|
|
#include "parser.tab.h"
|
|
|
|
int main(int argc, char **argv) {
|
|
stmt_node *program;
|
|
sol_state_t state;
|
|
char *c;
|
|
int printtree = 0, clean = 1;
|
|
FILE *prgstream = stdin;
|
|
int result = 0;
|
|
|
|
state.features = 0;
|
|
|
|
if(argc > 1) {
|
|
c = argv[1];
|
|
while(*c) {
|
|
switch(*c) {
|
|
case 'd':
|
|
yydebug = 1;
|
|
break;
|
|
|
|
case 't':
|
|
printtree = 1;
|
|
break;
|
|
|
|
case 'r':
|
|
if(argc < 2) {
|
|
printf("r option requires file\n");
|
|
return 2;
|
|
}
|
|
prgstream = fopen(argv[2], "r");
|
|
break;
|
|
|
|
case 'i':
|
|
state.features |= SOL_FT_NO_USR_INIT;
|
|
break;
|
|
}
|
|
c++;
|
|
}
|
|
}
|
|
|
|
if(!prgstream) {
|
|
printf("No input program (check filenames)\n");
|
|
return 2;
|
|
}
|
|
|
|
program = sol_compile_file(prgstream);
|
|
|
|
if(!program) {
|
|
printf("NULL program (probably a syntax error)\n");
|
|
return 2;
|
|
}
|
|
|
|
if(prgstream != stdin) {
|
|
fclose(prgstream);
|
|
}
|
|
|
|
if(!sol_state_init(&state)) {
|
|
printf("State init error (internal bug)\n");
|
|
result = 2;
|
|
clean = 0;
|
|
goto out_results;
|
|
}
|
|
|
|
if(printtree) {
|
|
st_print(&state, program);
|
|
}
|
|
|
|
sol_exec(&state, program);
|
|
|
|
out_results:
|
|
|
|
if(sol_has_error(&state)) {
|
|
printf("Error: ");
|
|
ob_print(state.error);
|
|
printf("\n");
|
|
result = 1;
|
|
}
|
|
|
|
if(state.ret) {
|
|
printf("Toplevel return: ");
|
|
ob_print(state.ret);
|
|
printf("\n");
|
|
if(sol_is_int(state.ret)) {
|
|
result = state.ret->ival;
|
|
}
|
|
}
|
|
st_free(program);
|
|
if(clean) sol_state_cleanup(&state);
|
|
|
|
return result;
|
|
}
|