parser: add identifiers
This commit is contained in:
parent
cd0c9c7c8c
commit
05664bb132
15
ast.cpp
15
ast.cpp
@ -3,11 +3,6 @@
|
||||
|
||||
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
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
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
|
||||
{
|
||||
os << '(' << s << ' ';
|
||||
|
3
ast.hpp
3
ast.hpp
@ -17,10 +17,11 @@ struct ast_printer_t : visitor_t<void>
|
||||
|
||||
std::ostream& os;
|
||||
|
||||
void visit(const unary_t * unary) const;
|
||||
void visit(const binary_t * binary) 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 unary_t * unary) const;
|
||||
|
||||
void visit(const op::alu_t * alu) const;
|
||||
void visit(const op::mov_ram_x_t * mov_ram_x) const;
|
||||
|
8
expr.hpp
8
expr.hpp
@ -44,6 +44,14 @@ struct grouping_t : expr_accept_t<grouping_t>
|
||||
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>
|
||||
{
|
||||
literal_t(num_t value)
|
||||
|
@ -150,7 +150,8 @@ expr_t * parser_t::orl()
|
||||
|
||||
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)) {
|
||||
expr_t * expr = expression();
|
||||
|
@ -5,9 +5,9 @@ namespace dsp {
|
||||
// expressions
|
||||
struct binary_t;
|
||||
struct grouping_t;
|
||||
struct identifier_t;
|
||||
struct literal_t;
|
||||
struct unary_t;
|
||||
struct assign_t;
|
||||
|
||||
// instructions
|
||||
namespace op {
|
||||
|
@ -8,10 +8,11 @@ namespace dsp {
|
||||
template <typename 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 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 unary_t * expr) 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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user