-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.c
More file actions
83 lines (70 loc) · 1.84 KB
/
ast.c
File metadata and controls
83 lines (70 loc) · 1.84 KB
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
// AST constructor functions
#include <stdlib.h> // for malloc
#include "ast.h" // AST header
Expr* ast_integer(int v) {
Expr* node = (Expr*) malloc(sizeof(Expr));
node->kind = E_INTEGER;
node->attr.value = v;
return node;
}
Expr* ast_operation(int operator, Expr* left, Expr* right) {
Expr* node = (Expr*) malloc(sizeof(Expr));
node->kind = E_OPERATION;
node->attr.op.operator = operator;
node->attr.op.left = left;
node->attr.op.right = right;
return node;
}
Expr* ast_id (char* id) {
Expr* node = malloc(sizeof(Expr));
node->kind = E_ID;
node->attr.id = id;
return node;
}
Cmd* ast_atribution(char* id, Expr* expr){
Cmd* node = (Cmd*) malloc(sizeof(Cmd));
node->kind = C_ASG;
node->attr._asg.id = id;
node->attr._asg.expr = expr;
return node;
}
Cmd* ast_while(Expr* expr, CmdList* cmds) {
Cmd* node = (Cmd*) malloc(sizeof(Cmd*));
node->kind = C_WHILE;
node->attr._while.expr = expr;
node->attr._while.cmds = cmds;
return node;
}
Cmd* ast_if(Expr* expr, CmdList* cmds) {
Cmd* node = (Cmd*) malloc(sizeof(Cmd));
node->kind = C_IF;
node->attr._if.expr = expr;
node->attr._if.cmds = cmds;
return node;
}
Cmd* ast_if_else(Expr* expr, CmdList* if_part, CmdList* else_part) {
Cmd* node = (Cmd*) malloc(sizeof(Cmd));
node->kind = C_IF_ELSE;
node->attr._if_else.expr = expr;
node->attr._if_else.if_part = if_part;
node->attr._if_else.else_part = else_part;
return node;
}
CmdList* ast_cmdlist(Cmd* cmd, CmdList* next){
CmdList* node = (CmdList*) malloc(sizeof(CmdList));
node->cmd = cmd;
node->next = next;
return node;
}
Cmd* ast_scan(char* id){
Cmd* node = (Cmd*) malloc(sizeof(Cmd));
node->kind = C_SCAN;
node->attr._scan.id = id;
return node;
}
Cmd* ast_print(char* id){
Cmd* node = (Cmd*) malloc(sizeof(Cmd));
node->kind = C_PRINT;
node->attr._print.id = id;
return node;
}