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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
struct Struct {
~uint8 name,
~Module methods,
utils.Vector members,
int size,
~parse.Node _up
}
~uint8 PRIMITIVE_1 = "bool,uint8,int8\0"
~uint8 PRIMITIVE_2 = "uint16,int16\0"
~uint8 PRIMITIVE_4 = "uint32,int32,float32\0"
~uint8 PRIMITIVE_8 = "uint64,int64,float64,int,uint,float,void\0"
/; is_primitive (~parse.Node tn) [int]
/; if (tn`.sub.count < 1)
return 0
;/
~parse.Node n = tn`.sub.get(0)
# Check for pointer, array
/; if (n`._type == parse.NTYPE_PRE_OP)
return 8
;/
# Check for ref
n = tn`.sub.get(tn`.sub.count - 1)
/; if (n`._type == parse.NTYPE_POST_OP)
return 8
;/
return 0
;/
/; _print_type(~parse.Node tn)
~parse.Node n
/; loop (int i = 0; i < tn`.sub.count) [i++]
/; if (i > 0)
_printf(".\0")
;/
n = tn`.sub.get(i)
/; if (n`._type == parse.NTYPE_ID)
_printf(n`.data)
;; else
return
;/
;/
;/
/; method Struct
/; init (~parse.Node node)
self._up = node
self.size = 0
self.methods = NULL
self.name = utils.strcpy(node`.data)
Var v
self.members.init(len v)
;/
/; add_member(~Var v)
self.members.push(v)
;/
/; get_member(~uint8 name) [~Var]
~Var out = NULL
~Var v
/; loop (int i = 0; i < self.members.count) [i++]
v = self.members.get(i)
/; if (utils.strcmp(v`.name, name) == true)
return v
;/
;/
return out
;/
/; _dlist [~parse.Node]
~parse.Node up = self._up
~parse.Node n
/; loop (int i = 0; i < up`.sub.count) [i++]
n = up`.sub.get(i)
/; if (n`._type == parse.NTYPE_DLIST)
return n
;/
;/
return NULL
;/
/; _compute_size (~Module parent)
/; if (self.size !== 0)
return
;/
self.size = self.size - 1
int total = 0
~parse.Node up = self._dlist()
~parse.Node n
int add_size = 0
/; loop (int i = 0; i < up`.sub.count) [i++]
n = up`.sub.get(i)
/; if (n`._type == parse.NTYPE_TYPE)
# Find type, compute size, set add_size to type size
~Struct ft = self._find_type(parent, n)
/; if (ft == NULL)
# Type not found
_printf("ERROR: Unable to find type '\0")
_print_type(n)
_printf("' for use in struct '\0")
_printf(self.name)
_printf("'\n\0")
return
;/
/; if (ft`.size == 0)
# Recurse
ft`._compute_size(parent)
;/
/; if (ft`.size < 0)
# Cyclical dependency
return
;/
add_size = ft`.size
;; else if (n`._type == parse.NTYPE_ID)
total = total + add_size
;/
;/
self.size = total
;/
/; _find_type (~Module parent, ~parse.Node tn) [~Struct]
# Init vector of strings
utils.Vector sv
sv.init(8)
~void str
~parse.Node n
bool seen_id = false
/; loop (int i = 0; i < tn`.sub.count) [i++]
n = tn`.sub.get(i)
/; if (n`._type == parse.NTYPE_ID)
str = n`.data
sv.push(~str)
seen_id = true
;; else if (seen_id == true)
i = tn`.sub.count
;/
;/
# Find struct and compute its size
~Struct out = parent.find(SEARCH_STRUCT, ~sv)
sv.pop()
~Module outp = parent
/; if (sv.count !== 0)
outp = parent.find(SEARCH_SUB, ~sv)
;/
sv.end()
return out
;/
/; end
_delete(self.name)
~Var v
/; loop (int i = 0; i < self.members.count) [i++]
v = self.members.get(i)
v`.end()
;/
self.members.end()
;/
;/
|