-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.h
72 lines (58 loc) · 1.4 KB
/
ast.h
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
60
61
62
63
64
65
66
67
68
69
70
71
72
/* ----------------------------------------------------------
ABSTRACT SYNTAX TREE HEADER
Typedefs and enumerations related to the AST.
The AST is a simple binary tree, with a Value attached to
each node. The value is represented by a tagged union. This
union must grow when the language grows to handle new values
such as strings and statements.
The Value struct also shows what types we choose to represent
the internal values with. For example, we use long long for
every number Value, and an operator is simply an enumeration
of Operators.
---------------------------------------------------------- */
#ifndef ast_h
#define ast_h
#include "token.h"
typedef enum {
OP_ADD,
OP_SUB,
OP_MUL,
OP_DIV
} Operator;
typedef enum {
OPERATOR,
NUMBER,
ERROR,
} ValueType;
typedef struct {
ValueType type;
union {
Operator op;
long long number;
};
} Value;
typedef struct Node Node;
typedef struct Node {
const char* err;
Token t;
Value* value;
Node* lc;
Node* rc;
} Node;
typedef struct {
int error;
Node* root;
} Ast;
Node* consNode(Node*, Value*, Node*);
Node* consErrorNode(const char*, Token);
Value* ttov(Token);
void freeAst(Node*);
void reportErrors(Node*);
void printAst(Node*);
void printNodeParens(Node*);
void printNode(Node*);
void printValue(Value*);
void printOp(Value*);
void printNum(Value*);
void printErrorNode(Node*);
#endif