-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjazzlang.js
129 lines (112 loc) · 3.4 KB
/
jazzlang.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
124
125
126
127
128
129
function Jazzlang(size = 32768) {
this.memory = new Array(size).fill(0);
this.code = [];
this.ptr = 0;
this.pc = 0;
this.jumpTo = {};
this.result = "";
}
Jazzlang.prototype.load = function (code) {
this.code = [];
const rawCode = code
.split("재즈가 뭐라고 생각하세요?")[1]
.split("이거야")[0]
.split("");
let read = 0;
while (read < rawCode.length) {
if (rawCode[read] === "샤") {
if (rawCode[read + 1] === "빱") {
this.code.push(">");
read += 2;
} else if (rawCode[read + 1] === "바" && rawCode[read + 2] === "다") {
this.code.push(",");
read += 3;
}
} else if (rawCode[read] === "사" && rawCode[read + 1] === "바") {
this.code.push("<");
read += 2;
} else if (rawCode[read] === "두") {
if (rawCode[read + 1] === "비") {
this.code.push("+");
read += 2;
} else if (rawCode[read + 1] === "밥") {
this.code.push("-");
read += 2;
} else if (rawCode[read + 1] === "붸" && rawCode[read + 2] === "둡") {
this.code.push(".");
read += 3;
} else if (rawCode[read + 1] === "봐") {
this.code.push("]");
read += 2;
}
} else if (rawCode[read] === "뚜" && rawCode[read + 1] === "비") {
this.code.push("[");
read += 2;
} else {
read += 1;
}
}
};
Jazzlang.prototype.preprocess = function () {
const stack = [];
for (let i = 0; i < this.code.length; i += 1) {
const command = this.code[i];
if (command === "[") {
stack.push(i);
} else if (command === "]") {
if (stack.length === 0) throw new Error("Syntax error");
this.jumpTo[i] = stack.pop();
this.jumpTo[this.jumpTo[i]] = i;
}
}
if (stack.length > 0) throw new Error("Syntax error");
};
Jazzlang.prototype.increasePtr = function () {
if (this.ptr >= this.memory.length - 1) throw new Error("Out of memory");
this.ptr += 1;
};
Jazzlang.prototype.decreasePtr = function () {
if (this.ptr <= 0) throw new Error("Out of memory");
this.ptr -= 1;
};
Jazzlang.prototype.increaseValue = function () {
this.memory[this.ptr] += 1;
};
Jazzlang.prototype.decreaseValue = function () {
this.memory[this.ptr] -= 1;
};
Jazzlang.prototype.printValue = function () {
this.result += String.fromCharCode(this.memory[this.ptr]);
};
Jazzlang.prototype.storingValue = function () {
this.memory[this.ptr] = prompt(
"아스키 코드 한 글자를 입력해주세요"
).charCodeAt(0);
};
Jazzlang.prototype.jump = function (command) {
if (command === "[" && this.memory[this.ptr] === 0) {
this.pc = this.jumpTo[this.pc];
} else if (command === "]" && this.memory[this.ptr] !== 0) {
this.pc = this.jumpTo[this.pc];
}
};
Jazzlang.prototype.run = function () {
this.memory = new Array(this.memory.length).fill(0);
this.ptr = 0;
this.pc = 0;
this.jumpTo = {};
this.result = "";
this.preprocess();
while (this.pc < this.code.length) {
const command = this.code[this.pc];
if (command === ">") this.increasePtr();
else if (command === "<") this.decreasePtr();
else if (command === "+") this.increaseValue();
else if (command === "-") this.decreaseValue();
else if (command === ".") this.printValue();
else if (command === ",") this.storingValue();
else if (command === "[" || command === "]") this.jump(command);
this.pc += 1;
}
};
window.jazz = new Jazzlang();