Skip to content

Commit

Permalink
01还有点问题,写了一下文件输出
Browse files Browse the repository at this point in the history
  • Loading branch information
tao1122334 committed Jan 5, 2024
1 parent 1d1abd0 commit 5d1d9a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions phase4/splc.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

def read_file(file_path):
current_paragraph = []
with open(file_path, 'r') as file:
with open(file_path+'.ir', 'r') as file:
for line in file:
if (line.startswith("LABEL") or line.startswith("FUNCTION")) and ":" in line:
current_paragraph = []
Expand Down Expand Up @@ -262,20 +262,23 @@ def translate(tac: str):
print("Usage: splc <ir_path>")
sys.exit(1)

print(data)
print('jal main')
print('j end')
print(pre)

with open(sys.argv[1], 'r') as ir:
with open(sys.argv[1] + '.s', 'w') as s:
s.write(data+"\n"+'jal main\nj end\n'+pre+"\n")

with open(sys.argv[1]+'.ir', 'r') as ir:
for tac in ir.read().splitlines():
res = translate(tac)
if not res:
# print(f'no translate: {tac}')
pass
else:
print("\n".join(res))
print()
print('end:')
with open(sys.argv[1]+'.s','a') as s:
s.write("\n".join(res))
s.write("\n")

with open(sys.argv[1] + '.s', 'a') as s:
s.write('end:')


# print(stack)
2 changes: 1 addition & 1 deletion phase4/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
input_prompt = 'Enter an integer:'

for i in range(3):
id = f'test_4_r{i:02}'
id = f'test_4_r0{i:0}'
with open(f'{id}.test', 'r') as tests:
subprocess.run(f'../bin/splc {id}.ir > {id}.s', shell=True)
for test in tests.read().split('\n\n'):
Expand Down

0 comments on commit 5d1d9a4

Please sign in to comment.