int main(int argc, char const *argv[]) { tokenadt *array, *stack; numdict *dict; valuepool *vpool; array = token_adt_new(MAXADTLEN); stack = token_adt_new(MAXADTLEN); dict = numdict_new(); vpool = value_pool_new(); char b[MAXINPUT]; int l; const char *info = GAKIO_REPL_INFO; fputs(info, stdout); fflush(stdout); const char *prmt = ">> "; while (1) { fputs(prmt, stdout); fflush(stdout); if (fgets(b, MAXINPUT, stdin) == NULL) return 0; l = strlen(b); /* 如果输入字符长度大于 MAXINPUT,将截取 MAXINPUT 长的字符串; * 并清空标准输入中剩余的字符 */ if (l > 0 && b[l-1] != '\n') { b[l-1] = '\n'; while (getchar() != '\n'); } if (l > 0 && b[l-1] == '\n') b[l-1] = '\0'; if (strcmp(b, ":q") != 0) { l = akio_lex(array, vpool, b); if (!l) { reduction(array, stack, dict, vpool); } token_adt_reset(array); token_adt_reset(stack); } else { break; } } token_adt_delete(array); token_adt_delete(stack); numdict_delete(dict); value_pool_delete(vpool); return 0; }
int main(int argc, char **argv) { char **real_argv = argv; struct scan_st *sc; struct symbol_table *stab; struct ast *a; char *source = NULL; int opt; int err_count = 0; #ifdef DEBUG int run_program = 1; int dump_symbols = 0; int dump_program = 0; int dump_icode = 0; #endif #ifdef DEBUG setvbuf(stdout, NULL, _IOLBF, 0); #endif /* * Get command-line arguments. */ while ((opt = getopt(argc, argv, OPTS)) != -1) { switch(opt) { #ifdef DEBUG case 'c': trace_scheduling++; break; #ifdef POOL_VALUES case 'd': trace_pool++; break; #endif case 'g': trace_gc++; break; #endif case 'G': gc_trigger = atoi(optarg); break; #ifdef DEBUG case 'i': dump_icode++; break; case 'l': trace_gen++; break; case 'm': trace_vm++; break; case 'n': run_program = 0; break; case 'o': trace_valloc++; break; case 'p': dump_program = 1; break; case 's': dump_symbols = 1; break; case 'v': trace_activations++; break; case 'y': trace_type_inference++; break; #endif case '?': default: usage(argv); } } argc -= optind; argv += optind; if (*argv != NULL) source = *argv; else usage(real_argv); #ifdef POOL_VALUES value_pool_new(); #endif gc_target = gc_trigger; if ((sc = scan_open(source)) != NULL) { stab = symbol_table_new(NULL, 0); global_ar = activation_new_on_heap(100, NULL, NULL); register_std_builtins(stab); report_start(); a = parse_program(sc, stab); scan_close(sc); #ifdef DEBUG if (dump_symbols) symbol_table_dump(stab, 1); if (dump_program) { ast_dump(a, 0); } #endif #ifndef DEBUG symbol_table_free(stab); types_free(); #endif err_count = report_finish(); if (err_count == 0) { struct iprogram *ip; struct vm *vm; struct process *p; unsigned char *program; program = bhuna_malloc(16384); ip = ast_gen_iprogram(a); iprogram_eliminate_nops(ip); iprogram_eliminate_useless_jumps(ip); iprogram_optimize_tail_calls(ip); iprogram_optimize_push_small_ints(ip); iprogram_eliminate_dead_code(ip); iprogram_gen(&program, ip); #ifdef DEBUG if (dump_icode > 0) iprogram_dump(ip, program); #endif vm = vm_new(program, 16384); vm_set_pc(vm, program); vm->current_ar = global_ar; p = process_new(vm); /* ast_dump(a, 0); */ if (RUN_PROGRAM) { process_scheduler(); } vm_free(vm); bhuna_free(program); /*value_dump_global_table();*/ } ast_free(a); /* XXX move on up */ /* gc(); */ /* actually do a full blow out at the end */ /* activation_free_from_stack(global_ar); */ #ifdef DEBUG symbol_table_free(stab); types_free(); if (trace_valloc > 0) { /* value_dump_global_table(); */ printf("Created: %8d\n", num_vars_created); printf("Cached: %8d\n", num_vars_cached); printf("Freed: %8d\n", num_vars_freed); } if (trace_activations > 0) { printf("AR's alloc'ed: %8d\n", activations_allocated); printf("AR's freed: %8d\n", activations_freed); } #ifdef POOL_VALUES if (trace_pool > 0) { pool_report(); } #endif #endif return(0); } else { fprintf(stderr, "Can't open `%s'\n", source); return(1); } }