示例#1
0
def funDecl():
    expr = Expr(ExprType.FUNCTION, nextToken())
    if not matchesToken(TokenType.IDENTIFIER):
        raise ParseException(
            "Expected fucntion identifier, found {0}".format(peekToken().text),
            peekToken())
    expr.value = consume(TokenType.IDENTIFIER).text
    expr.left = parameters()
    expr.right = block()
    expr.eval = evalFunc
    return expr
示例#2
0
def primary():
    result = Expr()
    if matchesToken(TokenType.LEFT_PAREN):
        result.type = ExprType.GROUPING
        result.token = nextToken()
        result.right = addition()
        result.eval = evalGrouping
        if not matchesToken(TokenType.RIGHT_PAREN):
            raise ParseException(
                "Expecting ')', found {0}".format(peekToken().text),
                peekToken())
        else:
            nextToken()
    elif matchesToken(TokenType.IDENTIFIER):
        result.type = ExprType.IDENTIFIER
        result.eval = evalVariableGet
        result.token = nextToken()
    else:
        result.type = ExprType.LITERAL
        result.eval = evalLiteral
        result.token = peekToken()
        if matchesToken(TokenType.FALSE):
            result.value = False
            result.dataType = DataType.BOOLEAN
        elif matchesToken(TokenType.TRUE):
            result.value = True
            result.dataType = DataType.BOOLEAN
        elif matchesToken(TokenType.NIL):
            result.value = None
            result.dataType = DataType.NIL
        elif matchesToken(TokenType.STRING):
            result.value = result.token.text
            result.dataType = DataType.STRING
        elif matchesToken(TokenType.NUMBER):
            if result.token.text.find(".") > -1:
                result.value = float(result.token.text)
                result.dataType = DataType.DOUBLE
            else:
                result.value = int(result.token.text)
                result.dataType = DataType.INTEGER
        nextToken()

    return result