-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.h
96 lines (76 loc) · 1.58 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// AST definitions
#ifndef __ast_h__
#define __ast_h__
// AST for expressions
struct _Expr {
enum {
E_INTEGER,
E_OPERATION,
E_ID,
} kind;
union {
int value; // for integer values
char* id;
struct {
int operator; // PLUS, MINUS, etc
struct _Expr* left;
struct _Expr* right;
} op; // for binary expressions
} attr;
};
//AST for commands
struct _Cmd {
enum {
C_ASG,
C_WHILE,
C_IF,
C_IF_ELSE,
C_PRINT,
C_SCAN
}kind;
union {
struct {
char* id;
struct _Expr* expr;
} _asg;
struct {
struct _Expr* expr;
struct _CmdList* cmds;
} _while;
struct {
struct _Expr* expr;
struct _CmdList* cmds;
}_if;
struct {
struct _Expr* expr;
struct _CmdList* if_part;
struct _CmdList* else_part;
}_if_else;
struct {
char* id;
}_scan;
struct{
char* id;
}_print;
} attr;
};
struct _CmdList {
struct _Cmd* cmd;
struct _CmdList* next;
};
typedef struct _Expr Expr;
typedef struct _BoolExpr BoolExpr;
typedef struct _Cmd Cmd;
typedef struct _CmdList CmdList;
// Constructor functions (see implementation in ast.c)
Expr* ast_integer(int v);
Expr* ast_operation(int operator, Expr* left, Expr* right);
Expr* ast_id (char* id);
Cmd* ast_if(Expr* expr, CmdList* cmd);
Cmd* ast_if_else(Expr* expr, CmdList* if_part, CmdList* else_part);
Cmd* ast_while(Expr* expr, CmdList* cmds);
Cmd* ast_atribution(char* id, Expr* expr);
Cmd* ast_scan(char* id);
Cmd* ast_print(char* id);
CmdList* ast_cmdlist(Cmd* cmd, CmdList* next);
#endif