Exemplo n.º 1
0
FunctionAST *Parser::visitFuncttionDefinition() {
    int bkup = Tokens->getCurIndex();

    PrototypeAST *proto = visitPrototype();
    if (!proto) {
        return NULL;
    } else if ((PrototypeTable.find(proto->getName()) != PrototypeTable.end() &&
                    PrototypeTable[proto->getName()] != proto->getParamNum()) ||
                FunctionTable.find(proto->getName()) != FunctionTable.end()) {
        fprintf(stderr, "Function: %s is redefined", proto->getName().c_str());
        SAFE_DELETE(proto);
        return NULL;
    }

    VariableTable.clear();
    FunctionStmtAST *func_stmt = visitFunctionStatement(proto);
    if (func_stmt) {
        FunctionTable[proto->getName()] = proto->getParamNum();
        return new FunctionAST(proto, func_stmt);
    } else {
        SAFE_DELETE(proto);
        Tokens->applyTokenIndex(bkup);
        return NULL;
    }
}
Exemplo n.º 2
0
PrototypeAST *Parser::visitFunctionDeclaration() {
    int bkup = Tokens->getCurIndex();
    PrototypeAST *proto = visitPrototype();
    if (!proto) {
        return NULL;
    }

    if (Tokens->getCurString() == ";") {
        if (PrototypeTable.find(proto->getName()) != ProtoytypeTable.end() ||
                (FunctionTable.find(proto->getName()) != FunctionTable.end() &&
                 FunctionTable[proto->getName()] != proto->getParamNum())) {
            fprintf(stderr, "Function; %s is redefined", proto->getName().c_str());
            SAFE_DELETE(proto);
            return NULL;
        }
        PrototypeTable[proto->getName()] = proto->getParamNum();
        Tokens->getNextToken();
        return proto;
    } else {
        SAFE_DELETE(proto);
        Tokens->applyTokenIndex(bkup);
        return NULL;
    }
}