diff options
| author | Kai Gunger <kgunger12@gmail.com> | 2025-12-25 02:31:36 -0500 |
|---|---|---|
| committer | Kai Gunger <kgunger12@gmail.com> | 2025-12-25 02:31:36 -0500 |
| commit | 0c7403f5894d46f513f8b57fb6fabbd568c9f5af (patch) | |
| tree | 24c17573f3838234558bd6076fcb812650608999 | |
| parent | 871bec579241882e8b05eb17cf177652e4f37781 (diff) | |
Add functions to member modules
| -rw-r--r-- | tnslc/compile/codegen.tnsl | 1 | ||||
| -rw-r--r-- | tnslc/compile/function.tnsl | 6 | ||||
| -rw-r--r-- | tnslc/compile/module.tnsl | 77 | ||||
| -rw-r--r-- | tnslc/test.tnsl | 21 |
4 files changed, 100 insertions, 5 deletions
diff --git a/tnslc/compile/codegen.tnsl b/tnslc/compile/codegen.tnsl index 2dfe9d5..ae67442 100644 --- a/tnslc/compile/codegen.tnsl +++ b/tnslc/compile/codegen.tnsl @@ -20,6 +20,7 @@ mod.init(~ast, ~buffer) _gen_prims(~mod) mod.update_children() + mod.collect_methods(~ast) # Compile code mod.compile(~buffer) diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl index 18d6c36..a30f383 100644 --- a/tnslc/compile/function.tnsl +++ b/tnslc/compile/function.tnsl @@ -5,12 +5,14 @@ struct Function { inputs, outputs, ~parse.Node _up, + bool m } /; method Function /; init (~parse.Node n) self.name = utils.strcpy(n`.data) self._up = n + self.m = false Var v self.inputs.init(len v) self.outputs.init(len v) @@ -38,9 +40,9 @@ struct Function { p.loc = reg reg++ ;; else - stack_up = stack_up + p.actual_size() p.loc = 0 - 1 p.offset = stack_up + stack_up = stack_up + p.actual_size() ;/ self.inputs.push(~p) ;/ @@ -134,6 +136,8 @@ struct Function { ;/ /; _end_func(~Scope scope, ~CompBuf cb) + cb`.add_c("\n\0") + scope`.place_end_label() cb`.add_c(" lea rsp, [rbp - 56]\n\0") cb`.add_c(" pop r15\n\0") cb`.add_c(" pop r14\n\0") diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl index e46c219..437edff 100644 --- a/tnslc/compile/module.tnsl +++ b/tnslc/compile/module.tnsl @@ -118,6 +118,83 @@ struct Module { ;/ ;/ + /; _method_id (~uint8 id) [utils.Vector] + utils.Vector tmp + tmp.init(1) + utils.Vector out + out.init(8) + + ~uint8 str + + /; loop (id` !== 0) [id++] + /; if (id` == '.') + str = tmp.as_cstr() + out.push(~str) + tmp.init(1) + ;; else + tmp.push(id) + ;/ + ;/ + + str = tmp.as_cstr() + ~uint8 last = utils.stradd("_#\0", str) + _delete(str) + out.push(~last) + + return out + ;/ + + /; _method_id_end(~utils.Vector v) + ~~uint8 str + /; loop (int i = 0; i < v`.count) [i++] + str = v`.get(i) + _delete(str`) + ;/ + v`.end() + ;/ + + /; _collect_methods(~parse.Node m) + ~parse.Node sub + /; loop (int i = 0; i < m`.sub.count) [i++] + sub = m`.sub.get(i) + + /; if (sub`._type == parse.NTYPE_FUNCTION) + Function f + f.init(sub) + f.m = true + self.funcs.push(~f) + ;/ + ;/ + ;/ + + /; collect_methods (~parse.Node mod) + ~parse.Node sub + /; loop (int i = 0; i < mod`.sub.count) [i++] + sub = mod`.sub.get(i) + /; if (sub`._type == parse.NTYPE_METHOD) + utils.Vector id = self._method_id(sub`.data) + ~Module m = self.find(SEARCH_SUB, ~id) + /; if (m !== NULL) + _printf("Found module for method\n\0") + m`._collect_methods(sub) + ;; else + _printf("Failed to find module for method\n\0") + ;/ + _method_id_end(~id) + ;; else if (sub`._type == parse.NTYPE_MODULE) + ~Module m = self._find_sub(sub`.data) + /; if (m !== NULL) + m`.collect_methods(sub) + ;/ + ;; else if (sub`._type == parse.NTYPE_EXPORT) + ~Module m = self._find_sub(sub`.data) + /; if (m !== NULL) + m`.collect_methods(sub) + ;/ + ;/ + ;/ + ;/ + /; _decl (~parse.Node decl) /; if (decl`.sub.count < 1) return diff --git a/tnslc/test.tnsl b/tnslc/test.tnsl index b36a0d2..59b337a 100644 --- a/tnslc/test.tnsl +++ b/tnslc/test.tnsl @@ -1,7 +1,20 @@ -/; main (int argc, ~~uint8 argv) [int] - asm "mov r10, rdi" - asm "mov r11, rsi" - return argc +struct str { + ~uint8 a +} + +/; method str + /; str_get() [~uint8] + return a + ;/ +;/ + +/; module moda + /; method str + /; str_set(~uint8 a) + self.a = a + ;/ + ;/ ;/ + |