Exemplo n.º 1
0
def convertLen(token, line, t, v, i, understood, variables):
    type = getType(token, i + 1, understood, variables)

    i += 1
    t, v, _, _, _ = token[i]
    temp, i, understood, variables = convertGrouping(token, "", t, v, i, understood, variables)
    if type == "LIST":
        line += "scalar " + temp
    else:
        line += "length " + temp
    return line, i, understood, variables
Exemplo n.º 2
0
def convertSorted(token, line, t,v,i,understood, variables):
    type = getType(token, i+1, understood, variables)

    i+= 1
    t,v,_,_,_ = token[i]
    temp, i, understood, variables = convertGrouping(token,'',t,v,i,understood,variables)
    if type[0] == 'LIST':
        line += 'sort ' + temp
    else:
        line += 'sort ' + temp
        understood = False
    return line, i, understood, variables
Exemplo n.º 3
0
def convertVariableName(token,line,t,v,i,understood,variables):
    if token[i+1][0] == tokenize.OP and token[i+1][1] == '=':
        type,understood = getType(token , i+2, understood,variables)
        if not (v in variables) or type != variables[v]:
            variables[v] = type
            line += 'my '
    elif token[i-1][0] == tokenize.NAME and token[i-1][1] == 'for' and token[i+2][1] == 'range':
        if not (v in variables) or "NONSTRINGSCALAR" != variables[v]:
            variables[v] = "NONSTRINGSCALAR"
    if v in variables and variables[v] == 'STRING':
        if len(token) - i > 1 and token[i+1][0] == tokenize.OP and token[i+1][1] == '%':
            line, i, understood, variables = convertSprintf(token,line,t,v,i,understood,variables)
        else:
            line += '$' + v + ' '
    elif v in variables and variables[v] == 'NONSTRINGSCALAR':
        line += '$' + v + ' '
    elif v in variables and variables[v] == 'LIST':
        if token[i+1][0] == tokenize.OP and token[i+1][1] == '[':
            line += '@' + v
        elif token[i+1][0] == tokenize.OP and token[i+1][1]   == '.' and\
             token[i+2][0] == tokenize.NAME and token[i+2][1] == 'append'and\
             token[i+3][0] == tokenize.OP and token[i+3][1] == '(':
            toAppend, i, understood, variables = convertGrouping(token,line,t,v,i+3,understood,variables)
            line += 'push(@'+v+','+ toAppend + ') '
        elif token[i+1][0] == tokenize.OP and token[i+1][1]   == '.' and\
             token[i+2][0] == tokenize.NAME and token[i+2][1] == 'pop'and\
             token[i+3][0] == tokenize.OP and token[i+3][1] == '(' and\
             token[i+4][0] == tokenize.OP and token[i+4][1] == ')':
             line += 'pop(@' + v + ') '
             i += 4
        else:
            line += '@' + v + ' '
    elif v in variables and variables[v] == 'DICT':
        if token[i+1][0] == tokenize.OP and token[i+1][1] == '[':
            line += '%' + v
        elif token[i+1][0] == tokenize.OP and token[i+1][1]   == '.' and\
             token[i+2][0] == tokenize.NAME and token[i+2][1] == 'keys'and\
             token[i+3][0] == tokenize.OP and token[i+3][1] == '(' and\
             token[i+4][0] == tokenize.OP and token[i+4][1] == ')':
            line += 'keys(' + v + ') '
            i+= 4
        else:
            line += '%' + v + ' '
    else:
        line += '$' + v + ' '
    return line, i, understood, variables
Exemplo n.º 4
0
def convertToken(token, line,t,v,i,understood,variables, oldIdent):
    """
    Converts a token in Python into an equivelent Perl string
    """
    if t == tokenize.COMMENT: line,i,understood,variables = convertComment(token,line,t,v,i,understood,variables)
    elif t == tokenize.NAME:
        if keyword.iskeyword(v):
            if   v == 'print': line, i, understood, variables = convertPrint(token,line,t,v,i,understood,variables)
            elif v == 'not'  : line, i, understood, variables = convertNot  (token,line,t,v,i,understood,variables)
            elif v == 'and'  : line += '&& '
            elif v == 'or'   : line += '|| '
            elif v == 'if'   : line, i, understood, variables = convertIf   (token,line,t,v,i,understood,variables, oldIdent)
            elif v == 'for'  : line, i, understood, variables = convertFor  (token,line,t,v,i,understood,variables, oldIdent)
            elif v == 'while': line, i, understood, variables = convertWhile(token,line,t,v,i,understood,variables, oldIdent)
            elif v == 'elif' : line, i, understood, variables = convertElif (token,line,t,v,i,understood,variables, oldIdent)
            elif v == 'else' : line, i, understood, variables = convertElse (token,line,t,v,i,understood,variables, oldIdent)
            elif v == 'break': line += 'last '
            elif v == 'continue': line += 'next'
            elif v == 'import': line += ''
            else:
                line += v + ' '
                understood = False
        elif v == 'range': line, i, understood, variables = convertRange(token, line, t,v,i,understood, variables)
        elif v == 'sys'  : line, i, understood, variables = convertSys   (token, line, t,v,i,understood, variables, oldIdent)
        elif v == 'int'  : line, i, understood, variables = convertInt   (token, line, t,v,i,understood, variables)
        elif v == 'len'  : line, i, understood, variables = convertLen   (token, line, t,v,i,understood, variables)
        elif v =='sorted': line, i, understood, variables = convertSorted(token, line, t,v,i,understood, variables)
        else: line, i, understood, variables = convertVariableName(token,line,t,v,i,understood,variables)
    elif t == tokenize.STRING: line,i,understood,variables = convertString(token,line, t, v, i,understood,variables)
    elif t == tokenize.NEWLINE: line, i,understood,variables = convertLineEnd(token,line,t,v,i,understood,variables);
    elif t == tokenize.NL: line, i,understood,variables = convertLineEnd(token,line,t,v,i,understood,variables);
    elif t == tokenize.NUMBER:line += v + ' '
    elif t == tokenize.OP and re.match(r'^[<>&^|~=*,/%-]$|^\*\*$|<<|>>|>=|<=|!=|==|\+=|-=|\*=|%=|&=',v): line += v + ' '
    elif t == tokenize.OP and v == '+' and len(token) - i > 1 and\
         (((token[i+1][1] in variables) and variables[token[i+1][1]] == 'STRING') or
          token[i-1][0] == tokenize.STRING or
          token[i+1][0] == tokenize.STRING or
          getType(token, i+1, understood, variables)[0] == 'STRING'):
        line += '. '
    elif t == tokenize.OP and v == '+': line += '+ ';
    elif t == tokenize.OP and v == '[': line, i, understood, variables = convertSquareBraketOP(token, line, t,v,i,understood, variables)
    elif t == tokenize.OP and re.match('[(]',v): line, i,understood,variables = convertGrouping(token,line,t,v,i,understood,variables)
    elif t == tokenize.ENDMARKER: print "",
    else:
Exemplo n.º 5
0
def convertInt(token, line, t,v,i,understood, variables):
     if token[i+1][0] == tokenize.OP   and token[i+1][1] == '(':
         toConvert= ''
         toConvert, i, understood,variables = convertGrouping(token,toConvert,t,v,i+1,understood,variables)
         line += '(sprintf ("%d", ' +'scalar'+ toConvert + ') + 1 - 1)'
     return line,i,understood,variables