forked from KPGPMC/furlang
Add paren expression
Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
@@ -58,6 +58,11 @@ std::ostream& unaryop_expression_node::print(std::ostream& os) const {
|
||||
return os;
|
||||
}
|
||||
|
||||
bool unaryop_expression_node::equal(const node& rhsNode) const {
|
||||
const auto& rhs = reinterpret_cast<const unaryop_expression_node&>(rhsNode);
|
||||
return expression_node::equal(rhsNode) && m_type == rhs.m_type && m_node == rhs.m_node;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, binop_expression_node_t type) {
|
||||
switch (type) {
|
||||
case binop_expression_node_t::Add: return os << '+';
|
||||
@@ -77,6 +82,15 @@ bool binop_expression_node::equal(const node& rhsNode) const {
|
||||
return expression_node::equal(rhsNode) && m_type == rhs.m_type && m_lhs == rhs.m_lhs && m_rhs == rhs.m_rhs;
|
||||
}
|
||||
|
||||
std::ostream& paren_expression_node::print(std::ostream& os) const {
|
||||
return os << *m_node;
|
||||
}
|
||||
|
||||
bool paren_expression_node::equal(const node& rhsNode) const {
|
||||
const auto& rhs = reinterpret_cast<const paren_expression_node&>(rhsNode);
|
||||
return expression_node::equal(rhsNode) && m_node == rhs.m_node;
|
||||
}
|
||||
|
||||
bool declaration_node::equal(const node& rhs) const {
|
||||
return declaration_type() == reinterpret_cast<const declaration_node&>(rhs).declaration_type();
|
||||
}
|
||||
|
||||
@@ -118,8 +118,8 @@ ast::statement_node_h parser::parse_statement() {
|
||||
return { token.location(), "unexpected token "s + token->type + ", expected statement, declaration or expression" };
|
||||
}
|
||||
|
||||
ast::expression_node_h parser::parse_expression() {
|
||||
return parse_expression_rhs(parse_expression_unary(16), 16);
|
||||
ast::expression_node_h parser::parse_expression(std::uint32_t precedence) {
|
||||
return parse_expression_rhs(parse_expression_unary(precedence), precedence);
|
||||
}
|
||||
|
||||
ast::literal_node_h parser::parse_literal() {
|
||||
@@ -145,10 +145,20 @@ ast::literal_node_h parser::parse_literal() {
|
||||
|
||||
ast::expression_node_h parser::parse_expression_primary() {
|
||||
const auto& tok = peek_token();
|
||||
|
||||
auto literal = parse_literal();
|
||||
if (literal.present()) return std::move(literal);
|
||||
return { tok.location(), "unexpected token"s + tok->type + ", expected expression or literal" };
|
||||
switch (tok->type) {
|
||||
case token_t::Lparen: {
|
||||
auto tok = next_token();
|
||||
auto node = parse_expression();
|
||||
auto err = eat_token(token_t::Rparen);
|
||||
if (err.has_error()) return err;
|
||||
return ast::paren_expression_node_h{ tok.location(), m_arena, std::move(node) };
|
||||
}
|
||||
default: {
|
||||
auto literal = parse_literal();
|
||||
if (literal.present()) return std::move(literal);
|
||||
return { tok.location(), "unexpected token"s + tok->type + ", expected expression or literal" };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct unaryop_info {
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
#include <iostream>
|
||||
|
||||
int main(void) {
|
||||
furc::front::parser parser("<TEMP>", "func main() {\n return 6 - -7 * 9++;\n}");
|
||||
furc::front::parser parser("<TEMP>", "func main() {\n return (6 - -7) * 9++;\n}");
|
||||
std::cout << parser.parse() << '\n';
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user