From aef2a1e4fae812220b29ec939be239cee5225b1a Mon Sep 17 00:00:00 2001 From: Kai Gunger Date: Tue, 30 Dec 2025 23:16:01 -0500 Subject: finding parent scopes --- tnslc/compile/function.tnsl | 26 ++++++++++++++++++++++++++ tnslc/compile/scope.tnsl | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) (limited to 'tnslc/compile') diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl index 5db1bd3..1f0fb3f 100644 --- a/tnslc/compile/function.tnsl +++ b/tnslc/compile/function.tnsl @@ -240,7 +240,33 @@ struct Function { /; if (utils.strcmp(n`.data, "return\0") == true) # Compute value and return self._compile_value(s, n) + ;; else if (utils.strcmp(n`.data, "continue\0") == true) + ~Scope lp = s`.closest_loop() + /; if (lp == NULL) + return + ;/ + + ~uint8 lab = lp`.rep_label() + s`.cb`.add_c(" jmp \0") + s`.cb`.add_c(lab) + s`.cb`.add_c("\n\0") + ;; else if (utils.strcmp(n`.data, "break\0") == true) + ~Scope br = s`.closest_break() + /; if (br == NULL) + return + ;/ + + ~uint8 lab = br`.rep_label() + s`.cb`.add_c(" jmp \0") + s`.cb`.add_c(lab) + s`.cb`.add_c("\n\0") ;/ + + _printf("COMPILER ERROR: The following was detected as flow control, but we do not handle it: '\0") + _printf(n`.data) + _printf("'\n\0") + _printf("COMPILER ERROR: this is likely a bug with the compiler, please report it along with the\n\0") + _printf("COMPILER ERROR: code snippit which caused the bug!\n\0") ;/ # Should handle computing a value, delegate to other funcs when needed diff --git a/tnslc/compile/scope.tnsl b/tnslc/compile/scope.tnsl index 900d626..b7df075 100644 --- a/tnslc/compile/scope.tnsl +++ b/tnslc/compile/scope.tnsl @@ -241,6 +241,51 @@ struct Scope { return out ;/ + # Get closest breakable scope + + /; _closest_break [~Scope] + /; if (utils.ends_with(self.name, "#wrap\0")) + return ~self + ;; else if (utils.ends_with(self.name, "#loop\0")) + return ~self + ;/ + + /; if (self.parent == NULL) + return NULL + ;/ + + return self.parent`.closest_loop() + ;/ + + /; closest_break [~Scope] + /; if (self.parent == NULL) + return NULL + ;/ + + ~uint8 pname = self.parent`.name + /; if (utils.ends_with(pname, "#wrap\0")) + ~Scope par2 = self.parent`.parent + /; if (par2 !== NULL) + return par2`._closest_break() + ;/ + ;/ + + return self._closest_break() + ;/ + + # Get closest loop + /; closest_loop [~Scope] + /; if (utils.ends_with(self.name, "#loop\0")) + return ~self + ;/ + + /; if (self.parent == NULL) + return NULL + ;/ + + return self.parent`.closest_loop() + ;/ + # # Label generation -- cgit v1.2.3