-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·116 lines (90 loc) · 6.19 KB
/
run.sh
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
#!/bin/bash
: <<'````````bash'
# Programming Exercise 1: Brainfuck-Interpreter
## Implementation
Write an efficient interpreter for Brainfuck with 8-bit cells by compiling Brainfuck to bytecode. Unmatched brackets are valid as long as the branch is never taken (i.e., `+[` is a valid program). You can define your bytecode as you like, but focus on efficient generation, optimization, and interpretation. Combine instruction sequences (both sequences of identical operations and loop patterns) and implement super-instructions/combined execution of multiple neighboring instructions as optimization.
## Analysis (write answers as comment in your code)
Find suitably large and complex code examples to test the correctness and performance of your submission and document the results.
Compare the compile-time and the run-time -- at what point are the optimizations worth the effort?
How can more advanced optimizations be implemented? Why is this difficult? Give one Brainfuck code example and explain which optimization could lead to better bytecode.
## Command Line Interface
usage: ./brainfuck [-c] program_file
Execute a Brainfuck program.
-c: print bytecode instead of executing
program_file: file that contains the Brainfuck program
Note that program_file is not necessarily a regular file, but can also be a pipe as used in the tests below, where you cannot determine the input size without reading it.
You can add extra options, e.g., to control optimizations or for debugging.
## Submission
- Submission deadline: 2024-10-23 23:59
- Submit using `curl --data-binary "@<your-submission-file>" 'https://db.in.tum.de/teaching/ws2425/codegen/submit.py?hw=1&matrno=<your-matrno>'`
- Write your solution in a single C++ file.
- Include answers to theory questions as comments at the top of the source file.
- Make sure your submission passes as many tests as possible from this test script.
- No external dependencies, build systems other than Makefile, etc.
- If you really, *really* need more than just the C++ file, need to write a Makefile, or want to use C instead, combine all files s.t. this command sequence works: `split-file <submission> somedir || cp <submission> somedir/brainfuck.cc; cd somedir; bash <hw1.txt>`
````````bash
set -euo pipefail
FAILED=0
testcase() { ./brainfuck "$2" | "${@:3}" || (echo "FAILED: $1"; FAILED=1); }
CC=gcc CXX=g++ CFLAGS="-O3 -std=gnu11" CXXFLAGS="-O3 -std=c++20" make brainfuck
# NOTE: test cases use cmp to compare stdout with expected output
testcase "+." <(echo "+.") cmp <(echo -ne '\x01')
testcase "+-." <(echo "+-.") cmp <(echo -ne '\x00')
testcase "+>." <(echo "+>.") cmp <(echo -ne '\x00')
testcase "3434" <(base64 -d <<EOF | gunzip
H4sIAAAAAAAAA9PWJhnYka5FW9tGz04PhLkAIcEna3EAAAA=
EOF
) cmp <(echo -n "3434")
testcase "scrub right" <(echo ">+>->->+<[[-]<].>.>.>.>.") cmp <(echo -ne '\x00\x00\x00\x00\x01')
testcase "helloworld" <(base64 -d <<EOF | gunzip
H4sIAAAAAAAAAzWLyQ0AIQwD/7QS2RWgaQTRfxvrLGDJzuSqOlq8bDiekTaFYmrN3S0GSb6PbjDO
abJZv24JkHXG4wMEkgyIawAAAA==
EOF
) cmp <(echo "Hello World!")
testcase "mandelbrot" <(base64 -d <<EOF | gunzip
H4sIAAAAAAAAA+Va244bKRB9r6/wO2K+oIW0K+UrWjxMso5kJZlIjnej/fttLnUBCpq2+22RRmN3
F3XnVAG+/HH58f7x1/X75/vPx+XX9XH5en//8nj/fvnndv19vV9uH5fP9/fbx9e/v3y7/L7fHo/r
9uTfy6f77dvlz5+/ttlg5FitM8Y5F7+Ej2aJwzuHT42z1joaphzrCvTKm3XB4emp3R7j59V654lG
od4IDOSHWcGVhRuFPX4yrjtATNRlr6wk6hpU7ekKpCtrOquomMKzYHZa14CoLusPQb0sKfPJ0yzx
CywNSxrGxYGI4VIwpolkP/EeMQd+O81RKjTMwxi9nb+huSEfgHNqTLw6y2YYok/aG7JkBRtf+/TV
5HeeM9crUqSYmm7z4er0tFv4Y49CsQ7W6JnCnHX7WqdSgI3kAI+Gm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