diff options
Diffstat (limited to 'tnslc/compile')
-rw-r--r-- | tnslc/compile/codegen.tnsl | 4 | ||||
-rw-r--r-- | tnslc/compile/compbuf.tnsl | 45 | ||||
-rw-r--r-- | tnslc/compile/compile.tnsl | 10 | ||||
-rw-r--r-- | tnslc/compile/function.tnsl | 36 | ||||
-rw-r--r-- | tnslc/compile/generate.tnsl | 22 | ||||
-rw-r--r-- | tnslc/compile/module.tnsl | 68 | ||||
-rw-r--r-- | tnslc/compile/outbuf.tnsl | 31 | ||||
-rw-r--r-- | tnslc/compile/scope.tnsl | 14 | ||||
-rw-r--r-- | tnslc/compile/struct.tnsl | 46 | ||||
-rw-r--r-- | tnslc/compile/type.tnsl | 588 | ||||
-rw-r--r-- | tnslc/compile/var.tnsl | 33 |
11 files changed, 253 insertions, 644 deletions
diff --git a/tnslc/compile/codegen.tnsl b/tnslc/compile/codegen.tnsl new file mode 100644 index 0000000..165fe12 --- /dev/null +++ b/tnslc/compile/codegen.tnsl @@ -0,0 +1,4 @@ + +/; generate (~utils.File fin, fout) +;/ + diff --git a/tnslc/compile/compbuf.tnsl b/tnslc/compile/compbuf.tnsl new file mode 100644 index 0000000..b4ce261 --- /dev/null +++ b/tnslc/compile/compbuf.tnsl @@ -0,0 +1,45 @@ + +struct CompBuf { + utils.Vector + sec_head, + sec_data, + sec_code +} + +/; method CompBuf + /; init + self.sec_head.init(1) + self.sec_data.init(1) + self.sec_code.init(1) + ;/ + + /; add_h(~uint8 text) + self.sec_head.push_cstr(text) + ;/ + + /; add_d(~uint8 text) + self.sec_data.push_cstr(text) + ;/ + + /; add_c(~uint8 text) + self.sec_code.push_cstr(text) + ;/ + + /; write_to(~utils.File fout) + fout`.write_cstr("BITS 64\0\n") + fout`.write_cstr(self.sec_head.as_cstr()) + + fout`.write_cstr("\nsection .data\0\n\n") + fout`.write_cstr(self.sec_data.as_cstr()) + + fout`.write_cstr("\nsection .text\0\n\n") + fout`.write_cstr(self.sec_code.as_cstr()) + ;/ + + /; end + self.sec_head.end() + self.sec_data.end() + self.sec_code.end() + ;/ +;/ + diff --git a/tnslc/compile/compile.tnsl b/tnslc/compile/compile.tnsl index b980b62..338e28d 100644 --- a/tnslc/compile/compile.tnsl +++ b/tnslc/compile/compile.tnsl @@ -1,5 +1,9 @@ /; module compile - :import "type.tnsl" - :import "outbuf.tnsl" - :import "generate.tnsl" + :import "compbuf.tnsl" + :import "struct.tnsl" + :import "var.tnsl" + :import "function.tnsl" + :import "module.tnsl" + :import "codegen.tnsl" ;/ + diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl new file mode 100644 index 0000000..cf42db1 --- /dev/null +++ b/tnslc/compile/function.tnsl @@ -0,0 +1,36 @@ + +struct Function { + ~uint8 name, + utils.Vector + inputs, + outputs, + ~parse.Node body +} + +/; method Function + /; init (~uint8 name) + self.name = name + Var v + self.inputs.init(len v) + self.outputs.init(len v) + ;/ + + /; end + _delete(self.name) + self.body`.end() + + ~Var v + /; loop (int i = 0; i < self.inputs.count) [i++] + v = self.inputs.get(i) + v`.end() + ;/ + self.inputs.end() + + /; loop (int i = 0; i < self.outputs.count) [i++] + v = self.outputs.get(i) + v`.end() + ;/ + self.outputs.end() + ;/ +;/ + diff --git a/tnslc/compile/generate.tnsl b/tnslc/compile/generate.tnsl deleted file mode 100644 index c05a304..0000000 --- a/tnslc/compile/generate.tnsl +++ /dev/null @@ -1,22 +0,0 @@ -/; generate_module (~OutBuf buf, ~Module mod) -;/ - -/; generate (~utils.File fin, fout) - parse.Node n = parse.generate_ast(fin) - n.update_children() - parse.print_ast(~n) - - OutBuf buf - buf.init() - - Module mod = transform_tree(~n, ~buf) - mod.print(0) - - - generate_module(~buf, ~mod) - buf.write_to_file(fout) - - mod.end() - buf.end() - n.end() -;/ diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl new file mode 100644 index 0000000..6dbef6c --- /dev/null +++ b/tnslc/compile/module.tnsl @@ -0,0 +1,68 @@ + +struct Module { + # Text name of module + ~uint8 name, + + # Various contained elements + utils.Vector + vars, + structs, + funcs, + subs, + + # Whether we export or not + bool e +} + +/; method Module + + /; init (~uint8 name, bool exp) + Var v + Struct s + Function f + Module m + + self.vars.init(len v) + self.structs.init(len s) + self.funcs.init(len f) + self.subs.init(len m) + + self.name = name + self.e = exp + ;/ + + /; end + _delete(self.name) + + ~Var v + /; loop (int i = 0; i < self.vars.count) [i++] + v = self.vars.get(i) + v`.end() + ;/ + self.vars.end() + + ~Struct s + /; loop (int i = 0; i < self.structs.count) [i++] + s = self.structs.get(i) + s`.end() + ;/ + self.structs.end() + + ~Function f + /; loop (int i = 0; i < self.funcs.count) [i++] + f = self.funcs.get(i) + f`.end() + ;/ + self.funcs.end() + + ~Module m + /; loop (int i = 0; i < self.subs.count) [i++] + m = self.subs.get(i) + m`.end() + ;/ + self.subs.end() + + ;/ + +;/ + diff --git a/tnslc/compile/outbuf.tnsl b/tnslc/compile/outbuf.tnsl deleted file mode 100644 index 7394feb..0000000 --- a/tnslc/compile/outbuf.tnsl +++ /dev/null @@ -1,31 +0,0 @@ -struct OutBuf { - utils.Vector - hsec, - dsec, - csec -} - -/; method OutBuf - /; init - self.hsec.init(1) - self.dsec.init(1) - self.csec.init(1) - ;/ - - /; write_to_file (~utils.File fout) - fout`.open() - fout`.write_cstr("bits 64\n\0") - fout`.write_cstr(self.hsec.as_cstr()) - fout`.write_cstr("\nsection .data\n\0") - fout`.write_cstr(self.dsec.as_cstr()) - fout`.write_cstr("\nsection .text\n\0") - fout`.write_cstr(self.csec.as_cstr()) - fout`.close() - ;/ - - /; end - self.hsec.end() - self.dsec.end() - self.csec.end() - ;/ -;/ diff --git a/tnslc/compile/scope.tnsl b/tnslc/compile/scope.tnsl new file mode 100644 index 0000000..f95d0fd --- /dev/null +++ b/tnslc/compile/scope.tnsl @@ -0,0 +1,14 @@ + +struct Scope { + ~uint8 name, + utils.Vector + reg_vars, + stack_vars, + int + c_const, + c_sub +} + +/; method Scope +;/ + diff --git a/tnslc/compile/struct.tnsl b/tnslc/compile/struct.tnsl new file mode 100644 index 0000000..d175aef --- /dev/null +++ b/tnslc/compile/struct.tnsl @@ -0,0 +1,46 @@ + +struct Struct { + ~uint8 name, + ~Module methods, + utils.Vector members, + int size +} + +/; method Struct + + /; init (~uint8 name) + self.name = name + Var v + self.members.init(len v) + ;/ + + /; add_member(~Var v) + self.members.push(v) + ;/ + + /; get_member(~uint8 name) [~Var] + ~Var out = NULL + + ~Var v + /; loop (int i = 0; i < self.members.count) [i++] + v = self.members.get(i) + /; if (utils.strcmp(v`.name, name) == true) + return v + ;/ + ;/ + + return out + ;/ + + /; end + _delete(self.name) + ~Var v + /; loop (int i = 0; i < self.members.count) [i++] + v = self.members.get(i) + v`.end() + ;/ + self.members.end() + ;/ + +;/ + diff --git a/tnslc/compile/type.tnsl b/tnslc/compile/type.tnsl deleted file mode 100644 index 28813ce..0000000 --- a/tnslc/compile/type.tnsl +++ /dev/null @@ -1,588 +0,0 @@ -/; _indent (int indent) - /; loop (int i = 0; i < indent) [i++] - _printf(" \0") - ;/ -;/ - -struct Variable { - ~uint8 name, - ~Type _type, - utils.Vector ptr -} - -/; method Variable - /; init (~uint8 name) - self.name = name - self.ptr.init(8) - ;/ - - /; add_ptr(uint ptp) - self.ptr.push(~ptp) - ;/ - - /; get_ptr [uint] - /; if (self.ptr.count == 0) - return 0 - ;/ - ~uint p = self.ptr.get(self.ptr.count - 1) - return p` - ;/ - - /; pop_ptr [uint] - /; if (self.ptr.count == 0) - return 0 - ;/ - ~uint p = self.ptr.get(self.ptr.count - 1) - uint out = p` - self.ptr.pop() - return out - ;/ - - /; end - _delete(self.name) - self.ptr.end() - ;/ - - /; _prnt(int indent) - _printf(self.name) - - # todo: types and such - - _indent(indent) - _printf("}\n\0") - ;/ - - /; print (int indent) - _indent(indent) - _printf("{ Variable: \0") - _prnt(indent) - ;/ - - /; print_mem (int indent) - _indent(indent) - _printf("{ Member: \0") - _prnt(indent) - ;/ -;/ - -struct Type { - ~uint8 name, - uint size, - utils.Vector vars, - ~Module methods, -} - -/; method Type - /; init(~uint8 name) - self.name = name - Variable tmp - self.vars.init(len tmp) - ;/ - - /; add_var (~Variable v) - self.vars.push(v) - ;/ - - /; end - _delete(self.name) - /; loop (int i = 0; i < self.vars.count) [i++] - ~Variable v = self.vars.get(i) - v`.end() - ;/ - self.vars.end() - ;/ - - /; print (int indent) - _indent(indent) - _printf("{ Type: \0") - _printf(self.name) - _print_num(" { size: %d }\n\0", self.size) - - /; loop (int i = 0; i < self.vars.count) [i++] - ~Variable v = self.vars.get(i) - v`.print_mem(indent + 1) - ;/ - - _indent(indent) - _printf("}\n\0") - ;/ -;/ - -struct Function { - ~uint8 - name, - ~parse.Node - body, - utils.Vector - inputs, - outputs -} - -/; method Function - /; init (~uint8 name) - self.name = name - Variable vtmp - ~uint i - self.inputs.init(len vtmp) - self.outputs.init(len i) - ;/ - - /; add_input (~Variable v) - self.inputs.push(v) - ;/ - - /; add_output (~Type t) - self.outputs.push(~t) - ;/ - - /; end - _delete(self.name) - - /; loop (int i = 0; i < self.inputs.count) [i++] - ~Variable v = self.inputs.get(i) - v`.end() - ;/ - self.inputs.end() - - self.outputs.end() - ;/ -;/ - -struct Enum { - ~uint8 name, - ~Type _type, - utils.Vector vals -} - -/; method Enum - /; init (~uint8 name) - self.name = name - Variable vtmp - self.vals.init(len vtmp) - ;/ - - /; end - _delete(self.name) - /; loop (int i = 0; i < self.vals.count) [i++] - ~Variable v = self.vals.get(i) - v`.end() - ;/ - self.vals.end() - ;/ - - /; print (int indent) - _indent(indent) - _printf("{ Enum: \0") - _printf(self.name) - _printf("\n\0") - - /; loop (int i = 0; i < self.vals.count) [i++] - ~Variable v = self.vals.get(i) - v`.print(indent + 1) - ;/ - - _indent(indent) - _printf("}\n\0") - ;/ -;/ - -struct Module { - ~uint8 name, - ~Module parent, - bool exp, mth, - utils.Vector - sub, - vars, - types, - funcs, - enums - -} - -uint8 MOD_FIND_SUB = 0 -uint8 MOD_FIND_VAR = 1 -uint8 MOD_FIND_TYP = 2 -uint8 MOD_FIND_FUN = 3 -uint8 MOD_FIND_ENM = 4 - -/; method Module - /; init (~uint8 name) - self.name = name - Module mtmp - Variable vtmp - Type ttmp - Function ftmp - Enum etmp - self.sub.init(len mtmp) - self.vars.init(len vtmp) - self.types.init(len ttmp) - self.funcs.init(len ftmp) - self.enums.init(len etmp) - ;/ - - /; update_children - /; loop (int i = 0; i < self.sub.count) [i++] - ~Module s = self.sub.get(i) - s`.parent = ~self - ;/ - ;/ - - /; add_sub(~Module m) [~Module] - self.sub.push(m) - /; loop (int i = 0; i < self.sub.count) [i++] - ~Module s = self.sub.get(i) - s`.update_children() - ;/ - ~Module out = self.sub.get(self.sub.count - 1) - out`.parent = ~self - return out - ;/ - - /; add_var (~Variable v) - self.vars.push(v) - ;/ - - /; add_type (~Type t) - self.types.push(t) - ;/ - - /; add_funcs (~Function f) - self.funcs.push(f) - ;/ - - /; add_enum (~Enum e) - self.enums.push(e) - ;/ - - /; _find_rec (utils.Artifact pth, uint8 typ, int sub) [~void] - /; if (sub !< pth.count) - return NULL - ;; else if ((sub + 1) < pth.count) - /; loop (int i = 0; i < self.sub.count) [i++] - ~Module m = self.sub.get(i) - /; if (utils.strcmp(pth.get(sub), m`.name) == true && m`.mth == false) - return _find_rec(pth, typ, sub + 1) - ;/ - ;/ - ;; else - /; if (typ == MOD_FIND_SUB) - /; loop (int i = 0; i < self.sub.count) [i++] - ~Module m = self.sub.get(i) - /; if (utils.strcmp(pth.get(sub), m`.name) == true && m`.mth == false) - return self.sub.get(i) - ;/ - ;/ - ;; else if (typ == MOD_FIND_VAR) - /; loop (int i = 0; i < self.vars.count) [i++] - ~Variable v = self.vars.get(i) - /; if (utils.strcmp(pth.get(sub), v`.name) == true) - return self.vars.get(i) - ;/ - ;/ - ;; else if (typ == MOD_FIND_TYP) - /; loop (int i = 0; i < self.types.count) [i++] - ~Type t = self.types.get(i) - /; if (utils.strcmp(pth.get(sub), t`.name) == true) - return self.types.get(i) - ;/ - ;/ - ;; else if (typ == MOD_FIND_FUN) - /; loop (int i = 0; i < self.funcs.count) [i++] - ~Function f = self.funcs.get(i) - /; if (utils.strcmp(pth.get(sub), f`.name) == true) - return self.funcs.get(i) - ;/ - ;/ - ;; else if (typ == MOD_FIND_ENM) - /; loop (int i = 0; i < self.enums.count) [i++] - ~Enum e = self.enums.get(i) - /; if (utils.strcmp(pth.get(sub), e`.name) == true) - return self.enums.get(i) - ;/ - ;/ - ;/ - ;/ - - /; if (self.parent == NULL || sub !== 0) - return NULL - ;/ - - return self.parent._find_rec(pth, typ, 0) - ;/ - - /; find (utils.Artifact pth, uint8 typ) [~void] - return _find_rec(pth, typ, 0) - ;/ - - /; end - _delete(self.name) - - /; loop (int i = 0; i < self.sub.count) [i++] - ~Module m = self.sub.get(i) - m`.end() - ;/ - self.sub.end() - - /; loop (int i = 0; i < self.vars.count) [i++] - ~Variable v = self.vars.get(i) - v`.end() - ;/ - self.vars.end() - - /; loop (int i = 0; i < self.types.count) [i++] - ~Type t = self.types.get(i) - t`.end() - ;/ - self.types.end() - - /; loop (int i = 0; i < self.funcs.count) [i++] - ~Function f = self.funcs.get(i) - f`.end() - ;/ - self.funcs.end() - - /; loop (int i = 0; i < self.enums.count) [i++] - ~Enum e = self.enums.get(i) - e`.end() - ;/ - self.enums.end() - ;/ - - /; print (int indent) - _indent(indent) - /; if (self.mth == true) - _printf("{ Method: \0") - ;; else - _printf("{ Module: \0") - ;/ - _printf(self.name) - _printf("\n\0") - - - /; loop (int i = 0; i < self.sub.count) [i++] - ~Module s = self.sub.get(i) - s`.print(indent + 1) - ;/ - - /; loop (int i = 0; i < self.types.count) [i++] - ~Type s = self.types.get(i) - s`.print(indent + 1) - ;/ - - /; loop (int i = 0; i < self.enums.count) [i++] - ~Enum s = self.enums.get(i) - s`.print(indent + 1) - ;/ - - _indent(indent) - _printf("}\n\0") - ;/ -;/ - -{}~uint8 GEN_VAR_NAMES = { "int\0", "int8\0", "int16\0", "int32\0", "int64\0", "uint\0", "uint8\0", "uint16\0", "uint32\0", "uint64\0", "float\0", "float32\0", "float64\0", "vect\0", "bool\0", "void\0" } - -{}uint GEN_VAR_SIZES = { 8, 1, 2, 4, 8, 8, 1, 2, 4, 8, 8, 4, 8, 0, 1, 8} - -/; find_type(utils.Artifact a, ~parse.Node n) [~parse.Node] - return NULL -;/ - -/; transform_struct(~parse.Node n, ~Module m) -;/ - -/; transform_enum(~parse.Node n, ~OutBuf b, ~Module m) -;/ - -/; _tfn_mod_loop (~parse.Node n, ~OutBuf b, ~Module m) - /; loop (int i = 0; i < n`.sub.count) [i++] - ~parse.Node s = n`.sub.get(i) - /; if (s`._type == parse.NTYPE_MODULE || s`._type == parse.NTYPE_EXPORT) - transform_module(s, b, m) - ;; else if (s`._type == parse.NTYPE_STRUCT) - transform_struct(s, m) - ;; else if (s`._type == parse.NTYPE_ENUM) - transform_enum(s, b, m) - ;/ - ;/ -;/ - -/; transform_module (~parse.Node n, ~OutBuf b, ~Module parent) - ~Module s = NULL - - /; loop (int i = 0; i < parent`.sub.count) [i++] - ~Module tmp = parent`.sub.get(i) - /; if (utils.strcmp(n`.data, tmp`.name) == true) - s = tmp - ;/ - ;/ - - ~int cmp = s - /; if (cmp == NULL) - Module out - out.init(utils.strcpy(n`.data)) - - /; if (n`._type == parse.NTYPE_EXPORT) - out.exp = true - ;; else - out.exp = false - ;/ - - s = parent`.add_sub(~out) - ;/ - - _tfn_mod_loop(n, b, s) -;/ - -# -# GEN DEFAULT TYPES -# - -/; _tfn_gen_default_types (~Module m) - Module d - d.init(utils.strcpy("#\0")) - ~Module dp = m`.add_sub(~d) - /; loop (int i = 0; i < len GEN_VAR_NAMES) [i++] - Type t - t.init(utils.strcpy(GEN_VAR_NAMES{i})) - t.size = GEN_VAR_SIZES{i} - t.methods = dp - m`.add_type(~t) - ;/ -;/ - -# -# GEN TYPES & ENUMS FIRST PASS -# - -/; _tfn_gen_types_mod(~parse.Node n, ~Module m) - ~Module s = NULL - - /; loop (int i = 0; i < m`.sub.count) [i++] - ~Module tmp = m`.sub.get(i) - /; if (utils.strcmp(n`.data, tmp`.name) == true) - s = tmp - ;/ - ;/ - - ~int cmp = s - /; if (cmp == NULL) - Module out - out.init(utils.strcpy(n`.data)) - - /; if (n`._type == parse.NTYPE_EXPORT) - out.exp = true - ;; else - out.exp = false - ;/ - - s = m`.add_sub(~out) - ;/ - - _tfn_gen_types(n, s) -;/ - -/; _tfn_gen_types (~parse.Node n, ~Module m) - /; loop (int i = 0; i < n`.sub.count) [i++] - ~parse.Node s = n`.sub.get(i) - /; if (s`._type == parse.NTYPE_MODULE || s`._type == parse.NTYPE_EXPORT) - _tfn_gen_types_mod(s, m) - ;; else if (s`._type == parse.NTYPE_STRUCT) - Module mth - mth.init(utils.stradd("_#\0", s`.data)) - ~Module mp = m`.add_sub(~mth) - - Type t - t.size = 0 - t.init(utils.strcpy(s`.data)) - t.methods = mp - m`.add_type(~t) - ;; else if (s`._type == parse.NTYPE_ENUM) - Enum e - e.init(utils.strcpy(s`.data)) - m`.add_enum(~e) - ;/ - ;/ -;/ - -# Main transform functions - -/; _td_rec(~parse.Node n, ~utils.Artifact a) - /; if (n`.eq(".\0") == true && n`.sub.count > 1) - ~parse.Node tmp = n`.sub.get(0) - _td_rec(tmp, a) - tmp = n`.sub.get(1) - _td_rec(tmp, a) - ;; else if (n`._type == parse.NTYPE_ID) - a`.push(utils.strcpy(n`.data)) - ;/ -;/ - -/; transform_dot(~parse.Node n) [utils.Artifact] - utils.Artifact out - out.init() - - _td_rec(n, ~out) - - return out -;/ - -/; art_from_type(~parse.Node n) [utils.Artifact] - utils.Artifact out - out.init() - - /; if (n`._type !== parse.NTYPE_TYPE) - return out - ;/ - - /; loop (int i = 0; i < n`.sub.count) [i++] - ~parse.Node s = n`.sub.get(i) - /; if (s`._type == parse.NTYPE_ID) - out.push(utils.strcpy(s`.data)) - ;/ - ;/ - - return out -;/ - -/; vec_from_type(~parse.Node n) [utils.Vector] - utils.Vector out - out.init(8) - - /; if (n`._type !== parse.NTYPE_TYPE) - return out - ;/ - - int nm - /; loop (int i = 0; i < n`.sub.count) [i++] - ~parse.Node s = n`.sub.get(i) - /; if (s`._type == parse.NTYPE_PRE_OP) - /; if (s`.eq("~\0") == true) - nm = 0 - 1 - ;; else if (s`.eq("{\0") == true) - # TODO: WHATEVER - nm = 0 - 2 - ;/ - out.push(~nm) - ;; else if (s`._type == parse.NTYPE_POST_OP) - /; if (s`.eq("`\0") == true) - nm = 0 - out.push(~nm) - ;/ - ;/ - ;/ - - return out -;/ - -/; transform_tree (~parse.Node n, ~OutBuf buf) [Module] - Module out - out.init(utils.strcpy(n`.data)) - out.exp = true - - _tfn_gen_default_types(~out) - _tfn_gen_types(n, ~out) - out.update_children() - _tfn_mod_loop(n, buf, ~out) - - return out -;/ diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl new file mode 100644 index 0000000..9526fc6 --- /dev/null +++ b/tnslc/compile/var.tnsl @@ -0,0 +1,33 @@ + +struct Var { + ~uint8 name, + ~Struct _type, + utils.Vector ptrc, + int loc +} + +/; method Var + /; init (~uint8 name) + self.name = name + self.ptrc.init(4) + ;/ + + /; ptr [int32] + ~int32 i + i = self.ptrc.get(self.ptrc.count - 1) + return i` + ;/ + + /; ptr_push (int32 p) + self.ptrc.push(~p) + ;/ + + /; ptr_pop + self.ptrc.pop() + ;/ + + /; end + _delete(self.name) + self.ptrc.end() + ;/ +;/ |