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 struct ast
*ast_unop(enum unop op
, struct ast
*l
)
45 struct ast
*res
= ast_alloc();
47 res
->data
.an_unop
.op
= op
;
48 res
->data
.an_unop
.l
= l
;
51 static const char *binop_str
[] = {
68 static const char *unop_str
[] = {
73 void ast_print(struct ast
* ast
, FILE *out
)
80 ast_print(ast
->data
.an_binop
.l
, out
);
81 fprintf(out
, "%s", binop_str
[ast
->data
.an_binop
.op
]);
82 ast_print(ast
->data
.an_binop
.r
, out
);
86 ast_print(ast
->data
.an_cons
.el
, out
);
88 ast_print(ast
->data
.an_cons
.tail
, out
);
91 fprintf(out
, "%d", ast
->data
.an_int
);
95 ast_print(ast
->data
.an_unop
.l
, out
);
99 fprintf(stderr
, "Unsupported AST node\n");
104 void ast_free(struct ast
*ast
)
110 ast_free(ast
->data
.an_binop
.l
);
111 ast_free(ast
->data
.an_binop
.r
);
114 ast_free(ast
->data
.an_cons
.el
);
115 ast_free(ast
->data
.an_cons
.tail
);
120 fprintf(stderr
, "Unsupported AST node\n");