Beispiel #1
0
def calForLoop(varDict, curLine, nxtLine):
    parts = curLine.split(';')
    #print(parts)
    curVar = findVar(parts[0], varDict)
    eq = isPresent(parts[0], '=')
    val = findVar(parts[0][eq + 1:], varDict)

    if val == 1:
        varDict[curVar] = calInt(parts[0][eq + 1:])

    else:
        varDict[curVar] = varDict[val]

    cmp = findComp(parts[1])
    pos = isPresent(parts[1], cmp)

    tmp = findVar(parts[1][pos + 1:], varDict)

    if tmp == 1:
        j = calInt(parts[1][pos + 1:])

    else:
        j = varDict[tmp]

    sign = findSign(parts[2])

    signPos = isPresent(parts[2], sign)
    last = isPresent(parts[2], ')')
    red = calInt(parts[2][signPos + 1:last])

    printLoop(varDict, curVar, j, red, curLine, nxtLine, cmp, sign)
Beispiel #2
0
def fileRead():
    varDict = {}
    varStr = input('Enter variables separated by space : ').split()
    for s in varStr:
        varDict[s] = 0

    with open('cProgram.txt', 'r') as f:
        curLine = f.readline().strip()
        while isPresent(curLine, 'main()') == -1:
            curLine = f.readline().strip()

        curLine = f.readline().strip()
        while isPresent(curLine, 'int') == -1:
            curLine = f.readline().strip()

        for curLine in f:
            curLine = curLine.strip()
            if curLine.strip() == '' or curLine.strip() == '{':
                continue

            if isPresent(curLine, 'printf') == -1 or isPresent(
                    curLine, 'return') == -1:
                if isPresent(curLine, 'for') != -1:
                    #print('yes')
                    calForLoop(varDict, curLine, f.readline().strip())

                else:
                    calVar(curLine, varDict)

            print(f"\n[ {curLine} ] >>>> ", end=' ')
            printVar(varDict)
Beispiel #3
0
def findComp(str1):
    res = ''

    if isPresent(str1, '<=') != -1:
        res = 'le'

    elif isPresent(str1, '<') != -1:
        res = 'l'

    if isPresent(str1, '>=') != -1:
        res = 'ge'

    elif isPresent(str1, '>') != -1:
        res = 'g'

    return res
Beispiel #4
0
def findSign(str1):
    sign = ''

    if isPresent(str1, '+') != -1:
        sign = '+'

    elif isPresent(str1, '-') != -1:
        sign = '-'

    elif isPresent(str1, '*') != -1:
        sign = '*'

    else:
        sign = '/'

    return sign
Beispiel #5
0
def calNxtLine(nxtLine, varDict):
    eq = isPresent(nxtLine, '=')
    nCur = findVar(nxtLine[:eq], varDict)

    ns = findSign(nxtLine[eq:])
    pos = isPresent(nxtLine, ns)

    tmp = findVar(nxtLine[eq:pos], varDict)
    if tmp == 1:
        nl = calInt(nxtLine[eq + 1:pos])

    else:
        nl = tmp

    tmp = findVar(nxtLine[pos:], varDict)
    if tmp == 1:
        nr = calInt(nxtLine[pos + 1:])

    else:
        nr = tmp

    return (nCur, nl, nr, ns)
Beispiel #6
0
def calVar(curLine, varDict):
    isPos = isPresent(curLine, '+')
    isNeg = isPresent(curLine, '-')
    isMul = isPresent(curLine, '*')
    isDiv = isPresent(curLine, '/')
    isEql = isPresent(curLine, '=')

    if isPos == isNeg == isMul == isDiv == -1:
        if isEql != -1:
            assignVar(curLine, varDict, isEql)

    elif isEql != -1:
        if isPos != -1:
            mathOps(curLine, varDict, isEql, isPos, '+')

        elif isNeg != -1:
            mathOps(curLine, varDict, isEql, isNeg, '-')

        elif isMul != -1:
            mathOps(curLine, varDict, isEql, isMul, '*')

        elif isDiv != -1:
            mathOps(curLine, varDict, isEql, isDiv, '/')