-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_arduino.py
62 lines (45 loc) · 1.83 KB
/
compile_arduino.py
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
#! /usr/bin/python
# Written by Folkert van Heusden
# Released in the public domain
import sys
from compile_c import CompileToC
class CompileToArduino(CompileToC):
def header(self):
print('Brainfuck to Arduino compiler.', file=sys.stderr)
def get_name():
return ('arduino', None)
def genindent(self, level):
return ' ' * (level * 4)
def emitCharacter(self, n, dot):
for i in range(0, n):
print('%sSerial.print((char)data_mem[data_ptr]);' % self.genindent(self.lindentlevel))
def emitProgramBootstrap(self, file):
print('#include <stdint.h>')
print('')
print(f'// This is a translation of "{file}".')
print('uint8_t data_mem[1536];')
print('uint16_t data_ptr = 0;')
print('')
self.addComments(self.copyrightNotice)
print('')
print('void setup() {')
self.lindentlevel += 1
print('%sSerial.begin(115200);' % self.genindent(self.lindentlevel))
print('%sSerial.println(F("Brainfuck for Arduino"));' % self.genindent(self.lindentlevel))
self.lindentlevel -= 1
print('}')
def emitMainFunction(self):
self.lindentlevel += 1
print('void loop()')
print('{')
print('%sunsigned long int start = millis();' % self.genindent(self.lindentlevel))
self.translate(0, len(self.allCode))
print('')
print('%sunsigned long int end = millis();' % self.genindent(self.lindentlevel))
print('%sSerial.print(F("Took: "));' % self.genindent(self.lindentlevel))
print('%sSerial.println(end - start);' % self.genindent(self.lindentlevel))
print('')
print('%sfor(;;)' % self.genindent(self.lindentlevel))
print('%sdelay(1);' % self.genindent(self.lindentlevel + 1))
print('}')
self.lindentlevel -= 1