-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
395 lines (325 loc) · 12 KB
/
index.html
File metadata and controls
395 lines (325 loc) · 12 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BrainF*ck Compiler</title>
</head>
<body>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
/* dark style */
background-color: #1e1e1e;
color: #fff;
}
h1 {
text-align: center;
}
a {
color: #fff;
}
a:hover {
color: #fffaaa
}
.section {
display: flex;
flex-direction: column;
height: 85vh;
gap: 20px;
padding: 50px;
box-sizing: border-box;
}
.grid-2 {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 20px;
}
#intro {}
#lexer {
display: none;
}
#parser {
display: none;
}
#semantic {
display: none;
}
#ir {
display: none;
}
#output {
display: none;
text-align: center;
}
#lexer-section {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 10px;
flex-grow: 1;
}
#lexer-code {
grid-column: 1;
}
#lexer-output {
grid-column: 2;
}
.row2 {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 10px;
}
#parser-split {
/* l/r */
display: grid;
grid-template-rows: 1fr 1fr;
grid-gap: 10px;
}
#parser-intro {
/* l */
grid-column: 1;
}
.cfg {
/* r */
grid-column: 2;
}
#cfgimg {
background-image: url("https://i.imgur.com/5XZQ1YH.png");
background-size: contain;
background-repeat: no-repeat;
background-position: center;
height: 100%;
}
.button {
display: block;
margin: auto;
padding: 10px;
background-color: #1e1e1e;
color: #fff;
border: 1px solid #fff;
border-radius: 5px;
font-size: 16px;
width: 100px;
}
.button:hover {
background-color: #fff;
color: #1e1e1e;
cursor: pointer;
}
h2 {
text-align: center;
}
textarea {
width: 90%;
height: 90%;
resize: none;
background-color: #1e1e1e;
color: #fff;
border: 1px solid #fff;
border-radius: 5px;
padding: 10px;
-webkit-user-modify: read-write-plaintext-only;
}
</style>
<h1>BrainF*ck Compiler</h1>
<div id="intro" class="section">
<div class="grid-2">
<div>
<h2>About</h2>
<p>
BrainF*ck is a programming language created in 1993 by Urban Müller, and is notable for its extreme
minimalism. It is a Turing tarpit, designed to challenge and amuse programmers, and was not made to
be
suitable for practical use. It was inspired by the programming language FALSE.
</p>
<p>
There are 8 commands in BrainF*ck:
<ul>
<li> > - increment the data pointer (to point to the next cell to the right).</li>
<li>
< - decrement the data pointer (to point to the next cell to the left).</li>
<li> + -
increment (increase by one) the byte at the data pointer.
</li>
<li> - - decrement (decrease by one) the byte at the data pointer.</li>
<li> . - output the byte at the data pointer.</li>
<li> , - accept one byte of input, storing its value in the byte at the data pointer.</li>
<li> [ - if the byte at the data pointer is zero, then instead of moving the instruction pointer
forward to the next command, jump it forward to the command after the matching ] command.
</li>
<li> ] - if the byte at the data pointer is nonzero, then instead of moving the instruction
pointer
forward to the next command, jump it back to the command after the matching [ command.</li>
</ul>
</p>
</div>
<div>
<div>
<h2>Project</h2>
<p>
For our project, we will be creating a compiler for the BrainF*ck language. The compiler is
written
in
Python, and will be able to compile BrainF*ck code into LLVM IR, which can then be compiled into
machine
code.
There are 4 main parts to the compiler:
<ul>
<li>Lexer - converts the source code into tokens</li>
<li>Parser - converts the tokens into an abstract syntax tree</li>
<li>Semantic Analyzer - checks the syntax tree for errors</li>
<li>Code Generator - converts the syntax tree into LLVM IR code</li>
</ul>
</p>
<p>
<h4>Members</h4>
<ul>
<li>Kival Mahadew - 221001688</li>
<li>Anele Skhosana- 220049255</li>
<li>Sphephelo Jali - 220055031</li>
<li>Shaileshin Manickum - 221024591</li>
<li>Sizwe Ngema- 218010150 </li>
<li>Luqmaan Essay - 220025354</li>
<li>Phakamani Mchunu - 219061179</li>
<li>Simphiwe Mdokha - 219001646</li>
</ul>
</p>
</div>
</div>
</div>
<button id="run" class="button" onclick="pywebview.api.intro()">Next</button>
</div>
<div id="lexer" class="section">
<div>
<h2>Lexer</h2>
<p>
The lexer is the first part of the compiler. It converts the source code into tokens. Tokens are the
smallest unit of a programming language. For example, in the statement "int x = 5;", the tokens are
"int", "x", "=", and "5". The lexer will also remove comments from the source code. In our language, we
only have 8 types of tokens, each consisting of a single character.
</p>
</div>
<div id="lexer-section">
<!-- code bloc -->
<div id="lexer-code">
<h4>Code</h4>
<textarea id="lexer-code-input" cols="30" rows="9" placeholder="Enter BrainF*ck code here..."
onchange="lexer()"></textarea>
</div>
<!-- output -->
<div>
<h4>Output</h4>
<div id="lexer-output">
</div>
</div>
</div>
<button id="run" class="button" onclick="pywebview.api.to_parser()">Next</button>
</div>
</div>
</div>
<div id="parser" class="section">
<h2>Parser</h2>
<div id="parser-split">
<div id="parser-intro">
<p>
Since BF is a regular language, we can define a set of CFG rules to parse the
tokens into an
abstract
syntax
tree.
The following is the CFG for BF:
<ul>
<li>program -> (loop | operation)* </li>
<li>loop -> loop_start program loop_end </li>
<li>operation -> (increment | decrement | move_right | move_left | input | output) </li>
<li>loop_start -> [ </li>
<li>loop_end -> ] </li>
</ul>
<p>
We used a recursive descent parser to parse the tokens into an abstract
syntax tree.
<a href="https://en.wikipedia.org/wiki/Recursive_descent_parser">Read more
about recursive descent parsers here.</a>
<p>
</div>
<!-- canvas to draw cfg -->
<div id="cfgimg">
</div>
</div>
<button id="run" class="button" onclick="pywebview.api.to_semantic()">Next</button>
</div>
<div id="semantic" class="section">
<h2>Semantic Analysis</h2>
<div id="" class="grid-2">
<div id="">
<p>
Semantic analysis is the process of checking the syntax tree for errors. For
example, in the
statement
"int x = 5;", the semantic analyzer will check if the variable "x" has been declared
before it is used.
In our language we check the CFG for the following errors:
<ul>
<li>Unmatched loop</li>
<li>Unmatched bracket</li>
<li>Invalid token</li>
</ul>
</div>
<!-- canvas to draw cfg -->
<div>
<h4>Errors</h4>
<pre id="semantic-output">
[Error] Unmatched loop at character 5 <br>
[Error] Unmatched bracket at character 10 <br>
[Error] Invalid token at character 15 <br>
</pre>
</div>
</div>
<button id="run" class="button" onclick="pywebview.api.to_ir()">Next</button>
</div>
<div id="ir" class="section">
<h2>Code Generator</h2>
<div id="">
<div id="parser-intro">
Here we convert the syntax tree into LLVM IR code.
This is the final step of the compiler. We have roughly based this code generator on the
https://github.com/itchyny/llvm-brainfuck project
</div>
<!-- canvas to draw cfg -->
<div>
<h4>LLVM IR Output</h4>
<pre id="ir-output">
[Error] Unmatched loop at character 5
[Error] Unmatched bracket at character 10
[Error] Invalid token at character 15
</pre>
</div>
</div>
<button id="run" class="button" onclick="pywebview.api.to_compiler()">Next</button>
</div>
<div id="output" class="section">
<h2>Run the Program</h2>
<div id="parser-split">
<div id="parser-intro">
The program has now been converted to LLVM IR, and then to machine code. We can now
execute this program and view its output. The output could not be piped to the GUI and will be displayed
in the console.
</div>
</div>
<button id="run" class="button" onclick="pywebview.api.finish()">Next</button>
</div>
<script>
// on lexer input change
function lexer() {
// get value of code editor
var code = document.getElementById("lexer-code-input").value
pywebview.api.lexer(code).then(function (output) {
console.log(output)
document.getElementById("lexer-output").innerHTML = output
})
}
// set on input change
// document.getElementById("lexer-code-input").onchange = lexer
document.getElementById("lexer-code-input").onkeyup = lexer
</script>
</body>
</html>