-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize.js
257 lines (187 loc) · 7.68 KB
/
tokenize.js
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
/*****************************************************************************
TOKENIZE
*****************************************************************************/
function tokenizeCurrentSourceLine() {
// Clear global token array
// str type
// str value
// num source_line_index
token = [];
//=========================================================================
// Helper functions
//=========================================================================
function isName( character ) {
if ( ( character >= 'a' && character <= 'z' ) || ( character >= 'A' && character <= 'Z' ) ) {
return true
} else {
return false
}
}
function isNum( character ) {
if ( ( character >= '0' && character <= '9' ) || character == '$' || character == '%' ) {
return true
} else {
return false
}
}
function isWhite( character ) {
if ( character == ' ' || character == '\t' ) {
return true
} else {
return false
}
}
function isString( character ) {
if ( character == '"' || character == "'" ) {
return true
} else {
return false
}
}
function nextTwoCharactersAre( c1, c2 ) {
if ( source_line[current_source_line_index].line[current_character_position] == c1 &&
source_line[current_source_line_index].line[current_character_position+1] == c2 ) {
return true
} else {
return false
}
}
//*********************************************************************************************************
// Tokenizer loop
//*********************************************************************************************************
let current_character_position = 0; // starts with 0, hence "position = current_character_position+1" below
while( current_character_position < source_line[current_source_line_index].line.length ) {
let character = source_line[current_source_line_index].line[current_character_position];
//-----------------------------------------------------------------------
// Whitespace
//-----------------------------------------------------------------------
if ( isWhite( character ) ) {
// token.push({ // for debugging, see whites in token stream
// type: 'white',
// value: " ",
// position: current_character_position+1
// });
current_character_position++;
continue;
}
//-----------------------------------------------------------------------
// Line comment
//-----------------------------------------------------------------------
if ( character == ';' ) {
break;
}
//-----------------------------------------------------------------------
// Left underscore (not allowed because the compiler uses it)
//-----------------------------------------------------------------------
if ( character == '_' ) {
ThrowSyntaxError( "Underscore characters '_' are not allowed as first character of a token.\nIt is used by the compiler" )
}
//-----------------------------------------------------------------------
// Name
//-----------------------------------------------------------------------
if ( isName( character ) ) {
let value = '';
let position = current_character_position+1; // +1 because string pos starts at 0
while( isName( character ) ) {
value += character;
character = source_line[current_source_line_index].line[++current_character_position];
}
token.push({
type: 'name',
value: value,
position: position
});
continue;
}
//-----------------------------------------------------------------------
// Number
//-----------------------------------------------------------------------
if ( isNum( character ) ) {
let value = '';
let position = current_character_position+1;
while( isNum( character ) ) {
value += character;
character = source_line[current_source_line_index].line[++current_character_position];
}
token.push({
type: 'number',
value: value,
position: position
});
continue;
}
//-----------------------------------------------------------------------
// String
//-----------------------------------------------------------------------
if ( isString( character ) ) {
let value = "";
let position = current_character_position+1;
let string_terminator = character; // either " or '
// skip over opening " or '
character = source_line[current_source_line_index].line[++current_character_position];
// collect string
while( character !== string_terminator ) {
value += character;
character = source_line[current_source_line_index].line[++current_character_position];
// Take end of line into account
if ( current_character_position > source_line[current_source_line_index].line.length ) {
ThrowSyntaxError( "Missing closing quote or double quote in string" );
}
}
// skip over closing " or '
current_character_position++;
// Push string to "token"
token.push({
type: 'string',
value: '"' +value + '"', // include " or ' in this compiler
position: position
});
continue;
}
//-----------------------------------------------------------------------
// Other token type: << shift left
//-----------------------------------------------------------------------
if ( nextTwoCharactersAre( "<", "<" ) ) {
token.push({ // for debugging, see whites in token stream
type: 'other',
value: "<<",
position: current_character_position+1
});
current_character_position += 2;
continue;
}
//-----------------------------------------------------------------------
// Other token type: << shift right
//-----------------------------------------------------------------------
if ( nextTwoCharactersAre( ">", ">" ) ) {
token.push({ // for debugging, see whites in token stream
type: 'other',
value: ">>",
position: current_character_position+1
});
current_character_position += 2;
continue;
}
//-----------------------------------------------------------------------
// Other token type, if it is nothing from above
//-----------------------------------------------------------------------
token.push({
type: 'other',
value: character,
position: current_character_position+1
});
current_character_position++;
} // wend *************************************************************************************************
//=========================================================================
// Push End Of Line 'EOL' if lenght of line is reached
//=========================================================================
token.push({
type: 'EOL',
value: "",
position: current_character_position+1
});
// After the line is tokenized
// we start at token 0 as current token
// global var "current_token_index"
current_token_index = 0;
}