From 20b9f5a8418670665ff5c5d3f5de538d35ec161b Mon Sep 17 00:00:00 2001 From: Kai Gunger Date: Tue, 19 May 2026 00:13:35 -0400 Subject: [tnslc] various updates --- tnslc/compile/function.tnsl | 60 ++++++++++++++++++-- tnslc/compile/scope.tnsl | 15 +++++ tnslc/compile/var.tnsl | 131 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 195 insertions(+), 11 deletions(-) diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl index 9188ca3..4947d75 100644 --- a/tnslc/compile/function.tnsl +++ b/tnslc/compile/function.tnsl @@ -194,7 +194,6 @@ struct Function { /; _call_label [~uint8] Scope tmp - self.mod`._print(0) tmp.init(self.mod, NULL, self.name) ~uint8 out = tmp.base_label() tmp.end() @@ -370,7 +369,7 @@ struct Function { /; if (last_var == true) last_var = false - /; if (v.is_struct() == false) + /; if (v.is_bool() == true) # Do cond jmp ~CompBuf buf = s`.cb ~uint8 lab = s`.end_label() @@ -436,7 +435,7 @@ struct Function { /; if (last_var == true) last_var = false - /; if (v.is_struct() == false) + /; if (v.is_bool() == true) # Do cond jmp ~CompBuf buf = s`.cb ~uint8 lab = s`.end_label() @@ -1078,8 +1077,8 @@ struct Function { ;/ /; _set_var_ptr(~void v, ~void new_val) - Var v - int sz = len v + Var vv + int sz = len vv /; loop (int i = 0; i < sz) [i++] ~uint8 out = v + i ~uint8 in = new_val + i @@ -1829,7 +1828,56 @@ struct Function { uint8 val = utils.unquote_cha(dat) out.offset = val ;; else if (n`.data{0} == '\"') - _printf("TODO: in-function strings\n\0") + ~Struct t = self._find_literal_type(s, "uint8\0") + ~uint8 dlab = s`.gen_const_name() + out._init(t) + _delete(out.name) + out.name = dlab + out.ptr_push(1) + + ~CompBuf buf = s`.cb + buf`.add_d(dlab) + buf`.add_d(":\n db \0") + + utils.Vector vout = utils.unquote_str(n`.data) + + /; loop (int i = 1; i < vout.count) [i++] + ~uint8 uptr = vout.get(i - 1) + uint8 utmp = uptr` + tmp = utils.int_to_str(utmp) + buf`.add_d(tmp) + buf`.add_d(", \0") + _delete(tmp) + ;/ + + /; if (vout.count > 0) + int i = vout.count + ~uint8 uptr = vout.get(i - 1) + uint8 utmp = uptr` + tmp = utils.int_to_str(utmp) + buf`.add_d(tmp) + _delete(tmp) + ;; else + buf`.add_d("0\0") + ;/ + + buf`.add_d("\n\0") + + # Generate response text + vout.end() + tmp = self._global_ptr() + vout.from_cstr(" dq \0") + vout.push_cstr(tmp) + _delete(tmp) + + int idx = self.ptrc.count - 1 + idx = idx - depth + ~int32 pc = self.ptrc.get(idx) + /; if (pc` < 1) + vout.push_cstr(" + 8\0") + ;/ + vout.push_cstr("\n\0") + ;; else ~Struct t = self._find_literal_type(s, "int\0") out._init(t) diff --git a/tnslc/compile/scope.tnsl b/tnslc/compile/scope.tnsl index 569c48a..35f6b8b 100644 --- a/tnslc/compile/scope.tnsl +++ b/tnslc/compile/scope.tnsl @@ -639,6 +639,7 @@ struct Scope { return out ;/ + # Get closest breakable scope /; _closest_break [~Scope] /; if (utils.ends_with(self.name, "#wrap\0")) @@ -748,5 +749,19 @@ struct Scope { self.cb`.add_c(":\n\0") _delete(el) ;/ + + /; gen_const_name [~uint8] + utils.Vector v = self._base_label() + + ~uint8 u = utils.int_to_str(self.unique) + v.push_char('#') + v.push_cstr(u) + v.push_cstr("#const\0") + _delete(u) + + self.unique++ + + return v.as_cstr() + ;/ ;/ diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl index 5f87cc7..3ec7e8d 100644 --- a/tnslc/compile/var.tnsl +++ b/tnslc/compile/var.tnsl @@ -257,6 +257,30 @@ struct Var { return p` < 0 ;/ + /; first_non_ref_idx [int] + int idx = 0 + idx = idx - 1 + /; loop (int i = 0; i < self.ptrc.count) [i++] + /; if (self.is_ptrc(i, 0) == false) + idx = i + i = self.ptrc.count + ;/ + ;/ + return idx + ;/ + + /; first_non_ref [int32] + int idx = self.first_non_ref_idx() + + /; if (idx < 0) + return 0 + ;/ + + int count = self.ptrc.count - idx + ~int32 out = self.ptrc.get(count) + return out` + ;/ + # Returns true if the variable is an array /; is_arr [bool] ~int32 p = self.top_ptrc() @@ -289,6 +313,19 @@ struct Var { return _is_primitive(self._type`.name) == 0 ;/ + /; is_bool [bool] + # Check first if we are a pointer of some sort + ~int32 p + /; loop (int i = 0; i < self.ptrc.count) [i++] + p = self.ptrc.get(i) + /; if (p` !== 0) + return false + ;/ + ;/ + + return utils.strcmp(self._type`.name, "bool\0") == true + ;/ + # Compute and add the correct pointer chain value for an array # type prefix /; _arr_ptr(~parse.Node a) @@ -1330,12 +1367,24 @@ struct Var { return out ;/ + /; index_size [int] + /; loop (int i = 0; i < self.ptrc.count) [i++] + /; if (self.is_ptrc(i, 0) == false) + /; if (i + 1 < self.ptrc.count) + return 8 + ;/ + ;/ + ;/ + + return self._type`.size + ;/ + /; index (~CompBuf buf, ~Var idx, int reg) [Var] buf`.add_c(" ; Gen index\n\0") # Create a literal with the size of the type # Don't need to init or end this one - uint mul_sz = self.type_size() + uint mul_sz = self.index_size() Var oo oo.offset = mul_sz oo.loc = 0 @@ -1422,7 +1471,31 @@ struct Var { return ;/ - self.standard_op(buf, other, "add\0") + /; if (self.first_non_ref() < 0) + int idx = self.first_non_ref_idx() + + Var vl + vl.loc = 0 + /; if (idx + 1 < self.ptrc.count) + vl.offset = 8 + ;; else + vl.offset = self._type`.size + ;/ + + int32 ppc = 0 + ppc = ppc - 1 + Var vo = other`.copy() + vo.strip_refs() + vo.ptr_push(ppc) + vo.loc = 3 + vo.set(buf, ~vl) + vo.mul(buf, other) + + self.standard_op(buf, ~vo, "add\0") + vo.end() + ;; else + self.standard_op(buf, other, "add\0") + ;/ ;/ /; sub (~CompBuf buf, ~Var other) @@ -1431,7 +1504,31 @@ struct Var { return ;/ - self.standard_op(buf, other, "sub\0") + /; if (self.first_non_ref() < 0) + int idx = self.first_non_ref_idx() + + Var vl + vl.loc = 0 + /; if (idx + 1 < self.ptrc.count) + vl.offset = 8 + ;; else + vl.offset = self._type`.size + ;/ + + int32 ppc = 0 + ppc = ppc - 1 + Var vo = other`.copy() + vo.strip_refs() + vo.ptr_push(ppc) + vo.loc = 3 + vo.set(buf, ~vl) + vo.mul(buf, other) + + self.standard_op(buf, ~vo, "sub\0") + vo.end() + ;; else + self.standard_op(buf, other, "sub\0") + ;/ ;/ /; and (~CompBuf buf, ~Var other) @@ -1650,7 +1747,19 @@ struct Var { return ;/ - self._unary(buf, "inc\0") + /; if (self.first_non_ref() < 0) + int idx = self.first_non_ref_idx() + Var vv + vv.loc = 0 + /; if (idx + 1 < self.ptrc.count) + vv.offset = 8 + ;; else + vv.offset = self._type`.size + ;/ + self.standard_op(buf, ~vv, "add\0") + ;; else + self._unary(buf, "inc\0") + ;/ ;/ /; dec (~CompBuf buf) @@ -1659,7 +1768,19 @@ struct Var { return ;/ - self._unary(buf, "dec\0") + /; if (self.first_non_ref() < 0) + int idx = self.first_non_ref_idx() + Var vv + vv.loc = 0 + /; if (idx + 1 < self.ptrc.count) + vv.offset = 8 + ;; else + vv.offset = self._type`.size + ;/ + self.standard_op(buf, ~vv, "sub\0") + ;; else + self._unary(buf, "dec\0") + ;/ ;/ # Boolean related -- cgit v1.2.3