parser: add identifiers

This commit is contained in:
Zack Buhman 2023-08-20 17:42:57 +00:00
parent cd0c9c7c8c
commit 05664bb132
6 changed files with 25 additions and 9 deletions

15
ast.cpp
View File

@ -3,11 +3,6 @@
namespace dsp { namespace dsp {
void ast_printer_t::visit(const unary_t * unary) const
{
parenthesize((unary->oper).lexeme, unary->right);
}
void ast_printer_t::visit(const binary_t * binary) const void ast_printer_t::visit(const binary_t * binary) const
{ {
parenthesize((binary->oper).lexeme, binary->left, binary->right); parenthesize((binary->oper).lexeme, binary->left, binary->right);
@ -18,11 +13,21 @@ void ast_printer_t::visit(const grouping_t * grouping) const
parenthesize("grouping", grouping->expr); parenthesize("grouping", grouping->expr);
} }
void ast_printer_t::visit(const identifier_t * identifier) const
{
parenthesize("identifier", identifier->name.lexeme);
}
void ast_printer_t::visit(const literal_t * literal) const void ast_printer_t::visit(const literal_t * literal) const
{ {
os << std::to_string(literal->value); os << std::to_string(literal->value);
} }
void ast_printer_t::visit(const unary_t * unary) const
{
parenthesize((unary->oper).lexeme, unary->right);
}
void ast_printer_t::parenthesize(const std::string_view s, const expr_t * a) const void ast_printer_t::parenthesize(const std::string_view s, const expr_t * a) const
{ {
os << '(' << s << ' '; os << '(' << s << ' ';

View File

@ -17,10 +17,11 @@ struct ast_printer_t : visitor_t<void>
std::ostream& os; std::ostream& os;
void visit(const unary_t * unary) const;
void visit(const binary_t * binary) const; void visit(const binary_t * binary) const;
void visit(const grouping_t * grouping) const; void visit(const grouping_t * grouping) const;
void visit(const identifier_t * identifier) const;
void visit(const literal_t * literal) const; void visit(const literal_t * literal) const;
void visit(const unary_t * unary) const;
void visit(const op::alu_t * alu) const; void visit(const op::alu_t * alu) const;
void visit(const op::mov_ram_x_t * mov_ram_x) const; void visit(const op::mov_ram_x_t * mov_ram_x) const;

View File

@ -44,6 +44,14 @@ struct grouping_t : expr_accept_t<grouping_t>
const expr_t * expr; const expr_t * expr;
}; };
struct identifier_t : expr_accept_t<identifier_t>
{
identifier_t(token_t name)
: name(name) {}
const token_t name;
};
struct literal_t : expr_accept_t<literal_t> struct literal_t : expr_accept_t<literal_t>
{ {
literal_t(num_t value) literal_t(num_t value)

View File

@ -150,7 +150,8 @@ expr_t * parser_t::orl()
expr_t * parser_t::primary() expr_t * parser_t::primary()
{ {
if (match(number)) return new literal_t(std::get<num_t>(previous().literal)); if (match(number)) return new literal_t(std::get<num_t>(previous().literal));
else if (match(identifier)) return new identifier_t(previous());
if (match(left_paren)) { if (match(left_paren)) {
expr_t * expr = expression(); expr_t * expr = expression();

View File

@ -5,9 +5,9 @@ namespace dsp {
// expressions // expressions
struct binary_t; struct binary_t;
struct grouping_t; struct grouping_t;
struct identifier_t;
struct literal_t; struct literal_t;
struct unary_t; struct unary_t;
struct assign_t;
// instructions // instructions
namespace op { namespace op {

View File

@ -8,10 +8,11 @@ namespace dsp {
template <typename T> template <typename T>
struct visitor_t struct visitor_t
{ {
virtual T visit(const unary_t * expr) const = 0;
virtual T visit(const binary_t * expr) const = 0; virtual T visit(const binary_t * expr) const = 0;
virtual T visit(const grouping_t * expr) const = 0; virtual T visit(const grouping_t * expr) const = 0;
virtual T visit(const identifier_t * expr) const = 0;
virtual T visit(const literal_t * expr) const = 0; virtual T visit(const literal_t * expr) const = 0;
virtual T visit(const unary_t * expr) const = 0;
virtual T visit(const op::alu_t * stmt) const = 0; virtual T visit(const op::alu_t * stmt) const = 0;
virtual T visit(const op::mov_ram_x_t * mov_ram_x) const = 0; virtual T visit(const op::mov_ram_x_t * mov_ram_x) const = 0;