summaryrefslogtreecommitdiff
path: root/tnslc/compiler.tnsl
blob: fd5cc15f4991653fc862ffc338aab635f38bbb1b (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
##############
# TEXT UTILS #
##############

{}uint8 e_tc_nl = "\n\0"
{}uint8 e_noquit = "[TNSLC] [UB] PRE-ALPHA VERSION OF COMPILER UNABLE TO EXIT! UNDEFINED BEHAVIOUR AHEAD\n\0"

{}uint8 e_unknown_meta = "[TNSLC] [WARNING] Unknown meta character '%c'!\n\0"
/; parse_meta (~uint8 str, int idx) [uint8]
	uint8 c = str{idx}

	/; if (c == 'n')
		return '\n'
	;; else if (c == 'r')
		return '\r'
	;; else if (c == '\\')
		return '\\'
	;; else if (c == '"')
		return '"'
	;; else if (c == '\'')
		return '\''
	;; else if (c == '0')
		return 0
	;/
	
	_print_num(~e_unknown_meta{0}, c)
	_printf(~e_noquit{0})
	return c
;/

/; unquote_char (~uint8 str) [uint8]
	int l = cstr_len(str)

	/; if (l < 3)
		return 0
	;; else if (str{1} == '\\')
		return parse_meta(str, 2)
	;/

	return str{1}
;/

/; unquote_string (~uint8 str) [~uint8]
	~uint8 out = _alloc(1)
	out{0} = 0
	
	int ln = 0
	int l = cstr_len(str)
	
	/; loop (int i = 1; i < l - 1) [i++]
		/; if (str{i} == '\\')
			out{ln} = parse_meta(str, i + 1)
			i++
		;; else
			out{ln} = str{i}
		;/

		out = _realloc(out, ln + 2)
		out{ln + 1} = 0
		ln++
	;/

	return out
;/

{}uint8 e_str_parse = "[TNSLC] [ERROR] Error when parsing int from string. %d\n\0"
/; str_to_int (~uint8 str) [int]
	int out = 0
	int l = cstr_len(str)

	/; loop (int i = 0; i < l) [i++]
		uint8 c = str{i}
		/; if (c < '0' || c > '9')
			_print_num(~e_str_parse{0}, c)
			_printf(~e_noquit{0})
			break
		;; else
			out = out * 10
			out = out + c - '0'
		;/
	;/

	return out
;/

{}uint8 w_method_guard = "_#\0"
{}uint8 w_enum_guard = "__#\0"

####################
# FIND/PARSE UTILS #
####################

# finds the closing delim in a vector of tokens
{}uint8 e_dmiss = "[TNSLC] [ERROR] Delimiter missmatch\n\0"
/; matching_delim (Vector v, int c) [int]
	~Token cur
	cur = v.get(c)
	uint8 op, first
	first = cur`.data{0}

	/; if (first == '(')
		op = ')'
	;; else if (first == '[')
		op = ']'
	;; else if (first == '{')
		op = '}'
	;; else
		op = ';'
	;/

	int p, b, s, f
	p = 0 # Parens
	b = 0 # Braces
	s = 0 # Squiggly Braces
	f = 0 # Funcs

	/; loop (c++; cur < v.num_el) [c++]
		cur = v.get(c)
		/; if (cur`._type !== TOKEN_TYPE.DELIMITER)
			continue
		;/

		first = cur`.data{0}
		# Increments
		/; if (first == '(')
			p++
			continue
		;; else if (first == '[')
			b++
			continue
		;; else if (first == '{')
			s++
			continue
		;; else if (first == '/')
			f++
			continue
		;/

		# Check end
		/; if (first == op)
			/; if (p == 0 && b == 0 && s == 0 && f == 0)
				return c
			;/
		;/

		# Decrement
		/; if (first == ')')
			p--
		;; else if (first == ']')
			b--
		;; else if (first == '}')
			s--
		;; else if (first == ';')
			first = cur`.data{1}
			/; if (first == '/')
				f--
			;/
		;/

		# Mismatch
		/; if (p < 0 || b < 0 || s < 0 || f < 0)
			_printf(~e_dmiss{0})
			_printf(~e_noquit{0})
		;/
	;/

	return 0 - 1
;/

# Entrypoint for round two
/; round_two (Path in, ~Module m) [CompData]
	CompData out
	out.start()	
	return out
;/


{}uint8 e_circular = "[TNSLC] [ERROR] Circular struct definition detected in structs:\n\0"
# Structure sizing for the first round
/; size_struct (~Type t, ~Module m)
	/; if (t`.s !== 0)
		return
	;/

	t`.s = 0 - 1
	int s = 0
	
	~Variable mb
	~Module mbm
	~Type mbt
	/; loop (int i = 0; i < t`.members.num_el) [i++]
		mb = t`.members.get(i)
		mbt = ~mb`.data_type
		/; if (mbt`.ptr_chain.num_el > 0)
			s = s + 8
		;; else if (mbt`.s > 0)
			s = s + mbt`.s
		;; else if (mbt`.s == 0)
			Vector v
			v.start(8)
			v.push(~mbt`.name)
			
			mbm = mbt`.mod
			~Type tmp
			tmp = mbm`._find_type(v, 0)
			size_struct(tmp, mbm)

			mbt`.s = tmp`.s
			s = s + tmp`.s

			v._del()
		;; else if (mbt`.s < 0)
			_printf(~e_circular{0})

			_printf(t`.name)
			_printf(~e_tc_nl{0})

			_printf(mbt`.name)
			_printf(~e_tc_nl{0})
			
			_printf(~e_noquit{0})
		;/
	;/

	t`.s = s
;/

/; flush_structs (~Module m)
	~Type t
	/; loop(int i = 0; i < m`.typ.num_el) [i++]
		t = m`.typ.get(i)
		size_struct(t, m)
	;/
	
	~Module s
	/; loop(int i = 0; i < m`.sub.num_el) [i++]
		s = m`.sub.get(i)
		flush_structs(s)
	;/
;/


/; round_one_file (Path in, ~Module root)	
	~uint8 pth = in.full_path()
	Vector v = tokenize_file(pth)
	_delete(pth)

	# round_one_tokens(in, v, root, 0, v.num_el)

	# Clean up tokens
	~Token t
	/; loop (int i = 0; i < v.num_el) [i++]
		t = v.get(i)
		_delete(t`.data)
	;/
;/


/; compile (Path in, out)
	Module root
	root.start()
	root.exp = true

#	round_one_file(in, ~root)
#	flush_structs(root)
#	CompData dat = round_two(in, ~root)
	
	~void fd = out.open_write()
	dat.write_file(fd)
	_close_file(fd)
;/