diff options
Diffstat (limited to 'tnslc/parse')
| -rw-r--r-- | tnslc/parse/ast.tnsl | 199 | ||||
| -rw-r--r-- | tnslc/parse/tokenizer.tnsl | 2 |
2 files changed, 110 insertions, 91 deletions
diff --git a/tnslc/parse/ast.tnsl b/tnslc/parse/ast.tnsl index 588f3fd..46b1025 100644 --- a/tnslc/parse/ast.tnsl +++ b/tnslc/parse/ast.tnsl @@ -204,6 +204,20 @@ int errors_shown = 0 return true ;/ +# Canonical operation order from strongest (lowest number prec) to weakest (highest number prec) +# Parenthesis or function call done before any of these +# 0: deref - (`) +# 1: member - (.) +# 2: refer - (~) +# 3: inc/dec - (++ --) +# 4: bit shift - (<< >>) +# 5: len prefix op - (len) +# 6: mul/div - (* / %) +# 7: add/sub - (+ -) +# 8: bitwise ops - (! & | ^ !& !| !^) +# 9: comparison - (== < > <== >== !== !< !>) +# 10: boolean - (&& || ^^ !&& !|| !^^) +# 11: assignment - variants of = /; _op_order(~uint8 op) [int] int l = utils.strlen(op) @@ -255,6 +269,8 @@ int errors_shown = 0 return 5 ;; else if (op{1} == '=') return 9 + ;; else if (op{2} == '=') + return 11 ;/ return 10 ;/ @@ -903,8 +919,6 @@ int errors_shown = 0 uint8 end = _get_closing_delim(first`.data`) first` = produce_next_token(fin, first`) - int ln = first`.line - /; loop (bool run = true; run == true && first`._type !== TTYPE_ERR && first`.data` !== end) /; if (_advance_check(fin, first, "asm\0") == true) _ast_asm(fin, ~list, first) @@ -935,8 +949,6 @@ int errors_shown = 0 mod`.add_child(~list) - ln = first`.line - /; if (first`._type !== TTYPE_DELIM || first`.data` !== end) _ast_print_err(fin, first, "Expected closing at end of statement list\0") ;; else @@ -945,7 +957,6 @@ int errors_shown = 0 first` = tmp ;/ - return ln ;/ @@ -1093,7 +1104,7 @@ int errors_shown = 0 _ast_block_pass(fin, first) - /; if (_advance_check(fin, first`, ";/\0") == false) + /; if (_advance_check(fin, first, ";/\0") == false) _ast_print_err(fin, ~blf, "Could not find closing ;/ for top block\0") ;/ blf.end() @@ -1236,6 +1247,9 @@ int errors_shown = 0 mod`.add_child(~id) first` = produce_next_token(fin, first`) ;; else + _printf("Returning mod at \0") + _printf(first`.data) + _printf("\n\0") return mod ;/ @@ -1295,39 +1309,56 @@ int errors_shown = 0 ;/ ;/ - /; loop (idx < _type`.sub.count && sub`._type == NTYPE_ID) - /; if (idx < _type`.sub.count - 1) - sub = _type`.sub.get(idx + 1) - /; if (sub`._type == NTYPE_ID) - sub = _type`.sub.get(idx) - Node op, n + /; loop (idx < _type`.sub.count) + /; if (sub`._type == NTYPE_ID) + int cmp = 0 + /; if (cur`.eq(".\0") == true) + cmp++ + ;/ + + /; if (cur`.sub.count > cmp) + # Create bin op + ~Node mid + mid = cur`.sub.get(cur`.sub.count - 1) + + Node op op.init(NTYPE_BIN_OP, utils.strcpy(".\0")) - n.init(NTYPE_ID, utils.strcpy(sub`.data)) - /; if (cur`.sub.count == 0) - op.add_child(~n) - ;; else - sub = cur`.sub.get(cur`.sub.count - 1) - sub`.add_child(~n) - op.add_child(sub) - cur`.sub.pop() - ;/ - cur`.add_child(~op) - ;; else - sub = _type`.sub.get(idx) - Node n - n.init(NTYPE_ID, utils.strcpy(sub`.data)) - /; if (cur`.sub.count !== 0) - cur = cur`.sub.get(cur`.sub.count - 1) - ;/ - cur = cur`.add_child(~n) + op.add_child(mid) + cur`.sub.pop() + + cur = cur`.add_child(~op) ;/ - ;; else + Node n n.init(NTYPE_ID, utils.strcpy(sub`.data)) - /; if (cur`.sub.count !== 0) - cur = cur`.sub.get(cur`.sub.count - 1) + cur`.add_child(~n) + + ;; else if (sub`._type == NTYPE_TLIST && sub`.eq("(\0") == true) + ~Node mid + mid = cur`.sub.get(cur`.sub.count - 1) + + Node lst + lst.init(NTYPE_VLIST, utils.strcpy("(\0")) + + /; loop (int i = 0; i < sub`.sub.count) [i++] + ~Node lsub = sub`.sub.get(i) + at = _mhf_transform(lsub, at) + /; if (at == lsub) + at = lst.add_child(lsub) + ;; else + lst.add_child(lsub) + ;/ + ;/ + + /; loop (sub`.sub.count > 0) + sub`.sub.pop() + ;/ + + /; if (at == sub) + at = mid`.add_child(~lst) + ;; else + mid`.add_child(~lst) ;/ - cur = cur`.add_child(~n) ;/ idx++ @@ -1337,34 +1368,6 @@ int errors_shown = 0 ;/ ;/ - ~int pt1 = at - /; if (idx < _type`.sub.count && sub`._type == NTYPE_TLIST && sub`.eq("(\0") == true) - Node lst - lst.init(NTYPE_VLIST, utils.strcpy("(\0")) - - /; loop (int i = 0; i < sub`.sub.count) [i++] - ~Node lsub = sub`.sub.get(i) - at = _mhf_transform(lsub, at) - ~int pt2 = lsub - /; if (at == lsub) - at = lst.add_child(lsub) - ;; else - lst.add_child(lsub) - ;/ - ;/ - - /; loop (sub`.sub.count > 0) - sub`.sub.pop() - ;/ - - ~int pt2 = sub - /; if (pt1 == pt2) - at = cur`.add_child(~lst) - ;; else - cur`.add_child(~lst) - ;/ - ;/ - _type`.end() _type` = out _type`.update_children() @@ -1421,20 +1424,22 @@ int errors_shown = 0 /; _mhf_lower (~Node cur) [~Node] /; loop (bool run = true; run == true) /; if (cur`.sub.count > 0) - cur = cur`.sub.get(cur`.sub.count - 1) + int i = cur`.sub.count + i = i - 1 + cur = cur`.sub.get(i) ;; else return cur ;/ - /; if (cur`.eq(".\0") || cur`._type == NTYPE_ID) - return cur + /; if (cur`._type == NTYPE_ID) + return cur`.parent ;/ ;/ # should never happen return NULL ;/ -/; _mhf_post_value(~utils.File fin, ~Node cur, ~Token first, bool lnok) [~Node] +/; _mhf_post_value (~utils.File fin, ~Node cur, ~Token first, bool lnok) [~Node] int ln = first`.line /; loop (bool run = true; run == true && first`._type !== TTYPE_ERR) /; if (ln !== first`.line && lnok == false) @@ -1447,7 +1452,6 @@ int errors_shown = 0 id.init(NTYPE_ID, first`.data) ln = first`.line - cur = cur`.parent ~Node sub = cur`.sub.get(cur`.sub.count - 1) op.add_child(sub) @@ -1464,27 +1468,27 @@ int errors_shown = 0 ;; else if (_op_postfix(first) == true) Node op op.init(NTYPE_POST_OP, first`.data) - /; if (cur`.eq(".\0") == true) + /; if (cur`._type == NTYPE_ID) + cur`.add_child(~op) + ;; else if (cur`.sub.count > 0) cur = cur`.sub.get(cur`.sub.count - 1) cur`.add_child(~op) cur = cur`.parent - ;; else if (cur`._type == NTYPE_ID) - cur`.add_child(~op) ;; else - _ast_print_err(fin, first, "Unsure where we are at in the tree ('`'). [CMPERR]") + _ast_print_err(fin, first, "Unsure where we are at in the tree ('`'). [CMPERR]\0") op.end() return NULL ;/ first` = produce_next_token(fin, first`) ;; else if (first`.eq("(\0") == true) - /; if (cur`.eq(".\0") == true) + /; if (cur`._type == NTYPE_ID) + ln = _ast_list_value(fin, cur, first) + ;; else if (cur`.sub.count > 0) cur = cur`.sub.get(cur`.sub.count - 1) ln = _ast_list_value(fin, cur, first) cur = cur`.parent - ;; else if (cur`._type == NTYPE_ID) - ln = _ast_list_value(fin, cur, first) ;; else - _ast_print_err(fin, first, "Unsure where we are at in the tree ('('). [CMPERR]") + _ast_print_err(fin, first, "Unsure where we are at in the tree ('('). [CMPERR]\0") return NULL ;/ ;; else if (first`.eq("{\0") == true) @@ -1495,14 +1499,14 @@ int errors_shown = 0 _ast_print_err(fin, first, "Expected value for index operation after '{'\0") ;/ - /; if (cur`.eq(".\0") == true) + /; if (cur`._type == NTYPE_ID) + cur`.add_child(~ind) + ;; else if (cur`.sub.count > 0) cur = cur`.sub.get(cur`.sub.count - 1) cur`.add_child(~ind) cur = cur`.parent - ;; else if (cur`._type == NTYPE_ID) - cur`.add_child(~ind) ;; else - _ast_print_err(fin, first, "Unsure where we are at in the tree ('{'). [CMPERR]") + _ast_print_err(fin, first, "Unsure where we are at in the tree ('{'). [CMPERR]\0") ind.end() return NULL ;/ @@ -1523,12 +1527,13 @@ int errors_shown = 0 /; _mhf_inner_value(~utils.File fin, ~Node cur, ~Token first, ~int ln) [~Node] cur = _mhf_lower(cur) - #/; if (cur`._type !== NTYPE_ID && cur`.eq(".\0") == false && cur`.sub.count == 0 && first`._type == TTYPE_LITRL) + # /; if (cur`._type == NTYPE_TYPE && cur`.sub.count == 0 && first`._type == TTYPE_LITRL) # Node lt # lt.init(NTYPE_LITERAL, first`.data) # first` = produce_next_token(fin, first`) # cur = cur`.add_child(~lt) - #;/ + # cur = _mhf_lower(cur) + # ;/ cur = _mhf_post_value(fin, cur, first, true) /; if (cur == NULL) @@ -1603,6 +1608,19 @@ int errors_shown = 0 ;/ /; _mhf_outer_list(~utils.File fin, ~Node cur, ~Token first, ~int ln) [~Node] + /; if (cur`.sub.count > 0) + # Check for and remove empty value if exists + int idx = cur`.sub.count - 1 + ~Node last = cur`.sub.get(idx) + /; if (last`._type == NTYPE_VALUE) + /; if (last`.sub.count == 0) + # Remove it + last`.end() + cur`.sub.pop() + ;/ + ;/ + ;/ + /; loop (first`._type !== TTYPE_ERR && first`.eq(")\0") == false) _ast_value(fin, cur, first, true) @@ -1627,10 +1645,10 @@ int errors_shown = 0 ~int p1 = cur ~int p2 = mod int ln = first`.line - bool firstRun = true - + # While we are in subtree /; loop (bool run = true; run == true && p1 !== p2 && first`._type !== TTYPE_ERR) + ~Node tmp = cur # First loop for while we are in value /; if (tmp`._type == NTYPE_VALUE) @@ -1778,7 +1796,6 @@ int errors_shown = 0 ~Node cur = _mhf_post(fin, ~out, first, lnok) /; if (cur == NULL) _mhf_finish_decl(fin, ~out, first, lnok) - ;; else cur = _mhf_transform(~out, cur) _mhf_finish_value(fin, ~out, first, cur, lnok) @@ -1848,8 +1865,9 @@ int errors_shown = 0 return ;/ - - ~uint8 rel = utils.unquote_str(first`.data) + + utils.Vector relv = utils.unquote_str(first`.data) + ~uint8 rel = relv.as_cstr() utils.File imp = fin`.relative(rel) _delete(rel) @@ -1880,7 +1898,8 @@ int errors_shown = 0 ;/ Node an - an.init(NTYPE_ASM, utils.unquote_str(first`.data)) + utils.Vector asmv = utils.unquote_str(first`.data) + an.init(NTYPE_ASM, asmv.as_cstr()) mod`.add_child(~an) Token tmp = produce_next_token(fin, first`) @@ -2096,7 +2115,7 @@ int errors_shown = 0 ;; else if (first._type == TTYPE_KEYTP || first._type == TTYPE_USRWD || first.eq("~\0") == true || first.eq("{\0") == true) _ast_decl(fin, mod, ~first) ;; else - _ast_print_err(fin, ~first, "Expected 'import', 'struct', 'asm', block, or declaration in top level\0") + _ast_print_err(fin, ~first, "Expected 'import', 'struct', 'asm', block (starts with '/;'), or type (for variable declaration) in top level\0") Token tmp = produce_next_token(fin, first) first.end() @@ -2115,7 +2134,7 @@ int errors_shown = 0 utils.Vector v v.init(1) - out.init(NTYPE_MODULE, v.as_cstr()) + out.init(NTYPE_EXPORT, v.as_cstr()) _ast_file(fin, ~out) @@ -2187,7 +2206,7 @@ int errors_shown = 0 ;/ /; print_node_head (~Node n) - _printf("{ NODE_TYPE: \0") + _printf("{ \0") print_node_type(n) _printf(", DATA: \0") _printf(n`.data) diff --git a/tnslc/parse/tokenizer.tnsl b/tnslc/parse/tokenizer.tnsl index b60e57c..5c742b1 100644 --- a/tnslc/parse/tokenizer.tnsl +++ b/tnslc/parse/tokenizer.tnsl @@ -428,7 +428,7 @@ uint MAX_MULTI = 3 /; print_token_list (~utils.Vector vec) ~Token tok /; loop (uint i = 0; i < vec`.count) [i++] - tok = vec.get(i) + tok = vec`.get(i) print_token(tok`) ;/ ;/ |