-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
81 lines (73 loc) · 2.84 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<!--<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />-->
<script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js"></script>
<style>
.centered-input {
text-align: center; /* Center the contents horizontally */
display: flex;
flex-direction: column; /* Center the contents vertically */
align-items: center;
justify-content: center;
height: 100vh; /* Make the div take up the full viewport height */
}
.centered-input input {
width: 300px; /* Set the width of the input */
height: 40px; /* Set the height of the input */
font-size: 18px; /* Increase the font size */
padding: 10px; /* Add padding for spacing inside the input */
}
</style>
</head>
<body>
<div class="centered-input">
<h1>Brain F*ck Interpreter</h1>
<input type="text" id="code" placeholder="Paste your brain f*ck code here"/>
<script>
document.getElementById('code').value = '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.';
</script>
<button id="get-code" py-onClick="get_and_run_code()">Run!</button>
<div id="display"></div>
</div>
<py-script>
def get_and_run_code(*args, **kwargs):
tokens = list(Element("code").value)
result = []
tape = [0]*30000
stack = []
tape_ptr = 0
token_ptr = 0
while token_ptr < len(tokens):
match tokens[token_ptr]:
case ">":
tape_ptr += 1
case "<":
if tape_ptr == 0: raise IndexError("Left-side tape overflow")
tape_ptr -= 1
case "+":
if tape[tape_ptr] == 255: raise OverflowError("Tape byte exceeding 255")
tape[tape_ptr] += 1
case "-":
if tape[tape_ptr] == 0: raise OverflowError("Tape byte below 0")
tape[tape_ptr] -= 1
case ".":
result.append(chr(tape[tape_ptr]))
case "[":
# skip tokens in loop if current tape byte is zero.
if tape[tape_ptr] == 0:
count = 0
while tokens[token_ptr] != "]" or count:
if tokens[token_ptr] == "[": count += 1
elif tokens[token_ptr] == "]": count -= 1
token_ptr += 1
else:
stack.append(token_ptr)
case "]":
if tape[tape_ptr] == 0: stack.pop()
else: token_ptr = stack[-1]
token_ptr += 1
Element("display").write(''.join(result))
</py-script>
</body>
</html>