From fe7d97a0a608398f500b1c64ff885acecf2541d5 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Thu, 18 May 2023 23:30:18 -0400 Subject: File reading/writing --- tnslc/tnslc.tnsl | 71 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 26 deletions(-) (limited to 'tnslc/tnslc.tnsl') diff --git a/tnslc/tnslc.tnsl b/tnslc/tnslc.tnsl index f75284d..a225041 100644 --- a/tnslc/tnslc.tnsl +++ b/tnslc/tnslc.tnsl @@ -3719,9 +3719,26 @@ ;/ ;/ +/; combine_tok_lists (int cut_a, cut_b, {}Token a, b) [{}Token] + ;{}Token out = {} + /; loop (int i = 0; i < cut_a) [i++] + ;out.append(a{i}) + ;/ + + /; loop (int i = 0; i < len b) [i++] + ;out.append(b{i}) + ;/ + + /; loop (int i = cut_b; i < len a) [i++] + ;out.append(a{i}) + ;/ + + ;return out +;/ + # First compiler pass on a file # Only creates structs, enums, and moduless -/; compile_file_pass_one (Path f, ~Module current) +/; compile_file_pass_one (Path f, ~Module current) [{}Token] ;{}Token tok = tokenize(f) ;log_info(string_add("Number of tokens generated: ", int_to_string(len tok))) @@ -3730,8 +3747,9 @@ /; if (tok{i}.cmp(":")) ;log_debug("INCLUDE") /; if (tok{i + 2}.type_is(TOKEN.LITERAL)) - ;compile_file_pass_one(f.relative(unquote_str(tok{i + 2}.data)), current) - ;i = i + 2 + ;{}Token inc = compile_file_pass_one(f.relative(unquote_str(tok{i + 2}.data)), current) + ;tok = combine_tok_lists(i, i + 3, tok, inc) + ;i = i + len inc ;/ ;continue ;; else if (tok{i}.cmp("/;") || tok{i}.cmp(";;")) @@ -3744,6 +3762,8 @@ ;new_type(~tok, ~i, current) ;/ ;/ + + ;return tok ;/ /; size_struct (~Type t, ~Module m) @@ -3784,50 +3804,49 @@ # Second pass of compiler # Does code generation, ignores structs and enums -/; compile_file_pass_two (Path f, ~Module current) [CompData] +/; compile_file_pass_two (~{}Token tok, ~Module current) [CompData] ;CompData out = {"", "", ""} - ;{}Token tok = tokenize(f) - /; loop (int i = next_non_nl(~tok, 0); i < len tok) [i = next_non_nl(~tok, i+1)] + /; loop (int i = next_non_nl(tok, 0); i < len tok`) [i = next_non_nl(tok, i+1)] ;log_vis(".") - /; if (tok{i}.cmp(":")) + /; if (tok`{i}.cmp(":")) ;log_debug("INCLUDE") - /; if (tok{i + 2}.type_is(TOKEN.LITERAL)) - ;CompData tmp = compile_file_pass_two(f.relative(unquote_str(tok{i + 2}.data)), current) + /; if (tok`{i + 2}.type_is(TOKEN.LITERAL)) + ;CompData tmp = compile_file_pass_two(f.relative(unquote_str(tok`{i + 2}.data)), current) ;out.hsec = string_add(out.hsec, tmp.hsec) ;out.dsec = string_add(out.dsec, tmp.dsec) ;out.csec = string_add(out.csec, tmp.csec) ;i = i + 2 ;/ ;continue - ;; else if (tok{i}.cmp("/;") || tok{i}.cmp(";;")) + ;; else if (tok`{i}.cmp("/;") || tok`{i}.cmp(";;")) ;log_debug("Root block") - /; if (tok{i + 1}.cmp("export") || tok{i + 1}.cmp("module")) - ;module_pass_two(~tok, ~i, current, ~out, f) + /; if (tok`{i + 1}.cmp("export") || tok`{i + 1}.cmp("module")) + ;module_pass_two(tok, ~i, current, ~out, f) ;; else - ;compile_block(~tok, ~i, current, ~out) + ;compile_block(tok, ~i, current, ~out) ;/ - ;; else if (tok{i}.cmp("struct")) + ;; else if (tok`{i}.cmp("struct")) ;log_debug("File struct") - ;skip_struct(~tok, ~i) - ;; else if (tok{i}.cmp("enum")) + ;skip_struct(tok, ~i) + ;; else if (tok`{i}.cmp("enum")) ;log_debug("File enum") - ;compile_enum(~tok, ~i, current, ~out) - ;; else if (is_definition(~tok, ~i, current)) + ;compile_enum(tok, ~i, current, ~out) + ;; else if (is_definition(tok, ~i, current)) ;log_debug("File def") - ;compile_file_def(~tok, ~i, current, ~out) - ;; else if (tok{i}.cmp("asm")) + ;compile_file_def(tok, ~i, current, ~out) + ;; else if (tok`{i}.cmp("asm")) ;log_debug("File asm") /; if (len (out.csec) == 0 && len (out.dsec) == 0) - ;out.hsec = string_add(out.hsec, unquote_str(tok{i + 1}.data)) + ;out.hsec = string_add(out.hsec, unquote_str(tok`{i + 1}.data)) ;out.hsec.append('\n') ;; else - ;out.dsec = string_add(out.dsec, unquote_str(tok{i + 1}.data)) + ;out.dsec = string_add(out.dsec, unquote_str(tok`{i + 1}.data)) ;out.dsec.append('\n') ;/ ;i++ - ;; else if (!(tok{i}.cmp("\n"))) - ;log_err(string_add("Failed to recognize file-level statement", tok{i}.sprint())) + ;; else if (!(tok`{i}.cmp("\n"))) + ;log_err(string_add("Failed to recognize file-level statement", tok`{i}.sprint())) ;/ ;/ @@ -3841,11 +3860,11 @@ ;{}uint8 out = "" ;Module root = {0, true, {}, {}, {}, {}, {}} - ;compile_file_pass_one(f, ~root) + ;{}Token tok = compile_file_pass_one(f, ~root) ;flush_structs(~root) ;log_info("First pass DONE") - ;CompData data = compile_file_pass_two(f, ~root) + ;CompData data = compile_file_pass_two(~tok, ~root) ;log_info("Second pass DONE") ;out = string_join({ -- cgit v1.2.3