-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertWhile.py
executable file
·43 lines (35 loc) · 1.79 KB
/
convertWhile.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
import re
import tokenize
from convertSuite import convertSuite
import convertToken
__author__ = 'Alexis Shaw'
def convertWhile(token,line,t,v,i,understood,variables, oldIdent):
"""
"""
condition = ''
i += 1
while len(token)-i > 0 :
(t, v, _, _,_) = token[i]
if t == tokenize.NAME:
condition,i,understood,variables = convertToken.convertToken(token, condition,t,v,i,understood,variables, oldIdent)
elif t == tokenize.OP and re.match(r'^[()[\]<>&^|~=+*%,/-]$|^\*\*$|<<|>>|>=|<=|!=|==',v):
condition,i,understood,variables = convertToken.convertToken(token, condition,t,v,i,understood,variables, oldIdent)
elif t == tokenize.NL or t == tokenize.NUMBER:
condition,i,understood,variables = convertToken.convertToken(token,condition,t,v,i,understood,variables, oldIdent)
elif t == tokenize.STRING:
condition,i,understood,variables = convertToken.convertToken(token,condition,t,v,i,understood,variables, oldIdent)
elif t == tokenize.COMMENT and token[i+1][0] == tokenize.NL:
condition,i,understood,variables = convertToken.convertToken(token,condition,t,v,i,understood,variables, oldIdent)
elif t == tokenize.OP and v == ":":
break
i += 1
i += 1
(t, v, _, _,_) = token[i]
body, i,understood,variables, singleLine, noSimpleStatements, comment = convertSuite(token,t,v,i,understood,variables, oldIdent)
if singleLine and noSimpleStatements <= 1:
line += body + ' ' + 'while ' + condition + ';' + comment + '\n'
elif singleLine:
line += 'while (' + condition + ') {' + body + '}' + comment + '\n'
else:
line += 'while (' + condition + ') {\n' + body + '\n'+ oldIdent+ '}\n' + oldIdent
return line, i, understood, variables