feat(furc/parser): add variable and group expressions
This commit is contained in:
@@ -79,8 +79,6 @@ public:
|
|||||||
virtual decl_type_e decl_type() const = 0;
|
virtual decl_type_e decl_type() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class expr_node;
|
|
||||||
|
|
||||||
struct var_decl_node final : public decl_node {
|
struct var_decl_node final : public decl_node {
|
||||||
decl_type_e decl_type() const override { return Variable; }
|
decl_type_e decl_type() const override { return Variable; }
|
||||||
|
|
||||||
@@ -103,6 +101,8 @@ public:
|
|||||||
enum expr_type_e {
|
enum expr_type_e {
|
||||||
Literal,
|
Literal,
|
||||||
|
|
||||||
|
VarRead,
|
||||||
|
Group,
|
||||||
BinaryOp,
|
BinaryOp,
|
||||||
UnaryOp,
|
UnaryOp,
|
||||||
};
|
};
|
||||||
@@ -114,6 +114,21 @@ public:
|
|||||||
virtual expr_type_e expr_type() const = 0;
|
virtual expr_type_e expr_type() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct var_read_expr_node final : public expr_node {
|
||||||
|
expr_type_e expr_type() const override { return VarRead; }
|
||||||
|
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
var_read_expr_node(std::string&& name)
|
||||||
|
: name(std::move(name)) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct group_expr_node final : public expr_node {
|
||||||
|
expr_type_e expr_type() const override { return Group; }
|
||||||
|
|
||||||
|
expr_node* inner = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
struct binary_op_expr_node final : public expr_node {
|
struct binary_op_expr_node final : public expr_node {
|
||||||
enum binary_op_type {
|
enum binary_op_type {
|
||||||
Add = 0,
|
Add = 0,
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ private:
|
|||||||
stmt_node* parse_stmt();
|
stmt_node* parse_stmt();
|
||||||
decl_node* parse_decl();
|
decl_node* parse_decl();
|
||||||
expr_node* parse_expr();
|
expr_node* parse_expr();
|
||||||
lit_node* parse_lit();
|
|
||||||
|
|
||||||
ast_type parse_type();
|
ast_type parse_type();
|
||||||
comp_stmt_node parse_comp();
|
comp_stmt_node parse_comp();
|
||||||
|
|||||||
+19
-14
@@ -89,19 +89,6 @@ expr_node* parser::parse_expr() {
|
|||||||
return parse_expr_right(parse_expr_unary());
|
return parse_expr_right(parse_expr_unary());
|
||||||
}
|
}
|
||||||
|
|
||||||
lit_node* parser::parse_lit() {
|
|
||||||
auto token = eat_token(token::Integer, token::Char);
|
|
||||||
switch (token.type) {
|
|
||||||
case token::Integer: {
|
|
||||||
return m_arena->allocate<int_lit_node>(int_lit_node(token.value.integer));
|
|
||||||
}
|
|
||||||
case token::Char: {
|
|
||||||
return m_arena->allocate<char_lit_node>(char_lit_node(token.value.character));
|
|
||||||
}
|
|
||||||
default: throw std::runtime_error("unreachable");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ast_type parser::parse_type() {
|
ast_type parser::parse_type() {
|
||||||
auto token =
|
auto token =
|
||||||
eat_token(token::S8, token::U8, token::S16, token::U16, token::S32, token::U32, token::S64, token::U64);
|
eat_token(token::S8, token::U8, token::S16, token::U16, token::S32, token::U32, token::S64, token::U64);
|
||||||
@@ -131,7 +118,25 @@ comp_stmt_node parser::parse_comp() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expr_node* parser::parse_expr_primary() {
|
expr_node* parser::parse_expr_primary() {
|
||||||
return parse_lit();
|
auto token = eat_token(token::Identifier, token::LParen, token::Integer, token::Char);
|
||||||
|
switch (token.type) {
|
||||||
|
case token::Identifier: {
|
||||||
|
return m_arena->allocate<var_read_expr_node>(std::string(token.value.string));
|
||||||
|
}
|
||||||
|
case token::LParen: {
|
||||||
|
group_expr_node group;
|
||||||
|
group.inner = parse_expr();
|
||||||
|
eat_token(token::RParen);
|
||||||
|
return m_arena->allocate<group_expr_node>(std::move(group));
|
||||||
|
}
|
||||||
|
case token::Integer: {
|
||||||
|
return m_arena->allocate<int_lit_node>(int_lit_node(token.value.integer));
|
||||||
|
}
|
||||||
|
case token::Char: {
|
||||||
|
return m_arena->allocate<char_lit_node>(char_lit_node(token.value.character));
|
||||||
|
}
|
||||||
|
default: throw std::runtime_error("unreachable");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
expr_node* parser::parse_expr_unary() {
|
expr_node* parser::parse_expr_unary() {
|
||||||
|
|||||||
Reference in New Issue
Block a user