-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.js
60 lines (58 loc) · 1.77 KB
/
lexer.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
function is_number(c)
{
const code = c.charCodeAt(0);
return code >= '0'.charCodeAt(0) && code <= '9'.charCodeAt(0);
}
function is_letter(c)
{
const code = c.charCodeAt(0);
return code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0) || code >= 'a'.charCodeAt(0) && code <= 'z'.charCodeAt(0);
}
function is_whitespace(c)
{
return c === '\n' || c === '\r' || c === ' ' || c === '\t';
}
function is_identifier_char(c)
{
return is_letter(c) || is_number(c) || c === '_';
}
function is_identifier_start(c)
{
return is_letter(c) || c === '_';
}
function multi_char_equal(token, chars, idx)
{
if(idx + token.length > chars.length)return false;
for(let i=0; i<token.length; i++)if(chars[idx+i] !== token.charAt(i))return false;
return true;
}
function make_token(type, data = {})
{
return {type, ...data};
}
function lex_string(_str)
{
const tokens = [];
const chars = _str.split("");
for(let i=0; i<chars.length; i++)
{
let c = chars[i];
if(is_whitespace(c));
else if(is_number(c))
{
let number = c;
while(i+1 < chars.length && (is_number(chars[i+1]) || chars[i+1] === '.'))number += chars[++i];
tokens.push(make_token(TOKEN_NUMBER, {value: parseFloat(number)}));
}
else if(c === '+')tokens.push(make_token(TOKEN_PLUS));
else if(c === '-')tokens.push(make_token(TOKEN_MINUS));
else if(multi_char_equal("**", chars, i))tokens.push(make_token(TOKEN_POWER)), i++;
else if(c === '*')tokens.push(make_token(TOKEN_MULTIPLY));
else if(c === '/')tokens.push(make_token(TOKEN_DIVIDE));
else if(c === '(')tokens.push(make_token(TOKEN_OPEN_PAREN));
else if(c === ')')tokens.push(make_token(TOKEN_CLOSE_PAREN));
else tokens.push(make_token(TOKEN_UNKNOWN, {char:c}));
}
tokens.push(make_token(TOKEN_EOI));
return tokens;
}