diff options
Diffstat (limited to '')
-rw-r--r-- | src/mds-kbdc/make-tree.c | 66 | ||||
-rw-r--r-- | test-files/mds-kbdc/make-tree/valid/array | 36 | ||||
-rw-r--r-- | test-files/mds-kbdc/make-tree/valid/for | 17 | ||||
-rw-r--r-- | test-files/mds-kbdc/make-tree/valid/let | 14 |
4 files changed, 109 insertions, 24 deletions
diff --git a/src/mds-kbdc/make-tree.c b/src/mds-kbdc/make-tree.c index 13bf682..533b028 100644 --- a/src/mds-kbdc/make-tree.c +++ b/src/mds-kbdc/make-tree.c @@ -717,20 +717,23 @@ int parse_to_tree(const char* restrict filename, mds_kbdc_tree_t** restrict resu end = line + strlen(line); END; line = end, prev_end_char = '\0'; - stack_ptr--; in_array = 0; + stack_ptr -= 2; + tree_stack[stack_ptr] = &(tree_stack[stack_ptr][0]->next); break; } - { + else + { #define node subnode - NEW_NODE(string, STRING); - NO_JUMP; - CHARS(string); - LEAF; - *end = prev_end_char; - line = end; + NEW_NODE(string, STRING); + NO_JUMP; + CHARS(string); + LEAF; + node->loc_end = (size_t)(end - source_code.lines[line_i]); + *end = prev_end_char; + line = end; #undef node - } + } } continue; } @@ -877,10 +880,19 @@ int parse_to_tree(const char* restrict filename, mds_kbdc_tree_t** restrict resu NEW_NODE(let, LET); CHARS(variable); TEST_FOR_KEYWORD(":"); - LEAF; *end = prev_end_char; while (*line && (*line == ' ')) line++; + if (*line == '{') + { +#define inner value + BRANCH(NULL); +#undef inner + } + else + { + LEAF; + } if (*line == '\0') { line = original, end = line + strlen(line), prev_end_char = '\0'; @@ -892,6 +904,7 @@ int parse_to_tree(const char* restrict filename, mds_kbdc_tree_t** restrict resu NEW_NODE(string, STRING); NO_JUMP; CHARS(string); + node->loc_end = (size_t)(end - source_code.lines[line_i]); #undef node node->value = (mds_kbdc_tree_t*)subnode; END; @@ -899,8 +912,8 @@ int parse_to_tree(const char* restrict filename, mds_kbdc_tree_t** restrict resu else { #define node subnode - NEW_NODE(array, ARRAY); /* FIXME memory leak */ #define inner elements + NEW_NODE(array, ARRAY); BRANCH("}"); node->loc_end = node->loc_start + 1; #undef inner @@ -1006,21 +1019,26 @@ int parse_to_tree(const char* restrict filename, mds_kbdc_tree_t** restrict resu { char* line = NULL; char* end = NULL; - NEW_ERROR(0, ERROR, "premature end of file"); - while (stack_ptr--) + while (stack_ptr && keyword_stack[stack_ptr - 1] == NULL) + stack_ptr--; + if (stack_ptr) { - if (keyword_stack[stack_ptr] == NULL) - continue; - line_i = tree_stack[stack_ptr][0]->loc_line; - line = source_code.lines[line_i] + tree_stack[stack_ptr][0]->loc_start; - end = source_code.lines[line_i] + tree_stack[stack_ptr][0]->loc_end; - if (!strcmp(keyword_stack[stack_ptr], "}")) - { - NEW_ERROR(1, NOTE, "missing associated ‘%s’", keyword_stack[stack_ptr]); - } - else + NEW_ERROR(0, ERROR, "premature end of file"); + while (stack_ptr--) { - NEW_ERROR(1, NOTE, "missing associated ‘end %s’", keyword_stack[stack_ptr]); + if (keyword_stack[stack_ptr] == NULL) + continue; + line_i = tree_stack[stack_ptr][0]->loc_line; + line = source_code.lines[line_i] + tree_stack[stack_ptr][0]->loc_start; + end = source_code.lines[line_i] + tree_stack[stack_ptr][0]->loc_end; + if (!strcmp(keyword_stack[stack_ptr], "}")) + { + NEW_ERROR(1, NOTE, "missing associated ‘%s’", keyword_stack[stack_ptr]); + } + else + { + NEW_ERROR(1, NOTE, "missing associated ‘end %s’", keyword_stack[stack_ptr]); + } } } } diff --git a/test-files/mds-kbdc/make-tree/valid/array b/test-files/mds-kbdc/make-tree/valid/array new file mode 100644 index 0000000..31b0c73 --- /dev/null +++ b/test-files/mds-kbdc/make-tree/valid/array @@ -0,0 +1,36 @@ +let \1 : { 1 2 4 8 16 32 48 } +let \2 : { 1 2 4 8 + 16 32 48 + } + +# (let (@ 1 0-3) ‘\1’ +# (.value nil +# (array (@ 1 9-10) +# (.elements +# (string (@ 1 11-12) ‘1’) +# (string (@ 1 13-14) ‘2’) +# (string (@ 1 15-16) ‘4’) +# (string (@ 1 17-18) ‘8’) +# (string (@ 1 19-21) ‘16’) +# (string (@ 1 22-24) ‘32’) +# (string (@ 1 25-27) ‘48’) +# ) +# ) +# ) +# ) +# (let (@ 2 0-3) ‘\2’ +# (.value nil +# (array (@ 129-10) +# (.elements +# (string (@ 2 11-12) ‘1’) +# (string (@ 2 13-14) ‘2’) +# (string (@ 2 15-16) ‘4’) +# (string (@ 2 17-18) ‘8’) +# (string (@ 3 11-13) ‘16’) +# (string (@ 3 14-16) ‘32’) +# (string (@ 3 17-19) ‘48’) +# ) +# ) +# ) +# ) + diff --git a/test-files/mds-kbdc/make-tree/valid/for b/test-files/mds-kbdc/make-tree/valid/for new file mode 100644 index 0000000..d8d549a --- /dev/null +++ b/test-files/mds-kbdc/make-tree/valid/for @@ -0,0 +1,17 @@ +for 0 to 9 as \1 + <letter \add(\1, "0")> : \add(\1, "0") +end for + +# (for (@ 1 0-3) ‘0’ ‘9’ (.variable ‘\1’) +# (.inner +# (map (@ 2 2-2) +# (.sequence +# (keys (@ 2 2-24) ‘<letter \add(\1, "0")>’) +# ) +# (.result +# (string (@ 2 27-40) ‘\add(\1, "0")’) +# ) +# ) +# ) +# ) + diff --git a/test-files/mds-kbdc/make-tree/valid/let b/test-files/mds-kbdc/make-tree/valid/let new file mode 100644 index 0000000..aa0c5da --- /dev/null +++ b/test-files/mds-kbdc/make-tree/valid/let @@ -0,0 +1,14 @@ +let \1 : "0" +let \2 : 48 + +# (let (@ 1 0-3) ‘\1’ +# (.value +# (string (@ 1 9-12) ‘"0"’) +# ) +# ) +# (let (@ 2 0-3) ‘\2’ +# (.value +# (string (@ 2 9-11) ‘48’) +# ) +# ) + |