-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (112 loc) · 3.13 KB
/
index.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
114
115
116
117
118
119
120
121
122
123
import fs from "fs";
// LE STACK
var stack = [];
let funcs = new Map();
var inProgFunc;
var funcStat = false;
const readfile = (location) =>
new Promise((resolve, reject) => {
fs.readFile(location, 'utf-8', (err,data)=>{
if(err){
console.error(err);
return reject(err);
}else{
resolve(data);
}
});
});
function exec(x){
// console.log(x);
switch(x){
case "": break;
case "+":{
stack[stack.length-2] = parseInt(stack[stack.length-1]) + parseInt(stack[stack.length-2]);
stack.pop();
break;
}
case "-":{
stack[stack.length-2] = parseInt(stack[stack.length-1]) - parseInt(stack[stack.length-2]);
stack.pop();
break;
}
case "*":{
stack[stack.length-2] = parseInt(stack[stack.length-1]) * parseInt(stack[stack.length-2]);
stack.pop();
break;
}
case "/":{
stack[stack.length-2] = parseInt(stack[stack.length-1]) / parseInt(stack[stack.length-2]);
stack.pop();
break;
}
case "%":{
stack[stack.length-2] = parseInt(stack[stack.length-1]) % parseInt(stack[stack.length-2]);
stack.pop();
break;
}
case "exile":{
stack.pop();
break;
}
case "twice":{
stack.push(stack[stack.length-1]);
break;
}
case "print":{
console.log(stack[stack.length-1]);
break;
}
case "printS":{
console.log(stack);
break;
}
default:{
if(x.startsWith('scribe(')){
// start scribing a function (or var, pretty much the same here)
funcStat = true;
funcs.set(x.substring(7,x.length-2), []);
inProgFunc = x.substring(7,x.length-2);
}else if(x.startsWith('(')){
// call a function
let func = funcs.get(x.substring(1, x.length-1));
for(let i of func){
exec(i);
}
}else if(x.startsWith('"') && x.endsWith('"')){
stack.push(x.substring(1,x.length-1));
}else if(x.startsWith(':')){
// compose a line
let compline = x.split(' ');
compline.shift();
for(let i of compline) exec(i);
}else if(x.startsWith('//')){
break;
}else{
stack.push(x);
}
break;
}
}
}
function mainLoop(main){
for(let x of main){
let y=x.trim();
if(!funcStat){
exec(y);
} else if(funcStat){
if(y!='}'){
funcs.get(inProgFunc).push(y);
}else{
funcStat = false;
}
}
}
}
async function main(){
const pathto = process.argv[2];
// console.log(pathto);
const p1 = await readfile(pathto);
const program = p1.split('\n');
mainLoop(program);
}
main();