diff options
author | Camil Staps | 2018-04-03 19:51:47 +0200 |
---|---|---|
committer | Camil Staps | 2018-04-03 19:51:47 +0200 |
commit | 23bfe904b6d572e96314d3b38908103c2efec939 (patch) | |
tree | 810dbbdd5972b07934343d96a44b3d06de7812af | |
parent | Change _H_* to _H_FUSPEL_* for compatibility with other libraries (diff) |
Resolve #5: list notation shortcut in patterns
-rw-r--r-- | interpreter/parse.c | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/interpreter/parse.c b/interpreter/parse.c index 6c3ed24..9de6a15 100644 --- a/interpreter/parse.c +++ b/interpreter/parse.c @@ -104,23 +104,51 @@ bool parse_simple_expression(struct expression *expr, struct parsing_list *list) expr->var2 = my_calloc(1, sizeof(struct expression)); if (!parse_simple_expression(expr->var1, list) || - list->tokens->elems[list->i].kind != TOKEN_COLON) { + (list->tokens->elems[list->i].kind != TOKEN_COLON && + list->tokens->elems[list->i].kind != TOKEN_COMMA && + list->tokens->elems[list->i].kind != TOKEN_CLOSE_SQ)) { free_expression(expr->var1); return false; + } else if (list->tokens->elems[list->i].kind == TOKEN_CLOSE_SQ) { + ((struct expression *) expr->var2)->kind = EXPR_LIST; + ((struct expression *) expr->var2)->var1 = NULL; + ((struct expression *) expr->var2)->var2 = NULL; + list->i++; + return true; + } else if (list->tokens->elems[list->i].kind == TOKEN_COMMA) { + struct expression *_expr = expr; + bool loop = true; + while (loop && list->tokens->elems[list->i].kind == TOKEN_COMMA) { + _expr = _expr->var2; + _expr->kind = EXPR_LIST; + _expr->var1 = my_calloc(1, sizeof(struct expression)); + _expr->var2 = my_calloc(1, sizeof(struct expression)); + list->i++; + loop = parse_simple_expression(_expr->var1, list); + } + if (list->i >= list->tokens->length || + (list->tokens->elems[list->i].kind != TOKEN_CLOSE_SQ && + list->tokens->elems[list->i].kind != TOKEN_COLON)) { + free_expression(expr); + return false; + } else if (list->tokens->elems[list->i].kind == TOKEN_COLON) { + list->i++; + if (!parse_simple_expression(((struct expression*) expr->var2)->var2, list) || + list->tokens->elems[list->i].kind != TOKEN_CLOSE_SQ) { + free_expression(expr); + return false; + } else { + list->i++; + return true; + } + } else if (list->tokens->elems[list->i].kind == TOKEN_CLOSE_SQ) { + ((struct expression*) _expr->var2)->kind = EXPR_LIST; + ((struct expression*) _expr->var2)->var1 = NULL; + ((struct expression*) _expr->var2)->var2 = NULL; + list->i++; + return true; + } } - - list->i++; - if (!parse_simple_expression(expr->var2, list) || - list->tokens->elems[list->i].kind != TOKEN_CLOSE_SQ) { - free_expression(expr->var1); - my_free(expr->var1); - free_expression(expr->var2); - my_free(expr->var2); - return false; - } - - list->i++; - return true; default: return false; |