summaryrefslogtreecommitdiff
path: root/tnslc/compile/type.tnsl
blob: 474f70507f50452a2ee0c1c5df2d347595028ff9 (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
struct Variable {
	~uint8 name,
	~Type _type,
	utils.Vector ptr
}

/; method Variable
	/; init (~uint8 name)
		self.name = name
		self.ptr.init(8)
	;/

	/; add_ptr(uint ptp)
		self.ptr.push(~ptp)
	;/

	/; get_ptr [uint]
		/; if (self.ptr.count == 0)
			return 0
		;/
		~uint p = self.ptr.get(self.ptr.count - 1)
		return p`
	;/

	/; pop_ptr [uint]
		/; if (self.ptr.count == 0)
			return 0
		;/
		~uint p = self.ptr.get(self.ptr.count - 1)
		uint out = p`
		self.ptr.pop()
		return out
	;/

	/; end
		_delete(self.name)
		self.ptr.end()
	;/
;/

struct Type {
	~uint8 name,
	uint size,
	utils.Vector vars,
	~Module methods,
}

/; method Type
	/; init(~uint8 name)
		self.name = name
		Variable tmp
		self.vars.init(len tmp)
	;/

	/; add_var (~Variable v)
		self.vars.push(v)
	;/

	/; end
		_delete(self.name)
		/; loop (int i = 0; i < self.vars.count) [i++]
			~Variable v = self.vars.get(i)
			v`.end()
		;/
		self.vars.end()
	;/
;/

struct Function {
	~uint8
		name,
	~parse.Node
		body,
	utils.Vector
		inputs,
		outputs
}

/; method Function
	/; init (~uint8 name)
		self.name = name
		Variable vtmp
		~uint i
		self.inputs.init(len vtmp)
		self.outputs.init(len i)
	;/

	/; add_input (~Variable v)
		self.inputs.push(v)
	;/

	/; add_output (~Type t)
		self.outputs.push(~t)
	;/

	/; end
		_delete(self.name)

		/; loop (int i = 0; i < self.inputs.count) [i++]
			~Variable v = self.inputs.get(i)
			v`.end()
		;/
		self.inputs.end()

		self.outputs.end()
	;/
;/

struct Enum {
	~uint8 name,
	~Type _type,
	utils.Vector vals
}

/; method Enum
	/; init (~uint8 name)
		self.name = name
		Variable vtmp
		self.vals.init(len vtmp)
	;/

	/; end
		_delete(self.name)
		/; loop (int i = 0; i < self.vals.count) [i++]
			~Variable v = self.vals.get(i)
			v`.end()
		;/
		self.vals.end()
	;/
;/

struct Module {
	~uint8 name,
	~Module parent,
	bool exp,
	utils.Vector
		sub,
		vars,
		types,
		funcs,
		enums

}

uint8 MOD_FIND_SUB = 0
uint8 MOD_FIND_VAR = 1
uint8 MOD_FIND_TYP = 2
uint8 MOD_FIND_FUN = 3
uint8 MOD_FIND_ENM = 4

/; method Module
	/; init (~uint8 name)
		self.name = name
		Module mtmp
		Variable vtmp
		Type ttmp
		Function ftmp
		Enum etmp
		self.sub.init(len mtmp)
		self.vars.init(len vtmp)
		self.types.init(len ttmp)
		self.funcs.init(len ftmp)
		self.enums.init(len etmp)
	;/

	/; update_children
		/; loop (int i = 0; i < self.sub.count) [i++]
			~Module s = self.sub.get(i)
			s`.parent = ~self
		;/
	;/

	/; add_sub(~Module m) [~Module]
		self.sub.push(m)
		/; loop (int i = 0; i < self.sub.count) [i++]
			~Module s = self.sub.get(i)
			s`.update_children()
		;/
		return self.sub.get(self.sub.count - 1)
	;/

	/; add_var (~Variable v)
		self.vars.push(v)
	;/

	/; add_type (~Type t)
		self.types.push(t)
	;/

	/; add_funcs (~Function f)
		self.funcs.push(f)
	;/

	/; add_enum (~Enum e)
		self.enums.push(e)
	;/

	/; _find_rec (utils.Artifact pth, uint8 typ, int sub) [~void]
		/; if (sub !< pth.count)
			return NULL
		;; else if ((sub + 1) < pth.count)
			/; loop (int i = 0; i < self.sub.count) [i++]
				~Module m = self.sub.get(i)
				/; if (utils.strcmp(pth.get(sub), m`.name) == true)
					return _find_rec(pth, typ, sub + 1)
				;/
			;/
		;; else
			/; if (typ == MOD_FIND_SUB)
				/; loop (int i = 0; i < self.sub.count) [i++]
					~Module m = self.sub.get(i)
					/; if (utils.strcmp(pth.get(sub), m`.name) == true)
						return self.sub.get(i)
					;/
				;/
			;; else if (typ == MOD_FIND_VAR)
				/; loop (int i = 0; i < self.vars.count) [i++]
					~Variable v = self.vars.get(i)
					/; if (utils.strcmp(pth.get(sub), v`.name) == true)
						return self.vars.get(i)
					;/
				;/
			;; else if (typ == MOD_FIND_TYP)
				/; loop (int i = 0; i < self.types.count) [i++]
					~Type t = self.types.get(i)
					/; if (utils.strcmp(pth.get(sub), t`.name) == true)
						return self.types.get(i)
					;/
				;/
			;; else if (typ == MOD_FIND_FUN)
				/; loop (int i = 0; i < self.funcs.count) [i++]
					~Function f = self.funcs.get(i)
					/; if (utils.strcmp(pth.get(sub), f`.name) == true)
						return self.funcs.get(i)
					;/
				;/
			;; else if (typ == MOD_FIND_ENM)
				/; loop (int i = 0; i < self.enums.count) [i++]
					~Enum e = self.enums.get(i)
					/; if (utils.strcmp(pth.get(sub), e`.name) == true)
						return self.enums.get(i)
					;/
				;/
			;/
		;/
		
		/; if (self.parent == NULL || sub !== 0)
			return NULL
		;/

		return self.parent._find_rec(pth, typ, 0)
	;/

	/; find (utils.Artifact pth, uint8 typ) [~void]
		return _find_rec(pth, typ, 0)
	;/

	/; end
		_delete(self.name)

		/; loop (int i = 0; i < self.sub.count) [i++]
			~Module m = self.sub.get(i)
			m`.end()
		;/
		self.sub.end()

		/; loop (int i = 0; i < self.vars.count) [i++]
			~Variable v = self.vars.get(i)
			v`.end()
		;/
		self.vars.end()

		/; loop (int i = 0; i < self.types.count) [i++]
			~Type t = self.types.get(i)
			t`.end()
		;/
		self.types.end()

		/; loop (int i = 0; i < self.funcs.count) [i++]
			~Function f = self.funcs.get(i)
			f`.end()
		;/
		self.funcs.end()

		/; loop (int i = 0; i < self.enums.count) [i++]
			~Enum e = self.enums.get(i)
			e`.end()
		;/
		self.enums.end()
	;/
;/

{}~uint8 GEN_VAR_NAMES = { "int\0", "int8\0", "int16\0", "int32\0", "int64\0", "uint\0", "uint8\0", "uint16\0", "uint32\0", "uint64\0", "float\0", "float32\0", "float64\0", "vect\0", "bool\0", "void\0" }

{}uint GEN_VAR_SIZES = { 8, 1, 2, 4, 8, 8, 1, 2, 4, 8, 8, 4, 8, 0, 1, 8}

/; find_type(utils.Artifact a, ~parse.Node n) [~parse.Node]
	return NULL
;/

/; transform_struct(~parse.Node n, ~Module parent)
;/

/; _tfn_mod_loop (~parse.Node n, ~Module m)
	/; loop (int i = 0; i < n`.sub.count) [i++]
		~parse.Node s = n`.sub.get(i)
		/; if (s`._type == parse.NTYPE_MODULE || s`._type == parse.NTYPE_EXPORT)
			transform_module(s, m)
		;; else if (s`._type == parse.NTYPE_STRUCT)
			transform_struct(s, m)
		;/
	;/
;/

/; transform_module (~parse.Node n, ~Module parent)
	~Module s = NULL

	/; loop (int i = 0; i < parent`.sub.count) [i++]
		~Module tmp = parent`.sub.get(i)
		/; if (utils.strcmp(n`.data, tmp`.name) == true)
			s = tmp
		;/
	;/
	
	~int cmp = s
	/; if (cmp == NULL)
		Module out
		out.init(utils.strcpy(n`.data))

		/; if (n`._type == parse.NTYPE_EXPORT)
			out.exp = true
		;; else
			out.exp = false
		;/

		s = parent`.add_sub(~out)
	;/

	_tfn_mod_loop(n, s)
;/

/; _tfn_gen_default_types (~Module m)
	/; loop (int i = 0; i < len GEN_VAR_NAMES) [i++]
		Type t
		t.init(utils.strcpy(GEN_VAR_NAMES{i}))
		t.size = GEN_VAR_SIZES{i}
		m`.add_type(~t)
	;/
;/

/; transform_tree (~parse.Node n) [Module]
	Module out
	out.init(utils.strcpy(n`.data))
	out.exp = true

	_tfn_gen_default_types(~out)
	_tfn_mod_loop(n, ~out)

	return out
;/