-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesolanginterpreter1.js
99 lines (85 loc) · 3.34 KB
/
esolanginterpreter1.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
// // kata - https://www.codewars.com/kata/586dd26a69b6fd46dd0000c0
function myFirstInterpreter(code) {
code = code.replace(/[^+\.]/g, '');
let cell = 0;
let result = '';
for (const x of [...code]) {
x === '+' ? ++cell : (result += String.fromCharCode(cell));
cell = cell % 256;
}
return result;
}
// class Command {
// static get increment() {
// return '+';
// }
// static get output() {
// return '.';
// }
// static get all() {
// return [Command.increment, Command.output];
// }
// constructor(value) {
// this.value = value;
// }
// }
// class Memory {
// constructor(value) {
// this.value = value;
// }
// increment() {
// this.value += 1;
// if (this.value > 255) {
// this.value = 0;
// }
// }
// output() {
// return String.fromCharCode(this.value);
// }
// }
// class Output {
// constructor(value) {
// this.value = value;
// }
// add(char) {
// this.value += char;
// }
// }
// class Interpreter {
// constructor(code) {
// this.commands = code.split('').map((command) => new Command(command));
// this.memory = new Memory(0);
// this.output = new Output('');
// }
// run() {
// for (const command of this.commands) {
// switch (command.value) {
// case Command.increment:
// this.memory.increment();
// break;
// case Command.output:
// this.output.add(this.memory.output());
// break;
// default:
// break;
// }
// }
// }
// }
// function myFirstInterpreter(code) {
// const interpreter = new Interpreter(code);
// interpreter.run();
// return interpreter.output.value;
// }
// Hello World Program - outputs the string "Hello, World!"
console.log(
myFirstInterpreter(
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++.+++.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.'
),
'Hello, World!'
);
// Outputs the uppercase English alphabet
console.log(
myFirstInterpreter('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.'),
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
);