Ejemplo n.º 1
0
// <functionast>        ::= <prototype>  '{' <expression>* '}'
FunctionAst *Parser::parseFunctionDefinition() {
    PrototypeAst *proto = parsePrototype();
    if (proto == nullptr) {
        return nullptr;
    }
    if (_curTokenType != '{') {
        return Error("Expected '{' at start of function body.");
    }
    next(); // eat '{'

    std::vector<IAstExpression*> functionBody;
    while (_curTokenType != '}') {
        IAstExpression *blockExpr = parseBlockExpression();
        if (blockExpr == nullptr) {
            return Error("Unexpected token.");
        }
        functionBody.push_back(blockExpr);
    }
    next(); // eat '}'

    if (functionBody.size() == 0) {
        Warning("Empty function body.");
    }

    return new FunctionAst(proto, functionBody, _curToken->Line(), _curToken->Column());
}
Ejemplo n.º 2
0
std::unique_ptr<FunctionAST> Parser::parseDefinition(s_cursor_t &it, const s_cursor_t &end) {
    getNextToken(it, end);

    auto proto = parsePrototype(it, end);
    if (!proto)
        return nullptr;

    if (auto e = parseExpression(it, end))
        return llvm::make_unique<FunctionAST>(&_builder, _module, std::move(proto), std::move(e), _symTable);

    return nullptr; // no definition so far
}
Ejemplo n.º 3
0
std::unique_ptr<PrototypeAST> Parser::parseExtern(s_cursor_t &it, const s_cursor_t &end) {
    getNextToken(it, end);

    return parsePrototype(it, end);
}