diff --git a/ast.cpp b/ast.cpp index 3002657..6d8cf8b 100644 --- a/ast.cpp +++ b/ast.cpp @@ -2,6 +2,26 @@ namespace dsp { +void ast_printer_t::visit(const unary_t * unary) const +{ + parenthesize((unary->oper).lexeme, unary->expr); +} + +void ast_printer_t::visit(const binary_t * binary) const +{ + parenthesize((binary->oper).lexeme, binary->left, binary->right); +} + +void ast_printer_t::visit(const grouping_t * grouping) const +{ + parenthesize(std::string_view(""), grouping->expr); +} + +void ast_printer_t::visit(const literal_t * literal) const +{ + os << std::to_string(literal->value); +} + void ast_printer_t::parenthesize(const std::string_view s, const expr_t * a) const { os << '(' << s << ' '; @@ -18,14 +38,4 @@ void ast_printer_t::parenthesize(const std::string_view s, const expr_t * a, con os << ')'; } -void ast_printer_t::visit(const literal_t * literal) const -{ - os << std::to_string(literal->value); -} - -void ast_printer_t::visit(const assign_t * expr) const -{ - parenthesize((expr->name).lexeme, &(expr->value)); -} - } diff --git a/ast.hpp b/ast.hpp index 21e2487..b1d3ad2 100644 --- a/ast.hpp +++ b/ast.hpp @@ -16,7 +16,9 @@ struct ast_printer_t : visitor_t std::ostream& os; - void visit(const assign_t * expr) const; + void visit(const unary_t * unary) const; + void visit(const binary_t * binary) const; + void visit(const grouping_t * grouping) const; void visit(const literal_t * literal) const; void parenthesize(const std::string_view s, const expr_t * a) const; diff --git a/expr.hpp b/expr.hpp index 86e38f2..bb179c6 100644 --- a/expr.hpp +++ b/expr.hpp @@ -24,13 +24,22 @@ struct expr_accept_t : expr_t { } }; -struct assign_t : expr_accept_t +struct binary_t : expr_accept_t { - assign_t(token_t& name, expr_t& value) - : name(name), value(value) {} + binary_t(expr_t * left, token_t oper, expr_t * right) + : left(left), oper(oper), right(right) {} - const token_t name; - const expr_t& value; + const expr_t * left; + const token_t oper; + const expr_t * right; +}; + +struct grouping_t : expr_accept_t +{ + grouping_t(expr_t * expr) + : expr(expr) {} + + const expr_t * expr; }; struct literal_t : expr_accept_t @@ -41,4 +50,13 @@ struct literal_t : expr_accept_t const num_t value; }; +struct unary_t : expr_accept_t +{ + unary_t(token_t oper, expr_t * expr) + : oper(oper), expr(expr) {} + + const token_t oper; + const expr_t * expr; +}; + } diff --git a/main.cpp b/main.cpp index 2472531..544e9ac 100644 --- a/main.cpp +++ b/main.cpp @@ -15,9 +15,9 @@ bool had_error = false; static void print() { dsp::literal_t l(56); - std::string_view s("asdf"); - dsp::token_t t({0, 0}, dsp::token_t::identifier, s); - dsp::assign_t a(t, l); + std::string_view s("-"); + dsp::token_t t({0, 0}, dsp::token_t::minus, s); + dsp::unary_t a(t, &l); dsp::ast_printer_t p(std::cout); p.visit(&a); } diff --git a/parser.cpp b/parser.cpp new file mode 100644 index 0000000..41f57dd --- /dev/null +++ b/parser.cpp @@ -0,0 +1,4 @@ +struct parser_t +{ + +} diff --git a/stmt.hpp b/stmt.hpp new file mode 100644 index 0000000..5a23fb4 --- /dev/null +++ b/stmt.hpp @@ -0,0 +1,9 @@ + +struct assign_t : stmt_accept_t +{ + assign_t(token_t name, expr_t * value) + : name(name), value(value) {} + + const token_t name; + const expr_t * value; +}; diff --git a/visitable.hpp b/visitable.hpp index 2da8c02..6ab5880 100644 --- a/visitable.hpp +++ b/visitable.hpp @@ -2,7 +2,10 @@ namespace dsp { -struct assign_t; +struct binary_t; +struct grouping_t; struct literal_t; +struct unary_t; +struct assign_t; } diff --git a/visitor.hpp b/visitor.hpp index 55b2175..6bcf7e1 100644 --- a/visitor.hpp +++ b/visitor.hpp @@ -8,7 +8,9 @@ namespace dsp { template struct visitor_t { - virtual T visit(const assign_t * expr) const = 0; + 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 literal_t * expr) const = 0; };