-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconvert-to-basic.py
95 lines (73 loc) · 2.83 KB
/
convert-to-basic.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
#!/usr/bin/env python
#
# ******************************************************************************
#
# CONVERT REPOSITORY ASSEMBLER FORMAT TO BBC BASIC
#
# Written by Mark Moxon
#
# This script converts source code from the repository style into BBC BASIC ARM
# Assembler style.
#
# ******************************************************************************
import re
def convert(input_file, output_file):
in_code = False
for line in input_file:
if in_code:
if re.match(r"^ *\]", line):
in_code = False
else:
in_code = re.match(r"^ *\[", line)
# Change (...) in comments to [...]
while re.search(r"(\\.*)\(", line):
line = re.sub(r"(\\.*)\(", r"\1[", line)
while re.search(r"(\\.*)\)", line):
line = re.sub(r"(\\.*)\)", r"\1]", line)
# Change " in comments to '
while re.search(r"(\\.*)\"", line):
line = re.sub(r"(\\.*)\"", r"\1'", line)
# Change : in comments to ;
while re.search(r"(\\.*):", line):
line = re.sub(r"(\\.*):", r"\1;", line)
# Replace comma-separated EQUs with individual EQUs
while re.search(r"\b(EQU.) *([^:\\,\n]+), *", line):
line = re.sub(r"\b(EQU.) *([^:\\,\n]+), *", r"\1 \2 : \1 ", line)
if not in_code:
# Change \ to : REM
line = re.sub(r"^\\", r"REM", line)
line = re.sub(r"\\", r": REM", line)
# Change GameCode.bin to GameCode
line = re.sub(r"GameCode\.bin", r"GameCode", line)
if in_code:
# Change ALIGN to FN_AlignWithZeroes
line = re.sub(r"ALIGN", r"OPT FN_AlignWithZeroes", line)
# Write updated line
output_file.write(line)
# Write FNs
output_file.write("\n END\n\n")
output_file.write(" DEF FN_AlignWithZeroes\n")
output_file.write(" a = P% AND 3\n")
output_file.write(" IF a > 0 THEN\n")
output_file.write(" FOR I% = 1 TO 4 - a\n")
output_file.write(" [\n")
output_file.write(" OPT pass%\n")
output_file.write(" EQUB 0\n")
output_file.write(" ]\n")
output_file.write(" NEXT I%\n")
output_file.write(" ENDIF\n")
output_file.write(" =pass%\n")
print("Converting 1-source-files/Lander.arm")
source_file = open("1-source-files/main-sources/Lander.arm", "r")
basic_file = open("3-assembled-output/LanderSrc,fff", "w")
convert(source_file, basic_file)
source_file.close()
basic_file.close()
print("3-assembled-output/LanderSrc,fff file saved")
print("Converting 1-source-files/RunImage.arm")
source_file = open("1-source-files/main-sources/RunImage.arm", "r")
basic_file = open("3-assembled-output/RunImgSrc,fff", "w")
convert(source_file, basic_file)
source_file.close()
basic_file.close()
print("3-assembled-output/RunImage,fff file saved")