From 06dfc4e0bf7dbf2edeae463603c8b048c0cc5065 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Sat, 18 Mar 2023 22:02:58 -0400 Subject: Fix set + proper member variables --- tnslc/c_wrap.tnsl | 62 +++++++++++++++++++---- tnslc/simple.tnsl | 7 +-- tnslc/tnslc.tnsl | 144 +++++++++++++++++++++++++++++++----------------------- 3 files changed, 138 insertions(+), 75 deletions(-) (limited to 'tnslc') diff --git a/tnslc/c_wrap.tnsl b/tnslc/c_wrap.tnsl index 93ea3e4..3d5fa02 100644 --- a/tnslc/c_wrap.tnsl +++ b/tnslc/c_wrap.tnsl @@ -51,26 +51,70 @@ asm "extern free" # there's no more to do 'cause free returns nothing ;/ -struct Array { - int el_size, - int num_el, +struct Vector { + uint + el_size, + num_el, + dat_size, ~void dat } -/; method Array +/; method Vector - /; resize (int num_el) - _realloc(self.dat, self.num_el * self.el_size) + /; resize (uint num_el) + self.dat_size = num_el + _realloc(self.dat, num_el * self.el_size) ;/ - /; get (int i) [~void] - /; if (i > self.num_el) + /; get (uint i) [~void] + /; if (i !< self.num_el) return self.dat ;/ return self.dat + (i * self.el_size) ;/ - /; set (int i, ~void data) + /; set (uint i, ~void data) + ~void index = get(i) + /; loop (i = 0; i < self.el_size) [index = index + 1; data = data + 1; i++] + index = data + ;/ + ;/ + + /; push (~void data) + self.num_el++ + self.set(self.num_el - 1, data) + /; if (self.num_el !< self.dat_size) + self.resize(2*self.dat_size) + ;/ + ;/ + + /; pop + self.num_el-- + /; if (self.num_el !== 0 && self.num_el < self.dat_size / 4) + self.resize(self.dat_size / 2) + ;/ + ;/ + + /; remove (int index) + index++ + /; loop (index < self.num_el) [index++] + self.set(index - 1, self.get(index)) + ;/ + self.pop() + ;/ + + /; start (int el_size) + self.num_el = 0 + self.el_size = el_size + self.dat_size = 1 + self.dat = _alloc(self.el_size) + ;/ + /; clean + self.num_el = 0 + self.el_size = 0 + self.dat_size = 0 + _delete(self.dat) + self.dat = 0 ;/ ;/ diff --git a/tnslc/simple.tnsl b/tnslc/simple.tnsl index ca11667..4d727ac 100644 --- a/tnslc/simple.tnsl +++ b/tnslc/simple.tnsl @@ -14,10 +14,7 @@ mod.test tv = { 1, 2, false, 0 } - /; main [int] - ~int b = ~mod.a - ~int c = b+8 - ~~int d = ~c - return d`` + ~int i = ~tv.i + return i` ;/ diff --git a/tnslc/tnslc.tnsl b/tnslc/tnslc.tnsl index 4aa6429..90c580d 100644 --- a/tnslc/tnslc.tnsl +++ b/tnslc/tnslc.tnsl @@ -5,7 +5,7 @@ ;{}uint8 log_level = "2" /; log_err ({}uint8 msg) - ;tnsl.io.print("\n[TNSLC] [ERR]: ") + ;tnsl.io.print("[TNSLC] [ERR]: ") ;tnsl.io.println(msg) ;tnsl.exit() ;/ @@ -13,7 +13,7 @@ /; log_info ({}uint8 msg) ;uint l = string_to_int(log_level) /; if (l > 0) - ;tnsl.io.print("\n[TNSLC] [INFO]: ") + ;tnsl.io.print("[TNSLC] [INFO]: ") ;tnsl.io.println(msg) ;/ ;/ @@ -21,7 +21,7 @@ /; log_debug ({}uint8 msg) ;uint l = string_to_int(log_level) /; if (l > 1) - ;tnsl.io.print("\n[TNSLC] [DEBUG]: ") + ;tnsl.io.print("[TNSLC] [DEBUG]: ") ;tnsl.io.println(msg) ;/ ;/ @@ -575,6 +575,9 @@ # TODO: Match a type to another type if possible # this is the type inference engine /; match_types (Variable to_match, ~CompData data) [Variable] + /; if (to_match.loc_type == LOCATION.LITERAL) + ;return to_match + ;/ ;return to_match ;/ @@ -606,6 +609,10 @@ ;return false ;/ + /; is_prim [bool] + ;return len(self.data_type.ptr_chain) > 1 || is_primitive(self.data_type.name) !< 0 + ;/ + /; strip_one (~CompData data) ;data`.csec = string_join( { data`.csec, @@ -615,7 +622,7 @@ ;/ /; strip_refs (~CompData data) [Variable] - ;int pc = len (self.data_type.ptr_chain) + ;Variable out = {"#ref", self.data_type, 4, LOCATION.REGISTER} /; if (self.loc_type == LOCATION.REGISTER) ;data`.csec = string_join( { @@ -625,11 +632,12 @@ ;; else ;data`.csec = string_join( { data`.csec, - "\tmov rsi, ", self.norm_loc(8), "\n" + "\tlea rsi, ", self.norm_loc(8), "\n" }, "") + ;out.data_type.ptr_chain.append(PTYPE.REFERENCE) ;/ - ;Variable out = {"#ref", self.data_type, 4, LOCATION.REGISTER} + ;int pc = len (out.data_type.ptr_chain) /; loop (pc > 1) [pc = len (out.data_type.ptr_chain)] /; if (out.data_type.ptr_chain{pc - 1} == PTYPE.REFERENCE) ;out.strip_one(data) @@ -780,23 +788,52 @@ # Todo: Make sure this works well. /; set (Variable v, ~CompData data) ;int sz = self.norm_size() + ;v = self.match_types(v, data) /; if (self.loc_type == LOCATION.LITERAL && v.loc_type == LOCATION.LITERAL) ;self.location = v.location - ;; else if (is_primitive(self.data_type.name) < 0 && len (self.data_type.ptr_chain) == 0) - ;v = self.match_types(v, data) + ;; else if (self.is_prim()) + /;if (self.is_ref() && v.is_ref()) + ;data`.csec = string_join( { + data`.csec, + "\tmov ", get_reg(4, sz), ", ", v.norm_loc(sz), "\n", + "\tmov ", self.norm_loc(sz), ", ", get_reg(4, sz), "\n" + }, "") + ;; else + ;data`.csec = string_join( { + data`.csec, + "\tmov ", self.norm_loc(sz), ", ",v.norm_loc(sz), "\n" + }, "") + ;/ + ;; else + /; if (self.is_ref()) + ;data`.csec = string_join( { + data`.csec, + "\tmov rdi, ", self.norm_loc(8), "\n" + }, "") + ;; else + ;data`.csec = string_join( { + data`.csec, + "\tlea rdi, ", self.norm_loc(8), "\n" + }, "") + ;/ + + /; if (v.is_ref()) + ;data`.csec = string_join( { + data`.csec, + "\tmov rdi, ", v.norm_loc(8), "\n" + }, "") + ;; else + ;data`.csec = string_join( { + data`.csec, + "\tlea rdi, ", v.norm_loc(8), "\n" + }, "") + ;/ + ;data`.csec = string_join( { data`.csec, - "\tlea rsi, ", v.norm_loc(0), "\n", - "\tlea rdi, ", self.norm_loc(0), "\n", "\tmov rcx, ", int_to_string(sz), "\n", "\trep movsb\n" }, "") - ;; else - ;v = self.match_types(v, data) - ;data`.csec = string_join( { - data`.csec, - "\tmov ", self.norm_loc(sz), ", ", v.norm_loc(sz), "\n" - }, "") ;/ ;/ @@ -806,27 +843,22 @@ data`.csec, "\tmov ", get_reg(new_reg, sz), ", ", get_reg(self.location, sz), "\n" }, "") + ;self.location = new_reg ;/ /; ref (Variable to_ref, ~CompData data) ;int l = len (to_ref.data_type.ptr_chain) - /; if (l > 0) - /; if (to_ref.data_type.ptr_chain{l - 1} == PTYPE.REFERENCE) - ;to_ref.data_type.ptr_chain{l - 1} = PTYPE.POINTER - ;self.set(to_ref, data) - ;; else - ;data`.csec = string_join( { - data`.csec, - "\tlea rsi, ", to_ref.norm_loc(0), "\n", - "\tmov ", self.norm_loc(8), ", rsi\n" - }, "") - ;/ + /; if (to_ref.is_ref()) + ;to_ref.data_type.ptr_chain{l - 1} = PTYPE.POINTER + ;self.set(to_ref, data) ;; else if (to_ref.loc_type !== LOCATION.REGISTER && to_ref.loc_type !== LOCATION.LITERAL) ;data`.csec = string_join( { data`.csec, "\tlea rsi, ", to_ref.norm_loc(0), "\n", "\tmov ", self.norm_loc(8), ", rsi\n" }, "") + ;; else + ;log_err(string_add("Failure to create reference from register or literal value ", to_ref.sprint())) ;/ ;/ @@ -840,37 +872,26 @@ ;return out ;/ - /; member (Variable s, ~CompData data, {}uint8 name) - ;log_debug("member") + /; member ({}uint8 name, ~CompData data) [Variable] + ;Variable out = self.strip_refs(data) ;int accum = 0 - ;self.data_type.name = "" ;int i = 0 - /; loop(i < len (s.data_type.members)) [i++] - /; if (string_equate(s.data_type.members{i}.name, name)) + /; loop(i < len (out.data_type.members)) [i++] + /; if (string_equate(out.data_type.members{i}.name, name)) ;break ;/ - ;accum = accum + s.data_type.members{i}.data_type.s + ;accum = accum + out.data_type.members{i}.data_type.s ;/ - - /; if (s.is_ref() && (s.loc_type == LOCATION.STACK || s.loc_type == LOCATION.LABEL)) - ;data`.csec = string_join( { - data`.csec, - "\tmov rsi, ", s.norm_loc(8), "\n" - }, "") - ;; else - ;data`.csec = string_join( { - data`.csec, - "\tlea rsi, ", s.norm_loc(8), "\n" - }, "") - ;/ - + + ;out.data_type = out.data_type.members{i}.data_type + ;out.data_type.ptr_chain.append(PTYPE.REFERENCE) + ;data`.csec = string_join( { data`.csec, "\tadd rsi, ", int_to_string(accum), "\n" }, "") - /; if (self.is_ref()) - ;/ + ;return out ;/ /; index (Variable i, ~CompData data) [Variable] @@ -1877,35 +1898,36 @@ ;return wk ;/ - ;Variable refer = {"#tmp", wk.data_type, 1, LOCATION.REGISTER} - /; if (alt) - ;refer.location = 2 - ;/ - ;log_debug(string_add("Eval dot initial val: ", wk.sprint())) - ;refer.data_type.ptr_chain.append(PTYPE.REFERENCE) /; if (tok`{start}.cmp(".")) - ;log_debug("Pre loop ref") - ;refer.ref(wk, out) + ;log_debug("Pre loop member") + ;wk = wk.member(tok`{start + 1}.data, out) ;; else ;log_debug("Pre loop deref") - ;refer = wk.deref(out) + ;wk = wk.deref(out) ;/ - ;start++ + + ;start = start + 2 /; loop (start < max) [start++] /; if (tok`{start}.cmp("`")) ;log_debug("Loop deref") - ;refer = refer.deref(out) + ;wk = wk.deref(out) ;; else if (tok`{start}.cmp(".") && tok`{start + 1}.type_is(TOKEN.DEFWORD)) ;log_debug("Loop member") - ;refer.member(refer, out, tok`{start + 1}.data) + ;wk = wk.member(tok`{start + 1}.data, out) ;start++ ;/ ;/ - ;return refer + /; if (alt) + ;wk.move_register(2, out) + ;; else + ;wk.move_register(1, out) + ;/ + + ;return wk ;/ /; _eval_call (~{}Token tok, int start, max, ~CompData out, ~Module current, ~Scope scope, Type t) [Variable] -- cgit v1.2.3