From 6fcd9b168c2667c3e757bce7f68377954917224a Mon Sep 17 00:00:00 2001 From: Kai Gunger Date: Mon, 16 Mar 2026 00:26:51 -0400 Subject: Various fixes --- tnslc/compile/module.tnsl | 16 ++++++++++++---- tnslc/compile/var.tnsl | 2 +- tnslc/parse/ast.tnsl | 16 ++++++++++++++++ tnslc/test.tnsl | 3 +-- tnslc/tnslc.tnsl | 5 ++++- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl index f20b84b..d3380f4 100644 --- a/tnslc/compile/module.tnsl +++ b/tnslc/compile/module.tnsl @@ -90,7 +90,7 @@ struct Module { /; loop (int i = 0; i < mod`.sub.count) [i++] sub = mod`.sub.get(i) - # TODO: Vars, Enums, Method blocks + # TODO: Enums /; if (sub`._type == parse.NTYPE_MODULE) Module m @@ -161,6 +161,7 @@ struct Module { /; if (sub`._type == parse.NTYPE_FUNCTION) Function f f.init(sub) + # Make function aware that it is in a method block f.m = true self.funcs.push(~f) ;/ @@ -185,11 +186,15 @@ struct Module { ~Module m = self._find_sub(sub`.data) /; if (m !== NULL) m`.collect_methods(sub) + ;; else + _printf("Failed to find module for method\n\0") ;/ ;; else if (sub`._type == parse.NTYPE_EXPORT) ~Module m = self._find_sub(sub`.data) /; if (m !== NULL) m`.collect_methods(sub) + ;; else + _printf("Failed to find module for method\n\0") ;/ ;/ ;/ @@ -209,7 +214,7 @@ struct Module { ~parse.Node id /; loop (int i = 1; i < decl`.sub.count) [i++] id = decl`.sub.get(i) - /; if (tn`._type == parse.NTYPE_ID) + /; if (id`._type == parse.NTYPE_ID) # Add a new variable to the list Var v v.init(tn, id) @@ -221,8 +226,8 @@ struct Module { /; compile (~CompBuf cb) # First, since all the types are in place, we need to size all of them. self._size_structs() - - # Finally, write all functions to the code section + + # Finally, write all functions to the code section and globals to the data section self._compile(cb) ;/ @@ -255,18 +260,21 @@ struct Module { ;/ /; _compile (~CompBuf cb) + # Static compile all global variables ~Var v /; loop (int i = 0; i < self.vars.count) [i++] v = self.vars.get(i) v`._static_compile(~self, cb) ;/ + # Write function to code section ~Function f /; loop (int i = 0; i < self.funcs.count) [i++] f = self.funcs.get(i) f`._compile(~self, cb) ;/ + # Recurse ~Module m /; loop (int i = 0; i < self.subs.count) [i++] m = self.subs.get(i) diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl index a47e182..e5608bb 100644 --- a/tnslc/compile/var.tnsl +++ b/tnslc/compile/var.tnsl @@ -295,7 +295,7 @@ struct Var { ;/ /; _static_compile (~Module parent, ~CompBuf buf) - # TODO: + # TODO: everything ;/ /; ptr [int32] diff --git a/tnslc/parse/ast.tnsl b/tnslc/parse/ast.tnsl index f9ad1f9..24773f2 100644 --- a/tnslc/parse/ast.tnsl +++ b/tnslc/parse/ast.tnsl @@ -204,6 +204,20 @@ int errors_shown = 0 return true ;/ +# Canonical operation order from strongest (lowest number prec) to weakest (highest number prec) +# Parenthesis or function call done before any of these +# 0: deref - (`) +# 1: member - (.) +# 2: refer - (~) +# 3: inc/dec - (++ --) +# 4: bit shift - (<< >>) +# 5: len prefix op - (len) +# 6: mul/div - (* / %) +# 7: add/sub - (+ -) +# 8: bitwise ops - (! & | ^ !& !| !^) +# 9: comparison - (== < > <== >== !== !< !>) +# 10: boolean - (&& || ^^ !&& !|| !^^) +# 11: assignment - variants of = /; _op_order(~uint8 op) [int] int l = utils.strlen(op) @@ -255,6 +269,8 @@ int errors_shown = 0 return 5 ;; else if (op{1} == '=') return 9 + ;; else if (op{2} == '=') + return 11 ;/ return 10 ;/ diff --git a/tnslc/test.tnsl b/tnslc/test.tnsl index 94c36f2..bedadcb 100644 --- a/tnslc/test.tnsl +++ b/tnslc/test.tnsl @@ -1,6 +1,5 @@ /; main [int] - int a = 0 - return a + return 0 ;/ diff --git a/tnslc/tnslc.tnsl b/tnslc/tnslc.tnsl index d49bcc9..6176026 100644 --- a/tnslc/tnslc.tnsl +++ b/tnslc/tnslc.tnsl @@ -56,8 +56,11 @@ options: ;/ fout.init(argv{arg_counter}) ;; else - fout.init(DEFAULT_FOUT) + _printf(USAGE) + return 1 ;/ + ;; else + fout.init(DEFAULT_FOUT) ;/ /; if (arg_counter !< argc) -- cgit v1.2.3