Exemplo n.º 1
0
def Factor():
    token = Tokenizer.PeakToken()

    if token.IsVariable():

        Tokenizer.Consume()
        return ConstantFunction(Token.Variable.get_var(token.text))

    elif token.IsSymbol('('):

        Tokenizer.Consume()
        parseTree = Expression() 
        EndParens()
        return parseTree

    elif token.IsNumber() or \
            token.IsSymbol(['+','-']):

        return Number()

    elif token.IsFunc():

        Tokenizer.Consume()
        parseTree = UnaryFunction(token.text)
        parseTree.child(Factor())
        return parseTree

    else:
        raise ValueError("Factor")
Exemplo n.º 2
0
def FactorTail(leftParseTree):
    token = Tokenizer.PeakToken()
    if token.IsSymbol(['*','/','^']):

        Tokenizer.Consume()
        parseTree = BinaryFunction(token.text)
        parseTree.lchild(leftParseTree)
        parseTree.rchild(Factor())

        return FactorTail(parseTree)

    elif token.IsFunc():
        # implicit multiply
        Tokenizer.Consume()
        parseTree = BinaryFunction('*')
        rightParseTree = UnaryFunction(token.text)
        rightParseTree.child(Factor())

        parseTree.lchild(leftParseTree)
        parseTree.rchild(rightParseTree)

        return FactorTail(parseTree)

    elif token.IsEOF() or \
        token.IsSymbol(['+','-',')']):

        return leftParseTree

    else:
        raise ValueError("Factor Tail")        
Exemplo n.º 3
0
def Number():
    token = Tokenizer.PeakToken()
    negate = token.IsSymbol(['-'])
    if token.IsSymbol(['+','-']):
        # ignore unary +
        # use negate for unary -
        Tokenizer.Consume()
        token = Tokenizer.PeakToken()

    if token.IsNumber():

        Tokenizer.Consume()
        text = ('-' if negate else '') + token.text
        val = float(text) if '.' in text else int(text)
        return ConstantFunction(val)

    else:
        raise ValueError("Invalid Number")
Exemplo n.º 4
0
def TermTail(leftParseTree):
    token = Tokenizer.PeakToken()
    if token.IsSymbol(['+','-']):

        Tokenizer.Consume()

        parseTree = BinaryFunction(token.text)
        parseTree.lchild(leftParseTree)
        parseTree.rchild(Term())

        return TermTail(parseTree)

    elif token.IsEOF() or \
            token.IsSymbol(')'):

        return leftParseTree

    else:
        raise ValueError("Term Tail")
Exemplo n.º 5
0
def EndParens():
    token = Tokenizer.PeakToken()
    if token.IsSymbol(')'):
        Tokenizer.Consume()
    else:
        raise ValueError("Unmatched parenthesis")