-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-tokenizer.js
96 lines (79 loc) · 2.14 KB
/
1-tokenizer.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
// 1-tokenizer.js
//tokenの作成
export function tokenizer(input) {
let current = 0; //コードのcurrentの位置
let tokens = [];
while (current < input.length) {
let char = input[current]; //inputのcurrentの文字
if (char === '(') {
tokens.push({
type: 'paren',
value: '(',
});
current++;
continue;
}
if (char === ')') {
tokens.push({
type: 'paren',
value: ')',
});
current++;
continue;
}
//空白のcheck トークンとして格納する必要はない
const WHITESPACE = /\s/;
if (WHITESPACE.test(char)) {
current++;
continue;
}
//数字
// 数値は任意の数の文字にして一連の文字全体を1つのトークンとしてキャプチャする
//1文字ではなく(123 456)の場合などが考えられるので以下の処理
const NUMBERS = /[0-9]/;
if (NUMBERS.test(char)) {
let value = '';
//数値が続く限り
while (NUMBERS.test(char)) {
value += char;
char = input[++current];
}
tokens.push({ type: 'number', value });
continue;
}
//文字列
// concat "foo" "bar"
if (char === '"') {
let value = '';
//token内の最初の「 " 」はskip
char = input[++current];
//最後の「 " 」まで繰り返す
while (char != '"') {
value += char;
char = input[++current];
}
//閉じ「 " 」もskip
char = input[++current];
tokens.push({ type: 'string', value });
continue;
}
// name token(lisp構文の関数名)
// (add 2 4) のadd部分
const LETTERS = /[a-z]/i;
if (LETTERS.test(char)) {
let value = '';
while (LETTERS.test(char)) {
value += char;
char = input[++current];
}
tokens.push({ type: 'name', value});
continue;
}
//例外処理
//文字が一致していない場合
alert('正しい構文を入力してください');
throw new TypeError('I dont know what this character is: ' + char);
}
//tokens配列を返す
return tokens;
}