-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.bah
286 lines (254 loc) · 8.49 KB
/
lexer.bah
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
#import "iostream.bah"
#import "string.bah"
//tokens types
#define tokenType char
const TOKEN_NO_TYPE = <tokenType>-1
const TOKEN_TYPE_INT = <tokenType>0
const TOKEN_TYPE_FLOAT = <tokenType>1
const TOKEN_TYPE_VAR = <tokenType>2
const TOKEN_TYPE_ENCL = <tokenType>3
const TOKEN_TYPE_SEP = <tokenType>4
const TOKEN_TYPE_STR = <tokenType>5
const TOKEN_TYPE_KEYWORD = <tokenType>6
const TOKEN_TYPE_CHAR = <tokenType>7
const TOKEN_TYPE_BOOL = <tokenType>8
const TOKEN_TYPE_SYNTAX = <tokenType>10
const TOKEN_TYPE_FUNC = <tokenType>11
const TOKEN_TYPE_CAST = <tokenType>12
//a token (the atomic unit for describing the inputed Bah file)
struct Tok {
cont: str = ""
ogCont: str = ""
type: tokenType = TOKEN_NO_TYPE
pos: uint32 = 0
linePos: uint32 = 0
line: uint32 = 1
begLine: uint32 = 1
isValue: bool = false
highlighted: bool = false
}
inArray(needle char, arr []char) bool {
i=0; for i < len(arr) {
if needle == arr[i] {
return true
}
i++
}
return false
}
inArrayStr(needle str, arr []str) bool {
i=0; for i < len(arr) {
if needle == arr[i] {
return true
}
i++
}
return false
}
makeToken(pos int, linePos int, lineNb int, cont []char, type tokenType) Tok {
t = Tok{}
t.cont = arrToStr(cont)
t.ogCont = t.cont
clear(cont)
t.pos = pos
t.line = lineNb
t.linePos = linePos
t.type = type
if type == TOKEN_TYPE_INT || type == TOKEN_TYPE_STR || type == TOKEN_TYPE_FLOAT || type == TOKEN_TYPE_VAR || type == TOKEN_TYPE_BOOL || type == TOKEN_TYPE_CHAR {
t.isValue = true
}
return t
}
isMinus(c char, nc char) bool {
return c == '-' && isNumber(nc)
}
enclavers = []char{'(', ')', '{', '}', '[', ']'}
syntaxes = []char{'!', '=', '|', <char>38, '%', '+', '-', '*', '/', ',', '<', '>', ':', <char>59, '^'}
//& //;
keywords = []str{"if", "else", "for", "struct", "const", "return", "extend", "new", "break", "continue", "default", "switch", "case", "while", "typedef", "function", "async", "in", "chan", "map", "buffer", "capture", "then"}
lexer(s <any>, codeLength int) []Tok {
tokens = []Tok
memory = []char
lineNb = 1 //current line number (in source file)
posOffset = 0
i=0; for i < codeLength, i++ {
c = s[i]
nc char = <char>0
if i+1 < codeLength {
nc = s[i+1]
}
//we have it the new line character
if c == <char>10 {
lineNb++
posOffset = i+1
}
//token is a string
if c == <char>34 || c == <char>39 {
pos = i
begLine = lineNb
memory[0] = c
i++
for i < codeLength, i++ {
c = s[i]
pc = s[i-1]
//string ending
if c == <char>34 || c == <char>39 {
//except if the previous char is a backslash
if pc != <char>92 {
memory[len(memory)] = c
break
}
}
//escaping line returns
if c == <char>10 {
lineNb++
posOffset = i+1
}
memory[len(memory)] = c
}
tokens[len(tokens)] = makeToken(pos, pos - posOffset, lineNb, memory, TOKEN_TYPE_STR)
lt = tokens[len(tokens) - 1]
lt.begLine = begLine
tokens[len(tokens) - 1] = lt
} else if isNumber(c) || isMinus(c, nc) { //token is a number
memory[0] = c
pos = i
i++
currentType = TOKEN_TYPE_INT
isHex = false
for i < codeLength, i++ {
c = s[i]
if c == <char>46 {
currentType = TOKEN_TYPE_FLOAT
} else if isNumber(c) == false {
if isHex == false {
if c == 'x' {
isHex = true
} else {
break
}
} else {
if isUpper(c) {
c += <char>32
}
if c < 'a' || c > 'f' {
break
}
}
if isHex == false {
break
}
}
memory[len(memory)] = c
}
i--
tokens[len(tokens)] = makeToken(pos, pos - posOffset, lineNb, memory, currentType)
} else if c == <char>35 { //is a hash keyword
pos = i
memory[0] = c
i++
for i < codeLength, i++ {
c = s[i]
if isAlphaNumeric(c) == false {
break
}
memory[len(memory)] = c
}
i--
tokens[len(tokens)] = makeToken(pos, pos - posOffset, lineNb, memory, TOKEN_TYPE_KEYWORD)
} else if inArray(c, enclavers) { //token is enclaver
memory[0] = c
tokens[len(tokens)] = makeToken(i, i - posOffset, lineNb, memory, TOKEN_TYPE_ENCL)
} else if inArray(c, syntaxes) { //token is a syntax element
if c == '<' {
pos = i
isCast = false
memory[0] = c
i++
for i < codeLength, i++ {
c = s[i]
if c == '>' {
isCast = true
memory[len(memory)] = c
break
}
if isAlphaNumeric(c) == false && c != '*' && c != ':' && c != '_' && c != '[' && c != ']' {
break
}
memory[len(memory)] = c
}
if isCast == true {
tokens[len(tokens)] = makeToken(pos, pos - posOffset, lineNb, memory, TOKEN_TYPE_CAST)
continue
}
i = pos
c = '<'
clear(memory)
}
memory[0] = c
pos = i
i++
fc = c
for i < codeLength, i++ {
c = s[i]
if c in syntaxes == false {
break
}
//for <=, >=, ==, !=, +=, -=, &&, ||, <<
if fc == '<' {
if c != '-' && c != '=' && c != '<' {
break
}
} else if c == '|' {
if fc != c {
break
}
} else if c == '&' {
if fc != c {
break
}
} else if c != '=' {
if c != '>' {
break
}
}
memory[len(memory)] = c
}
i--
tokens[len(tokens)] = makeToken(pos, pos - posOffset, lineNb, memory, TOKEN_TYPE_SYNTAX)
} else if c == '.' { //token is a separator
memory[0] = c
tokens[len(tokens)] = makeToken(i, i - posOffset, lineNb, memory, TOKEN_TYPE_SEP)
} else if isAlphaNumeric(c) || c == '_' { //token is a var / keyword
memory[0] = c
pos = i
i++
//get full token
for i < codeLength, i++ {
c = s[i]
if isAlphaNumeric(c) == false {
if c != '_' {
if c == '>' {
lc = memory[len(memory)-1]
if lc == '-' {
memory[len(memory)-1] = <char>0
i--
break
}
}
break
}
}
memory[len(memory)] = c
}
i--
currentType = TOKEN_TYPE_VAR
memstr = arrToStr(memory)
if memstr in keywords {
currentType = TOKEN_TYPE_KEYWORD
}
tokens[len(tokens)] = makeToken(pos, pos - posOffset, lineNb, memory, currentType)
}
}
return tokens
}