diff options
| -rw-r--r-- | tnslc/compile/codegen.tnsl | 58 | ||||
| -rw-r--r-- | tnslc/compile/function.tnsl | 70 | ||||
| -rw-r--r-- | tnslc/compile/module.tnsl | 75 | ||||
| -rw-r--r-- | tnslc/compile/struct.tnsl | 32 | ||||
| -rw-r--r-- | tnslc/compile/var.tnsl | 125 | ||||
| -rw-r--r-- | tnslc/test.tnsl | 3 |
6 files changed, 289 insertions, 74 deletions
diff --git a/tnslc/compile/codegen.tnsl b/tnslc/compile/codegen.tnsl index 48545f3..f057f83 100644 --- a/tnslc/compile/codegen.tnsl +++ b/tnslc/compile/codegen.tnsl @@ -1,9 +1,15 @@ +/; _indent (int idt) + /; loop (int i = 0; i < idt) [i++] + _printf(" \0") + ;/ +;/ /; generate (~utils.File fin, fout) # Parse files into AST parse.Node ast = parse.generate_ast(fin) ast.update_children() - parse.print_ast(~ast) + + # parse.print_ast(~ast) # Create output buffer CompBuf buffer @@ -18,6 +24,8 @@ _gen_prims(~mod) mod.compile(~buffer) + mod.print() + # Write assembly to output file fout.create() buffer.write_to(fout) @@ -29,57 +37,57 @@ ast.end() ;/ -/; _gen_prims (~Module m) +/; _gen_prim(int size, ~uint8 id) [Struct] Var t - Struct s - s.members.init(len t) + s.size = size s.methods = NULL + s.members.init(len t) + s.name = utils.strcpy(id) s._up = NULL + return s +;/ + +/; _gen_prims (~Module m) - ~uint8 str + Struct s # One byte prims - s.size = 1 - s.name = utils.strcpy("bool\0") + s = _gen_prim(1, "bool\0") m`.structs.push(~s) - s.name = utils.strcpy("uint8\0") + s = _gen_prim(1, "uint8\0") m`.structs.push(~s) - s.name = utils.strcpy("int8\0") + s = _gen_prim(1, "int8\0") m`.structs.push(~s) # Two byte prims - s.size = 2 - s.name = utils.strcpy("uint16\0") + s = _gen_prim(2, "uint16\0") m`.structs.push(~s) - s.name = utils.strcpy("int16\0") + s = _gen_prim(2, "int16\0") m`.structs.push(~s) # Four byte prims - s.size = 4 - s.name = utils.strcpy("uint32\0") + s = _gen_prim(4, "uint32\0") m`.structs.push(~s) - s.name = utils.strcpy("int32\0") + s = _gen_prim(4, "int32\0") m`.structs.push(~s) - s.name = utils.strcpy("float32\0") + s = _gen_prim(4, "float32\0") m`.structs.push(~s) # Eight byte prims - s.size = 8 - s.name = utils.strcpy("uint64\0") + s = _gen_prim(8, "uint64\0") m`.structs.push(~s) - s.name = utils.strcpy("int64\0") + s = _gen_prim(8, "int64\0") m`.structs.push(~s) - s.name = utils.strcpy("float64\0") + s = _gen_prim(8, "float64\0") m`.structs.push(~s) - s.name = utils.strcpy("uint\0") + s = _gen_prim(8, "uint\0") m`.structs.push(~s) - s.name = utils.strcpy("int\0") + s = _gen_prim(8, "int\0") m`.structs.push(~s) - s.name = utils.strcpy("float\0") + s = _gen_prim(8, "float\0") m`.structs.push(~s) - s.name = utils.strcpy("void\0") + s = _gen_prim(8, "void\0") m`.structs.push(~s) - ;/ diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl index 4f7615b..b0a1d00 100644 --- a/tnslc/compile/function.tnsl +++ b/tnslc/compile/function.tnsl @@ -1,42 +1,52 @@ struct Function { - ~uint8 name, - utils.Vector - inputs, - outputs, - ~parse.Node _up, + ~uint8 name, + utils.Vector + inputs, + outputs, + ~parse.Node _up, } /; method Function - /; init (~parse.Node n) - self.name = utils.strcpy(n`.data) - self._up = n - Var v - self.inputs.init(len v) - self.outputs.init(len v) - ;/ + /; init (~parse.Node n) + self.name = utils.strcpy(n`.data) + self._up = n + Var v + self.inputs.init(len v) + self.outputs.init(len v) + ;/ - /; _resolve_type (~Module parent) - ;/ + /; _resolve_type (~Module parent) + ;/ - /; _compile (~Module parent, ~CompBuf cb) - ;/ + /; _compile (~Module parent, ~CompBuf cb) + ;/ - /; end - _delete(self.name) + /; _print (int idt) + _indent(idt) + _printf("{ Function : \0") + _printf(self.name) + _printf("\n\0") - ~Var v - /; loop (int i = 0; i < self.inputs.count) [i++] - v = self.inputs.get(i) - v`.end() - ;/ - self.inputs.end() + _indent(idt) + _printf("}\n\0") + ;/ - /; loop (int i = 0; i < self.outputs.count) [i++] - v = self.outputs.get(i) - v`.end() - ;/ - self.outputs.end() - ;/ + /; end + _delete(self.name) + + ~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/module.tnsl b/tnslc/compile/module.tnsl index 6aa6149..9d362c8 100644 --- a/tnslc/compile/module.tnsl +++ b/tnslc/compile/module.tnsl @@ -78,6 +78,8 @@ struct Module { Function f f.init(sub) self.funcs.push(~f) + ;; else if (sub`._type == parse.NTYPE_DECL) + self._decl(sub) ;; else if (sub`._type == parse.NTYPE_ASM) buf`.add_h(sub`.data) buf`.add_h("\n\0") @@ -85,6 +87,29 @@ struct Module { ;/ ;/ + /; _decl (~parse.Node decl) + /; if (decl`.sub.count < 1) + return + ;/ + + ~parse.Node tn = decl`.sub.get(0) + /; if (tn`._type !== parse.NTYPE_TYPE) + # Type must be first + return + ;/ + + ~parse.Node id + /; loop (int i = 1; i < decl`.sub.count) [i++] + id = decl`.sub.get(i) + /; if (tn`._type == parse.NTYPE_ID) + # Add a new variable to the list + Var v + v.init(tn, id) + self.vars.push(~v) + ;/ + ;/ + ;/ + /; compile (~CompBuf cb) # First, since all the types are in place, we need to size all of them. self._size_structs() @@ -146,6 +171,8 @@ 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) return self._find(stype, key, 0) ;/ @@ -154,6 +181,7 @@ struct Module { /; if ((lvl + 1) < key`.count) ~Module m ~uint8 str = key`.get(lvl) + /; loop (int i = 0; i < self.subs.count) [i++] m = self.subs.get(i) /; if (utils.strcmp(str, m`.name) == true) @@ -167,13 +195,13 @@ struct Module { ;; else ~uint8 str = key`.get(key`.count - 1) /; if (stype == SEARCH_VAR) - return _find_var(str) + return self._find_var(str) ;; else if (stype == SEARCH_STRUCT) - return _find_struct(str) + return self._find_struct(str) ;; else if (stype == SEARCH_FUNC) - return _find_func(str) + return self._find_func(str) ;; else if (stype == SEARCH_SUB) - return _find_sub(str) + return self._find_sub(str) ;/ ;/ @@ -228,6 +256,45 @@ struct Module { return NULL ;/ + /; print + self._print(0) + ;/ + + /; _print (int indent) + + _indent(indent) + _printf("{ Module : \0") + _printf(self.name) + _printf("\n\0") + + ~Var v + /; loop (int i = 0; i < self.vars.count) [i++] + v = self.vars.get(i) + v`._print(indent + 1) + ;/ + + ~Struct s + /; loop (int i = 0; i < self.structs.count) [i++] + s = self.structs.get(i) + s`._print(indent + 1) + ;/ + + ~Function f + /; loop (int i = 0; i < self.funcs.count) [i++] + f = self.funcs.get(i) + f`._print(indent + 1) + ;/ + + ~Module m + /; loop (int i = 0; i < self.subs.count) [i++] + m = self.subs.get(i) + m`._print(indent + 1) + ;/ + + _indent(indent) + _printf("}\n\0") + ;/ + /; end _delete(self.name) diff --git a/tnslc/compile/struct.tnsl b/tnslc/compile/struct.tnsl index 5403553..4b1ccfb 100644 --- a/tnslc/compile/struct.tnsl +++ b/tnslc/compile/struct.tnsl @@ -62,6 +62,30 @@ struct Struct { self.members.init(len v) ;/ + /; _print (int idt) + _indent(idt) + _printf("{ Struct : \0") + _printf(self.name) + _printf("\n\0") + + _indent(idt) + _printf(" size:\0") + _print_num("%d\0", self.size) + _printf("\n\0") + + _indent(idt) + _printf(" members:\n\0") + + ~Var v + /; loop (int i = 0; i < self.members.count) [i++] + v = self.members.get(i) + v._print(idt + 1) + ;/ + + _indent(idt) + _printf("}\n\0") + ;/ + /; add_member(~Var v) self.members.push(v) ;/ @@ -145,7 +169,7 @@ struct Struct { utils.Vector sv sv.init(8) - ~void str + ~uint8 str ~parse.Node n bool seen_id = false @@ -153,7 +177,7 @@ struct Struct { n = tn`.sub.get(i) /; if (n`._type == parse.NTYPE_ID) str = n`.data - sv.push(~str) + sv.push(str) seen_id = true ;; else if (seen_id == true) i = tn`.sub.count @@ -161,11 +185,11 @@ struct Struct { ;/ # Find struct and compute its size - ~Struct out = parent.find(SEARCH_STRUCT, ~sv) + ~Struct out = parent`.find(SEARCH_STRUCT, ~sv) sv.pop() ~Module outp = parent /; if (sv.count !== 0) - outp = parent.find(SEARCH_SUB, ~sv) + outp = parent`.find(SEARCH_SUB, ~sv) ;/ sv.end() return out diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl index 9a07aff..597a7ea 100644 --- a/tnslc/compile/var.tnsl +++ b/tnslc/compile/var.tnsl @@ -4,10 +4,13 @@ int VLOC_STCK = 2 int VLOC_LITL = 1 int VLOC_DATA = 0 -int PTYPE_NONE = 2 -int PTYPE_PTR = 1 -int PTYPE_REF = 0 -int PTYPE_ARR = 1 +# Should be -2 +int32 PTYPE_NONE = 2 +# Should be -1 +int32 PTYPE_PTR = 1 +int32 PTYPE_REF = 0 +# 1 Arr is ptr to arr, larger #s are static size arrs +int32 PTYPE_ARR = 1 int PRIM_NON = 0 int PRIM_BOO = 1 @@ -96,17 +99,123 @@ struct Var { utils.Vector ptrc, int loc, - ~parse.Node _up + ~parse.Node _tn, _id } /; method Var - /; init (~parse.Node n) - self.name = utils.strcpy(n`.data) + # Initial init function, requires type node and + # identifier node + /; init (~parse.Node tn, id) + self.name = utils.strcpy(id`.data) self.ptrc.init(4) - self._up = n + self._tn = tn + self._id = id ;/ + /; _print (int idt) + _indent(idt) + _print("{ Var : \0") + _print(self.name) + _print("\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") + + _indent(idt) + _print(" computed type name: \0") + /; if (self._type == NULL) + _print("NULL\0") + ;; else + _print(self._type`.name) + ;/ + _print("\n\0") + + _indent(idt) + _print("}\n\0") + ;/ + + /; _arr_ptr(~parse.Node a) + int32 ptr = 1 + /; if (a`.sub.count > 0) + ~parse.Node l = a`.sub.get(0) + ptr = utils.cstr_to_int(l`.data) + /; if (ptr < 2) + return + ;/ + ;/ + self.ptrc.push(~ptr) + ;/ + + # Sets up both the ptrc and the _type members, requires + # parent module for resolution of types /; _resolve_type (~Module parent) + int idx = 0 + bool running = true + ~parse.Node t, _tn + _tn = self._tn + + # Pre-op pointer + /; loop (running == true) [idx++] + /; if (idx !< _tn`.sub.count) + running = false + ;; else + t = _tn`.sub.get(idx) + /; if (t`._type == parse.NTYPE_PRE_OP) + /; if (utils.strcmp(t`.data, "~\0") == true) + int32 ptr = 0 + ptr = ptr - PTYPE_PTR + self.ptrc.push(~ptr) + ;; else + self._arr_ptr(t) + ;/ + ;; else + running = false + ;/ + ;/ + ;/ + + # After pre-ops comes id + utils.Vector strv + strv.init(8) + running = true + /; loop (running == true) [idx++] + /; 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) + ;; else + running = false + ;/ + ;/ + ;/ + + # Main type resolution + # TODO: FUNCTION POINTER + self._type = parent`.find(SEARCH_STRUCT, ~strv) + strv.end() + + # Post-op pointer + running = true + /; loop (running == true) [idx++] + /; if (idx !< _tn`.sub.count) + running = false + ;; else + t = _tn`.sub.get(idx) + /; if (t`._type == parse.NTYPE_POST_OP) + int32 ptr = 0 + self.ptrc.push(~ptr) + ;/ + ;/ + ;/ ;/ /; _static_compile (~Module parent, ~CompBuf buf) diff --git a/tnslc/test.tnsl b/tnslc/test.tnsl index 9fe6f2c..d65d7af 100644 --- a/tnslc/test.tnsl +++ b/tnslc/test.tnsl @@ -3,6 +3,3 @@ struct Box { int a } -/; main [int] -;/ - |