-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_pascal.py
102 lines (74 loc) · 2.8 KB
/
compile_pascal.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
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
# Written by Folkert van Heusden
# Released in the public domain
# www.vanheusden.com
import sys
from compile_ada import CompileToAda
class CompileToPascal(CompileToAda):
loopNr = 0
lnrs = []
def header(self):
print('Brainfuck to Pascal compiler.', file=sys.stderr)
def get_name():
return ('pascal', None)
def addToData(self, n, dot, position):
print('%sdata_mem[data_ptr] := data_mem[data_ptr] + %d;' % (self.genindent(self.lindentlevel), n))
def subFromData(self, n, dot, position):
print('%sdata_mem[data_ptr] := data_mem[data_ptr] - %d;' % (self.genindent(self.lindentlevel), n))
def emitCharacter(self, n, dot):
for i in range(0, n):
print('%sWrite(chr(data_mem[data_ptr]));' % self.genindent(self.lindentlevel))
def startLoop(self, n, position):
for j in range(0, n):
ind = self.genindent(self.lindentlevel)
print('%sWhile data_mem[data_ptr] > 0 do' % ind)
print('%sBegin' % ind)
self.lindentlevel += 1
def finishLoop(self, n, dot, position):
for j in range(0, n):
self.lindentlevel -= 1
print('%sEnd;' % self.genindent(self.lindentlevel))
def emitProgramBootstrap(self, file):
print('Program My_Brainfuck_application;')
print('Uses Crt;')
print(f'(* This is a translation of "{file}" *)')
print('')
for i in self.copyrightNotice:
print('(* %s *)' % i)
print('')
def emitFunctions(self):
print('Var')
ind = self.genindent(self.lindentlevel)
print('%sdata_mem : Array[0..32767] of Byte;' % ind)
print('%sdata_ptr : Integer;' % ind)
print('%si : Integer;' % ind)
print('')
for blkLoop in range(0, len(self.blocks)):
print('Procedure f%d;' % blkLoop)
print('Begin')
self.lindentlevel += 1
self.translate(self.blocks[blkLoop][0], self.blocks[blkLoop][1])
self.lindentlevel -= 1
print('End;')
print('')
def emitMainFunction(self):
print('Begin')
self.lindentlevel += 1
ind = self.genindent(self.lindentlevel)
print('%sdata_ptr := 0;' % ind)
print('%sfor i := 0 to 32767 do' % ind)
print('%sbegin' % ind)
self.lindentlevel += 1
print('%sdata_mem[i] := 0;' % self.genindent(self.lindentlevel))
self.lindentlevel -= 1
print('%send;' % ind)
self.lindentlevel -= 1
self.translate(0, len(self.allCode))
print('End.')
def addComment(self, s):
print('(* %s *)' % s)
def multilineCommentStart(self):
print('(*')
def multilineCommentLine(self, s):
print(' * %s' % s)
def multilineCommentEnd(self):
print(' *)')