-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdash.pegjs
157 lines (147 loc) · 2.51 KB
/
dash.pegjs
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
start=a:(com/expr)*{
return a.filter(x=>!x.big)
}
expr=a:(_/type)';'?{
return a
}
//separators
_=[ \n]
//types
type=str/bin/oct/hex/num/bool/cond/ls/ev/obj/var/aapp/app/def/arg/fn/a/ref
//comments
com='#.'[^\n]*{return''}
//strings
str='"'a:([^"\\]/'\\'.)*'"'?{
return{
type:'str',
body:a.map(x=>x.pop?x[1]=='"'?'"':x.join``:x).join``.replace(/\\\\/g,'\\')
}
}
//numbers
num=a:([0-9]+('.'[0-9]+)?('e''_'?[0-9]+)?/'.'[0-9]+('e''_'?[0-9]+)?/'oo'){
var f
return{
type:'num',
body:a!='oo'?(f=z=>z.map(x=>x&&x.pop?f(x):x).join``.replace(/_/g,'-'))(a):'Infinity'
}
}
bin=a:('0b'([01]+('.'[01]+)?/'.'[0-9]+)){
var f
return{
type:'num',
body:(f=z=>z.map(x=>x&&x.pop?f(x):x).join``.replace(/_/g,'-'))(a)
}
}
oct=a:('0o'([0-8]+('.'[0-8]+)?/'.'[0-8]+)){
var f
return{
type:'num',
body:(f=z=>z.map(x=>x&&x.pop?f(x):x).join``.replace(/_/g,'-'))(a)
}
}
hex=a:('0x'([0-9A-Fa-f]+('.'[0-9A-Fa-f]+)?/'.'[0-9A-Fa-f]+)){
var f
return{
type:'num',
body:(f=z=>z.map(x=>x&&x.pop?f(x):x).join``.replace(/_/g,'-'))(a)
}
}
//bool
bool=a:[TF]{
return{
type:'bool',
body:+(a=='T')
}
}
//list
ls='['a:expr*']'?{
a=a.filter(x=>!x.big)
return a.reduce((x,y)=>({
type:'app',
body:{
type:'app',
body:{type:'fn',body:'cat'},
f:x
},
f:y
}),{type:'ls',body:[]})
}
//object
obj='{'_*a:((num/fn/str)_*'\\'_*type _*';'?)*_*'}'?{
var o={}
return{
type:'obj',
body:(a.map(x=>o[x[0].body]=x[4]),o)
}
}
//expression list (holds multiple expressions)
arg='('a:expr*')'?{
return a.filter(x=>!x.big)
}
//argument reference
a=a:('#'[0-9]+){
return{
type:'a',
body:+a[1].join``
}
}
ref=a:('#'fn){
return{
type:'ref',
body:a[1].body
}
}
//function reference
fn=a:[^ \n;0-9".[\]\\(){}@#TF?]+{
return{
type:'fn',
body:a.join``
}
}
//function application
app=a:(fn/def)_* b:type{
return{
type:'app',
body:a.pop?a[1]:a,
f:b
}
}
aapp=a:(app/arg) _*b:type{
return{
type:'app',
body:a,
f:b
}
}
//function definition
def='@'_*b:type{
return{
type:'def',
body:b
}
}
var=a:fn _*'\\'_*b:type{
return{
type:'var',
body:a,
f:b
}
}
//conditionals
cond='['_*a:type _*'?'_*b:expr*_*'?'_*c:expr*_*']'?{
return{
type:'cond',
body:a,
f:b&&b.length?b.filter(x=>!x.big):{type:'bool',body:1},
g:c&&c.length?c.filter(x=>!x.big):{type:'bool',body:0}
}
}
//eval block
ev='{'a:expr*'}'_*b:var{
return{
type:'ev',
body:a.filter(x=>!x.big),
f:b.body,
g:b.f
}
}