From 8b9b469242cb628fe469edaa4cc55bf952de178b Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Wed, 13 Sep 2023 02:44:19 -0400 Subject: a few string operations --- tnslc/compiler.tnsl | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 162 insertions(+), 5 deletions(-) diff --git a/tnslc/compiler.tnsl b/tnslc/compiler.tnsl index da72b11..efa74a7 100644 --- a/tnslc/compiler.tnsl +++ b/tnslc/compiler.tnsl @@ -1,8 +1,153 @@ -/; matching_delim (Vector v, int cur) [int] +############## +# TEXT UTILS # +############## + +/; parse_meta (~uint8 str, int idx) [uint8] + + /; if (str{idx} == 'n') + return '\n' + ;; else if (str{idx} == '0') + return 0 + ;/ + + return str{idx} +;/ + +/; unquote_char (~uint8 str) [uint8] + int l = cstr_len(str) + + /; if (l < 3) + return 0 + ;; else if (str{1} == '\\') + return parse_meta(str, 2) + ;/ + + return str{1} +;/ + +/; unquote_string (~uint8 str) [~uint8] + ~uint8 out = _alloc(1) + out{0} = 0 + + int ln = 0 + int l = cstr_len(str) + + /; loop (int i = 1; i < l - 1) [i++] + /; if (str{i} == '\\') + out{ln} = parse_meta(str, i + 1) + ;; else + out{ln} = str{i} + ;/ + + out = _realloc(out, ln + 2) + out{ln + 1} = 0 + ln++ + ;/ + + return out +;/ + +{}uint8 e_str_parse = "[TNSLC] [ERROR] Error when parsing int from string. %d\n\0" +/; str_to_int (~uint8 str) [int] + int out = 0 + int l = cstr_len(str) + + /; loop (int i = 0; i < l) [i++] + uint8 c = str{i} + /; if (c < '0' || c > '9') + _print_num(~e_str_parse{0}, c) + _printf(~e_noquit{0}) + break + ;; else + out = out * 10 + out = out + c - '0' + ;/ + ;/ + + return out +;/ + +{}uint8 w_method_guard = "_#" +{}uint8 w_enum_guard = "__#" + + +#################### +# FIND/PARSE UTILS # +#################### + +# finds the closing delim in a vector of tokens +{}uint8 e_dmiss = "[TNSLC] [ERROR] Delimiter missmatch\n\0" +/; matching_delim (Vector v, int c) [int] ~Token cur - cur = v.get(cur) + cur = v.get(c) + uint8 op + + /; if (cur`.data{0} == '(') + op = ')' + ;; else if (cur`.data{0} == '[') + op = ']' + ;; else if (cur`.data{0} == '{') + op = '}' + ;; else + op = ';' + ;/ + + int p, b, s, f + p = 0 # Parens + b = 0 # Braces + s = 0 # Squiggly Braces + f = 0 # Funcs + + /; loop (c++; cur < v.num_el) [c++] + cur = v.get(c) + /; if (cur`._type !== TOKEN_TYPE.DELIMITER) + continue + ;/ + uint8 first = cur`.data{0} + # Increments + /; if (first == '(') + p++ + continue + ;; else if (first == '[') + b++ + continue + ;; else if (first == '{') + s++ + continue + ;; else if (first == '/') + f++ + continue + ;/ + + # Check end + /; if (first == op) + /; if (p == 0 && b == 0 && s == 0 && f == 0) + return c + ;/ + ;/ + + # Decrement + /; if (first == ')') + p-- + ;; else if (first == ']') + b-- + ;; else if (first == '}') + s-- + ;; else if (first == ';') + /; if (cur`.data{1} == '/') + f-- + ;/ + ;/ + + # Mismatch + /; if (p < 0 || b < 0 || s < 0 || f < 0) + _printf(~e_dmiss{0}) + _printf(~e_noquit{0}) + ;/ + ;/ + return 0 - 1 ;/ # Entrypoint for round two @@ -39,8 +184,16 @@ Vector v v.start(8) v.push(~mbt`.name) - m`._find_type(v, 0) - size_struct + + mbm = mbt`.mod + ~Type tmp + tmp = mbm`._find_type(v, 0) + size_struct(tmp, mbm) + + mbt`.s = tmp`.s + s = s + tmp`.s + + v._del() ;; else if (mbt`.s < 0) _printf(~e_circular{0}) @@ -71,10 +224,14 @@ ;/ ;/ -/; create_struct +# Parses a struct (and skips that many tokens) +/; create_struct (Vector v, ~int c) [Type] ;/ /; create_module (~uint8 name, bool e, bool m) [Module] + Module out + out.start() + out.name ;/ {}uint8 r1_export = "export\0" -- cgit v1.2.3