void toi_init(int argc, char **argv) { gv_tbl = st_init_numtable(); Init_thread(); THREAD(cur_thr)->recv = main_thread(); if (THREAD(main_thread())->env_tbl) st_free_table(THREAD(main_thread())->env_tbl); THREAD(main_thread())->env_tbl = gv_tbl; Init_symbol(); Init_class(); /* have to call Init_thread() again */ Init_thread(); Init_kernel(); cself = cKernel; Init_numeric(); Init_float(); Init_integer(); Init_array(); Init_hash(); Init_string(); Init_iostream(); Init_exception(); toi_set_argv(argc, argv); Init_gc(); signal(SIGINT, handle_sigint); }
int main(int argc, char *argv[]) { init(); init2(); static_context_t ctx; ctx.file = (char *)__FILE__; ctx.method = (char *)__FUNCTION__; ctx.line = __LINE__; ctx.parent = 0; ctx.block = 0; static_context_t *this_context = &ctx; /* awesome hardcoding work here */ load_path = array_new(); array_push(this_context, load_path, string_new_cstr(".")); array_push(this_context, load_path, string_new_cstr("./mspec/lib")); jump_begin(JUMP_RESCUE) send3(Kernel, s_define_method, intern("load"), kernel_load, 1); send3(Kernel, s_define_method, intern("require"), kernel_require, 1); send3(Kernel, s_define_method, intern("eval"), kernel_eval, -1); Init_hash(this_context); Init_regexp(this_context); init_core(&ctx, g_main); if (argc > 1) { set_argv(this_context, argc - 2, argv + 2); kernel_load(this_context, g_main, string_new_cstr(argv[1])); } else { set_argv(this_context, 0, 0); simple_irb(this_context, g_main); } jump_rescue object_t *cls = send0(exc, s_class); printf("%s: %s\n", ((symbol_t *)((class_t *)cls)->name)->string, ((string_t *)((exception_t *)exc)->message)->bytes); array_t *array = (array_t *)((exception_t *)exc)->backtrace; int i; for (i = 0; i < array->used; i++) printf("\tfrom %s\n", ((string_t *)array->values[i])->bytes); jump_ensure jump_end exit(0); return 0; }
void Forth_interpreter(void) { static unsigned char Forth_state = 0; //char temp; //char *scrathpad, *c_address; struct Wordtype *Current_word; struct Entry_type *Current_entry; unsigned char i; static int Error_no = 0; Stack_item temp2; unsigned char uc_char; //uint8_t uc_char; //uint8_t uc_flag; switch (Forth_state){ case 0: /* Start the interpreter with version info */ revision_info(); printf("\n\rForth Cortex M4 v0.37b 2013-11-25\n\r"); printf("Kernel v0.38 2014-01-06\n\r\n"); printf("\n\r"); Init_hash(); Forth_state = 1; break; case 1: /* Forth_state1; Reset the text input */ Cmdline_index = 0; Forth_state = 2; break; case 2: /* Forth_state2; Compile the command line string */ uc_char = getchar(); //uc_char = 0; //uc_flag = uart_read(CONSOLE_UART, &uc_char); //if (!uc_flag) { if (uc_char) { cpu_irq_disable(); switch ((char)uc_char){ case '\r': case '\n': if (Cmdline_index != 0){ Command_Line[Cmdline_index] = '\0'; Cmdline_index = 0; Forth_state = 3; } printf("\n\r"); break; case '\b': if (Cmdline_index != 0) Cmdline_index--; printf("\b \b"); break; default: /* Echo character and insert into command string */ putchar(uc_char); Command_Line[Cmdline_index++] = (char)uc_char; Cmdline_index %= CMD_LENGTH; break; } cpu_irq_enable(); } break; case 3: /* Forth_state3; The command line in parsed and the different words are looked up in the dictionaries */ if (STATE == 0){ /* Compilation flag STATE == 0 means NOT in compilation mode */ Word(); if (Cpop().c_address == NULL) /* NULL indicates the end of the command line */ Forth_state = 4; else{ /* Start with the hash dictionary for new word definitions */ if ((Current_entry = Find_in_hash(substring)) != NULL) switch (Current_entry -> Xtoken){ case 1: /* Found a variable in 'heap'. Push a stack item that contains */ /* the address to the variable */ temp2.a_address = (int *)(&(Current_entry -> parameter)); #ifdef DEBUG_PRINT printf("Address to variable: %lx \n\r", (uint32_t)temp2.a_address); #endif Cpush(temp2); break; default: /* xt 0 */ temp2.n = Current_entry -> parameter.i; /* uputi(temp2.n, BASE); */ Cpush(temp2); break; } else if ((Current_word = Find_in_dict(substring)) != NULL) switch (Current_word -> behavior){ case 1: /* Behaviour 1 is not used */ //temp2.a_address = (int)(Current_word -> behavior); break; case 6: /* Behavior 6 is not used */ break; default: /* Behaviour 0 */ Current_word -> funk(); break; } else{ temp2.n = Get_number(substring, BASE, &Error_no); Cpush(temp2); } } } else /* STATE == -1 i.e VM is in compilation mode */ Forth_state = 5; break; case 4: /* Forth_state4; Show the depth of the stack with N dots followed by "ok"*/ for (i = Cdepth(); i>0; i--) putchar('.'); printf(" ok \n\r\n\r"); Forth_state = 1; break; case 5: /* Forth_state5; The virtual machine is now in compile mode. Compile each word into an */ /* element of a single linked list */ Word(); if ((Current_entry = Find_in_hash(substring)) != NULL){ Compile_entry -> parameter.code = Current_entry; Compile_entry -> Xtoken = Current_entry -> Xtoken; } else{ if ((Current_word = Find_in_dict(substring)) != NULL){ if (Current_word -> behavior == 3){ /* Semicolon => Stop compiling */ Forth_state = 4; } else{ Compile_entry -> parameter.funk = Current_word -> funk; Compile_entry -> Xtoken = 0; } } else{ Compile_entry -> parameter.i = Get_number(substring, BASE, &Error_no); Compile_entry -> Xtoken = 4; } } Compile_entry -> Previous_def = New_entry(); break; case 6: /* Empty */ break; default: /* The virtual machine exits compile mode and enters interpret mode */ break; } }