Exemplo n.º 1
0
def chainEvaluation(network, chaine, knownFunctions, stringDict):
    (elementList, elementType) = decomposition.decompo(chaine)
    i = 0
    decomposition.doublePoint(elementList, elementType, network)
    while i < len(elementList):
        if elementType[i] == 'cell':
            try:
                elementList[i] = str(network.getCellByName(elementList[i]).value)
            except decomposition.Error as e:
                raise decomposition.Error(e.reason)
        elif elementType[i] == 'function' and elementList[i] not in stringDict:
            try:
                value = eval_function(network, elementList, elementType, i, knownFunctions)
            except decomposition.Error as e:
                raise decomposition.Error(e.reason)
            if decomposition.isError(str(value)):
                return value
            end = decomposition.endOfFunction(elementList, i)
            elementList[i:end + 1] = [str(value)]
            elementType[i:end + 1] = ['nombre']
        i += 1
    try:
        return eval(''.join(elementList), None, stringDict)
    except SyntaxError as e:
        print("Error de syntaxe: {}".format(elementList))
        raise decomposition.Error('Syntaxe ({})'.format(e.msg))
    except ZeroDivisionError:
        raise decomposition.Error('Division par 0')
    except NameError as e:
        stringDict[str(e).split("'")[1]] = str(e).split("'")[1]
        return chainEvaluation(network, ''.join(elementList), knownFunctions, stringDict)
    except Exception as e:
        print("Can't evaluate '{0}'".format(''.join(elementList)), end='')
        raise decomposition.Error(str(e))
Exemplo n.º 2
0
def eval_function(network, elementList, elementType, k, knownFunctions):
    element = elementList[k]
    if element not in knownFunctions.dict:  # Si la fonction n'est pas connue, on renvoie une erreur
        raise decomposition.Error('{} n\'est pas connue'.format(elementList[k]))
    try:
        if elementType[k + 1] != 'p_ouvrante':
            raise decomposition.Error('\'(\' attendue après {}'.format(elementList[k]))
    except IndexError:
        raise decomposition.Error('\'()\' attendue après {}'.format(elementList[k]))
    p_count = 1  # Pour verifier le parenthesage
    args = []
    currentArg = ""
    k += 2
    while p_count > 0 and k < len(elementList):
        if elementType[k] == 'function':  # Si c'est une fonction, on l'évalue recursivement
            value = eval_function(network, elementList, elementType, k, knownFunctions)
            if decomposition.isError(str(value)):
                return value
            k = decomposition.endOfFunction(elementList, k)
            args.append(value)
            currentArg = ""
        elif elementType[k] == 'sep':
            if currentArg != "":  # S'il n'y a rien avant un separateur, la syntaxe n'est pas correcte
                args.append(chainEvaluation(network, currentArg, knownFunctions, {}))
                currentArg = ""
        elif elementType[k] == 'p_fermante':  # On gère le parenthesage
            p_count -= 1
            if p_count > 0:
                currentArg += elementList[k]
        elif elementType[k] == 'p_ouvrante':
            p_count += 1
            currentArg += elementList[k]
        else:
            currentArg += elementList[k]
        k += 1
    if p_count != 0:
        raise decomposition.Error('parenthesage incorrect')
    if currentArg != "":
        args.append(chainEvaluation(network, currentArg, knownFunctions, {}))
    return knownFunctions.dict[str(element)].value(args)