static parse_err_t parse_atom(parse_state_t *state, node_t **result) { switch(token_type(state->ts)) { case TOK_SYM: *result = node_symbol_new(state->ms, token_sym(state->ts)); break; case TOK_LIT: *result = node_value_new(state->ms, token_lit(state->ts)); break; default: return PARSE_EXPECTED_ATOM; } assert(*result); token_chomp(state->ts); return PARSE_OK; }
static IJsSymbol* node_symbol_get_member (IJsSymbol *obj, const gchar * name) { NodeSymbol* self = NODE_SYMBOL (obj); NodeSymbolPrivate *priv = NODE_SYMBOL_PRIVATE(self); gchar *tname = get_complex_node_type (priv->node, priv->my_cx); if (!tname) return NULL; if (js_context_get_member_list (priv->my_cx, tname)) //TODO:Fix mem leak { return IJS_SYMBOL ( node_symbol_new (js_context_get_member (priv->my_cx, tname, name), name, priv->my_cx)); } IJsSymbol *t = global_search (tname); if (t) return ijs_symbol_get_member (t, name); return NULL; }
static eval_err_t do_makesym(memory_state_t *ms, node_t **args, node_t **out, void *p) { char name[MAX_SYM_LEN], *cursor;; node_t *val, *val_iter; (void) p; val = args[0]; if(node_type(val) != NODE_CONS) { *out = val; return eval_err(EVAL_ERR_EXPECTED_CONS); } val_iter = val; cursor = name; for(val_iter = val; val_iter; val_iter = node_cons_cdr(val_iter)) { val = node_cons_car(val_iter); if(node_type(val) != NODE_VALUE) { *out = val; return eval_err(EVAL_ERR_EXPECTED_VALUE); } if(node_value(val) > 255 ) { *out = val; return eval_err(EVAL_ERR_VALUE_BOUNDS); } *cursor++ = node_value(val); if(cursor - &(name[0]) >= (ssize_t) sizeof(name)) { break; } val_iter = node_cons_cdr(val_iter); } *cursor = 0; *out = node_symbol_new(ms, name); return EVAL_OK; }
int main(int argc, char *argv[]) { node_t *env_handle = NULL, *ARGV; int status = 0; memory_state_t ms; node_memstate_init(&ms, libc_malloc_wrap, libc_free_wrap, NULL); if(argc < 2) { goto cleanup; } dbgtrace_setstream(g_stream_stdout); dbgtrace_enable( 0 //| TC_MEM_ALLOC //| TC_GC_TRACING //| TC_MEM_RC //| TC_GC_VERBOSE //| TC_NODE_GC //| TC_NODE_INIT //| TC_EVAL //| TC_FMC_ALLOC ); /* initialize environment */ env_handle = node_lockroot(node_handle_new(&ms, NULL)); { node_t *name, *val; name = node_symbol_new(&ms, "_load-lib"); val = node_foreign_new(&ms, foreign_loadlib); environ_add(env_handle, name, val); name = node_symbol_new(&ms, "_read-eval"); val = node_foreign_new(&ms, foreign_read_eval); environ_add(env_handle, name, val); // NB: argv[0] is interp name ARGV = generate_argv(&ms, argc-1, argv+1); environ_add(env_handle, node_symbol_new(&ms, "ARGV"), ARGV); environ_add_builtins(env_handle, startenv, ARR_LEN(startenv)); } { node_t *eval_in_hdl, *eval_out_hdl; eval_err_t eval_stat; eval_in_hdl = node_lockroot(node_handle_new(&ms, NULL)); eval_out_hdl = node_lockroot(node_handle_new(&ms, NULL)); /* bootstrap routine: ( _read-eval ( quote ARGV[0] ) ) ( _read-eval . ( ( quote . ( ARGV[0] . () ) ) . () ) ) */ { node_t *quote_call; quote_call = node_cons_new(&ms, node_symbol_new(&ms, "quote"), node_cons_new(&ms, node_cons_car(ARGV), NULL ) ); node_handle_update(eval_in_hdl, node_cons_new(&ms, node_symbol_new(&ms, "_read-eval"), node_cons_new(&ms, quote_call, NULL ) ) ); } eval_stat = eval(&ms, env_handle, eval_in_hdl, eval_out_hdl); if(eval_stat != EVAL_OK) { status = eval_stat; } node_droproot(eval_in_hdl); node_droproot(eval_out_hdl); } cleanup: if(getenv("PAREN_FINALENV")) { environ_print(env_handle, g_stream_stdout); } if(getenv("PAREN_MEMSTAT")) { printf("total alloc: %llu total free: %llu iters: %llu cycles: %llu\n", (unsigned long long) memory_gc_count_total(&ms), (unsigned long long) memory_gc_count_free(&ms), (unsigned long long) memory_gc_count_iters(&ms), (unsigned long long) memory_gc_count_cycles(&ms)); } if(getenv("PAREN_LEAK_CHECK")) { uintptr_t total_alloc, free_alloc; memory_gc_cycle(&ms); memory_gc_cycle(&ms); total_alloc = memory_gc_count_total(&ms); free_alloc = memory_gc_count_free(&ms); if(total_alloc != free_alloc) { printf("warning: %llu allocations remain at exit!\n", (unsigned long long) (total_alloc - free_alloc)); } } if(getenv("PAREN_DUMPMEM")) { memory_gc_print_state(&ms, g_stream_stdout); } memory_state_reset(&ms); return status; }