forked from KaustubhDamania/Three-Address-Code-Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhile.py
More file actions
52 lines (42 loc) · 1.48 KB
/
while.py
File metadata and controls
52 lines (42 loc) · 1.48 KB
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
from prettytable import PrettyTable
def while_loop(cleaned_code):
final_code = []
while_idx = None
for i in range(len(cleaned_code)):
codeline = cleaned_code[i]
if 'while' in codeline:
while_idx = i
# The loop condition would be enclosed in brackets
start_idx = codeline.index('(')
end_idx = codeline.index(')')
# Select the substring between start_idx and end_idx
bool_condn = ''.join(codeline[start_idx:end_idx+1])
final_code.append('if !{} goto({})'.format(bool_condn,None))
while_idx = i
elif '}' in codeline:
final_code.append('goto({})'.format(while_idx+1))
final_code[while_idx] = final_code[while_idx].replace('None',str(i+2))
while_idx = None
else:
final_code.append(codeline)
return final_code
with open('code.txt') as f:
code = f.readlines()
print('The Statement is:')
print(''.join(code))
cleaned_code = []
for i in range(len(code)):
if code[i] != '\n':
if code[i][-1] == '\n':
# don't include the \n at the end of each line
cleaned_code.append(code[i][:-1].strip())
else:
cleaned_code.append(code[i].strip())
final_code = while_loop(cleaned_code)
final_code.append('END')
print('\nThe Three Code Generated is:')
x1 = PrettyTable()
x1.field_names = ['Index','Code']
for i in range(len(final_code)):
x1.add_row([i+1,final_code[i]])
print(x1)