From: Mart Lubbers Date: Mon, 8 Feb 2021 07:39:42 +0000 (+0100) Subject: add tuples X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=74029a48cbc1e62f84bb82d7f00ece954389ec65;p=ccc.git add tuples --- diff --git a/ast.c b/ast.c index 97bd1ad..95ef8fd 100644 --- a/ast.c +++ b/ast.c @@ -12,7 +12,8 @@ static const char *ast_type_str[] = { [an_fundecl] = "fundecl", [an_ident] = "ident", [an_if] = "if", [an_int] = "int", [an_nil] = "nil", [an_list] = "list", [an_return] = "return", [an_stmt_expr] = "stmt_expr", - [an_unop] = "unop", [an_vardecl] = "vardecl", [an_while] = "while", + [an_tuple] = "tuple", [an_unop] = "unop", [an_vardecl] = "vardecl", + [an_while] = "while", }; #endif static const char *binop_str[] = { @@ -276,6 +277,16 @@ struct ast *ast_stmt_expr(struct ast *expr) return res; } +struct ast *ast_tuple(struct ast *left, struct ast *right) +{ + struct ast *res = ast_alloc(); + res->type = an_tuple; + res->data.an_tuple.left = left; + res->data.an_tuple.right = right; + return res; +} + + struct ast *ast_unop(enum unop op, struct ast *l) { struct ast *res = ast_alloc(); @@ -437,6 +448,13 @@ void ast_print(struct ast *ast, int indent, FILE *out) ast_print(ast->data.an_stmt_expr, indent, out); safe_fprintf(out, ";\n"); break; + case an_tuple: + safe_fprintf(out, "("); + ast_print(ast->data.an_tuple.left, indent, out); + safe_fprintf(out, ", "); + ast_print(ast->data.an_tuple.right, indent, out); + safe_fprintf(out, ")"); + break; case an_unop: safe_fprintf(out, "(%s", unop_str[ast->data.an_unop.op]); ast_print(ast->data.an_unop.l, indent, out); @@ -531,6 +549,10 @@ void ast_free(struct ast *ast) case an_stmt_expr: ast_free(ast->data.an_stmt_expr); break; + case an_tuple: + ast_free(ast->data.an_tuple.left); + ast_free(ast->data.an_tuple.right); + break; case an_unop: ast_free(ast->data.an_unop.l); break; diff --git a/ast.h b/ast.h index 13d3c58..12263a7 100644 --- a/ast.h +++ b/ast.h @@ -14,7 +14,7 @@ enum unop {negate,inverse}; enum ast_type { an_assign, an_binop, an_bool, an_char, an_cons, an_funcall, an_fundecl, an_ident, an_if, an_int, an_list, an_nil, an_return, an_stmt_expr, - an_unop, an_vardecl, an_while + an_tuple, an_unop, an_vardecl, an_while }; struct ast { enum ast_type type; @@ -66,6 +66,10 @@ struct ast { //struct { } an_nil; struct ast *an_return; struct ast *an_stmt_expr; + struct { + struct ast *left; + struct ast *right; + } an_tuple; struct { enum unop op; struct ast *l; @@ -97,6 +101,7 @@ struct ast *ast_list(struct ast *llist); struct ast *ast_nil(); struct ast *ast_return(struct ast *rtrn); struct ast *ast_stmt_expr(struct ast *expr); +struct ast *ast_tuple(struct ast *left, struct ast *right); struct ast *ast_unop(enum unop op, struct ast *l); struct ast *ast_vardecl(struct ast *ident, struct ast *l); struct ast *ast_while(struct ast *pred, struct ast *body); diff --git a/parse.y b/parse.y index c79a1ec..4311833 100644 --- a/parse.y +++ b/parse.y @@ -114,6 +114,7 @@ expr | expr POWER expr { $$ = ast_binop($1, power, $3); } | MINUS expr { $$ = ast_unop(negate, $2); } | INVERSE expr %prec TIMES { $$ = ast_unop(inverse, $2); } + | BOPEN expr COMMA expr BCLOSE { $$ = ast_tuple($2, $4); } | BOPEN expr BCLOSE { $$ = $2; } | IDENT BOPEN fargs BCLOSE { $$ = ast_funcall($1, ast_list($3)); } | INTEGER diff --git a/todo.txt b/todo.txt index e4b1181..edf9d6f 100644 --- a/todo.txt +++ b/todo.txt @@ -1,3 +1,2 @@ parser: -- tuples - types