root/interface.c
/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- add_native_functions
- CRB_create_interpreter
- CRB_compile
- CRB_interpret
- release_global_strings
- CRB_dispose_interpreter
- CRB_add_native_function
#include "MEM.h"
#include "DBG.h"
#define GLOBAL_VARIABLE_DEFINE
#include "crowbar.h"
static void
add_native_functions(CRB_Interpreter *inter)
{
CRB_add_native_function(inter, "print", crb_nv_print_proc);
CRB_add_native_function(inter, "fopen", crb_nv_fopen_proc);
CRB_add_native_function(inter, "fclose", crb_nv_fclose_proc);
CRB_add_native_function(inter, "fgets", crb_nv_fgets_proc);
CRB_add_native_function(inter, "fputs", crb_nv_fputs_proc);
CRB_add_native_function(inter, "new_array", crb_nv_new_array_proc);
CRB_add_native_function(inter, "new_object", crb_nv_new_object_proc);
}
CRB_Interpreter *
CRB_create_interpreter(void)
{
MEM_Storage storage;
CRB_Interpreter *interpreter;
storage = MEM_open_storage(0);
interpreter = MEM_storage_malloc(storage,
sizeof(struct CRB_Interpreter_tag));
interpreter->interpreter_storage = storage;
interpreter->execute_storage = NULL;
interpreter->variable = NULL;
interpreter->function_list = NULL;
interpreter->statement_list = NULL;
interpreter->current_line_number = 1;
interpreter->stack.stack_alloc_size = 0;
interpreter->stack.stack_pointer = 0;
interpreter->stack.stack
= MEM_malloc(sizeof(CRB_Value) * STACK_ALLOC_SIZE);
interpreter->heap.current_heap_size = 0;
interpreter->heap.current_threshold = HEAP_THRESHOLD_SIZE;
interpreter->heap.header = NULL;
interpreter->top_environment = NULL;
interpreter->current_exception = NULL;
crb_set_current_interpreter(interpreter);
add_native_functions(interpreter);
return interpreter;
}
void
CRB_compile(CRB_Interpreter *interpreter, FILE *fp)
{
extern int yyparse(void);
extern FILE *yyin;
crb_set_current_interpreter(interpreter);
yyin = fp;
if (yyparse()) {
fprintf(stderr, "Error ! Error ! Error !\n");
exit(1);
}
crb_reset_string_literal_buffer();
}
void
CRB_interpret(CRB_Interpreter *interpreter)
{
int error_code;
interpreter->execute_storage = MEM_open_storage(0);
if ((error_code = setjmp(interpreter
->current_recovery_environment.environment))
== 0) {
crb_execute_statement_list(interpreter, NULL,
interpreter->statement_list);
} else {
int i;
CRB_Value *message;
CRB_Value *stack_trace;
message = CRB_search_assoc_member(interpreter->current_exception,
EXCEPTION_MEMBER_MESSAGE);
fprintf(stderr, "%s\n", message->u.object->u.string.string);
stack_trace = CRB_search_assoc_member(interpreter->current_exception,
EXCEPTION_MEMBER_STACK_TRACE);
DBG_assert(stack_trace->type == CRB_ARRAY_VALUE,
("stack_trace->type..%d.\n", stack_trace->type));
for (i = 0; i < stack_trace->u.object->u.array.size; i++) {
CRB_Object *assoc;
CRB_Value *line_number;
CRB_Value *function_name;
char *str;
assoc = stack_trace->u.object->u.array.array[i].u.object;
line_number
= CRB_search_assoc_member(assoc,
EXCEPTION_MEMBER_LINE_NUMBER);
function_name
= CRB_search_assoc_member(assoc,
EXCEPTION_MEMBER_FUNCTION_NAME);
str = CRB_value_to_string(function_name);
fprintf(stderr, "%s ", str);
MEM_free(str);
str = CRB_value_to_string(line_number);
fprintf(stderr, "at %s\n", str);
MEM_free(str);
}
interpreter->current_exception = NULL;
}
crb_garbage_collect(interpreter);
}
static void
release_global_strings(CRB_Interpreter *interpreter) {
while (interpreter->variable) {
Variable *temp = interpreter->variable;
interpreter->variable = temp->next;
}
}
void
CRB_dispose_interpreter(CRB_Interpreter *interpreter)
{
release_global_strings(interpreter);
if (interpreter->execute_storage) {
MEM_dispose_storage(interpreter->execute_storage);
}
interpreter->variable = NULL;
crb_garbage_collect(interpreter);
DBG_assert(interpreter->heap.current_heap_size == 0,
("%d bytes leaked.\n", interpreter->heap.current_heap_size));
MEM_free(interpreter->stack.stack);
MEM_dispose_storage(interpreter->interpreter_storage);
}
void
CRB_add_native_function(CRB_Interpreter *interpreter,
char *name, CRB_NativeFunctionProc *proc)
{
CRB_FunctionDefinition *fd;
fd = crb_malloc(sizeof(CRB_FunctionDefinition));
fd->name = name;
fd->type = NATIVE_FUNCTION_DEFINITION;
fd->u.native_f.proc = proc;
fd->next = interpreter->function_list;
interpreter->function_list = fd;
}