-
Notifications
You must be signed in to change notification settings - Fork 0
/
customstyling.js
82 lines (73 loc) · 2.21 KB
/
customstyling.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
const fs = require('fs');
const { get } = require('http');
const fetch = require('node-fetch');
const fileToRead = process.argv[2];
const fileContent = fs.readFileSync(fileToRead, 'utf-8');
const tokens = fileContent.replace(/[\s\n\t\r]+/g, ' ').trim().split(' ');
const memory = {};
const program = async (input) => {
await statement_list(input);
};
const statement_list = async (input) => {
const statementFound = await statement(input);
if (statementFound) {
await statement_list(input);
}
};
const statement = async (input) => {
const token = input.shift();
if (token === 'point') {
const next = input.shift();
console.log(memory[next]);
return true;
} else if (token === 'surface') {
const fileName = input.shift();
input.shift();
const variableName = input.shift();
const fileRead = fs.readFileSync(fileName, 'utf-8');
memory[variableName] = fileRead;
return true;
} else if (token === 'polyline') {
const variableName = input.shift();
const filterType = input.shift();
const filter = input.shift();
input.shift();
const into = input.shift();
const splitByLine = memory[variableName].split('\n');
const filtered = splitByLine.filter(line => line.indexOf(filter) !== -1);
const joined = filtered.join('\n');
memory[into] = joined;
return true;
} else if (token === 'LOOP') {
await loop(input);
return true;
} else if (token === 'GET') {
await GET(input);
return true;
}
input.unshift(token);
return false;
};
const loop = async (input) => {
const amount = parseInt(input.shift());
let i = 0;
while (i++ < amount) {
if (i === amount) {
await statement_list(input);
} else {
await statement_list([...input]);
}
if (input[0] === 'STOP') {
input.shift();
}
}
};
const GET = async (input) => {
const url = input.shift();
input.shift();
variableName = input.shift();
await fetch(url)
.then(response => response.text())
.then(text => memory[variableName] = text);
};
program(tokens);