language

some fools attempt at an interpreted language
Log | Files | Refs | README

commit dd4b49f2738ff071951e45725617129394199f67
parent 0e652da6428dbc08882120a00dc24b3242a030a9
Author: Paul Longtine <paul@nanner.co>
Date:   Sat, 26 Sep 2020 01:50:17 -0400

Simplified and demystified the object type.

Added more support for objects in the compiler.

Diffstat:
Msrc/lc/interpreter.py | 20++++++++++++--------
Msrc/lc/lexer.py | 16++++++++++++++--
Msrc/lc/namespace.py | 15+++++++++++++--
Msrc/lc/parser.py | 69++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Msrc/lc/test_files/depthtest.ti | 16++++++++++++++--
Msrc/lc/test_files/example.ti | 4+---
Msrc/lc/test_files/fibb.ti | 8++++----
Dsrc/lc/test_files/new_mod_op.ti | 2--
Msrc/lc/test_files/problem.ti | 8+++-----
Asrc/lc/test_files/sillylinkedobject.ti | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/lc/test_files/test.ti -> src/lc/test_files/test_extern.ti | 0
Msrc/vm/inc/ns.h | 4++--
Msrc/vm/inc/object.h | 15+++------------
Msrc/vm/inc/var.h | 15++++-----------
Msrc/vm/src/ins_def.c | 45++++++++++++++++++++-------------------------
Msrc/vm/src/ns.c | 28++++++++++++++--------------
Msrc/vm/src/object.c | 42+++++++++---------------------------------
Msrc/vm/src/proc.c | 15++++++---------
Msrc/vm/src/rt.c | 7++-----
Msrc/vm/src/var.c | 68+++++++++++++++++++-------------------------------------------------
Asrc/vm/tests/cases/ns/test | 0
21 files changed, 249 insertions(+), 197 deletions(-)

