summaryrefslogtreecommitdiff
path: root/tnslc/parse/tokenizer.tnsl
blob: fcc3c5c996d2907d8fa77f2dcb7b483ba23568a5 (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

uint TTYPE_DELIM = 0
uint TTYPE_SEP   = 1
uint TTYPE_KEYWD = 2
uint TTYPE_KEYTP = 3
uint TTYPE_LITRL = 4
uint TTYPE_AUG   = 5
uint TTYPE_USRWD = 6

uint TTYPE_ERR   = 999

struct Token {
	uint _type,
	~uint8 data,
	uint
		line,
		col
}

/; method Token
	/; eq (~uint8 str) [bool]
		return utils.strcmp(self.data, str)
	;/

	/; end
		_delete(self.data)
	;/
;/

/; _in_csv (~uint8 csv, ~uint8 str) [bool]
	int along = 0

	/; loop (csv` !== 0) [csv++]
		/; if (csv` == ',')
			/; if (along !< 0 && str{along} == 0)
				return true
			;/
			along = 0
		;; else if (along !< 0 && str{along} == csv`)
			along++
		;; else
			along = 0
			along--
		;/
	;/

	return along !< 0 && str{along} == 0
;/

/; _str_contains (~uint8 str, uint8 ch) [bool]
	/; loop (str` !== 0) [str++]
		/; if (str` == ch)
			return true
		;/
	;/
	return false
;/


~uint8 KEYWORDS   = "import,using,module,export,struct,method,implements,interface,operator,enum,if,else,loop,continue,break,return,label,goto,asm\0"
~uint8 KEYTYPES   = "uint8,uint16,uint32,uint64,uint,int8,int16,int32,int64,int,float32,float64,float,bool,void,vect,type\0"
~uint8 LITERALS   = "false,true,null\0"

~uint8 RESERVED   = "~`!@#$%^&*()[]{}-+=\"\'\\|;:/?.>,<\0"

~uint8 OP         = "`~!%^&|*-=+./><\0"
~uint8 MULTI_OP   = "==,&&,||,^^,!==,!&&,!||,!^^,!<,!>,<<,>>,!&,!|,!^,++,--,>==,<==\0"
uint   MAX_MULTI  = 3
~uint8 MULTI_OP_W = "is,len\0"

~uint8 DELIMS     = "()[]{}\0"


/; produce_word_token (~utils.File fin, Token prev) [Token]
	Token out
	out.line = prev.line
	out.col = prev.col

	utils.Vector tmp
	tmp.init(1)
	
	uint8 ch = fin`.read()
	tmp.push(~ch)

	/; loop (bool run = true) [run == true]
		ch = fin`.read()
		/; if (ch == 0)
			run = false
		;; else if (is_reserved(ch) == true || is_whitespace(ch) == true)
			fin`.unread()
			run = false
		;; else
			tmp.push(~ch)
		;/
	;/

	~uint8 str = tmp.as_cstr()
	/; if (_in_csv(KEYWORDS, str) == true)
		out._type = TTYPE_KEYWD
	;; else if (_in_csv(KEYTYPES, str) == true)
		out._type == TTYPE_KEYTP
	;; else if (_in_csv(LITERALS, str) == true)
		out._type == TTYPE_LITRL
	;; else if (_in_csv(MULTI_OP_W, str) == true)
		out._type = TTYPE_AUG
	;; else
		out._type = TTYPE_USRWD
	;/

	return out
;/

/; produce_string_token (~utils.File fin, Token prev) [Token]
	Token out
	out._type = TTYPE_LITRL
	out.line = prev.line
	out.col = prev.col

	utils.Vector store
	store.init(1)
	uint8 delim = fin`.read()
	store.push(~delim)

	/; loop (fin`.at_end == false && delim !== 0)
		uint8 tmp = fin`.read()
		store.push(~tmp)
		/; if(tmp == '\\')
			tmp = fin`.read()
			store.push(~tmp)
		;; else if (tmp == delim)
			delim = 0
		;; else if (tmp == '\n')
			out.line++
		;/
	;/

	out.data = store.as_cstr()

	return out
;/

/; produce_reserved_token (~utils.File fin, Token prev) [Token]
	Token out
	utils.Vector tmp
	tmp.init(1)

	out.line = prev.line
	out.col = prev.col

	/; loop (int i = 0; i < MAX_MULTI) [i++]
		uint8 ch = fin`.read()
		/; if (is_reserved(ch) == true)
			tmp.push(~ch)
		;; else
			fin`.unread()
			i = MAX_MULTI
		;/
	;/
	
	/; loop (bool run = true) [run == true]
		/; if (tmp.count < 2)
			run = false
			~uint8 ch = tmp.get(0)
			/; if (ch` == ';' || ch` == ',')
				out._type = TTYPE_SEP
			;; else if (_str_contains(DELIMS, ch`) == true)
				out._type = TTYPE_DELIM
			;; else if (_str_contains(OP, ch`) == true)
				out._type = TTYPE_AUG
			;/
		;; else if (_in_csv(MULTI_OP, tmp.as_cstr()) == true)
			run = false
			out._type = TTYPE_AUG
		;; else if (tmp.count == 2)
			~uint8 cha = tmp.get(0)
			~uint8 chb = tmp.get(0)
			/; if (cha` == ';' && chb` == ';')
				run = false
			;; else if (cha` == '/' && chb` == ';')
				run = false
			;; else if (cha` == ';' && chb` == '/')
				run = false
			;/

			/; if (run == false)
				out._type = TTYPE_DELIM
			;/
		;; else
			tmp.pop()
			fin`.unread()
		;/
	;/

	out.data = tmp.as_cstr()

	return out
;/

/; produce_numeric_token (~utils.File fin, Token prev) [Token]
	Token out
	out._type = TTYPE_LITRL
	out.line = prev.line
	out.col = prev.col

	utils.Vector tmp
	tmp.init(1)
	uint8 ch = fin`.read()
	tmp.push(~ch)

	bool alt_base = false
	/; if (ch == '0')
		ch = fin`.read()
		/; if (ch !< 'a' && ch !> 'z')
			alt_base = true
		;; else if (ch !< 'A' && ch !> 'Z')
			alt_base = true
		;; else if (is_reserved(ch) == true)
			fin`.unread()
			out.data = tmp.as_cstr()
			return out
		;; else if (ch == 0)
			out.data = tmp.as_cstr()
			return out
		;/
		tmp.push(~ch)
	;/

	/; loop (bool run = true) [run == true]
		ch = fin`.read()
		/; if (is_numeric(ch) == false && alt_base == false)
			fin`.unread()
			run = false
		;; else if (is_reserved(ch) == true)
			fin`.unread()
			run = false
		;; else if (ch == 0 || fin`.at_end == true)
			run = false
		;; else
			tmp.push(~ch)
		;/
	;/

	out.data = tmp.as_cstr()
	return out
;/

/; is_whitespace (uint8 ch) [bool]
	/; if (ch > 8 && ch < 14)
		return true
	;; else if (ch == ' ')
		return true
	;/
	return false
;/

/; is_reserved (uint8 ch) [bool]
	return _str_contains(RESERVED, ch)
;/

/; is_numeric (uint8 ch) [bool]
	/; if (ch !< '0' && ch !> '9')
		return true
	;/
	return false
;/

/; produce_next_token (~utils.File fin, Token prev) [Token]
	/; if (prev._type !== TTYPE_ERR)
		prev.col = prev.col + utils.strlen(prev.data)
	;/

	uint8 first = fin`.read()
	/; loop (is_whitespace(first) == true)
		/; if (first == '\n')
			prev.line++
			prev.col = 0
		;/
		first = fin`.read()
		prev.col++
	;/
	fin`.unread()
	
	/; if (first == '\'' || first == '\"')
		return produce_string_token(fin, prev)
	;; else if (is_reserved(first) == true)
		return produce_reserved_token(fin, prev)
	;; else if (is_numeric(first) == true)
		return produce_numeric_token(fin, prev)
	;; else if (first !== 0)
		return produce_word_token(fin, prev)
	;/

	Token out
	out.line = prev.line
	out.col = prev.col
	out._type = TTYPE_ERR
	return out
;/

/; produce_first_token (~utils.File fin) [Token]
	Token tmp
	tmp.line = 1
	tmp.col = 1
	tmp._type = TTYPE_ERR
	
	return produce_next_token(fin, tmp)
;/