-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.h
84 lines (75 loc) · 1.93 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
// AST definitions
#ifndef __ast_h__
#define __ast_h__
#define VARSIZE 64
typedef struct _Expr Expr;
typedef struct _Command Command;
typedef struct _CommandList CommandList;
// AST for expressions
struct _Expr {
enum {
E_INTEGER,
E_IDENTIF,
E_OPERATION
} type;
union {
int value; // for integer values
char string[VARSIZE];
struct {
int operator; // PLUS, MINUS, etc
Expr* left;
Expr* right;
} op; // for binary expressions
} c;
};
// for commands
struct _Command{
enum{
CMD_ATRIB, CMD_IFS, CMD_ELSES, CMD_WHILE, CMD_FOR, CMD_DIS, CMD_INP
} type;
union{
char identifier[VARSIZE];
Expr* condition;
CommandList* elses;
struct {
Expr* e;
CommandList* list;
struct _Command* elses;
} ifs;
struct{
Expr* condition;
CommandList* commands;
} while_exp;
struct{
Command* left;
Expr* right;
Expr* increment;
CommandList* commands;
} for_exp;
struct{
char identifier[VARSIZE];
Expr* e;
} attrib;
struct{
char identifier[VARSIZE];
} input;
} c;
};
// List for commands
struct _CommandList{
Command* cmd;
CommandList* next;
};
// Constructor functions (see implementation in ast.c)
Expr* ast_integer(int v);
Expr* ast_variable(char* identifier);
Expr* ast_operation(Expr* left, int operator, Expr* right);
Command* ast_atrib(char* identifier, Expr* e);
Command* ast_ifs(Expr* e, CommandList* list, Command* elses);
Command* ast_elses(CommandList* elses);
Command* ast_while(Expr* condition, CommandList* commands);
Command* ast_for(Command* atrib, Expr* condition, Expr* increment, CommandList* commands);
Command* ast_disp(Expr* e);
Command* ast_inp(char* identifier);
CommandList* ast_CommandList(Command* cmd,CommandList* next);
#endif