예제 #1
0
def convertPrint(token,line, t, v, i,understood,variables):
    """

    """
    line += 'print '

    i += 1
    (t, v, _, _,_) = token[i]
    counter = 0
    while len(token)-i > 0 :
        counter += 1
        (t, v, _, _,_) = token[i]
        if t == tokenize.NEWLINE or t == tokenize.ENDMARKER:
            break
        elif t == tokenize.OP and re.match(';',v):
            break
        elif t == tokenize.OP and v == ',':
            if token[i+1][0] == tokenize.NEWLINE or token[i+1][0] == tokenize.ENDMARKER:
                line = line
            elif token[i+1][0] == tokenize.OP and token[i+1][1] == ';':
                line = line
            else:
                line, i, understood,variables = convertToken.convertToken(token,line,t,v,i,understood,variables,'')
        else:
            line, i, understood,variables = convertToken.convertToken(token,line,t,v,i,understood,variables,'')
        i += 1
    if not (i >= 1 and token[i-1][0] == tokenize.OP and token[i-1][1] == ',') and counter > 1:
        line += r'. "\n"'
    elif not (i >= 1 and token[i-1][0] == tokenize.OP and token[i-1][1] == ','):
        line += r' "\n"'
    i -= 1
    return line, i, understood,variables
예제 #2
0
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
예제 #3
0
def convertSuite(token,t,v,i,understood,variables, oldIdent):
    endOfLine = ""
    singleLine = True
    if t == tokenize.COMMENT:
        endOfLine += v
        i += 1
        (t, v, _, _,_) = token[i]
    if t == tokenize.NEWLINE or t == tokenize.NL:
        singleLine = False
        i += 1
    body = ''
    indentValue = oldIdent
    noSimpleStatements = 1
    while len(token)-i > 0 :
        (t, v, _, _,_) = token[i]
        if t == tokenize.NAME:
            body,i,understood,variables = convertToken.convertToken(token, body,t,v,i,understood,variables, indentValue)
        elif t == tokenize.OP and re.match(r'^[()<>&^|~=+*[\]%,/-]$|^\*\*$|<<|>>|>=|<=|!=|==|\+=|-=|\*=|%=|&=',v):
            body,i,understood,variables = convertToken.convertToken(token, body,t,v,i,understood,variables, indentValue)
        elif t == tokenize.NUMBER:
            body,i,understood,variables = convertToken.convertToken(token, body,t,v,i,understood,variables, indentValue)
        elif t == tokenize.STRING:
            body,i,understood,variables = convertToken.convertToken(token, body,t,v,i,understood,variables, indentValue)
        elif t == tokenize.COMMENT and token[i+1][0] == tokenize.NL:
            temp,i,understood,variables = convertToken.convertToken(token, "",t,v,i,understood,variables, indentValue)
            if singleLine:
                endOfLine += temp
            else:
                body += temp
        elif t == tokenize.NEWLINE or t == tokenize.ENDMARKER or t == tokenize.NL:
            if singleLine:
                break
            else:
                if t == tokenize.NEWLINE and i >= 1 and token[i-1][0] != tokenize.COMMENT:
                    body += ';'
                if not understood:
                    body = '#'+body
                if t == tokenize.NEWLINE and i >= 1 and token[i+1][0] != tokenize.DEDENT:
                    body += '\n'
                    body += indentValue
        elif t == tokenize.INDENT:
            indentValue = v
            body += indentValue
        elif t == tokenize.DEDENT:
            if not singleLine:
                body += v
                break
        elif t == tokenize.OP and v == ';':
            body += v + ' '
            if singleLine:
                noSimpleStatements += 1

        i += 1
    return body, i,understood,variables, singleLine, noSimpleStatements, endOfLine
예제 #4
0
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
def getFunctionExpression(token, line,t,v,i,understood,variables, oldIdent):
    while len(token)-i > 0 :
        (t, v, _, _,_) = token[i]
        if t == tokenize.NAME:
            line,i,understood,variables = convertToken.convertToken(token, line,t,v,i,understood,variables, oldIdent)
        elif t == tokenize.OP and re.match(r'^[([\]<>&^|~=+*%[-]$|^\*\*$|<<|>>|>=|<=|!=|==',v):
            line,i,understood,variables = convertToken.convertToken(token, line,t,v,i,understood,variables, oldIdent)
        elif t == tokenize.NL or t == tokenize.NUMBER:
            line,i,understood,variables = convertToken.convertToken(token,line,t,v,i,understood,variables, oldIdent)
        elif t == tokenize.STRING:
            line,i,understood,variables = convertToken.convertToken(token,line,t,v,i,understood,variables, oldIdent)
        elif t == tokenize.COMMENT and token[i+1][0] == tokenize.NL:
            line,i,understood,variables = convertToken.convertToken(token,line,t,v,i,understood,variables, oldIdent)
        elif t == tokenize.OP and re.match('[,)]', v):
            break
        else: understood = False
        i += 1
    i += 1
    return line,i,understood,variables
