-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
173 lines (159 loc) · 4.28 KB
/
script.js
File metadata and controls
173 lines (159 loc) · 4.28 KB
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
const textarea = document.getElementsByTagName("textarea")[0]
const title = document.getElementsByTagName("p")[0]
const inp = document.getElementsByTagName("textarea")[1]
function $(el){
return document.querySelector(el);
}
const ext = [
".txt", ".vbs", ".rb", ".vvvvvv", ".cmd", ".ts", ".tar", ".r", ".zip"
]
function tokenise(code){
var token="",
tokens=[],
index=0;
for(var char of code.split("")){
// Checking for spaces and escaping them
if(char == " "){
if(token[token.length-1] == "\\"){
//console.log(token)
token+=char;
} else {
tokens.push(token)
token="";
}
} else {
if(char!="."){
token+=char;
}
}
if(index == code.length-1){
tokens.push(token)
}
//Checking for file extension
if(char == "."){
if(token[token.length-1]!="\\"){
tokens.push(token)
token=""
}
token+=char
}
index++;
}
for(var token of tokens){
if(isNaN(token)==false){
token=parseInt(token)
}
}
return tokens.map(token => token.replace(/\\/g, ''))
}
const lexlex = (token) => {
if(ext.includes(token)){
return ({name:"command", value:token})
} else if(token.includes("\"")) {
return ({name:"string", value:token})
} else {
return ({name:"number", value:token})
}
}
const lex = (code) => {
var tokens=[],
token="",
index=0;
for(var char of code.split("")) {
if(char == "."){
if(token[token.length-1]!="\\"){
tokens.push(lexlex(token))
token=""
}
token+=char
} else if(char == " "){
if(token[token.length-1]=="\\"){
token.slice(0, -1);
console.log(token)
token+=char;
} else {
tokens.push(lexlex(token))
token=""
}
} else {
token+=char;
}
if(index == code.length-1){
tokens.push(lexlex(token))
token=""
}
index++;
}
return tokens;
}
var compiled=[];
const compile = (code, inpval) => {
var index=0;
compiled=[];
document.getElementById("output").value = "";
var stack=[];
console.log(code)
for(var token of code){
if(token.name=="command"){
console.log(token.value)
if(token.value==".cmd"){
for(var inptoken of inpval){
console.log(inptoken)
if(isNaN(inptoken) == true){
stack.push(inptoken)
} else {
stack.push(parseInt(inptoken))
}
}
} else {
console.log(stdlib[token.value](stack));
}
} else {
if(token.name=="string"){
stack.push(token.value)
} else {
stack.push(parseInt(token.value))
}
}
if(index==code.length-1){
console.log(compiled)
eval(compiled)
}
index++;
}
}
function url(){
var code = textarea.value,
input = inp.value;
if(code||input){
console.log(location.hostname+"?"+encodeURIComponent(btoa(code))+"&"+encodeURIComponent(btoa(input)));
}
}
$("#run").onclick = () => {
var textval=lex(textarea.value);
if(inp.value.length>0){
var inpval=tokenise(inp.value);
}
console.log(textval,inpval);
compile(textval, inpval);
}
$("#link").onclick = () => {
url()
}
window.onload = () => {
if(location.search){
var [code, inp] = location.search.slice(1).split("&").map(query => atob(decodeURIComponent(query)))
$("#code").value = code
$("#inp").value = inp
}
}
document.addEventListener("keydown", evt => {
if(evt.key=="Enter"&&(evt.ctrlKey||evt.metaKey)) {
var textval=lex(textarea.value);
if(inp.value.length>0){
var inpval=tokenise(inp.value);
}
console.log(textval,inpval);
compile(textval, inpval);
}
})