From cf10c82fe7c9bff5aa86572aef29d91cd3e463b6 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 11 Nov 2014 01:26:11 +0100 Subject: mds-kbdc: if, else if and else MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/mds-kbdc/make-tree.c | 51 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/mds-kbdc/make-tree.c b/src/mds-kbdc/make-tree.c index a09773f..1e85094 100644 --- a/src/mds-kbdc/make-tree.c +++ b/src/mds-kbdc/make-tree.c @@ -457,8 +457,53 @@ int parse_to_tree(const char* restrict filename, mds_kbdc_tree_t** restrict resu NAMES_1(name); BRANCH("macro"); } - else if (!strcmp(line, "if")) ; - else if (!strcmp(line, "else")) ; + else if (!strcmp(line, "if")) + { + NEW_NODE(if, IF); + CHARS(condition); + CHARS_END; + BRANCH("if"); + } + else if (!strcmp(line, "else")) + { + if (stack_ptr == 0) + { + NEW_ERROR(1, ERROR, "runaway ‘else’ statement"); + goto next; + } + line += strlen(line); + *end = prev_end_char, prev_end_char = '\0'; + end = line + strlen(line); + stack_ptr--; + while (*line && (*line == ' ')) + line++; + if (strcmp(keyword_stack[stack_ptr], "if")) + { + line = original, end = line + strlen(line); + NEW_ERROR(1, ERROR, "runaway ‘else’ statement"); + } + else if (*line == '\0') + { + /* else */ + mds_kbdc_tree_if_t* node = &(tree_stack[stack_ptr][0]->if_); + tree_stack[stack_ptr + 1] = &(node->otherwise); + keyword_stack[stack_ptr++] = "if"; + } + else if ((strstr(line, "if") == line) && ((line[3] == ' ') || (line[3] == '\0'))) + { + /* else if */ + NEW_NODE(if, IF); + tree_stack[stack_ptr][0]->if_.otherwise = (mds_kbdc_tree_t*)node; + line += 2; + CHARS(condition); + CHARS_END; + BRANCH("if"); + } + else + { + NEW_ERROR(1, ERROR, "expecting nothing or ‘if’"); + } + } else if (!strcmp(line, "for")) ; else if (!strcmp(line, "let")) ; else if (!strcmp(line, "have")) ; @@ -480,7 +525,7 @@ int parse_to_tree(const char* restrict filename, mds_kbdc_tree_t** restrict resu { if (stack_ptr == 0) { - NEW_ERROR(1, ERROR, "stray ‘end’ statement"); + NEW_ERROR(1, ERROR, "runaway ‘end’ statement"); goto next; } line += strlen(line); -- cgit v1.2.3-70-g09d2