Add: generate test from comment in assembly file

This commit is contained in:
brice.boisson
2023-11-21 18:36:10 +09:00
parent b95e79edc4
commit 7d60960831
3 changed files with 185 additions and 9 deletions

71
scripts/gen_test.py Normal file
View File

@@ -0,0 +1,71 @@
import re
source_code = open('test.S', 'r')
Lines = source_code.readlines()
test_file = []
def get_test(test, instr_addr, final = False):
result = ""
pattern_r = re.compile(r'R\[(\d+)\]=(\d+)')
pattern_pc = re.compile(r'PC=(\d+)')
pattern_mem = re.compile(r'MEM\[(\d+)\]=(\d+)')
# Use the patterns to search for matches in the input string
match_r = pattern_r.search(test)
match_pc = pattern_pc.search(test)
match_mem = pattern_mem.search(test)
if match_r:
number1 = match_r.group(1)
number2 = match_r.group(2)
result = f"{number1}={number2}"
elif match_pc:
number_pc = match_pc.group(1)
result = f"32={number_pc}"
elif match_mem:
number1_mem = match_mem.group(1)
number2_mem = match_mem.group(2)
result = f"{int(number1_mem) + 33}={number2_mem}"
if result != "" and not final:
result = f"{instr_addr}:{result}"
return result
instr_addr = 0
for line in Lines:
if line.isspace() or ':' in line or line[0] == '#':
continue
elif '#' in line:
tests = re.split(r'\s|,', line[line.find('#') + 1:])
for test in tests:
new_test = get_test(test, instr_addr)
if new_test != "":
test_file.append(new_test)
instr_addr += 4
# save test_file to a file named test.tmp
with open('test.tmp', 'w') as f:
for item in test_file:
f.write("%s\n" % item)
final_test_file = []
# go through Line in reverse order
for line in reversed(Lines):
if line.isspace() or ':' in line:
continue
elif line[0] == '#':
tests = re.split(r'\s|,', line[1:])
for test in tests:
new_test = get_test(test, instr_addr, True)
if new_test != "":
final_test_file.append(new_test)
else:
break
# save test_file to a file named test.tmp
with open('test.final.tmp', 'w') as f:
for item in final_test_file:
f.write("%s\n" % item)