From c4ece1f196c361f10515d1de41c1b23875769f54 Mon Sep 17 00:00:00 2001 From: CircleShift Date: Thu, 4 Dec 2025 01:53:29 -0500 Subject: [tnslc] setup scope --- tnslc/compile/function.tnsl | 63 +++++++++++++++++++++++++++++++-------------- tnslc/compile/module.tnsl | 2 ++ tnslc/compile/scope.tnsl | 48 ++++++++++++++++++++-------------- 3 files changed, 75 insertions(+), 38 deletions(-) (limited to 'tnslc') diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl index a768596..bf06f80 100644 --- a/tnslc/compile/function.tnsl +++ b/tnslc/compile/function.tnsl @@ -70,30 +70,49 @@ struct Function { ;/ ;/ - /; _fqn (~Module parent) [~uint8] - utils.Vector out - - return out.as_cstr() - ;/ - - /; _build_scope(~Module parent, ~CompBuf cb) [Scope] - ~uint8 fqn = self._fqn(parent) + /; _build_func(~Module parent, ~CompBuf cb) [Scope] Scope out - out.init(parent, cb, fqn) + out.init(parent, cb, self.name) out.parent = NULL - _delete(fqn) - # TODO: Write label to cb - # TODO: Push all saved vars and deal with parameters + /; if (parent`.e == true) + # Add to the global exports if the parent is exported + ~uint8 bl = out.base_label() + cb`.add_h("global \0") + cb`.add_h(bl) + cb`.add_h("\n\0") + _delete(bl) + ;/ + + # Write label and opening + out.place_base_label() + cb`.add_c(" push rbp\n\0") + cb`.add_c(" lea rbp, [rsp + 8]\n\0") + cb`.add_c(" push r10\n\0") + cb`.add_c(" push r11\n\0") + cb`.add_c(" push r12\n\0") + cb`.add_c(" push r13\n\0") + cb`.add_c(" push r14\n\0") + cb`.add_c(" push r15 ; scope init\n\n\0") + # TODO: Add all params to the scope return out ;/ - /; _end_scope(~Scope scope, ~CompBuf cb) + /; _end_func(~Scope scope, ~CompBuf cb) # TODO: place jmp label # TODO: pop all saved vars # TODO: ret + cb`.add_c(" lea rsp, [rbp - 56]\n\0") + cb`.add_c(" pop r15\n\0") + cb`.add_c(" pop r14\n\0") + cb`.add_c(" pop r13\n\0") + cb`.add_c(" pop r12\n\0") + cb`.add_c(" pop r11\n\0") + cb`.add_c(" pop r10\n\0") + cb`.add_c(" pop rbp\n\0") + cb`.add_c(" ret ; scope end\n\n\n\0") scope`.end() ;/ @@ -102,8 +121,8 @@ struct Function { # Sanity check ~parse.Node _up = self._up /; if (_up`.sub.count < 1) - ~Scope s = self._build_scope(parent, cb) - self._end_scope(s, cb) + ~Scope s = self._build_func(parent, cb) + self._end_func(s, cb) return ;/ @@ -120,11 +139,17 @@ struct Function { /; if (n`._type == parse.NTYPE_TLIST) i++ ;/ + + # Create scope + Scope fscope = self._build_func(parent, cb) + + + # Compile and then end scope + self._compile_statements(~fscope, i) + self._end_func(~fscope, cb) + ;/ - # Create scope and start compiling statements from here. - Scope fscope = self._build_scope(parent, cb) - fscope._compile_statements(_up, i) - self._end_scope(~fscope, cb) + /; _compile_statements(~Scope s, int off) ;/ /; _print (int idt) diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl index 0cfaa37..38653da 100644 --- a/tnslc/compile/module.tnsl +++ b/tnslc/compile/module.tnsl @@ -30,6 +30,8 @@ struct Module { Function f Module m + self.parent = NULL + self.vars.init(len v) self.structs.init(len s) self.funcs.init(len f) diff --git a/tnslc/compile/scope.tnsl b/tnslc/compile/scope.tnsl index 9cfc2ea..c152d00 100644 --- a/tnslc/compile/scope.tnsl +++ b/tnslc/compile/scope.tnsl @@ -12,21 +12,28 @@ struct Scope { } /; _recursive_mod_name(~Module mod, ~utils.Vector vec) - /; if (mod`.parent !== NULL) - _recursive_mod_name(mod`.parent, vec) - vec`.push_char('.') + ~Module p = mod`.parent + /; if (p !== NULL) + _recursive_mod_name(p, vec) + /; if (vec`.count !== 0) + vec`.push_char('.') + ;/ ;/ vec`.push_cstr(mod`.name) ;/ /; _recursive_scope_name(~Scope s, ~utils.Vector vec) - /; if (s`.parent == NULL) - /; if (s`.mod !== NULL) - _recursive_mod_name(s`.mod, vec) - vec`.push_char('.') + ~void p = s`.parent + /; if (p == NULL) + ~void m = s`.mod + /; if (m !== NULL) + _recursive_mod_name(m, vec) + /; if (vec`.count > 0) + vec`.push_char('.') + ;/ ;/ ;; else - _recursive_scope_name(s`.parent, vec) + _recursive_scope_name(p, vec) ;/ vec`.push_cstr(s`.name) ;/ @@ -43,9 +50,6 @@ struct Scope { self.vars.init(len v) ;/ - /; _compile_statements (~parse.Node up, int o) - ;/ - /; end _delete(self.name) @@ -54,6 +58,7 @@ struct Scope { v = self.vars.get(i) v`.end() ;/ + self.vars.end() ;/ # @@ -95,24 +100,29 @@ struct Scope { # - /; base_label [utils.Vector] + /; _base_label [utils.Vector] utils.Vector out out.init(1) - _recursive_mod_name(~self, ~out) + _recursive_scope_name(~self, ~out) return out ;/ + /; base_label [~uint8] + utils.Vector v = self._base_label() + return v.as_cstr() + ;/ + /; place_base_label - utils.Vector bl = self.base_label() - self.cb`.add_c(bl.as_cstr()) + ~uint8 bl = self.base_label() + self.cb`.add_c(bl) self.cb`.add_c(":\n\0") - bl.end() + _delete(bl) ;/ /; start_label [~uint8] - utils.Vector v = self.base_label() + utils.Vector v = self._base_label() v.push_cstr("#start\0") return v.as_cstr() ;/ @@ -125,7 +135,7 @@ struct Scope { ;/ /; rep_label [~uint8] - utils.Vector v = self.base_label() + utils.Vector v = self._base_label() v.push_cstr("#rep\0") return v.as_cstr() ;/ @@ -138,7 +148,7 @@ struct Scope { ;/ /; end_label [~uint8] - utils.Vector v = self.base_label() + utils.Vector v = self._base_label() v.push_cstr("#end\0") return v.as_cstr() ;/ -- cgit v1.2.3