From c06f8eb6a30a74a589537fc4d77be1c334edae72 Mon Sep 17 00:00:00 2001 From: Kai Gunger Date: Tue, 2 Dec 2025 23:19:53 -0500 Subject: [tnslc] resolve types for structs and functions --- tnslc/compile/codegen.tnsl | 2 +- tnslc/compile/function.tnsl | 66 ++++++++++++++++++++++++++++++++++++++++++ tnslc/compile/module.tnsl | 22 ++++++++------ tnslc/compile/struct.tnsl | 26 ++++++++++++----- tnslc/compile/var.tnsl | 50 ++++++++++++++++++-------------- tnslc/eee | 70 +++++++++++++++++++++++++++++++++++++++++++++ tnslc/test.tnsl | 6 ++-- 7 files changed, 202 insertions(+), 40 deletions(-) create mode 100644 tnslc/eee (limited to 'tnslc') diff --git a/tnslc/compile/codegen.tnsl b/tnslc/compile/codegen.tnsl index f057f83..867d00d 100644 --- a/tnslc/compile/codegen.tnsl +++ b/tnslc/compile/codegen.tnsl @@ -9,7 +9,7 @@ parse.Node ast = parse.generate_ast(fin) ast.update_children() - # parse.print_ast(~ast) + parse.print_ast(~ast) # Create output buffer CompBuf buffer diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl index b0a1d00..911b02c 100644 --- a/tnslc/compile/function.tnsl +++ b/tnslc/compile/function.tnsl @@ -16,7 +16,58 @@ struct Function { self.outputs.init(len v) ;/ + /; _resolve_dlist (~Module parent, ~parse.Node dl) + ~parse.Node tn = NULL + ~parse.Node n + /; loop (int i = 0; i < dl`.sub.count) [i++] + n = dl`.sub.get(i) + /; if (n`._type == parse.NTYPE_TYPE) + tn = n + ;; else if (n`._type == parse.NTYPE_ID) + /; if (tn == NULL) + _printf("Identifier declared in parameter list before any type was found!\n\0") + return + ;/ + Var p + p.init(tn, n) + p._resolve_type(parent) + self.inputs.push(~p) + ;/ + ;/ + ;/ + + /; _resolve_tlist (~Module parent, ~parse.Node tl) + ~parse.Node n + parse.Node dummy + dummy.data = "### OUTPUT ###\0" + /; loop (int i = 0; i < tl`.sub.count) [i++] + n = tl`.sub.get(i) + /; if (n`._type == parse.NTYPE_TYPE) + Var r + r.init(n, ~dummy) + r._resolve_type(parent) + self.outputs.push(~r) + ;/ + ;/ + ;/ + /; _resolve_type (~Module parent) + ~parse.Node _up = self._up + /; if (_up`.sub.count < 1) + return + ;/ + + ~parse.Node lst = _up`.sub.get(0) + /; if (lst`._type == parse.NTYPE_DLIST) + self._resolve_dlist(parent, lst) + /; if (_up`.sub.count > 1) + lst = _up`.sub.get(1) + ;/ + ;/ + + /; if (lst`._type == parse.NTYPE_TLIST) + self._resolve_tlist(parent, lst) + ;/ ;/ /; _compile (~Module parent, ~CompBuf cb) @@ -28,6 +79,21 @@ struct Function { _printf(self.name) _printf("\n\0") + _indent(idt) + _printf(" inputs:\n\0") + ~Var prtv + /; loop (int i = 0; i < self.inputs.count) [i++] + prtv = self.inputs.get(i) + prtv`._print(idt + 1) + ;/ + + _indent(idt) + _printf(" outputs:\n\0") + /; loop (int i = 0; i < self.outputs.count) [i++] + prtv = self.outputs.get(i) + prtv`._print(idt + 1) + ;/ + _indent(idt) _printf("}\n\0") ;/ diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl index 9d362c8..0cfaa37 100644 --- a/tnslc/compile/module.tnsl +++ b/tnslc/compile/module.tnsl @@ -171,8 +171,9 @@ struct Module { # /; find (int stype, ~utils.Vector key) [~void] - _printf("==== STARTING FIND WITH KEY ====\n\0") - _print_num("key parts %d\n\0", key`.count) + /; if (key`.count < 1) + return NULL + ;/ return self._find(stype, key, 0) ;/ @@ -194,19 +195,24 @@ struct Module { ;/ ;; else ~uint8 str = key`.get(key`.count - 1) + ~void out = NULL /; if (stype == SEARCH_VAR) - return self._find_var(str) + out = self._find_var(str) ;; else if (stype == SEARCH_STRUCT) - return self._find_struct(str) + out = self._find_struct(str) ;; else if (stype == SEARCH_FUNC) - return self._find_func(str) + out = self._find_func(str) ;; else if (stype == SEARCH_SUB) - return self._find_sub(str) + out = self._find_sub(str) ;/ - ;/ + /; if (out !== NULL) + return out + ;/ + ;/ + /; if (lvl == 0 && self.parent !== NULL) - return self.parent._find(stype, key, 0) + return self.parent`._find(stype, key, 0) ;/ return NULL diff --git a/tnslc/compile/struct.tnsl b/tnslc/compile/struct.tnsl index 4b1ccfb..7c18585 100644 --- a/tnslc/compile/struct.tnsl +++ b/tnslc/compile/struct.tnsl @@ -69,9 +69,7 @@ struct Struct { _printf("\n\0") _indent(idt) - _printf(" size:\0") - _print_num("%d\0", self.size) - _printf("\n\0") + _print_num(" size: %d\n\0", self.size) _indent(idt) _printf(" members:\n\0") @@ -79,15 +77,18 @@ struct Struct { ~Var v /; loop (int i = 0; i < self.members.count) [i++] v = self.members.get(i) - v._print(idt + 1) + v`._print(idt + 1) ;/ _indent(idt) _printf("}\n\0") ;/ - /; add_member(~Var v) - self.members.push(v) + /; add_member(~Module parent, ~parse.Node tn, ~parse.Node id) + Var v + v.init(tn, id) + v._resolve_type(parent) + self.members.push(~v) ;/ /; get_member(~uint8 name) [~Var] @@ -125,12 +126,16 @@ struct Struct { int total = 0 ~parse.Node up = self._dlist() - ~parse.Node n + ~parse.Node tn = NULL + ~parse.Node n = NULL int add_size = 0 /; loop (int i = 0; i < up`.sub.count) [i++] n = up`.sub.get(i) /; if (n`._type == parse.NTYPE_TYPE) + # Store for generating new variables + tn = n + # Find type, compute size, set add_size to type size ~Struct ft = self._find_type(parent, n) @@ -156,6 +161,13 @@ struct Struct { add_size = ft`.size ;; else if (n`._type == parse.NTYPE_ID) + /; if (tn == NULL) + _printf("ERROR: Unable to find type when trying to create member for struct '\0") + _printf(self.name) + _printf("\n\0") + return + ;/ + self.add_member(parent, tn, n) total = total + add_size ;/ ;/ diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl index 597a7ea..c26d227 100644 --- a/tnslc/compile/var.tnsl +++ b/tnslc/compile/var.tnsl @@ -114,30 +114,26 @@ struct Var { /; _print (int idt) _indent(idt) - _print("{ Var : \0") - _print(self.name) - _print("\n\0") + _printf("{ Var : \0") + _printf(self.name) + _printf("\n\0") - ~int32 ptr _indent(idt) - _print(" pointer chain: \0") - /; loop (int i = 0; i < self.ptrc.count) [i++] - ptr = self.ptrc.get(i) - _print_num("%d \0", ptr`) - ;/ - _print("\n\0") + _printf(" type: \0") + _printf(self._type`.name) + _printf("\n\0") _indent(idt) - _print(" computed type name: \0") - /; if (self._type == NULL) - _print("NULL\0") - ;; else - _print(self._type`.name) + _printf(" ptrc: \0") + ~int32 istr + /; loop (int i = 0; i < self.ptrc.count) [i++] + istr = self.ptrc.get(i) + _print_num("%d \0", istr`) ;/ - _print("\n\0") + _printf("\n\0") _indent(idt) - _print("}\n\0") + _printf("}\n\0") ;/ /; _arr_ptr(~parse.Node a) @@ -161,7 +157,7 @@ struct Var { _tn = self._tn # Pre-op pointer - /; loop (running == true) [idx++] + /; loop (running == true) /; if (idx !< _tn`.sub.count) running = false ;; else @@ -178,24 +174,32 @@ struct Var { running = false ;/ ;/ + + /; if (running == true) + idx++ + ;/ ;/ # After pre-ops comes id utils.Vector strv strv.init(8) running = true - /; loop (running == true) [idx++] + /; loop (running == true) /; if (idx !< _tn`.sub.count) running = false ;; else t = _tn`.sub.get(idx) /; if (t`._type == parse.NTYPE_ID) ~void str = t`.data - strv.push(~str) + strv.push(str) ;; else running = false ;/ ;/ + + /; if (running == true) + idx++ + ;/ ;/ # Main type resolution @@ -205,7 +209,7 @@ struct Var { # Post-op pointer running = true - /; loop (running == true) [idx++] + /; loop (running == true) /; if (idx !< _tn`.sub.count) running = false ;; else @@ -215,6 +219,10 @@ struct Var { self.ptrc.push(~ptr) ;/ ;/ + + /; if (running == true) + idx++ + ;/ ;/ ;/ diff --git a/tnslc/eee b/tnslc/eee new file mode 100644 index 0000000..150f6e9 --- /dev/null +++ b/tnslc/eee @@ -0,0 +1,70 @@ +==== STARTING FIND WITH KEY ==== +key parts 1 +==== STARTING FIND WITH KEY ==== +key parts 1 +{ Module : + { Struct : Box + size: 8 + members: + } + { Struct : bool + size: 1 + members: + } + { Struct : uint8 + size: 1 + members: + } + { Struct : int8 + size: 1 + members: + } + { Struct : uint16 + size: 2 + members: + } + { Struct : int16 + size: 2 + members: + } + { Struct : uint32 + size: 4 + members: + } + { Struct : int32 + size: 4 + members: + } + { Struct : float32 + size: 4 + members: + } + { Struct : uint64 + size: 8 + members: + } + { Struct : int64 + size: 8 + members: + } + { Struct : float64 + size: 8 + members: + } + { Struct : uint + size: 8 + members: + } + { Struct : int + size: 8 + members: + } + { Struct : float + size: 8 + members: + } + { Struct : void + size: 8 + members: + } +} diff --git a/tnslc/test.tnsl b/tnslc/test.tnsl index d65d7af..6163318 100644 --- a/tnslc/test.tnsl +++ b/tnslc/test.tnsl @@ -1,5 +1,5 @@ -struct Box { - int a -} +/; main (int argc, ~~uint8 argv) [int] + return 0 +;/ -- cgit v1.2.3