aboutsummaryrefslogtreecommitdiff
path: root/node.h
blob: f98257fc691b668f9d51fd0ccb84e45f9e0dc5ad (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once

#include "type.h"


enum node_type {
	N_LIST,  // [head] [tail]
	N_LIST_END,  // --
	N_BLOCK,  // [body]
	N_VAR_DECL_INIT,  // rtype name [value]
	N_VAR_DECL,  // rtype name
	N_FUNC_DECL,  // rtype name [params] [body]
	N_IF,  // [cond] [body] [else-body]
	N_WHILE,  // [cond] [body]
	N_RETURN,  // --
	N_RETURNV,  // [value]
	N_NUM,  // value
	N_VAR,  // name
	N_BINOP,  // [left] oper [right]
	N_UNOP,  // oper [arg]
	N_CALL,  // name [args]
};

enum operator {
	// binary
	OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD,
	OP_EQ, OP_NEQ, OP_LT, OP_GT, OP_LEQ, OP_GEQ,
	OP_AND, OP_OR,
	OP_ASSIGN,
	// unary
	OP_NEG, OP_NOT, OP_DEREF, OP_ADDROF,
};

struct node {
	enum node_type type;
	struct node *child1, *child2, *child3;
	struct type *rtype;
	char *name;
	int value;
	enum operator oper;
};


struct node* node_make_0(enum node_type type);
struct node* node_make_1(enum node_type type, struct node *c1);
struct node* node_make_2(enum node_type type, struct node *c1, struct node *c2);
struct node* node_make_3(enum node_type type, struct node *c1, struct node *c2, struct node *c3);

__attribute__((sentinel))
struct node* node_make_list_(struct node *first, ...);
#define node_make_list(...) node_make_list_(__VA_ARGS__, NULL)

int node_list_length(struct node *node);

void node_delete_recursive(struct node *node);

const char* node_type_string(enum node_type type);
const char* oper_string(enum operator oper);
void node_print(const struct node *node, FILE *f, int indent);