diff --git a/ast.cpp b/ast.cpp index fb6a288..331687b 100644 --- a/ast.cpp +++ b/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 << ' '; diff --git a/ast.hpp b/ast.hpp index be783c0..62faf2f 100644 --- a/ast.hpp +++ b/ast.hpp @@ -17,10 +17,11 @@ struct ast_printer_t : visitor_t 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; diff --git a/expr.hpp b/expr.hpp index 0d8318d..db2becc 100644 --- a/expr.hpp +++ b/expr.hpp @@ -44,6 +44,14 @@ struct grouping_t : expr_accept_t const expr_t * expr; }; +struct identifier_t : expr_accept_t +{ + identifier_t(token_t name) + : name(name) {} + + const token_t name; +}; + struct literal_t : expr_accept_t { literal_t(num_t value) diff --git a/parser.cpp b/parser.cpp index 6121e32..f55cd17 100644 --- a/parser.cpp +++ b/parser.cpp @@ -150,7 +150,8 @@ expr_t * parser_t::orl() expr_t * parser_t::primary() { - if (match(number)) return new literal_t(std::get(previous().literal)); + if (match(number)) return new literal_t(std::get(previous().literal)); + else if (match(identifier)) return new identifier_t(previous()); if (match(left_paren)) { expr_t * expr = expression(); diff --git a/visitable.hpp b/visitable.hpp index 604628d..95aac25 100644 --- a/visitable.hpp +++ b/visitable.hpp @@ -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 { diff --git a/visitor.hpp b/visitor.hpp index ccf232c..199a3ee 100644 --- a/visitor.hpp +++ b/visitor.hpp @@ -8,10 +8,11 @@ namespace dsp { template 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;