add tuples
[ccc.git] / ast.c
diff --git a/ast.c b/ast.c
index 97bd1ad..95ef8fd 100644 (file)
--- 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;