-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.c
122 lines (90 loc) · 3.07 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "ast.h"
#include <stdlib.h>
struct ast_position ast_position_create(uint32_t row, uint32_t col) {
struct ast_position pos;
pos.row = row;
pos.col = col;
return pos;
}
struct ast_literal ast_literal_create(enum ast_literal_type type, char * value, struct ast_position pos) {
struct ast_literal literal;
literal.type = type;
literal.value = value;
literal.pos = pos;
return literal;
}
void ast_literal_discard(struct ast_literal literal) {
free(literal.value);
}
struct ast_transformation_args *
ast_transformation_args_new(struct ast_literal argument, struct ast_transformation_args * next) {
struct ast_transformation_args * args = malloc(sizeof(struct ast_transformation_args));
args->argument = argument;
args->next = next;
return args;
}
void ast_transformation_args_delete(struct ast_transformation_args * transformation_args) {
struct ast_transformation_args * next = transformation_args;
struct ast_transformation_args * current;
while (next) {
current = next;
next = current->next;
ast_literal_discard(current->argument);
free(current);
}
}
struct ast_transformation_args *
ast_transformation_args_reverse(struct ast_transformation_args * transformation_args) {
struct ast_transformation_args * next = transformation_args;
struct ast_transformation_args * result = NULL;
struct ast_transformation_args * current;
while (next) {
current = next;
next = current->next;
result = ast_transformation_args_new(current->argument, result);
free(current);
}
return result;
}
struct ast_transformation
ast_transformation_create(char * module, char * name, struct ast_transformation_args * args, struct ast_position pos) {
struct ast_transformation transformation;
transformation.module = module;
transformation.name = name;
transformation.args = args;
transformation.pos = pos;
return transformation;
}
void ast_transformation_discard(struct ast_transformation transformation) {
free(transformation.module);
free(transformation.name);
ast_transformation_args_delete(transformation.args);
}
struct ast_script * ast_script_new(struct ast_transformation transformation, struct ast_script * next) {
struct ast_script * script = malloc(sizeof(struct ast_script));
script->transformation = transformation;
script->next = next;
return script;
}
void ast_script_delete(struct ast_script * script) {
struct ast_script * next = script;
struct ast_script * current;
while (next) {
current = next;
next = current->next;
ast_transformation_discard(current->transformation);
free(current);
}
}
struct ast_script * ast_script_reverse(struct ast_script * script) {
struct ast_script * next = script;
struct ast_script * result = NULL;
struct ast_script * current;
while (next) {
current = next;
next = current->next;
result = ast_script_new(current->transformation, result);
free(current);
}
return result;
}