-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
39 lines (33 loc) · 1.16 KB
/
tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "defs.h"
#include "data.h"
#include "decl.h"
// AST tree functions
// Copyright (c) 2019 Warren Toomey, GPL3
// Modification (c) 2022 Emin, GPL3
// Given an operator and two AST subtrees, return an AST tree with that operator
// in the root. The integer value is only used for A_INTLIT operators.
struct ASTnode* create_ast_node(int op,
struct ASTnode* left,
struct ASTnode* right,
int intvalue) {
// Malloc a new ASTnode
struct ASTnode* n = (struct ASTnode*)malloc(sizeof(struct ASTnode));
if (n == NULL) {
fprintf(stderr, "Unable to malloc in create_ast_node\n");
exit(1);
}
// Set up the contents
n->op = op;
n->left = left;
n->right = right;
n->v.intvalue = intvalue;
return n;
}
// Build and return an AST leaf node
struct ASTnode* create_ast_leaf(int op, int intvalue) {
return (create_ast_node(op, NULL, NULL, intvalue));
}
// Build and return an AST unary operator node
struct ASTnode* create_ast_unary(int op, struct ASTnode* left, int intvalue) {
return (create_ast_node(op, left, NULL, intvalue));
}