-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalysis.src
410 lines (338 loc) · 10.6 KB
/
analysis.src
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//
// All credits for the code below goes to Clover
// You are awesome! <3
SH={}
// LEXER CODE for creating custom shells
SH.Token=function(type,value,line)
out={"bool": 1}
out.out={"type": type, "value": value, "line": line}
return out
end function
SH.Lexer=function(input)
out={}
out.input=input
out.pos=0
out.line={"line": {"real": 0, "disp": 1}, "ind": {"real": 0, "disp": 1}}
out.getline=function(sub=0)
out={"line": {}, "ind": {}}
out.line.real=self.line.line.real
out.line.disp=self.line.line.disp
out.ind.real=self.line.ind.real
out.ind.disp=self.line.ind.disp - sub
return out
end function
out.err=function(token)
token.bool=0
return token
end function
out.gnt=function(start)
return self.input[start:self.pos]
end function
out.peek=function()
if self.pos >= self.input.len then return null else return self.input[self.pos]
end function
out.get=function()
out=self.input[self.pos]
self.pos=self.pos+1
self.line.ind.real=self.pos
self.line.ind.disp=self.line.ind.disp+1
if out == char(10) then
self.line.line.real=self.line.ind.real
self.line.line.disp=self.line.line.disp+1
self.line.ind.disp=1
end if
if self.pos-1 >= self.input.len then return null else return out
end function
out.atom=function(type)
return SH.Token(type, self.get, self.getline(1))
end function
out.next=function()
while [" ", " ", char(10)].indexOf(self.peek) != null
self.get
end while
c=self.peek
if not c then return SH.Token("End", null, self.getline(1))
if "abcdefghijklmnopqrstuvwxyz0123456789/._-$".values.indexOf(c.lower) != null then
return self.var
end if
// if c == "(" then
// return self.atom("LParen")
// else if c == ")" then
// return self.atom("RParen")
// else if c == "[" then
// return self.atom("LSquare")
// else if c == "]" then
// return self.atom("RSquare")
// else if c == "{" then
// return self.atom("LCurly")
// else if c == "}" then
// return self.atom("RCurly")
//end if
if "&!|>".values.indexOf(c) != null then
return self.sign
else if c == ";" then
return self.atom("Semi")
else if c == "," then
return self.atom("Comma")
end if
if ["""", "'"].indexOf(c) != null then
return self.chstr
end if
if c == "=" then
return self.atom("Assign")
end if
if self.pos >= self.input.len then return SH.Token("End", null, self.getline(1))
return self.err(SH.Token("Err", "Got "+c, self.getline))
end function
out.var=function()
start=self.pos
type="Word"
if self.peek == "$" then
type="Reference"
self.get
start=self.pos
end if
while self.peek and "abcdefghijklmnopqrstuvwxyz0123456789/._-".values.indexOf(self.peek.lower) != null
if type != "Word" and "/.-".values.indexOf(self.peek) != null then return self.err(SH.Token("Err", "Got "+self.peek, self.getline(1)))
self.get
end while
out=self.gnt(start)
return SH.Token(type,out,self.getline)
end function
out.sign=function()
out=self.get
chars=out+self.peek
if chars == "&&" then
self.get
return SH.Token("And", chars, self.getline(1))
else if chars == "!!" then
self.get
return SH.Token("Prev", chars, self.getline(1))
else if chars == "||" then
self.get
return SH.Token("Or", chars, self.getline(1))
else if chars == ">>" then
self.get
return SH.Token("RAppend",chars,self.getline(1))
end if
if out == "|" then
return SH.Token("Pipe", out, self.getline(1))
else if out == ">" then
return SH.Token("RArrow",out,self.getline(1))
end if
return self.err(SH.Token("Err", "Got "+out, self.getline(1)))
end function
out.chstr=function()
ch=self.get
out=""
while self.peek
s=self.get
if s == "\" then
tmp=self.get
if ch == """" and tmp == "n" then
out = out + char(10)
else
out = out + tmp
end if
continue
end if
if s == ch then
return SH.Token("String", out, self.getline)
end if
out=out+s
end while
return self.err(SH.Token("Err", "Expecting "+ch+" to end string.",self.getline))
end function
out.lex=function()
out={}
out.bool=1
out.out=[]
token=self.next
if not token.bool then return token
while token.out.type != "End"
if not token.bool then return token
out.out.push(token.out)
token=self.next
end while
out.out.push(token.out)
return out
end function
return out
end function
// PARSER CODE for creating custom shells
SH.AST={}
SH.AST.NoOp=function()
return {"classID": "NoOp"}
end function
SH.AST.CMDS=function(cmds)
out={}
out.classID="CMDS"
out.cmds=cmds
return out
end function
SH.AST.Pipe=function(cmds)
out={}
out.classID="Pipe"
out.cmds=cmds
return out
end function
SH.AST.AndOr=function(ops,cmds)
out={}
out.classID="AndOr"
out.ops=ops
out.cmds=cmds
return out
end function
SH.AST.Redir=function(cmd,op,redir=1)
out={}
out.classID="Redir"
out.cmd=cmd
out.op=op
out.redir=redir
return out
end function
SH.AST.CMD=function(word, args, redir=1)
out={}
out.classID="CMD"
out.word=word
out.cmd=word.value
out.args=args
return out
end function
SH.AST.Assign=function(name,content)
out={}
out.classID="Assign"
out.token=name
out.name=name.value
out.content=content
return out
end function
// SH.AST.CSV=function(l)
// out={}
// out.classID="CSV"
// out.l=l
// return out
// end function
SH.Parser=function(tokens)
out={}
out.err=function(tokentype,ttype,token)
return {"bool": 0, "message": "Got "+tokentype+", expected "+ttype, "token": token}
end function
out.tokens=tokens
out.ct=out.tokens.pull
out.eat=function(ttype)
out={"bool": 1}
if self.ct.type == ttype then out.out=self.tokens.pull else return self.err(self.ct.type, ttype, self.ct)
self.ct=out.out
return out
end function
out.empty=function()
return {"bool": 1, "out": SH.AST.NoOp}
end function
out.cmdlist=function()
node=self.state
if not node.bool then return node
results=[node.out]
while self.ct.type == "Semi"
self.eat("Semi")
n=self.state
if not n.bool then return n
results.push(n.out)
end while
if results.len == 1 then return {"bool": 1, "out": node.out}
return {"bool": 1, "out": SH.AST.CMDS(results)}
end function
out.state=function()
if self.ct.type != "Word" then
self.eat(self.ct.type)
return self.empty
end if
node=self.pipe
if not node.bool then return node
results=[node.out]
ops=[]
while ["And", "Or"].indexOf(self.ct.type) != null
ops.push(self.ct)
self.eat(self.ct.type)
n=self.pipe
if not n.bool then return n
results.push(n.out)
end while
if results.len == 1 then return {"bool": 1, "out": node.out}
return {"bool": 1, "out": SH.AST.AndOr(ops,results)}
end function
out.pipe=function()
node=self.redir
if not node.bool then return node
results=[node.out]
while self.ct.type == "Pipe"
self.eat("Pipe")
n=self.redir
if not n.bool then return n
results.push(n.out)
end while
if results.len == 1 then return {"bool": 1, "out": node.out}
return {"bool": 1, "out": SH.AST.Pipe(results)}
end function
out.redir=function()
node=self.cmd
if not node.bool then return node
result=node.out
op=self.ct
if ["RArrow", "RAppend"].indexOf(op.type) != null then
self.eat(op.type)
n=self.cmd
if not n.bool then return n
result=SH.AST.Redir(result, op, n.out)
end if
return {"bool": 1, "out": result}
end function
out.cmd=function()
var=self.ct
att=self.eat("Word")
if not att.bool then return att
if self.ct.type == "Assign" then
self.eat("Assign")
args=[]
content=self.ct
while ["Word", "String", "Reference"].indexOf(content.type) != null
self.eat(content.type)
if self.ct.type == "Comma" then
l=[content]
while self.ct.type == "Comma"
self.eat("Comma")
if ["Word", "String", "Reference"].indexOf(self.ct.type) == null then return self.err(self.ct.type, "Word / String / Reference", self.ct)
self.eat(self.ct.type)
l.push(self.ct)
end while
content=l
end if
args.push(content)
content=self.ct
end while
return {"bool": 1, "out": SH.AST.Assign(var, args)}
end if
args=[]
arg=self.ct
while ["Word", "String", "Reference"].indexOf(arg.type) != null
self.eat(arg.type)
if self.ct.type == "Comma" then
l=[arg]
while self.ct.type == "Comma"
self.eat("Comma")
if ["Word", "String", "Reference"].indexOf(self.ct.type) == null then return self.err(self.ct.type, "Word / String / Reference", self.ct)
l.push(self.ct)
self.eat(self.ct.type)
end while
arg=l
end if
args.push(arg)
arg=self.ct
end while
return {"bool": 1, "out": SH.AST.CMD(var, args)}
end function
out.parse=function()
return self.cmdlist
end function
return out
end function