3 Star 5 Fork 2

钫同学 / 中文编程语言Demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
interface.c 2.84 KB
一键复制 编辑 原始数据 按行查看 历史
钫同学 提交于 2020-04-11 18:45 . 调整代码结构
#include "MEM.h"
#include "DBG.h"
#define GLOBAL_VARIABLE_DEFINE
#include "wuji.h"
WJ_Interpreter *
WJ_create_interpreter(void)
{
MEM_Storage storage;
WJ_Interpreter *interpreter;
storage = MEM_open_storage(0);
interpreter = MEM_storage_malloc(storage,
sizeof(struct WJ_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(WJ_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;
wj_set_current_interpreter(interpreter);
add_native_functions(interpreter);
return interpreter;
}
void
WJ_compile(WJ_Interpreter *interpreter, FILE *fp)
{
extern int yyparse(void);
extern FILE *yyin;
wj_set_current_interpreter(interpreter);
yyin = fp;
if (yyparse()) {
/* BUGBUG */
fprintf(stderr, "Error ! Error ! Error !\n");
exit(1);
}
wj_reset_string_literal_buffer();
}
void
WJ_interpret(WJ_Interpreter *interpreter)
{
interpreter->execute_storage = MEM_open_storage(0);
wj_add_std_fp(interpreter);
wj_execute_statement_list(interpreter, NULL, interpreter->statement_list);
wj_garbage_collect(interpreter);
}
static void
release_global_strings(WJ_Interpreter *interpreter) {
while (interpreter->variable) {
Variable *temp = interpreter->variable;
interpreter->variable = temp->next;
}
}
void
WJ_dispose_interpreter(WJ_Interpreter *interpreter)
{
release_global_strings(interpreter);
if (interpreter->execute_storage) {
MEM_dispose_storage(interpreter->execute_storage);
}
interpreter->variable = NULL;
wj_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);
}
FunctionDefinition *
WJ_add_native_function(WJ_Interpreter *interpreter,
char *name, WJ_NativeFunctionProc *proc)
{
FunctionDefinition *fd;
fd = wj_malloc(sizeof(FunctionDefinition));
fd->name = name;
fd->type = NATIVE_FUNCTION_DEFINITION;
fd->is_closure = WJ_FALSE;
fd->u.native_f.proc = proc;
fd->next = interpreter->function_list;
interpreter->function_list = fd;
return fd;
}
C
1
https://gitee.com/zhuhaifang/wuji.git
git@gitee.com:zhuhaifang/wuji.git
zhuhaifang
wuji
中文编程语言Demo
master

搜索帮助