-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_old.js
113 lines (90 loc) · 2.91 KB
/
script_old.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
/*
arithmeticOperand: + - * /
compareOperand: ==
assignOperand: =
literal: 1,2,3,... | 1.9123
name: abc...
*/
class Token {
constructor(text, position) {
this.text = text;
this.position = position;
}
}
class ArithmeticOperandToken extends Token {
static regex = new RegExp('^[-*/+]');
static match(text, position) {
let match = text.match(this.regex);
if (!match) return null;
return new ArithmeticOperandToken(match[0], position);
}
}
class CompareOperandToken extends Token {
static regex = new RegExp('^(==|<=|>=|<|>)');
static match(text, position) {
let match = text.match(this.regex);
if (!match) return null;
return new CompareOperandToken(match[0], position);
}
}
class AssignOperandToken extends Token {
static regex = new RegExp('^=');
static match(text, position) {
let match = text.match(this.regex);
if (!match) return null;
return new AssignOperandToken(match[0], position);
}
}
class LiteralToken extends Token {
static regex = new RegExp('^([0-9]+\\.?[0-9]*)');
static match(text, position) {
let match = text.match(this.regex);
if (!match) return null;
return new LiteralToken(match[0], position);
}
}
class IdToken extends Token {
static regex = new RegExp('^[a-z][a-z0-9]*');
static match(text, position) {
let match = text.match(this.regex);
if (!match) return null;
return new IdToken(match[0], position);
}
}
function tokenize(text, offset) {
if (text == null || text.length === 0) return [];
const regexWhitespace = new RegExp('^\\s');
let whitespace = text.match(regexWhitespace);
if (whitespace) return tokenize(text.slice(whitespace[0].length), offset + whitespace[0].length);
let currentToken = ArithmeticOperandToken.match(text, offset) ||
CompareOperandToken.match(text, offset) ||
AssignOperandToken.match(text, offset) ||
LiteralToken.match(text, offset) ||
IdToken.match(text, offset);
if (!currentToken) {
console.error("SyntaxError");
return [];
}
let restTokens = tokenize(text.slice(currentToken.text.length), offset + currentToken.text.length);
return [currentToken].concat(restTokens);
}
function parse() {
}
function compile(text){
console.log(tokenize(text, 0));
}
let code = document.getElementById("code");
let compileButton = document.getElementById("compileButton");
compileButton.addEventListener('click', () => compile(code.value.replace('\n', ' ')));
compile(code.value.replace('\n', ' '));
/*
stmt := id "=" expr ";"
stmt := "{" stmtList "}"
stmt := if "(" expr ")" stmt
stmtList := stmt
stmtList := stmtList stmt
expr := term { ("<" | ">" | "<=" | ">=" | "==" | "!=") expr }
term := prod { ("+" | "-") term }
prod := prim { ( "*" | "\" ) prod }
prim := id | literal | "(" expr ")"
*/