From adc97807daa20efd16fcc9ef82f773bdd23f63bd Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Sat, 5 Aug 2023 17:48:15 -0400 Subject: Fix issue with cf block conditionals --- tnslc/c_wrap.tnsl | 2 +- tnslc/copy.tnsl | 9 +++--- tnslc/logging.tnsl | 5 ++++ tnslc/tnslc.tnsl | 50 ++++++++++++++++++++++---------- tnslc/tokenizer.tnsl | 77 ++++++++++++++++++++++++++++++++------------------ tnslc/vector.tnsl | 17 ++++++++++- tnslc/vector_test.tnsl | 7 +++-- 7 files changed, 115 insertions(+), 52 deletions(-) diff --git a/tnslc/c_wrap.tnsl b/tnslc/c_wrap.tnsl index d44e948..ac516da 100644 --- a/tnslc/c_wrap.tnsl +++ b/tnslc/c_wrap.tnsl @@ -222,7 +222,7 @@ asm "extern CloseHandle" asm "add rsp, 32" ;/ -/; _read_byte (~void handle, ~uint8 byte, ~int read) [int] +/; _read_byte (~void handle, ~uint8 byte, ~int read) [bool] int out asm "mov rax, rsp" diff --git a/tnslc/copy.tnsl b/tnslc/copy.tnsl index 90e1126..636a773 100644 --- a/tnslc/copy.tnsl +++ b/tnslc/copy.tnsl @@ -1,6 +1,5 @@ :include "c_wrap.tnsl" -:include "vector.tnsl" -:include "utils.tnsl" +# :include "utils.tnsl" {}uint8 wrong_args = "Usage: copy [from] [to]" @@ -25,8 +24,8 @@ uint8 buf = 0 int read_count = 0 int tries = 0 - _read_byte(read_handle, ~buf, ~read_count) - /; loop (_read_byte(read_handle, ~buf, ~read_count)) + /; loop + _read_byte(read_handle, ~buf, ~read_count) /; if (read_count == 0) break ;/ @@ -38,4 +37,4 @@ _close_file(write_handle) return 0 -;/ \ No newline at end of file +;/ diff --git a/tnslc/logging.tnsl b/tnslc/logging.tnsl index 7a60364..6fac3a0 100644 --- a/tnslc/logging.tnsl +++ b/tnslc/logging.tnsl @@ -63,3 +63,8 @@ int log_mode = 1 log_one(c) _printf(~_log_nl{0}) ;/ + +/; log_num_nl (int i) + _print_num(~_dec{0}, i) + _printf(~_log_nl{0}) +;/ diff --git a/tnslc/tnslc.tnsl b/tnslc/tnslc.tnsl index 7c31fa8..a518cc0 100644 --- a/tnslc/tnslc.tnsl +++ b/tnslc/tnslc.tnsl @@ -3417,20 +3417,17 @@ ;; else ;cf = cf.new_sub_cf(tok`{cur`}.data) ;/ - ;; else if (tok`{cur`}.cmp("(") || tok`{cur`}.cmp("[")) + ;; else if (tok`{cur`}.cmp("(")) ;int psl = find_closing(tok, cur) - ;int tmp = statement_list(tok, cur`, psl, current, ~outro, mov, ~cf) - /; if (tmp < 0 && condition !< 0) - ;tmp = condition + ;int tmp = statement_list(tok, cur`, psl, current, out, mov, ~cf) + + /; if (tmp !< 0 && condition < 0 && cf.cf_type("if") == false) + ;condition = tmp ;Type t = {1, "bool", "_bool", {}, {}} ;eval_value(tok, ~tmp, ~outro, mov, current, ~cf, t, true) - ;; else - ;condition = tmp ;/ - /; if (tok`{cur`}.cmp("(") && condition !< 0) - ;out`.add(outro) - ;outro = {"", "", ""} + /; if (tmp !< 0) /; if (cf.cf_type("if")) ;out`.csec = string_join( { out`.csec, @@ -3445,6 +3442,20 @@ }, "") ;/ ;/ + + ;cur` = psl + ;; else if (tok`{cur`}.cmp("[")) + ;CompData t_outro = {"", "", ""} + ;int psl = find_closing(tok, cur) + ;int tmp = statement_list(tok, cur`, psl, current, ~t_outro, mov, ~cf) + + /; if (tmp < 0 && condition !< 0) + ;t_outro.add(outro) + ;outro = t_outro + ;; else + ;outro = t_outro + ;/ + ;cur` = psl ;; else ;break @@ -3613,12 +3624,21 @@ ;out`.add(outro) ;/ - ;out`.csec = string_join( { - out`.csec, - "\ttest al, al\n", - "\tjnz ", cf.scope_start_label(), "\n", - cf.scope_end_label(), ":\n" - }, "") + /; if (condition !< 0) + ;out`.csec = string_join( { + out`.csec, + "\ttest al, al\n", + "\tjnz ", cf.scope_start_label(), "\n", + cf.scope_end_label(), ":\n" + }, "") + ;; else + ;out`.csec = string_join( { + out`.csec, + "\tjmp ", cf.scope_start_label(), "\n", + cf.scope_end_label(), ":\n" + }, "") + ;/ + ;; else ;out`.csec = string_join( { out`.csec, diff --git a/tnslc/tokenizer.tnsl b/tnslc/tokenizer.tnsl index ab22d7d..0ae71a1 100644 --- a/tnslc/tokenizer.tnsl +++ b/tnslc/tokenizer.tnsl @@ -180,39 +180,63 @@ struct Token { return a || b || c ;/ -/; str_char_literal(uint8 start, ~Token tmp, ~void file_in) - uint read_count = 0 +/; handle_comment (~void file_in) uint8 buf = 0 - - tmp`._type = TOKEN_TYPE.LITERAL - tmp`.append(buf) - /; loop (_read_byte(file_in, ~buf, ~read_count)) + int read_count = 0 + /; loop + _read_byte(file_in, ~buf, ~read_count) + /; if (buf == '\n' || read_count == 0) + break + ;/ + read_count = 0 + ;/ +;/ + +/; handle_str (~void file_in, Token tmp, ~int line, column, uint8 first) [Token] + uint8 buf = first + int read_count = 0 + tmp._type = TOKEN_TYPE.LITERAL + tmp.append(buf) + read_count = 0 + log_one_nl(first) + + /; loop + _read_byte(file_in, ~buf, ~read_count) + log_one_nl(buf) + log_num_nl(read_count) + /; if (read_count == 0) - break + log_one_nl('b') + break ;/ - + /; if (buf == '\\') - tmp`.append(buf) + log_one_nl('c') + tmp.append(buf) read_count = 0 _read_byte(file_in, ~buf, ~read_count) - column++ - tmp`.append(buf) - ;; else if (buf == start) - tmp`.append(buf) + column`++ + tmp.append(buf) + ;; else if (buf == first) + log_one_nl('d') + tmp.append(buf) break ;; else - tmp`.append(buf) + log_one_nl('e') + tmp.append(buf) ;/ /; if (buf == '\n') - line++ - column = 1 + line`++ + column` = 1 ;; else - column++ + column`++ ;/ read_count = 0 ;/ + + return tmp ;/ /; tokenize_file (~void file_in, file_out) @@ -226,10 +250,10 @@ struct Token { int read_count = 0 int line = 1 int column = 1 - # Start reading at beginning of file - _read_byte(file_in, ~buf, ~read_count) + # Read loop. - /; loop (_read_byte(file_in, ~buf, ~read_count)) [column++] + /; loop [column++] + _read_byte(file_in, ~buf, ~read_count) /; if (read_count == 0) break ;/ @@ -237,20 +261,19 @@ struct Token { /; if (buf == '#') # Handle comment - /; loop (_read_byte(file_in, ~buf, ~read_count)) - /; if (buf == '\n' || read_count == 0) - break - ;/ - ;/ + handle_comment(file_in) + line++ ;; else if (buf == '\'' || buf == '"') # Handle char/string literal - str_char_literal(buf, ~tmp, file_in) - + tmp = handle_str(file_in, tmp, ~line, ~column, buf) + print_token(tmp, file_out) tmp._del() tmp.start() + tmp.line = line + tmp.column = column ;; else if (break_token(~tmp, buf) == true) diff --git a/tnslc/vector.tnsl b/tnslc/vector.tnsl index f7ca9bc..d21f83c 100644 --- a/tnslc/vector.tnsl +++ b/tnslc/vector.tnsl @@ -56,6 +56,17 @@ struct Vector { self.pop() ;/ + /; copy (Vector v) + self.dat_size = v.dat_size + self.num_el = v.num_el + self.el_size = v.el_size + self.dat = _alloc(self.dat_size * self.el_size) + + /; loop (int i = 0; i < self.num_el) [i++] + self.set(i, v.dat + i * self.el_size) + ;/ + ;/ + /; start (int el_size) self.num_el = 0 self.el_size = el_size @@ -63,7 +74,11 @@ struct Vector { self.dat = _alloc(self.el_size) ;/ - /; clean + /; _len [uint] + return self.num_el + ;/ + + /; _del self.num_el = 0 self.el_size = 0 self.dat_size = 0 diff --git a/tnslc/vector_test.tnsl b/tnslc/vector_test.tnsl index ee4a25c..c74ddf9 100644 --- a/tnslc/vector_test.tnsl +++ b/tnslc/vector_test.tnsl @@ -15,8 +15,9 @@ Vector vec vec.start(1) + int mx = 100 uint8 buf = 0 - /; loop (int i = 0; i < 20) [i++] + /; loop (int i = 0; i < mx) [i++] buf = 'A' + i vec.push(~buf) ;/ @@ -26,10 +27,10 @@ _printf(vec.dat) - /; loop (int i = 0; i < 21) [i++] + /; loop (int i = 0; i < mx + 1) [i++] vec.pop() ;/ - vec.clean() + vec._del() return 0 ;/ \ No newline at end of file -- cgit v1.2.3