void solid_repl() { size_t n = 256; char *buffer = (char *) malloc(sizeof(char) * n); solid_vm *vm = solid_make_vm(); solid_set_namespace(solid_get_current_namespace(vm), solid_str("compile"), solid_define_c_function(solid_compile)); solid_set_namespace(solid_get_current_namespace(vm), solid_str("import"), solid_define_c_function(solid_import)); while (printf("%s", "solid> "), getline(&buffer, &n, stdin) != -1) { solid_object *curexpr = solid_parse_tree(solid_parse_expr(buffer)); solid_call_func(vm, curexpr); solid_push_stack(vm, vm->regs[255]); solid_print(vm); } }
int main(int argc, char *argv[]) { if (argc > 1) { solid_object *mainfunc = solid_parse_tree(solid_parse_file(argv[1])); solid_vm *vm = solid_make_vm(); solid_set_namespace(solid_get_current_namespace(vm), solid_str("compile"), solid_define_c_function(solid_compile)); solid_set_namespace(solid_get_current_namespace(vm), solid_str("import"), solid_define_c_function(solid_import)); solid_call_func(vm, mainfunc); } else { solid_repl(); } return 0; }
solid_vm *solid_make_vm() { solid_vm *ret = (solid_vm *) malloc(sizeof(solid_vm)); ret->stack = make_list(); ret->all_objects = make_list(); memset(ret->regs, 0, 256); memset(ret->namespace_stack, 0, 256); ret->namespace_stack[0] = solid_instance(ret); ret->namespace_stack_pointer = 0; solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, "!!"), solid_define_c_function(ret, solid_nth_list)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, "print"), solid_define_c_function(ret, solid_print)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, "clone"), solid_define_c_function(ret, solid_clone)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, ":"), solid_define_c_function(ret, solid_cons)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, "+"), solid_define_c_function(ret, solid_add)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, "-"), solid_define_c_function(ret, solid_sub)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, "*"), solid_define_c_function(ret, solid_mul)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, "/"), solid_define_c_function(ret, solid_div)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, "=="), solid_define_c_function(ret, solid_eq)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, "<"), solid_define_c_function(ret, solid_lt)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, "<="), solid_define_c_function(ret, solid_lte)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, ">"), solid_define_c_function(ret, solid_gt)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, ">="), solid_define_c_function(ret, solid_gte)); solid_set_namespace(solid_get_current_namespace(ret), solid_str(ret, "gc"), solid_define_c_function(ret, solid_gc)); return ret; }