summaryrefslogtreecommitdiff
path: root/tnslc/compile/ast.tnsl
blob: 6efee606b03e84f2e371e9309c6d2ffaee882402 (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

int NT_MODULE = 0
int NT_MOD_EX = 1
int NT_BLOCK = 2
int NT_FUNC = 3
int NT_PARAM = 4
int NT_RESULT = 5
int NT_DATA = 6
int NT_TYPE = 7
int NT_STRUCT = 8

struct Node {
	int _type,
	~uint8 data,
	utils.Vector sub
}

/; method Node
	/; init (int typ, ~uint8 dat)
		self._type = typ
		self.data = dat
		Node sub
		self.sub.init(len sub)
	;/

	/; end
		_delete(self.data)

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

;/

/; check_via_next (~utils.Iterator it, ~uint8 chk) [bool]
	it`.next()

	/; if (it`.at_end() == true)
		it`.prev()
		return false
	;/
	
	~Token cur = it`.get()
	/; if (utils.strcmp(cur`.data, cur) == true)
		return true
	;/

	it`.prev()
	return false
;/

/; check_and_advance (~utils.Iterator it, ~uint8 chk) [bool]
	/; if (it`.at_end() == true)
		return false
	;/

	~Token cur = it`.get()
	/; if (utils.strcmp(cur`.data, cur) == true)
		it`.next()
		return true
	;/

	return false
;/

/; build_struct (~utils.Iterator it, ~Node mod, ~utils.File fin)
;/

/; build_module (~utils.Iterator it, ~Node mod, ~utils.File fin)

	int NT = NT_MODULE
	~Token t = it`.get()
	/; if (utils.strcmp(t`.data, "export\0"))
		NT = NT_MOD_EX
		it`.next()
	;/

	Node sub
	it`.next()
	t = it`.get()

	/; if (t`._type !== TT_DEFWORD)
		_printf("Error in module decl")
		return
	;/
	
	sub.init(NT, utils.strcpy(t`.data))
	bool run = true

	/; loop (it`.at_end() == false && run == true)
		t = it`.get()
		
		/; if (utils.strcmp(t`.data, ";/\0") || utils.strcmp(t`.data, ";;\0"))
			run = false

		;; else if (utils.strcmp(t`.data, "/;\0"))
			_printf("Block detected!\n\0")
			build_block(it, ~sub, fin)

		;; else if (utils.strcmp(t`.data, "struct\0") == true)
			_printf("Struct detected!\n\0")
			build_struct(it, ~sub, fin)

		;; else if (utils.strcmp(t`.data, ":\0") == true)
			_printf("Preproc detected!\n\0")
			build_preproc(it, ~sub, fin)

		;; else if (at_defn(it) == true)
			_printf("Defn detected!\n\0")
			build_vardef(it, ~sub, fin)

		;; else
			# _printf("Error detected!\n\0")
			# TODO: ERROR
		;/

		/; if (run == true)
			it`.next()
		;/
	;/

	mod`.sub.push(~sub)
;/

/; build_method (~utils.Iterator it, ~Node mod, ~utils.File fin)
;/

/; build_function (~utils.Iterator it, ~Node mod, ~utils.File fin)
;/

/; build_block (~utils.Iterator it, ~Node mod, ~utils.File fin)
	it`.next()

	/; if (it`.at_end() == true)
		return
	;/

	~Token cur
	bool run = true

	/; loop (it`.at_end() == false && run == true)
		cur = it`.get()

		/; if (utils.strcmp(cur`.data, "module\0") || utils.strcmp(cur`.data, "export\0"))
			build_module(it, mod, fin)

		;; else if (utils.strcmp(cur`.data, "method\0") == true)
			build_method(it, mod, fin)

		;; else if (cur`._type == TT_DEFWORD)
			build_function(it, mod, fin)

		;; else
			# TODO: ERROR

		;/

		cur = it`.get()

		/; if (utils.strcmp(cur`.data, ";/\0") == true)
			run = false
		;; else if (utils.strcmp(cur`.data, ";;\0") == true)
			it`.next()
		;; else
			run = false
			# report error
		;/
	;/

;/

/; build_preproc (~utils.Iterator it, ~Node mod, ~utils.File fin)
	it`.next()
	/; if (it`.at_end() == true)
		return
	;/

	~Token cur = it`.get()
	/; if (utils.strcmp(cur`.data, "import\0") == true)
		# get file path
		it`.next()
		cur = it`.get()

		# gen new file struct
		~uint8 frel = utils.unquote_str(cur`.data)
		_printf("\nReading file: \0")
		_printf(frel)
		_printf("\n\0")
		utils.File fnew = fin`.relative(frel)
		
		# file import
		build_file(~fnew, mod)

		# cleanup
		_delete(frel)
		fnew.end()
	;; else
		# unknown preproc
		return
	;/
;/

/; build_vardef (~utils.Iterator it, ~Node mod, ~utils.File fin)
;/

/; at_defn (~utils.Iterator it) [bool]
	return false
;/

~uint8 TOKEN_COUNT = "Token count: %d\n\0"

/; build_file (~utils.File fin, ~Node mod)
	utils.Vector tokens = tokenize(fin)
	_print_num(TOKEN_COUNT, tokens.count)
	
	utils.Iterator tokit
	tokit.init(~tokens)

	/; loop (tokit.at_end() == false)
		~Token t = tokit.get()
		/; if (utils.strcmp(t`.data, "/;\0") || utils.strcmp(t`.data, ";;\0"))
			_printf("Block detected!\n\0")
			build_block(~tokit, mod, fin)
		;; else if (utils.strcmp(t`.data, "struct\0") == true)
			_printf("Struct detected!\n\0")
			build_struct(~tokit, mod, fin)
		;; else if (utils.strcmp(t`.data, ":\0") == true)
			_printf("Preproc detected!\n\0")
			build_preproc(~tokit, mod, fin)
		;; else if (at_defn(~tokit) == true)
			_printf("Defn detected!\n\0")
			build_vardef(~tokit, mod, fin)
		;; else
			# _printf("Error detected!\n\0")
			# TODO: ERROR
		;/

		tokit.next()
	;/

	free_token_list(~tokens)
;/