예제 #6
0
def convertListLiteral(token,line,t,v,i,understood,variables):
    line += '( '
    i += 1
    while len(token)-i > 0 :
        (t, v, _, _,_) = token[i]
        if t == tokenize.NAME:
            line,i,understood,variables = convertToken.convertToken(token, line,t,v,i,understood,variables,'')
        elif t == tokenize.OP and re.match(r'^[([<>&^|~=+,*%-]$|^\*\*$|<<|>>|>=|<=|!=|==',v):
            line,i,understood,variables = convertToken.convertToken(token, line,t,v,i,understood,variables,'')
        elif t == tokenize.NL or t == tokenize.NUMBER:
            line,i,understood,variables = convertToken.convertToken(token,line,t,v,i,understood,variables,'')
        elif t == tokenize.STRING:
            line,i,understood,variables = convertToken.convertToken(token,line,t,v,i,understood,variables,'')
        elif t == tokenize.COMMENT and token[i+1][0] == tokenize.NL:
            line,i,understood,variables = convertToken.convertToken(token,line,t,v,i,understood,variables,'')
        elif t == tokenize.OP and re.match('[\]]', v):
            line += ')' + ' '
            break
        else: understood = False
        i += 1
        #line += tokenize.tok_name[t]
    return line, i, understood, variables
예제 #7
0
def convertSprintf(token,line,t,v,i,understood,variables):
    string,i,understood,variables = convertString.getString(token,'',t,v,i,understood,variables)
    i += 2
    t,v,_,_,_ = token[i]
    format,i,understood,variables = convertToken.convertToken(token,'',t,v,i, understood,variables,'')
    i +=1
    t,v,_,_,_ = token[i]
    string = re.sub(r'\\(%|$|@)', r'\1', string)
    line += 'sprintf ('+string +', '+ format+' ) '

 #   print tokenize.tok_name[t], i

    return line, i, understood, variables
def convertSliceOperator(token,line,t,v,i,understood,variables):
    sliceOperators = []
    count = 0

    i+=1
    sliceOperators.append('')
    while len(token)-i > 0 :
        (t, v, _, _,_) = token[i]
        if t == tokenize.NAME:
            sliceOperators[count],i,understood,variables = convertToken.convertToken(token, sliceOperators[count],t,v,i,understood,variables,'')
        elif t == tokenize.OP and re.match(r'^[([<>&^|~=+,*%-]$|^\*\*$|<<|>>|>=|<=|!=|==',v):
            sliceOperators[count],i,understood,variables = convertToken.convertToken(token, sliceOperators[count],t,v,i,understood,variables,'')
        elif t == tokenize.NL or t == tokenize.NUMBER:
            sliceOperators[count],i,understood,variables = convertToken.convertToken(token,sliceOperators[count],t,v,i,understood,variables,'')
        elif t == tokenize.STRING:
            sliceOperators[count],i,understood,variables = convertToken.convertToken(token,sliceOperators[count],t,v,i,understood,variables,'')
        elif t == tokenize.COMMENT and token[i+1][0] == tokenize.NL:
            sliceOperators[count],i,understood,variables = convertToken.convertToken(token,sliceOperators[count],t,v,i,understood,variables,'')
        elif t == tokenize.OP and v == ']':
            break
        elif t == tokenize.OP and v == ':':
            sliceOperators.append('')
            count+= 1
        else: understood = False
        i += 1
    if len(sliceOperators) == 1:
        line += '['+sliceOperators[0]+'] '
    elif len(sliceOperators) == 2:
        if sliceOperators[0] == '':
            sliceOperators[0] = '0'
        if sliceOperators[1] == '':
            sliceOperators[1] = '-1'
        line += '['+sliceOperators[0]+'..'+sliceOperators[1]+']'
    elif len(sliceOperators) == 3:
        line += '['+sliceOperators[0]+'..'+sliceOperators[1]+'..'+sliceOperators[2]+']'
        understood == False
    return line, i, understood, variables,count
예제 #9
0
def convertAndPrint(f):
    """
    Converts a python program file into perl program file
    """
    g = tokenize.generate_tokens(f.readline)
    token = []
    for l in g:
        token.append(l)
    line = ""
    understood = True
    variables = {}
    i=0
    (t, v, _, _,_) = token[i]
    while len(token)-i > 0:
        (t, v, _, _,_) = token[i]
        line,i,understood,variables = convertToken(token,line,t,v,i,understood,variables, '')
        i += 1
    print line,
예제 #10
0
def convertNot(token,line,t,v,i,understood,variables):
    """

    """
    line += '!('
    i += 1
    while len(token)-i > 0 :
        (t, v, _, _,_) = token[i]
        if t == tokenize.NAME and not keyword.iskeyword(v):
            line,i,understood,variables = convertToken.convertToken(token, line,t,v,i,understood,variables,'')
        elif t == tokenize.OP and re.match(r'^[()[\]<>&^|/~=+*%,-]$|^\*\*$|<<|>>|>=|<=|!=|==|\+=|-=|\*=|%=|&=',v):
            line,i,understood,variables = convertToken.convertToken(token, line,t,v,i,understood,variables,'')
        elif t == tokenize.NL or t == tokenize.NUMBER:
            line,i,understood,variables = convertToken.convertToken(token,line,t,v,i,understood,variables,'')
        elif t == tokenize.STRING:
            line,i,understood,variables = convertToken.convertToken(token,line,t,v,i,understood,variables,'')
        elif t == tokenize.COMMENT and token[i+1][0] == tokenize.NL:
            line,i,understood,variables = convertToken.convertToken(token,line,t,v,i,understood,variables,'')
        else:
            line += ') '
            return convertToken.convertToken(token,line,t,v,i,understood,variables,'')
        i += 1