-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertFor.py
executable file
·61 lines (53 loc) · 2.7 KB
/
convertFor.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import re
import tokenize
from convertSuite import convertSuite
import convertToken
__author__ = 'Alexis Shaw'
def convertFor(token,line,t,v,i,understood,variables, oldIdent):
"""
"""
target = ''
i += 1
while len(token)-i > 0 :
(t, v, _, _,_) = token[i]
if t == tokenize.NAME:
if v == 'in':
break
target,i,understood,variables = convertToken.convertToken(token, target,t,v,i,understood,variables, '')
elif t == tokenize.OP and re.match(r'^[()[\]<>&^|~=+*%[,/-]$|^\*\*$|<<|>>|>=|<=|!=|==',v):
target,i,understood,variables = convertToken.convertToken(token, target,t,v,i,understood,variables, '')
elif t == tokenize.NL or t == tokenize.NUMBER:
target,i,understood,variables = convertToken.convertToken(token,target,t,v,i,understood,variables, '')
elif t == tokenize.STRING:
target,i,understood,variables = convertToken.convertToken(token,target,t,v,i,understood,variables, '')
elif t == tokenize.COMMENT and token[i+1][0] == tokenize.NL:
target,i,understood,variables = convertToken.convertToken(token,target,t,v,i,understood,variables, '')
else:
understood = False
i += 1
source = ''
i += 1
while len(token)-i > 0 :
(t, v, _, _,_) = token[i]
if t == tokenize.NAME:
source,i,understood,variables = convertToken.convertToken(token, source,t,v,i,understood,variables, "")
elif t == tokenize.OP and re.match(r'^[()[\]<>&^|~=+*%,/-]$|^\*\*$|<<|>>|>=|<=|!=|==',v):
source,i,understood,variables = convertToken.convertToken(token, source,t,v,i,understood,variables, '')
elif t == tokenize.NL or t == tokenize.NUMBER:
source,i,understood,variables = convertToken.convertToken(token,source,t,v,i,understood,variables, '')
elif t == tokenize.STRING:
source,i,understood,variables = convertToken.convertToken(token,source,t,v,i,understood,variables, '')
elif t == tokenize.COMMENT and token[i+1][0] == tokenize.NL:
source,i,understood,variables = convertToken.convertToken(token,source,t,v,i,understood,variables, '')
elif t == tokenize.OP and v == ":":
break
else: understood = False
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:
line += 'foreach ' + target + ' (' + source +'){' + body + '}' + comment + '\n'
else:
line += 'foreach ' + target + ' (' + source +'){\n' + body + '\n'+ oldIdent+'}\n'+ oldIdent
return line, i, understood, variables