diff --git a/src/lc/interpreter.py b/src/lc/interpreter.py @@ -48,13 +48,17 @@ class Label(AbstractToken): else: self.name = names[0] - t = self.i.ns.resolve(self.name) - - if t == None: - fatal_error(self.i, "Cannot resolve name " + self.name) + if self.name == "ref": + self.scope = 1 + self.expr = 0 else: - self.scope = t[0] - self.expr = t[1] + t = self.i.ns.resolve(self.name) + + if t == None: + fatal_error(self.i, "Cannot resolve name " + self.name) + else: + self.scope = t[0] + self.expr = t[1] def action(self, s=False): if s: @@ -89,7 +93,6 @@ class Type(AbstractToken): type_string = "" is_object = False def update(self): - n = None for x, t in enumerate(self.i.p.defined_types): r = t.match(self.data, 0) @@ -100,7 +103,7 @@ class Type(AbstractToken): if n == None: self.is_object = True self.type_string = self.data[0] - n = 6 + n = self.i.p.TYPE_OBJECT self.expr = n @@ -167,6 +170,7 @@ class Expression(AbstractToken): init=(lambda x,y: FunctionCall(Label(x,y[0]), Arguements(x,y[1:]))) ) + self.subexpr = Statement( "subexpr", expression=[ diff --git a/src/lc/lexer.py b/src/lc/lexer.py @@ -17,6 +17,12 @@ class CompoundSymbol(): self.symbols = symbols + def add(self, symbol): + if type(symbol) is str: + symbol = AtomicSymbol(symbol) + + self.symbols.append(symbol) + def match(self, tokenstring, index): rv = [] for i in self.symbols: @@ -107,9 +113,10 @@ class GroupingSymbol(PolySymbol): return [index, rv] if found else [False, None] class Statement(): - def __init__(self, name, expression=[], init=None): + def __init__(self, name, expression=[], onMatch=None, init=None): self.name = name self.expr = expression + self.onMatch= onMatch self.action = init def match(self, tokenstring): @@ -119,13 +126,18 @@ class Statement(): if index >= len(tokenstring): return False r = e.match(tokenstring, index) + if r[0]: rv.append(r[1]) index = r[0] else: break - return rv if index == len(tokenstring) else False + matched = index == len(tokenstring) + + return rv if matched else False + + class Tokenizer(): def __init__(self, symbol_delim, statement_delim): diff --git a/src/lc/namespace.py b/src/lc/namespace.py @@ -12,6 +12,7 @@ class Namespace(): self.stack = [] self.t = None + self.tShared = [] def name_dec(self, name): if name in self.ns[self.sc].keys(): @@ -48,13 +49,23 @@ class Namespace(): # Releases target def release(self): tmp = self.ns[0] + for name in self.tShared: + self.ns[self.sc][name][1] = tmp + self.pop() self.ns[0][self.t][1] = tmp self.t = None + self.tShared = [] def copy(self, new_name, name): - self.ns[self.sc][new_name][1] = self.obj_resolve(name) - + if self.t == name: + self.ns[self.sc][new_name][1] = self.ns[0] + else: + self.ns[self.sc][new_name][1] = self.obj_resolve(name) + + def is_obj(self, name): + return False + # Resolves name into object def obj_resolve(self, name): rv = None diff --git a/src/lc/parser.py b/src/lc/parser.py @@ -36,7 +36,8 @@ class Parser(): "for", "while", "func", - "class" + "class", + "var" ] self.TYPE_VOID = 0 @@ -72,6 +73,7 @@ class Parser(): "hashtable", "stack" ] + # Defines what integers look like self.int_def = AtomicSymbol("^[0-9]+$") # Regular expression for encapsulating text in `"`, simply @@ -79,8 +81,7 @@ class Parser(): # Defines what type names exists self.type_def = InclusiveSymbol(self.defined_types) # Defines what reserved names exists - self.label_def = ExclusiveSymbol(self.defined_types + - [self.int_def] + + self.label_def = ExclusiveSymbol([self.int_def] + [self.str_def] + self.known_tokens ) # Defines the parameter list defintion @@ -256,7 +257,7 @@ class Parser(): self.paramlist_def, AtomicSymbol("-"), AtomicSymbol(">"), - self.type_def, + self.label_def, AtomicSymbol(":") ], init=( @@ -278,7 +279,7 @@ class Parser(): self.label_def, AtomicSymbol("-"), AtomicSymbol(">"), - self.type_def, + self.label_def, AtomicSymbol(":") ], init=( @@ -301,6 +302,7 @@ class Parser(): self.paramlist_def, AtomicSymbol(":") ], + onMatch=(lambda x, y: x.add_objectType(y[1])), init=(lambda x: [ x.new_name(1), x.ns_persist(1), @@ -318,6 +320,7 @@ class Parser(): self.label_def, AtomicSymbol(":") ], + onMatch=(lambda x, y: x.add_objectType(y[1])), init=(lambda x: [ x.new_name(1), x.ns_persist(1), @@ -331,7 +334,7 @@ class Parser(): self.statement_new = Statement( "new", expression=[ - self.label_def, + AtomicSymbol("var"), self.label_def, AtomicSymbol("="), AtomicSymbol("new"), @@ -341,17 +344,35 @@ class Parser(): ], init=(lambda x: [ x.new_name(1), - x.ns_copy(1, 0), + x.ns_copy(1, 4), NewClass(x.eval_label(1), x.eval_label(4), x.eval_args(5)) ]) ) + self.statement_unbound_new = Statement( + "unbound_new", + expression=[ + self.label_def, + AtomicSymbol("="), + AtomicSymbol("new"), + self.label_def, + self.paramlist_def, + AtomicSymbol(";") + ], + init=(lambda x: [ + x.ns_copy(0, 3), + NewClass(x.eval_label(0), + x.eval_label(3), + x.eval_args(4)) + ]) + ) + self.statement_inst = Statement( "instantiation", expression=[ - self.type_def, + self.label_def, self.label_def, AtomicSymbol("="), self.expr_def, @@ -359,6 +380,7 @@ class Parser(): ], init=(lambda x: [ x.new_name(1), + x.ns_copy(1, 0), VariableNew(x.eval_label(1), x.eval_type(0)), VariableAssignment(x.eval_label(1), @@ -366,6 +388,23 @@ class Parser(): ]) ) + self.statement_declare = Statement( + "declare", + expression=[ + AtomicSymbol("var"), + self.label_def, + AtomicSymbol("as"), + self.label_def, + AtomicSymbol(";") + ], + init=(lambda x: [ + x.new_name(1), + x.ns_copy(1, 3), + VariableNew(x.eval_label(1), + x.eval_type(3)) + ]) + ) + self.statement_assign = Statement( "assignment", expression=[ @@ -380,6 +419,7 @@ class Parser(): ]) ) + self.statement_expression = Statement( "expression", expression=[ @@ -405,7 +445,9 @@ class Parser(): self.statement_proc, self.statement_pless_class, self.statement_class, + self.statement_declare, self.statement_new, + self.statement_unbound_new, self.statement_inst, self.statement_assign, self.statement_expression @@ -414,6 +456,8 @@ class Parser(): # This is the definition for what is a symbol self.symbols = Tokenizer(self.splitters, self.end_statements) + self.currentObjectType = "" + # This holds the program. data = "" # Open the file, and replace every newline with a space. @@ -423,6 +467,10 @@ class Parser(): # Now, parse our program into statements self.lines = self.symbols.generate_statements(data) + def add_objectType(self, name): + self.currentObjectType = name + pass + def get_statements(self): rv = [] # Go through our program statement by statement and get line numbers @@ -437,7 +485,10 @@ class Parser(): r = a.match(l) # If the line matches the token, if r: - + if a.onMatch != None: + print("running on match thing") + a.onMatch(self, l) + fail = False # If the token is an "incude" token, include the file # specified by the "include" directive diff --git a/src/lc/test_files/depthtest.ti b/src/lc/test_files/depthtest.ti @@ -1,12 +1,24 @@ include fibb; +func fibbioRecurse(int target, FullFibb obj) -> int: +{ + if target > 1: + { + return fibbioRecurse(target - 1, obj) + fibbioRecurse(target - 2, obj); + } + else: + { + return obj.do(2); + } +} + func fibbio(int target) -> int: { - FullFibb fibb = new FullFibb(); + var fibb = new FullFibb(); if target > 1: { - return fibbio(target - 1) + fibbio(target - 2); + return fibbioRecurse(target - 1, fibb) + fibbioRecurse(target - 2, fibb); } else: { diff --git a/src/lc/test_files/example.ti b/src/lc/test_files/example.ti @@ -1,5 +1,3 @@ -DEBUG; - class Bar: { int lala = 2; @@ -10,7 +8,7 @@ func main(Bar arg) -> void: print arg.lala; } -Bar foo = new Bar(); +var foo = new Bar(); print "Calling main()"; main(foo); print "Called main()"; diff --git a/src/lc/test_files/fibb.ti b/src/lc/test_files/fibb.ti @@ -12,7 +12,7 @@ class Adder: class Counter: { - Adder add_machine = new Adder(); + var add_machine = new Adder(); add_machine.operandA = 0; @@ -35,7 +35,7 @@ class Counter: class ObjectFibb(Counter acc): { - Adder add_machine = new Adder(); + var add_machine = new Adder(); add_machine.operandA = 0; add_machine.operandB = 1; @@ -59,8 +59,8 @@ class ObjectFibb(Counter acc): class FullFibb: { - Counter the_counter = new Counter(); - ObjectFibb fibb = new ObjectFibb(the_counter); + var the_counter = new Counter(); + var fibb = new ObjectFibb(the_counter); func do (int count) -> int: { diff --git a/src/lc/test_files/new_mod_op.ti b/src/lc/test_files/new_mod_op.ti @@ -1 +0,0 @@ -int x = 4 % 2; -\ No newline at end of file diff --git a/src/lc/test_files/problem.ti b/src/lc/test_files/problem.ti @@ -1,5 +1,3 @@ -DEBUG; - class UsedByUsedByTesting: { int property_three = 3; @@ -14,7 +12,7 @@ class UsedByTesting: { int property_two = 2; - UsedByUsedByTesting x = new UsedByUsedByTesting(); + var x = new UsedByUsedByTesting(); func action -> void: { @@ -26,7 +24,7 @@ class Testing: { int property_one = 1; - UsedByTesting y = new UsedByTesting(); + var y = new UsedByTesting(); print y.x.property_three; print y.property_two; @@ -39,5 +37,5 @@ class Testing: print property_one; } -Testing t = new Testing(); +var t = new Testing(); diff --git a/src/lc/test_files/sillylinkedobject.ti b/src/lc/test_files/sillylinkedobject.ti @@ -0,0 +1,49 @@ +class SillyLinkedObject(int id): +{ + string value = "pineapple"; + + var next as SillyLinkedObject; + + if (id - 1) > 0: + { + next = new SillyLinkedObject(id - 1); + } + + func get_id(int target_id) -> SillyLinkedObject: + { + var rv as SillyLinkedObject; + + if id == target_id: + { + rv = ref; + } + else: + { + if id > 0: + { + rv = next.get_id(target_id); + } + } + + return rv; + } + +} + +func do_test -> SillyLinkedObject: +{ + var rv = new SillyLinkedObject(20); + + return rv; +} + +SillyLinkedObject root = do_test(); + +SillyLinkedObject number_3 = root.get_id(3); + +number_3.value = "porcuepine"; +print number_3.id; +print number_3.value; + +print root.id; +print root.value; diff --git a/src/lc/test_files/test.ti b/src/lc/test_files/test_extern.ti diff --git a/src/vm/inc/ns.h b/src/vm/inc/ns.h @@ -12,14 +12,14 @@ typedef unsigned int ns_addr; typedef struct ns_cont { - long level; + unsigned long level; ns_addr size; var_cont** names; struct ns_cont* next; } ns_cont; typedef struct ns_t { - long id; + unsigned long id; ns_cont* root; ns_cont* last; } ns_t; diff --git a/src/vm/inc/object.h b/src/vm/inc/object.h @@ -7,26 +7,17 @@ #include <stdlib.h> #include "ns.h" -#include "var.h" #include "helper.h" -typedef struct obj_t { - ns_t* names; -} obj_t; - -/* Initalize an empty object of size - */ -obj_t* object_init(); +ns_t* object_get(var_cont*); /* Deconstruct an object */ void object_del(void*); -var_cont* object_get_name(obj_t*, ns_addr); - -void object_set_name(obj_t*, ns_addr, var_cont*); +var_cont* object_get_name(ns_t*, ns_addr); -var_cont* object_call_method(obj_t*, ns_addr); +void object_set_name(ns_t*, ns_addr, var_cont*); #endif // OBJECT_H diff --git a/src/vm/inc/var.h b/src/vm/inc/var.h @@ -32,14 +32,14 @@ typedef enum { } b_type; typedef struct var_cont { - long ownership; + unsigned long ownership; b_type type; void* data; } var_cont; #define MAXIMUM_TRACKING_VARS 0xFFFF struct var_track { - int ptr; + unsigned int ptr; var_cont* vars[MAXIMUM_TRACKING_VARS]; }; @@ -72,12 +72,6 @@ typedef struct var_data_objbldr { b_type* param; } var_data_objbldr; -typedef struct var_data_object { - ns_addr id; - void* ref; - void (*objfree)(void*); -} var_data_object; - typedef struct var_data_int { int v; } var_data_int; @@ -116,7 +110,7 @@ void* var_data_alloc_TYPE(b_type); void* var_data_alloc_PLIST(size_t); void* var_data_alloc_FUNC(b_type); void* var_data_alloc_OBJBLDR(void); -void* var_data_alloc_OBJECT(void (*freefunc)(void*)); +void* var_data_alloc_OBJECT(); void* var_data_alloc_G_INT(int); void* var_data_alloc_G_FLOAT(double); void* var_data_alloc_G_CHAR(char); @@ -141,8 +135,7 @@ void var_set(var_cont*, void*, b_type); b_type var_data_get_TYPE(var_cont*); var_data_func* var_data_get_FUNC(var_cont*); -var_data_objbldr*var_data_get_OBJBLDR(var_cont*); -var_data_object* var_data_get_OBJECT(var_cont*); +var_data_objbldr* var_data_get_OBJBLDR(var_cont*); int var_data_get_G_INT(var_cont*); double var_data_get_G_FLOAT(var_cont*); char var_data_get_G_CHAR(var_cont*); diff --git a/src/vm/src/ins_def.c b/src/vm/src/ins_def.c @@ -564,9 +564,7 @@ void _ins_def_GETN (rt_t* ctx, bc_cont* line) var_cont* obj = stk_pop(ctx->stack); - var_data_object* obj_var_data = var_data_get_OBJECT(obj); - - obj_t* object = (obj_t*)obj_var_data->ref; + ns_t* object = object_get(obj); var_cont* var = object_get_name(object, name); @@ -579,14 +577,12 @@ void _ins_def_SETN (rt_t* ctx, bc_cont* line) int name = var_data_get_G_INT(line->varg[0]); var_cont* obj = stk_pop(ctx->stack); - var_cont* var = stk_pop(ctx->stack); - var_data_object* obj_var_data = var_data_get_OBJECT(obj); - - obj_t* object = (obj_t*)obj_var_data->ref; + ns_t* object = object_get(obj); + var_cont* set = var_data_cpy(var); - object_set_name(object, name, var); + object_set_name(object, name, set); pc_inc(ctx->pc, 1); } @@ -596,9 +592,7 @@ void _ins_def_CALLM (rt_t* ctx, bc_cont* line) // Pop the stack to get the object var_cont* obj = stk_pop(ctx->stack); // Get the objects variable data - var_data_object* obj_var_data = var_data_get_OBJECT(obj); - // Cast reference to object type - obj_t* object = (obj_t*)obj_var_data->ref; + ns_t* object = object_get(obj); // Get the method to call var_cont* var = object_get_name(object, name); var_data_func* func = var_data_get_FUNC(var); @@ -606,7 +600,7 @@ void _ins_def_CALLM (rt_t* ctx, bc_cont* line) ns_ctx_push(ctx->varctx, ctx->vars); // Set current namespace to objects namespace - ctx->vars = object->names; + ctx->vars = object; // Call the function proc_function_call_handle(ctx, func); // Update the program counter @@ -636,8 +630,15 @@ void _ins_def_RETURN (rt_t* ctx, bc_cont* line) // Pop the namespace and get the return value var_cont* return_value = ns_pop(ctx->vars); - // Push the return value to the stack - stk_push(ctx->stack, return_value); + if (return_value->type != VOID) + { + // Push the return value to the stack + stk_push(ctx->stack, return_value); + } + else + { + var_del(return_value); + } // Return to the callee pc_return(ctx->pc); @@ -683,14 +684,11 @@ void _ins_def_NEW (rt_t* ctx, bc_cont* line) void _ins_def_ENDCLASS (rt_t* ctx, bc_cont* line) { var_cont* new = var_new(OBJECT); - var_data_object* data = var_data_alloc_OBJECT(object_del); - obj_t* obj = object_init(); - obj->names = ctx->vars; + var_set(new, ctx->vars, OBJECT); - data->ref = (void*)obj; - - var_set(new, data, OBJECT); + ns_dec(ctx->vars, OBJECT, 1, 0); + ns_set(ctx->vars, 1, 0, new); stk_poplevel(&ctx->stack); stk_poplevel(&ctx->argstk); @@ -706,10 +704,7 @@ void _ins_def_DENS (rt_t* ctx, bc_cont* line) { int name = var_data_get_G_INT(line->varg[0]); - ns_t* ns = ns_init(0xFFFF); - - var_data_object* namespace = var_data_alloc_OBJECT(rt_ns_del); - namespace->ref = (void*)ns; + ns_t* namespace = ns_init(MAXIMUM_TRACKING_VARS); var_cont* ns_var = var_new(OBJECT); var_set(ns_var, namespace, OBJECT); @@ -717,7 +712,7 @@ void _ins_def_DENS (rt_t* ctx, bc_cont* line) ns_dec(ctx->names, OBJECT, 1, name); ns_set(ctx->names, 1, name, ns_var); - ctx->vars = ns; + ctx->vars = namespace; } void _ins_def_DECLASS (rt_t* ctx, bc_cont* line) { diff --git a/src/vm/src/ns.c b/src/vm/src/ns.c @@ -84,7 +84,7 @@ ns_t* ns_init(ns_addr size) ns_t* ns = (ns_t*)malloc(sizeof(ns_t)); M_ASSERT(ns); - static int inc = 0; + static unsigned int inc = 0; ns->id = inc; @@ -105,27 +105,25 @@ var_cont* ns_cont_del(ns_cont* container, ns_addr to_return) var_cont* rv = NULL; + if (container->names[to_return] != NULL) + { + rv = container->names[to_return]; + if (rv->ownership == container->level) + { + rv->ownership = -1; + } + } for (int i = 0; i < container->size; i++) { - if (container->names[i] != NULL && i != to_return) + if (i != to_return && container->names[i] != NULL) { - if (container->names[i]->ownership == container->level) + if (container->names[i]->ownership == container->level && + container->names[i]->data != rv->data) { var_del(container->names[i]); } } - else if (i == to_return) - { - if (container->names[to_return] != NULL) - { - rv = container->names[to_return]; - if (rv->ownership == container->level) - { - rv->ownership = -1; - } - } - } } free(container->names); @@ -292,6 +290,8 @@ void ns_cont_set(ns_cont* container, var_cont* var, ns_addr address) var_cont* ns_get(ns_t* ns, int scope, ns_addr address) { N_ASSERT(ns, "ns_get\n"); + N_ASSERT(ns->root, "ns_get\n"); + N_ASSERT(ns->last, "ns_get\n"); ns_cont* scoped_ns = scope ? ns->root : ns->last; diff --git a/src/vm/src/object.c b/src/vm/src/object.c @@ -7,59 +7,35 @@ #include "helper.h" -obj_t* object_init() +ns_t* object_get(var_cont* object) { - obj_t* rv = (obj_t*)malloc(sizeof(obj_t)); - M_ASSERT(rv); - - rv->names = NULL; + N_ASSERT(object, "object_get\n"); - return rv; + return (ns_t*)object->data; } void object_del(void* object) { N_ASSERT(object, "object_del\n"); - obj_t* o = object; - - if (o->names != NULL) - ns_del(o->names); - - free(o); -} - -obj_t* object_get(void* object) -{ - N_ASSERT(object, "object_get\n"); - - obj_t* o = object; + ns_t* o = object; - return o; + ns_del(o); } -var_cont* object_get_name(obj_t* object, ns_addr name) +var_cont* object_get_name(ns_t* object, ns_addr name) { N_ASSERT(object, "object_get_name\n"); - var_cont* value = ns_get(object->names, 1, name); + var_cont* value = ns_get(object, 1, name); return value; } -void object_set_name(obj_t* object, ns_addr name, var_cont* var) +void object_set_name(ns_t* object, ns_addr name, var_cont* var) { N_ASSERT(object, "object_set_name\n"); N_ASSERT(var, "object_set_name\n"); - ns_set(object->names, 1, name, var); -} - -var_cont* object_call_method(obj_t* object, ns_addr name) -{ - N_ASSERT(object, "object_call_method\n"); - - var_cont* rv = NULL; - - return rv; + ns_set(object, 1, name, var); } diff --git a/src/vm/src/proc.c b/src/vm/src/proc.c @@ -155,10 +155,9 @@ void proc_setvar(rt_t* ctx, int scope, ns_addr name, var_cont* var) } else { var_cont* ns_var = ns_get(ctx->names, 1, x); - var_data_object* var_d = var_data_get_OBJECT(ns_var); - ns_t* ns = (ns_t*) var_d->ref; + ns_t* object = object_get(ns_var); - ns_set(ns, s, name, var); + ns_set(object, s, name, var); } } @@ -181,10 +180,9 @@ var_cont* proc_getvar(rt_t* ctx, int scope, ns_addr name) } else { var_cont* ns_var = ns_get(ctx->names, 1, x); - var_data_object* var_d = var_data_get_OBJECT(ns_var); - ns_t* ns = (ns_t*) var_d->ref; + ns_t* object = object_get(ns_var); - rv = ns_get(ns, s, name); + rv = ns_get(object, s, name); } return rv; @@ -205,10 +203,9 @@ void proc_function_call(rt_t* ctx, int scope, ns_addr name) ns_ctx_push(ctx->varctx, ctx->vars); var_cont* ns_var = ns_get(ctx->names, 1, x); - var_data_object* var_d = var_data_get_OBJECT(ns_var); - ns_t* ns = (ns_t*) var_d->ref; + ns_t* object = object_get(ns_var); - ctx->vars = ns; + ctx->vars = object; } var_data_func* func = var_data_get_FUNC(var); diff --git a/src/vm/src/rt.c b/src/vm/src/rt.c @@ -26,15 +26,12 @@ rt_t* rt_ctx_new(char* fname, stk_t* args) ctx->pc = pc_new(fname); ctx->stack = stk_new(); ctx->argstk = args; - ctx->vars = ns_init(0xFFFF); + ctx->vars = ns_init(MAXIMUM_TRACKING_VARS); ctx->names = ns_init(0x7F); ctx->varctx = ns_ctx_init(); - var_data_object* namespace = var_data_alloc_OBJECT(rt_ns_del); - namespace->ref = (void*)ctx->vars; - var_cont* ns_var = var_new(OBJECT); - var_set(ns_var, namespace, OBJECT); + var_set(ns_var, ctx->vars, OBJECT); ns_dec(ctx->names, OBJECT, 1, 1); ns_set(ctx->names, 1, 1, ns_var); diff --git a/src/vm/src/var.c b/src/vm/src/var.c @@ -2,6 +2,7 @@ #include <stdio.h> #include "var.h" +#include "object.h" #include "bc.h" #include "helper.h" @@ -63,20 +64,6 @@ void* var_data_alloc_OBJBLDR(void) return rv; } -void* var_data_alloc_OBJECT(void (*freefunc)(void*)) -{ - N_ASSERT(freefunc, "var_data_alloc_OBJECT\n"); - - var_data_object* rv = (var_data_object*)malloc(sizeof(var_data_object)); - M_ASSERT(rv); - - rv->id = 0; - rv->ref = NULL; - rv->objfree = freefunc; - - return rv; -} - void* var_data_alloc_G_INT(int value) { var_data_int* rv = (var_data_int*)malloc(sizeof(var_data_int)); @@ -152,32 +139,30 @@ void var_data_free(void* data, b_type type) { N_ASSERT(data, "var_data_free\n"); - if (type == FUNC) + switch (type) { + case FUNC: var_data_free_FUNC(data); - } - - if (type == OBJBLDR) - { + break; + case OBJBLDR: var_data_free_OBJBLDR(data); - } - - if (type == OBJECT) - { - var_data_free_OBJECT(data); - } - - if (type == G_STR) - { + break; + case G_STR: var_data_free_G_STR(data); - } - - if (type == PLIST) - { + break; + case PLIST: var_data_free_PLIST(data); + break; + case OBJECT: + object_del(data); + break; + default: + if (type != VOID) + { + free(data); + } + break; } - if (type != VOID) - free(data); } void var_data_free_PLIST(void* data) @@ -195,11 +180,6 @@ void var_data_free_OBJBLDR(void* data) var_data_objbldr* d = data; free(d->param); } -void var_data_free_OBJECT(void* data) -{ - var_data_object* d = data; - d->objfree(d->ref); -} void var_data_free_G_STR(void* data) { var_data_str* d = data; @@ -262,16 +242,6 @@ var_data_objbldr* var_data_get_OBJBLDR(var_cont* var) return t; } -var_data_object* var_data_get_OBJECT(var_cont* var) -{ - N_ASSERT(var, "var_data_get_OBJECT\n"); - ASSERT( var->type == OBJECT, "TypeError" ); - - var_data_object* t = var->data; - - return t; -} - int var_data_get_G_INT(var_cont* var) { N_ASSERT(var, "var_data_get_G_INT\n"); diff --git a/src/vm/tests/cases/ns/test b/src/vm/tests/cases/ns/test Binary files differ.