summaryrefslogtreecommitdiff
path: root/tnslc/parse/token.tnsl
blob: bf4d470873b62d03540bdac2b3b53d1263a427fd (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
/#
	Copyright 2021 Kyle Gunger

	This file is licensed under the CDDL 1.0 (the License)
	and may only be used in accordance with the License.
	You should have received a copy of the License with this
	software/source code. If you did not, a copy can be found
	at the following URL:

	https://opensource.org/licenses/CDDL-1.0

	THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO
	WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE
	EXPRESS OR IMPLIED
#/

/#
	Reserved words and characters, as well as
	helper funcs for checking their token types.
#/

;{}{}charp PREWORDS = {
	"include",
	"define",
	"extern",
	"size",
	"align",
	"address",
	"rootfile",
	"if",
	"else",
	"abi"
}

;{}{}charp KEYTYPES = {
	"int8",
	"int16",
	"int32",
	"int64",
	"int",
	
	"uint8",
	"uint16",
	"uint32",
	"uint64",
	"uint",

	"float32",
	"float64",
	"float",

	"bool",
	"void",
	"type"
}

;{}{}charp KEYWORDS = {
	"struct",
	"interface",
	"enum",
	"extends",
	
	"loop",
	"continue",
	"break",
	
	"match",
	"case",
	"default",
	
	"label",
	"goto",
	
	"if",
	"else",
	
	"const",
	"static",
	"volatile",
	
	"method",
	"override",
	"operator",
	
	"raw",
	"asm",
	"inline",
	"virtual",

	"delete",
	"alloc",
	"salloc",
	"realloc",
	
	"module",
	"export"
}

;{}{}charp LITERALS = {
	"true",
	"false",
	
	"null",

	"self",
	"super"
}

;{}charp RESERVED = "`~!#%^&*()-=+[]{}|;:,.<>/"

;{}charp DELIMITS = "()[]{}"
;{}charp LINESEPS = ";:#"
;{}charp INLNSEPS = ","
;{}charp AUGMENTS = "~`.&|^><!+-*/%"

;{}{}charp MDELIMITS = {
	# Code block
	"/;",
	";/",
	
	# Comment block
	"/#",
	"#/",
	
	# Preproc block
	"/:",
	":/",

	# Redef blocks
	";;",
	"::",
	";#",
	":#",
	"#;",
	"#:"
}

;{}{}charp MAUGMENTS = {
	# Boolean
	"==",
	"&&",
	"||",

	# Bitwise shifts
	"<<",
	">>",

	# PREaugmented augmentors
	"&=",
	"|=",
	"^=",
	"+=",
	"-=",
	"*=",
	"/=",
	"%=",
	"~=",
	"`=",

	# POSTaugmented augmentors
	"!&",
	"!|",
	"!^",
	"!==",
	"!&&",
	"!||",
	"!>",
	"!<",
	">==",
	"<==",

	# Increment and De-increment
	"++",
	"--",

	"is",
	"len",
	"size"
}

;int MAX_MRESERVED = 3

/##
	Checks if the character point p is in the string cmp

#; is_in_string (~{}charp cmp, charp p) [bool]

	/; loop (int i = 0; i < len cmp`) [i++]
		/; if (p == cmp`{i})
			;return true
		;/
	;/

	;return false
;/

/##
	Checks if the string s is in the list cmp

#; is_in_string_list (~{}{}charp cmp, ~{}charp s) [bool]

	/; loop (int i = 0; i < len cmp`) [i++]

		/; if (len s` == len cmp`{i})

			/; loop (int j = 0; j < len s`) [j++]

				/; if (s`{j} !== cmp`{i}{j})
					;break 1
				;/
			;/

			;return true
		;/

	;/

	;return false
;/

/; is_numeric_literal(~{}charp dat) [bool]
	/; if (len dat` == 0)
		;return false
	;/

	;bool dec = true, flt = false
	
	;int i = 0

	/; if (len dat` > 1)
		/; if (dat`{0} == '0' && !is_digit(dat`{1}) && dat`{1} !== '.')
			;dec = false
			;i = 2
		;/
	;/

	/; loop (i < len dat`) [i++]
		/; if (!is_digit(dat`{i}) && dec)
			/; if (dat`{i} == '.')
				/; if (flt || !dec)
					;return false
				;/
				;flt = true
			;; else if (dec)
				;return false
			;/
		;/
	;/
	
	;return true
;/

/; is_text_literal(~{}charp dat) [bool]
	/; if (len dat` < 1)
		;return false
	;/
	;return dat`{0} == '"' || dat`{0} == '\''
;/

/; string_closed ({}charp dat, charp c) [bool]
	/; if (len dat < 2)
		;return false
	;/

	;charp closing = dat{0}
	;bool escaping = false

	/; loop (int i = 1; i < len dat) [i++]
		/; if (dat{i} == closing && !escaping)
			;return true
		;; else if (dat{i} == '\\' && !escaping)
			;escaping = true
		;; else
			;escaping = false
		;/
	;/

	;return false
;/

/#
	Get the token_type value for a given string of character points

#; get_token_type (~{}charp s) [int]

	/; if (len s` > 1)

		/; if (is_numeric_literal(s) || s`{0} == '"' || s`{0} == '\'')
			;return TOKEN_TYPE.LITERAL
		;/

		/; if (is_in_string_list(~PREWORDS, s))
			;return TOKEN_TYPE.PREWORD
		;; else if (is_in_string_list(~KEYTYPES, s))
			;return TOKEN_TYPE.KEYTYPE
		;; else if (is_in_string_list(~KEYWORDS, s))
			;return TOKEN_TYPE.KEYWORD
		;; else if (is_in_string_list(~LITERALS, s))
			;return TOKEN_TYPE.LITERAL
		;; else if (is_in_string_list(~MDELIMITS, s))
			;return TOKEN_TYPE.DELIMIT
		;; else if (is_in_string_list(~MAUGMENTS, s))
			;return TOKEN_TYPE.AUGMENT
		;/

		;return TOKEN_TYPE.DEFWORD

	;;else if (len s` == 1)
		
		/; if (is_digit(s`{0}))
			;return TOKEN_TYPE.LITERAL
		;/

		/; if (is_in_string(~DELIMITS, s`{0}))
			;return TOKEN_TYPE.DELIMIT
		;; else if (is_in_string(~LINESEPS, s`{0}))
			;return TOKEN_TYPE.LINESEP
		;; else if (is_in_string(~INLNSEPS, s`{0}))
			;return TOKEN_TYPE.INLNSEP
		;; else if (is_in_string(~AUGMENTS, s`{0}))
			;return TOKEN_TYPE.AUGMENT
		;/

		;return TOKEN_TYPE.DEFWORD
	;/

	# What, we just produce vacant tokens now?
	# Something has gone wrong.

	;return -1
;/