-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.c
99 lines (77 loc) · 2.31 KB
/
ast.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
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
97
98
99
#include <stdlib.h> // for malloc
#include "ast.h" // AST header
#include <string.h> // for memcpy
Expr* ast_integer(int v) {
Expr* node = (Expr*) malloc(sizeof(Expr));
node->type = E_INTEGER;
node->c.value = v;
return node;
}
Expr* ast_variable(char* id){
Expr* node = (Expr*) malloc(sizeof(Expr));
node->type = E_IDENTIF;
memcpy(node->c.string, id, VARSIZE);
return node;
}
Expr* ast_operation(Expr* left, int operator, Expr* right) {
Expr* node = (Expr*) malloc(sizeof(Expr));
node->type = E_OPERATION;
node->c.op.operator = operator;
node->c.op.left = left;
node->c.op.right = right;
return node;
}
Command* ast_atrib(char* id,Expr* e){
Command* node = (Command*) malloc(sizeof(Command));
node->type = CMD_ATRIB;
memcpy(node->c.attrib.identifier, id, VARSIZE);
node->c.attrib.e = e;
return node;
}
Command* ast_while(Expr* condition,CommandList* commands){
Command* node = (Command*) malloc(sizeof(Command));
node->type = CMD_WHILE;
node->c.while_exp.condition = condition;
node->c.while_exp.commands = commands;
return node;
}
Command* ast_for(Command* atrib, Expr* condition, Expr* increment, CommandList* commands){
Command* node = (Command*) malloc(sizeof(Command));
node->type = CMD_FOR;
node->c.for_exp.right = condition;
node->c.for_exp.left = atrib;
node->c.for_exp.increment = increment;
node->c.for_exp.commands = commands;
return node;
}
Command* ast_ifs (Expr* e, CommandList* list, Command* elses) {
Command* node = (Command*) malloc(sizeof(Command));
node->type = CMD_IFS;
node->c.ifs.e = e;
node->c.ifs.list = list;
node->c.ifs.elses = elses;
return node;
}
Command* ast_elses (CommandList* elses) {
Command* node = (Command*) malloc(sizeof(Command));
node->type = CMD_ELSES;
node->c.elses = elses;
return node;
}
Command* ast_disp(Expr* e){
Command* node = (Command*) malloc(sizeof(Expr));
node->type = CMD_DIS;
node->c.condition = e;
return node;
}
Command* ast_inp(char* id){
Command* node = (Command*) malloc(sizeof(Command));
node->type = CMD_INP;
memcpy(node->c.input.identifier, id, VARSIZE);
return node;
}
CommandList* ast_CommandList(Command* cmd,CommandList* next) {
CommandList* node = (CommandList*) malloc(sizeof(CommandList));
node->cmd = cmd;
node-> next= next;
}