-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
177 lines (155 loc) · 5.3 KB
/
Copy pathmain.py
File metadata and controls
177 lines (155 loc) · 5.3 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#Python 2 Pseudo-Code:
# A python script to convert another file of Python to Pseudo-code based on CSP Exam Reference Sheet.
# https://apcentral.collegeboard.org/media/pdf/ap-computer-science-principles-exam-reference-sheet.pdf
#Assignment, Display, and Input
# a ← expression
# DISPLAY(expression)
# INPUT()
#Arithmetic Operators and Numeric Procedures
# +, -, *, /, MOD
# RANDOM(a, b) a, b inclusive
#Rational and Boolean Operators
# Operators
# =, ≠, >, <, ≥, ≤
# Conditional Keywords
# NOT, AND, OR,
#Selection
# IF(condition){}
# ELSE{}
#Iteration
# REPEAT n TIMES{}
# REPEAT UNTIL(condition){}
#List Operations (Index at 1)
# aList ← [Value1, Value2, ...]
# INSERT(list, index, value)
# APPEND(list, value)
# REMOVE(list, index)
# LENGTH(list)
# FOR EACH item IN list{}
#Procedures and Procedure Calls
# PROCEDURE name(parameter, ...) {}
# RETURN(expression)
import regex as re
def addIndentation(level):
return (' '*(4*level))
def conversion(word):
if word == '=':
return ('←')
elif word == 'print':
return ('DISPLAY')
elif word == 'input':
return ('INPUT')
elif word == '%':
return ('MOD')
elif word == 'random.randint' or word == 'randint':
return ('RANDOM')
elif word == '==':
return ('=')
elif word == '!=':
return ('≠')
elif word == '>=':
return ('≥')
elif word == '<=':
return ('≤')
elif word == 'not':
return ('NOT')
elif word == 'and':
return ('AND')
elif word == 'or':
return ('OR')
elif word == 'return':
return ('RETURN')
else:
return (word)
def newIndentWords(specialWord, line):
#Use regex to split by just colons
split = re.split('(\s+|:)', line)
split = [word for word in split if word != '']
#Find the index of the colon
colonIndex = split.index(':')
ifIndex = split.index(specialWord)
conditional = split[ifIndex+2:colonIndex]
for index, word in enumerate(conditional):
conditional[index] = conversion(word)
return conditional
def elifHandler(line):
global newIndentLevel
#Write ELSE, then create a new line with a higher indentation level, with the IF statement.
elifIndent = newIndentWords('elif', line)
tempList2 = ['ELSE\n', addIndentation(newIndentLevel) + '{\n']
newIndentLevel += 0
tempList = [addIndentation(newIndentLevel+1) + 'IF(', *elifIndent, ')']
for x in tempList:
tempList2.append(x)
return (tempList2)
def indentHandler(oldIndent, newIndent):
global newIndentLevel
if oldIndent < newIndent:
return (['\n' + addIndentation(newIndent-1) + '{\n'])
elif oldIndent > newIndent:
indent = []
while oldIndent > newIndent:
indent.append(addIndentation(oldIndent-1) + '}\n')
oldIndent -= 1
return(indent)
else:
return ([''])
lines = []
with open('test.py', 'r', encoding='utf-8') as file:
#Remove any empty lines or if the line starts with a #
file = [line for line in file if line.strip() != '' and line[0] != '#']
#file = [re.sub('^\s*', '', line) for line in file] #remove leading whitespace
oldIndentLevel = 0
newIndentLevel = 0
for line in file:
#use regex to find indentation
indent = re.search('^\s*', line)
indent = indent.group(0)
newIndentLevel = len(indent) // 4
print(newIndentLevel)
words = re.split('(\s+|\(|\))', line) #Split by spaces, and parentheses
words = [word for word in words if word != ''] #Remove empty strings
words = [conversion(word) for word in words] #Convert words
currentLine = []
#Check the indentation level
indentations = indentHandler(oldIndentLevel, newIndentLevel)
for indent in indentations:
currentLine.append(indent)
for word in words:
if word == 'if':
conditional = newIndentWords('if', line)
tempList = ['IF(', *conditional, ')']
for x in tempList:
currentLine.append(x)
break
elif word == 'elif':
print(newIndentLevel)
handler = elifHandler(line)
for x in handler:
currentLine.append(x)
break
elif word == 'def':
procedure = newIndentWords('def', line)
tempList = ['PROCEDURE ', *procedure, '']
for x in tempList:
currentLine.append(x)
break
else:
currentLine.append(word)
print(currentLine)
currentLine = ''.join(currentLine)
lines.append(currentLine)
oldIndentLevel = newIndentLevel
indentations = indentHandler(oldIndentLevel, 0)
alreadyIndented = False
for indent in indentations:
check = re.search('^\s*', indent)
check = check.group(0)
check = len(check) // 4
if check != 0 and alreadyIndented == False:
indent = '\n' + indent
alreadyIndented = True
lines.append(indent)
with open('output.txt', 'w', encoding='utf-8') as file:
for line in lines:
file.write(line)