diff options
author | Kyle Gunger <kgunger12@gmail.com> | 2022-10-14 01:21:12 -0400 |
---|---|---|
committer | Kyle Gunger <kgunger12@gmail.com> | 2022-10-14 01:21:12 -0400 |
commit | 01d6580a17d5dc00b84cc3e72e0a86b090c1c6bd (patch) | |
tree | 0e023e9fac2f030037a3ddce6da0db6d675fad48 | |
parent | 0d2c569ed2e408d0c1cbac7f170f4e281601eb24 (diff) |
Start to fill out compile_statement more
-rw-r--r-- | libtnsl/box/list.tnsl | 8 | ||||
-rw-r--r-- | libtnsl/box/string.tnsl | 25 | ||||
-rw-r--r-- | libtnsl/box/vector.tnsl | 6 | ||||
-rw-r--r-- | tnslc/compile/compile.tnsl | 106 | ||||
-rw-r--r-- | tnslc/compile/isa_x86.tnsl | 16 |
5 files changed, 118 insertions, 43 deletions
diff --git a/libtnsl/box/list.tnsl b/libtnsl/box/list.tnsl index 11ba4e1..cdf39d7 100644 --- a/libtnsl/box/list.tnsl +++ b/libtnsl/box/list.tnsl @@ -49,18 +49,18 @@ ;/ ;/ -;struct List { +;struct List (type T) { uint length, - LNode + LNode(T) first, last } -;struct DList { +;struct DList (type T) { uint length, - DLNode + DLNode(T) first, last }
\ No newline at end of file diff --git a/libtnsl/box/string.tnsl b/libtnsl/box/string.tnsl index d661676..675db06 100644 --- a/libtnsl/box/string.tnsl +++ b/libtnsl/box/string.tnsl @@ -30,19 +30,26 @@ } /; method String - /; cmp (String str) [int] - /; loop (self.) + # Returns index of first difference, or -1 if there is no difference + /; diff_index (String str) [int] + ;uint l = math.mint(uint, str.length, self.length) + /; loop (uint i = 0; i < l) [i++] + /; if (self{i} != str{i}) + ;return i [int] + ;/ + ;/ + + /; if () ;/ + + ;return -1 ;/ /; override operator == (String str) [bool] - ;return cmp(str) == 0 + /; if (self.encoding != str.encoding || self.length != str.length) + ;return false + ;/ + ;return diff_index(str) == -1 ;/ ;/ - -;struct WString extends Vector (uint16) {} - -/; method WString - -;/
\ No newline at end of file diff --git a/libtnsl/box/vector.tnsl b/libtnsl/box/vector.tnsl index 724393f..85e6c3c 100644 --- a/libtnsl/box/vector.tnsl +++ b/libtnsl/box/vector.tnsl @@ -19,7 +19,7 @@ EXPRESS OR IMPLIED #/ -;struct Vector (type T) { +;struct Vector (raw type T) { uint length, dataSize, @@ -109,7 +109,7 @@ /; add (T item, uint index) /; if (index < 0 || index > self.length) - ;throw Error{"Bad Index"} + ;throw Error("Bad index", ERROR_CODE.OUT_OF_RANGE) ;/ # If it checks out then we check if we should grow @@ -122,7 +122,7 @@ ;self.data{i} = self.data{i - 1} ;/ - ;self{} = replace + ;self{index} = replace ;self.length += 1 ;/ diff --git a/tnslc/compile/compile.tnsl b/tnslc/compile/compile.tnsl index 420f5b1..6876226 100644 --- a/tnslc/compile/compile.tnsl +++ b/tnslc/compile/compile.tnsl @@ -16,11 +16,10 @@ ;{}{}charp COMMON_ASM = { - "\tret", - "\tpush %", - "\tpop %" + "\tret\n" } +# Represents a type ;struct VType { uint _size, @@ -30,6 +29,7 @@ {}charp name } +# Tracks defined variables in a block ;struct VTrack { {}{}charp sym_names, @@ -60,6 +60,7 @@ # Null type ;VType NT = {0, 0, "null"} +# Returns an index in the vtrack for a given variable name /; name_to_index ({}charp name, ~VTrack tab) [int] /; loop (int i = 0; i < len tab`.sym_names) [i++] /; if (string_equate(tab`.sym_names{i}, name)) @@ -73,6 +74,8 @@ ;return -1 ;/ +# Given an index in the vtrack, returns a string representation of the +# register or memory where that variable is /; index_to_loc (int index, ~VTrack tab) [{}charp] ;{}charp out = "" ;int stack_bytes = 0 @@ -90,20 +93,34 @@ ;return out ;/ +# Is struct returns true if the type name given is a struct +/; is_struct ({}charp name) [bool] + /; loop (int i = 0; i < 15) [i++] + /; if (string_equate(type_table{i}, name)) + ;return false + ;/ + ;/ + ;return true +;/ + +# Checks if the current token's data is equal to a string /; token_is(~int cur, ~{}Token data, {}charp str) [bool] ;return string_equate(data`{cur`}.data`, str) ;/ +# Skips in a definition or list until it finds a name /; skip_to_name (~int cur, ~{}Token data) ;int tmp = 0 /; loop (cur` < len data`) [cur`++] ;tmp = cur` + 1 - /; if (data`{cur`}.token_type == TOKEN_TYPE.DEFWORD && ( token_is(~tmp, data, ",") || token_is(~tmp, data, ")") )) + /; if (data`{cur`}.token_type == TOKEN_TYPE.DEFWORD && + ( token_is(~tmp, data, ",") || token_is(~tmp, data, ")") || token_is(~tmp, data, "}") || token_is(~tmp, data, ";") )) ;break ;/ ;/ ;/ +# Searches the type table for a type /; vtype_by_name ({}charp name) [VType] /; loop (int i = 0; i < len type_table) [i++] ;VType tmp = tnslc.type_table{i} @@ -114,6 +131,7 @@ ;return NT ;/ +# Parses a type in a definition or list /; get_vtype (~int cur, ~{}Token data) [VType] ;uint ptr = 0 ;VType out = NT @@ -135,6 +153,8 @@ ;return out ;/ +# Assumes cur points to the beginning of the arguments list +# Sets up the VTrack struct that is pointed to. /; setup_vtrack (~int cur, ~{}Token data, ~VTrack tab) ;cur`++ @@ -150,9 +170,7 @@ ;tab`.sym_types.append(last) ;tab`.sym_names.append(data`{cur`}.data`) ;; else - ;cur` = pre_skip - ;last = get_vtype(cur, data) - ;skip_to_name(cur, data) + ;last = get_vtype(~pre_skip, data) ;tab`.sym_types.append(last) ;tab`.sym_names.append(data`{cur`}.data`) ;/ @@ -160,7 +178,8 @@ ;/ ;/ -/; compile_statement (~int cur, ~{}Token data, ~{}charp hsec, csec, dsec) +# +/; compile_statement (~int cur, ~{}Token data, ~VTrack tab, gsc, ~{}charp hsec, csec, dsec) [bool] ;cur`++ /; if (cur` < len data`) /; if (token_is(cur, data, "asm")) @@ -169,15 +188,48 @@ ;raw_asm.append('\n') ;csec`.append('\t') ;add_strings(csec, ~raw_asm) + ;; else if (token_is(cur, data, "return")) + ;return true + ;; else if (name_to_index(data`{cur`}.data`, tab) !< 0) + # set value + ;int i = name_to_index(data`{cur`}.data`, tab) + ;; else if (string_equate(data`{cur`+1}.data`, "(")) + # Function call + ;eval(cur, data, tab, gsc, hsec, csec, dsec, "ax") + ;; else + #Definition + ;VType def_t = get_vtype(cur, data) + ;cur`++ + + /; loop (data`{cur`}.token_type == TOKEN_TYPE.DEFWORD) + ;tab`.sym_types.append(def_t) + ;tab`.sym_names.append(data`{cur`}.data`) + ;cur`++ + /; if (token_is(cur, data, ",")) + ;cur`++ + ;; else if (token_is(cur, data "=")) + ;{}charp loc = index_to_loc(len tab`.sym_names - 1, tab) + ;eval(cur, data, tab, gsc, hsec, csec, dsec, loc) + /; if (token_is(cur, data, ",")) + ;cur`++ + ;/ + ;/ + ;/ + /; if (string_equate(data`{cur`+1}.data`, "=")) + + ;/ ;/ ;/ + + ;return false ;/ -/; compile_block (~int cur, ~{}Token data, ~{}charp hsec, csec, dsec) +/; compile_block (~int cur, ~{}Token data, ~VTrack gsc, ~{}charp hsec, csec, dsec) ;VTrack tab = { {}, {} } - + ;VType out_type = NT ;{}charp name = "" ;bool r = false + /; loop (cur`++; cur` < len data`) [cur`++] /; if (data`{cur`}.token_type == TOKEN_TYPE.DEFWORD && len name == 0) ;name = data`{cur`}.data` @@ -187,11 +239,9 @@ ;; else if (token_is(cur, data, "(")) ;setup_vtrack(cur, data, ~tab) ;; else if (token_is(cur, data, "[")) - /; loop (cur`++; cur` < len data`) [cur`++] - /; if (token_is(cur, data, "]")) - ;break - ;/ - ;/ + ;cur`++ + ;out_type = get_vtype(cur, data) + ;cur`++ ;; else if (token_is(cur, data, "raw")) ;r = true ;; else @@ -199,12 +249,18 @@ ;/ ;/ + ;tnsl.io.println(out_type.name) + /; if (!r) - ;header_guard(tab, csec) + ;header_guard(csec) ;/ - /; loop (cur` < len data`) [cur`++] + ;bool ret = false + /; loop (cur` < len data` && !ret) [cur`++] /; if (string_equate(data`{cur`}.data`, ";/")) + /; if (!r) + ;tail_guard(csec) + ;/ ;add_strings(csec, ~(tnslc.COMMON_ASM{0})) ;break ;; else if (string_equate(data`{cur`}.data`, "/;")) @@ -217,7 +273,13 @@ ;ch = string_equate(data`{cur`}.data`, ";;") ;/ ;; else if (string_equate(data`{cur`}.data`, ";")) - ;compile_statement(cur, data, hsec, csec, dsec) + ;ret = compile_statement(cur, data, ~tab, gsc, hsec, csec, dsec) + /; if (ret) + /; if (!r) + ;tail_guard(csec) + ;/ + ;add_strings(csec, ~(tnslc.COMMON_ASM{0})) + ;/ ;; else ;tnsl.io.print("Failed to compile token [compile_block]: ") ;data`{cur`}.print() @@ -226,10 +288,6 @@ ;/ ;/ - /; if (!r) - ;tail_guard(csec) - ;/ - ;csec`.append('\n') ;/ @@ -240,11 +298,13 @@ ;int j = len data` + ;VTrack global_scope = {{}, {}} + /; loop (int i = 0; i < j) [i++] /; if (string_equate(data`{i}.data`, "/;")) ;compile_block(~i, data, ~hsec, ~csec, ~dsec) ;; else if (string_equate(data`{i}.data`, ";")) - ;compile_statement(~i, data, ~hsec, ~csec, ~dsec) + ;compile_global(~i, data, ~hsec, ~csec, ~dsec) ;; else ;break ;/ diff --git a/tnslc/compile/isa_x86.tnsl b/tnslc/compile/isa_x86.tnsl index 0ab8910..e342c90 100644 --- a/tnslc/compile/isa_x86.tnsl +++ b/tnslc/compile/isa_x86.tnsl @@ -62,7 +62,7 @@ ;return tmp ;/ -/; header_guard [{}charp] +/; header_guard (~{}charp csec) [{}charp] ;{}charp out = "", tmp = "" ;tmp = push_asm("%r8") ;add_strings(~out, ~tmp) @@ -80,10 +80,10 @@ ;add_strings(~out, ~tmp) ;tmp = push_asm("%r15") ;add_strings(~out, ~tmp) - ;return out + ;add_strings(csec, ~out) ;/ -/; tail_guard [{}charp] +/; tail_guard (~{}charp csec) [{}charp] ;{}charp out = "", tmp = "" ;tmp = pop_asm("%r15") ;add_strings(~out, ~tmp) @@ -101,7 +101,7 @@ ;add_strings(~out, ~tmp) ;tmp = pop_asm("%r8") ;add_strings(~out, ~tmp) - ;return out + ;add_strings(csec, ~out) ;/ /# Accepted common names: @@ -158,3 +158,11 @@ ;return out ;/ + +/; make_label ({}charp func_name, func_place, ~{}charp csec) + ;func_name.append("_") + ;add_strings(~func_name, ~func_place) + ;add_strings(csec, ~func_name) + ;csec`.append(':') + ;csec`.append('\n') +;/ |