diff options
author | CircleShift <kgunger12@gmail.com> | 2025-01-28 12:21:58 -0500 |
---|---|---|
committer | CircleShift <kgunger12@gmail.com> | 2025-01-28 12:21:58 -0500 |
commit | 9001965292494a952737c56d26b19221dc0c1904 (patch) | |
tree | 5f7f4ace3cda1779d938219ef096b8aea3942439 | |
parent | 61e1e5ce377719c8e9e437e5ba79ba06fc1de4ba (diff) |
Scope type initial funcs
-rw-r--r-- | tnslc/compile/compile.tnsl | 1 | ||||
-rw-r--r-- | tnslc/compile/generate.tnsl | 7 | ||||
-rw-r--r-- | tnslc/compile/outbuf.tnsl | 2 | ||||
-rw-r--r-- | tnslc/compile/scope.tnsl | 127 | ||||
-rw-r--r-- | tnslc/compile/type.tnsl | 16 |
5 files changed, 152 insertions, 1 deletions
diff --git a/tnslc/compile/compile.tnsl b/tnslc/compile/compile.tnsl index b980b62..34fc6b3 100644 --- a/tnslc/compile/compile.tnsl +++ b/tnslc/compile/compile.tnsl @@ -1,5 +1,6 @@ /; module compile :import "type.tnsl" + :import "scope.tnsl" :import "outbuf.tnsl" :import "generate.tnsl" ;/ diff --git a/tnslc/compile/generate.tnsl b/tnslc/compile/generate.tnsl index c05a304..fbbb229 100644 --- a/tnslc/compile/generate.tnsl +++ b/tnslc/compile/generate.tnsl @@ -1,3 +1,10 @@ + +struct Scope { + ~Scope parent, + ~uint8 name, + +} + /; generate_module (~OutBuf buf, ~Module mod) ;/ diff --git a/tnslc/compile/outbuf.tnsl b/tnslc/compile/outbuf.tnsl index 7394feb..4197fd9 100644 --- a/tnslc/compile/outbuf.tnsl +++ b/tnslc/compile/outbuf.tnsl @@ -13,7 +13,7 @@ struct OutBuf { ;/ /; write_to_file (~utils.File fout) - fout`.open() + fout`.create() fout`.write_cstr("bits 64\n\0") fout`.write_cstr(self.hsec.as_cstr()) fout`.write_cstr("\nsection .data\n\0") diff --git a/tnslc/compile/scope.tnsl b/tnslc/compile/scope.tnsl new file mode 100644 index 0000000..a87f7b9 --- /dev/null +++ b/tnslc/compile/scope.tnsl @@ -0,0 +1,127 @@ +struct Scope { + ~uint8 name, + ~Module current, + + utils.Vector + stack_vars, + reg_vars, + + ~Scope parent, + + int + next_const, + next_bool +} + +/; method Scope + /; init (~uint8 name, ~Module mod) + self.current = mod + self.name = name + + Variable v + self.stack_vars.init(len v) + self.reg_vars.init(len v) + + self.next_const = 0 + self.next_bool = 0 + ;/ + + /; end + _delete(self.name) + + ~Variable v + /; loop (int i = 0; i < self.stack_vars.count) [i++] + v = self.stack_vars.get(i) + v`.end() + ;/ + + /; loop (int i = 0; i < self.reg_vars.count) [i++] + v = self.reg_vars.get(i) + v`.end() + ;/ + ;/ + + /; _name_rec (~utils.Vector out) + /; if (self.parent !== NULL) + self.parent`._name_rec(out) + out`.push_cstr("#") + ;/ + + out`.push_cstr(self.name) + ;/ + + /; _base_label [utils.Vector] + utils.Vector out + out.init(1) + + ~uint8 mod_str = self.current.label_prefix() + out.push_cstr(mod_str) + _delete(mod_str) + + self._name_rec(~out) + + return out + ;/ + + /; label_start [~uint8] + utils.Vector base = self._base_label() + base.push_cstr("#start\0") + return base.as_cstr() + ;/ + + /; label_rep [~uint8] + utils.Vector base = self._base_label() + base.push_cstr("#rep\0") + return base.as_cstr() + ;/ + + /; label_end [~uint8] + utils.Vector base = self._base_label() + base.push_cstr("#end\0") + return base.as_cstr() + ;/ + + /; label_next_const [~uint8] + utils.Vector base = self._base_label() + base.push_cstr("#const\0") + + ~uint8 str = utils.int_to_str(self.next_const) + base.push_cstr(str) + self.next_const++ + _delete(str) + + return base.as_cstr() + ;/ + + /; label_bool [~uint8] + utils.Vector base = self._base_label() + base.push_cstr("#bool\0") + + ~uint8 str = utils.int_to_str(self.next_bool) + base.push_cstr(str) + self.next_bool++ + _delete(str) + + return base.as_cstr() + ;/ + + /; label_bool_adv + self.next_bool++ + ;/ + + /; subscope (~uint8 name) [Scope] + Scope out + + utils.Vector str + str.from_cstr(name) + _delete(name) + + out.init(str.as_cstr(), self.current) + out.parent = ~self + + return out + ;/ +;/ + + + diff --git a/tnslc/compile/type.tnsl b/tnslc/compile/type.tnsl index 28813ce..71e805a 100644 --- a/tnslc/compile/type.tnsl +++ b/tnslc/compile/type.tnsl @@ -208,6 +208,7 @@ uint8 MOD_FIND_ENM = 4 /; method Module /; init (~uint8 name) self.name = name + self.parent = NULL Module mtmp Variable vtmp Type ttmp @@ -220,6 +221,21 @@ uint8 MOD_FIND_ENM = 4 self.enums.init(len etmp) ;/ + /; _label_rec (~utils.Vector vec) + /; if (self.parent !== NULL) + self.parent`._label_rec(vec) + vec.push_cstr(".\0") + ;/ + vec.push_cstr(self.name) + ;/ + + /; label_prefix [~uint8] + utils.Vector vec + vec.init(1) + _label_rec(~vec) + return vec.as_cstr() + ;/ + /; update_children /; loop (int i = 0; i < self.sub.count) [i++] ~Module s = self.sub.get(i) |