[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[] = {
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();
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);
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;
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;
//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;
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);
| 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