-
Notifications
You must be signed in to change notification settings - Fork 1
/
lillith001.html
74 lines (62 loc) · 2.03 KB
/
lillith001.html
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
<body></body>
<script>
var input = 'print{[hello] + (hi) + [ho] * 9}'
var input = 'a + b * c + d'
var input = 'a * b + c * d + a+2 func(hi+2, ho)'
var input = '{a:1, [2]:2 ,c:1+3, d:5}'
function L_tokenize(s) {
return s.match(/[a-zA-Z_][a-zA-Z_0-9]*|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?|\'(\\\'|[^\'])*\'|\"(\\\"|[^\"])*\"|\/\/.*|\/\*[\s\S]*?\*\/|[\{\}\[\]\(\)\,\+\-\*\/\=\:]/g)
}
var precedence = {
',' : 0.1,
':' : 0.2,
'+' : 1,
'-' : 1,
'*' : 2,
'/' : 2
}
function L_parse(s) {
var ts = L_tokenize(s)
var ti = 0
aa = [[null, []]]
var t = null
while (t = ts[ti++]) {
console.log('-----=====------')
console.log(JSON.stringify(aa, null, ' '))
console.log('token = ' + t)
if (t.match(/[a-zA-Z_][a-zA-Z_0-9]*|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?|\'(\\\'|[^\'])*\'|\"(\\\"|[^\"])*\"/)) {
aa[aa.length - 1][1].push(t)
} else if (t.match(/[\{\(\[]/)) {
aa.push([t, []])
} else if (t.match(/[\}\)\]]/)) {
while (!aa[aa.length - 1][0].match(/[\{\(\[]/)) {
var a = aa.pop()
aa[aa.length - 1][1].push(a)
}
var a = aa.pop()
aa[aa.length - 1][1].push(a)
} else if (t.match(/[\,\+\-\*\/\:]/)) {
while (aa[aa.length - 1][0].match(/[\,\+\-\*\/\:]/) && (precedence[aa[aa.length - 1][0]] > precedence[t])) {
var a = aa.pop()
if (aa.length == 0) {
aa.push([null, [a]])
} else {
aa[aa.length - 1][1].push(a)
}
}
if (aa[aa.length - 1][0] != t) {
aa.push([t, [aa[aa.length - 1][1].pop()]])
}
} else {
throw 'unexpected token: ' + t
}
}
while (aa.length > 1) {
var a = aa.pop()
aa[aa.length - 1][1].push(a)
}
console.log('======***========')
return aa[0]
}
console.log(JSON.stringify(L_parse(input), null, ' '))
</script>