Node* Parser::parseStatement()
	{
		if (match(Token::TYPE::LET))
			return parseLetExpr();
		else
			return parseExpression();
	}
Exemple #2
0
/**
Implements the parsing of expressions that are part of the core language

The core language supports:
- Functions (but not closures)
- Integer literals (decimal only)
- String literals
- Tuple literals [a,b]
- If-else expressions
- Sequencing blocks with {a;b}
- Function calls fun(a,b)
*/
Tuple coreParseExpr(Input* input)
{
    // Consume whitespace
    input->eatWS();

    // Numerical constant
    if (isdigit(input->peekCh()))
    {
        return parseInt(input);
    }

    // String literal
    if (input->matchCh('\''))
    {
        return parseStringLit(input, '\'');
    }
    if (input->matchCh('\"'))
    {
        return parseStringLit(input, '\"');
    }

    // Tuple expression
    if (input->matchCh('['))
    {
        return Tuple{
            Tuple("tuple"),
            parseExprList(input, ']')
        };
    }

    // Block/sequence expression (i.e { a; b; c }
    if (input->matchCh('{'))
    {
        return parseBlockExpr(input, '}');
    }

    // Keywords
    if (isalnum(input->peekCh()))
    {
        // If expression
        if (input->matchStr("if"))
            return parseIfExpr(input);

        // Let expression
        if (input->matchStr("let"))
            return parseLetExpr(input);

        // Function definition expression
        if (input->matchStr("fun"))
            return parseFunExpr(input);

        // Function call expression
        if (input->matchStr("call"))
            return parseCallExpr(input);
    }

    // Host/magic constants
    if (input->matchCh('$'))
    {
        return parseHostRef(input);
    }

    // Variable references
    if (input->peekCh() == '_' || isalpha(input->peekCh()))
    {
        return parseIdent(input);
    }

    // Parsing failed
    throw ParseError(input, "failed to parse expression");
}