-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
195 lines (173 loc) · 6.95 KB
/
index.html
File metadata and controls
195 lines (173 loc) · 6.95 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Playground</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
margin: 0;
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
}
#navbar {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px;
background-color: var(--navbar-bg);
color: var(--navbar-text);
border-bottom: 1px solid var(--border-color);
}
#navbar select, #navbar button {
margin-left: 10px;
padding: 8px 12px;
font-size: 14px;
}
.resizable-container {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
}
#codeInput {
flex: 8;
resize: none;
height: auto;
overflow-y: auto;
margin: 0;
padding: 10px;
border: none;
outline: none;
background-color: var(--input-bg);
color: var(--input-text);
}
#output {
flex: 2;
overflow-y: auto;
margin: 0;
padding: 10px;
background-color: var(--output-bg);
color: var(--output-text);
border-top: 1px solid var(--border-color);
}
:root {
--navbar-bg: #f4f4f4;
--navbar-text: #000;
--input-bg: #fff;
--input-text: #000;
--output-bg: #f4f4f4;
--output-text: #000;
--border-color: #ccc;
--body-bg: #fff;
--body-text: #000;
}
[data-theme="dark"] {
--navbar-bg: #333;
--navbar-text: #fff;
--input-bg: #222;
--input-text: #fff;
--output-bg: #121212;
--output-text: #fff;
--border-color: #444;
--body-bg: #121212;
--body-text: #fff;
}
body {
background-color: var(--body-bg);
color: var(--body-text);
}
</style>
</head>
<body>
<div id="navbar">
<div>
<button id="runButton">Run Code</button>
<select id="examplesDropdown">
<option value="">Select Example</option>
<option value="print "Hello, world!";">Hello World</option>
<option value="for (var i = 0; i < 5; i = i + 1) {
 print i;
}">Simple Loop</option>
<option value="fun greet(name) {
 return "Hello, " + name;
}

print greet("Alice");">Function Example</option>
<option value="if (true) {
 print "This is true!";
} else {
 print "This is false!";
}">If Statement</option>
<option value="class Vehicle {
 // constructor
 init(capacity) {
 this.capacity = capacity;
 }
}

// inheritance
class Car < Vehicle {
 init(model) {
 super.init(5);
 this.model = model;
 }

 honk() {
 print "Honk honk!";
 }
}

class Ship < Vehicle {
 init() {
 super.init(100);
 }

 sail() {
 print "AAAALL ABOARD";
 }
}

var ford = Car("ford focus");
ford.honk();
print ford.model + " capacity:";
print ford.capacity;

print "";

var titanic = Ship();
titanic.sail();
print "titanic capacity:";
print titanic.capacity;">Classes</option>
</select>
</div>
<button id="themeToggle">Toggle Theme</button>
</div>
<div class="resizable-container">
<textarea autocorrect="off" id="codeInput" placeholder="Select an example from the dropdown above or enter your code here..."></textarea>
<div id="output"></div>
</div>
<script>
const outputDiv = document.getElementById('output');
const runButton = document.getElementById('runButton');
const examplesDropdown = document.getElementById('examplesDropdown');
const themeToggle = document.getElementById('themeToggle');
const codeInput = document.getElementById('codeInput');
let interpret, vm_init, vm_free;
const stdout = (text) => {
outputDiv.innerText += text + '\n';
};
const stderr = (text) => {
outputDiv.innerText += 'Error: ' + text + '\n';
};
var Module = {
print: stdout,
printErr: stderr,
onRuntimeInitialized: () => {
try {
vm_init = Module.cwrap('vm_init', null, []);
vm_free = Module.cwrap('free_vm', null, []);
interpret = Module.cwrap('interpret', 'number', ['string']);
} catch (error) {
console.error('Failed to initialize the WebAssembly module:', error);
outputDiv.innerText = 'Error initializing the WebAssembly module.';
}
},
};
runButton.addEventListener('click', () => {
vm_init();
const code = codeInput.value;
outputDiv.innerText = '';
if (!interpret) {
outputDiv.innerText = 'Interpreter is not initialized yet.';
return;
}
try {
const result = interpret(code);
if (result === 1) {
outputDiv.innerText += '\nCompilation error.';
} else if (result === 2) {
outputDiv.innerText += '\nRuntime error.';
} else {
outputDiv.innerText += '\nCode executed successfully.';
}
} catch (error) {
console.error('Error during code execution:', error);
outputDiv.innerText = 'An error occurred while running the code.';
}
vm_free();
});
examplesDropdown.addEventListener('change', (event) => {
const exampleCode = event.target.value;
codeInput.value = exampleCode;
});
let isDarkTheme = window.matchMedia('(prefers-color-scheme: dark)').matches;
const applyTheme = () => {
document.body.setAttribute('data-theme', isDarkTheme ? 'dark' : 'light');
};
themeToggle.addEventListener('click', () => {
isDarkTheme = !isDarkTheme;
applyTheme();
});
applyTheme(); // Apply theme on page load
</script>
<script src="lox.js"></script>
</body>
</html>