6 struct ast
*ast_alloc()
8 struct ast
*res
= malloc(sizeof(struct ast
));
16 struct ast
*ast_cons(struct ast
*el
, struct ast
*tail
)
18 struct ast
*res
= ast_alloc();
20 res
->data
.an_cons
.el
= el
;
21 res
->data
.an_cons
.tail
= tail
;
25 struct ast
*ast_binop(struct ast
*l
, enum binop op
, struct ast
*r
)
27 struct ast
*res
= ast_alloc();
29 res
->data
.an_binop
.l
= l
;
30 res
->data
.an_binop
.op
= op
;
31 res
->data
.an_binop
.r
= r
;
35 struct ast
*ast_int(int integer
)
37 struct ast
*res
= ast_alloc();
39 res
->data
.an_int
= integer
;
43 static const char *binop_str
[] = {
50 void ast_print(struct ast
* ast
, FILE *out
)
57 ast_print(ast
->data
.an_binop
.l
, out
);
58 fprintf(out
, "%s", binop_str
[ast
->data
.an_binop
.op
]);
59 ast_print(ast
->data
.an_binop
.r
, out
);
63 ast_print(ast
->data
.an_cons
.el
, out
);
65 ast_print(ast
->data
.an_cons
.tail
, out
);
68 fprintf(out
, "%d", ast
->data
.an_int
);
71 fprintf(stderr
, "Unsupported AST node\n");
76 void ast_free(struct ast
*ast
)
82 ast_free(ast
->data
.an_binop
.l
);
83 ast_free(ast
->data
.an_binop
.r
);
86 ast_free(ast
->data
.an_cons
.el
);
87 ast_free(ast
->data
.an_cons
.tail
);
92 fprintf(stderr
, "Unsupported AST node\n");