expr: add more expression structures

This commit is contained in:
Zack Buhman 2023-08-15 23:01:35 +00:00
parent 97d35bfa4f
commit e2f587437d
8 changed files with 69 additions and 21 deletions

30
ast.cpp
View File

@ -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>"), 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));
}
}

View File

@ -16,7 +16,9 @@ struct ast_printer_t : visitor_t<void>
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;

View File

@ -24,13 +24,22 @@ struct expr_accept_t : expr_t {
}
};
struct assign_t : expr_accept_t<assign_t>
struct binary_t : expr_accept_t<binary_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>
{
grouping_t(expr_t * expr)
: expr(expr) {}
const expr_t * expr;
};
struct literal_t : expr_accept_t<literal_t>
@ -41,4 +50,13 @@ struct literal_t : expr_accept_t<literal_t>
const num_t value;
};
struct unary_t : expr_accept_t<unary_t>
{
unary_t(token_t oper, expr_t * expr)
: oper(oper), expr(expr) {}
const token_t oper;
const expr_t * expr;
};
}

View File

@ -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);
}

4
parser.cpp Normal file
View File

@ -0,0 +1,4 @@
struct parser_t
{
}

9
stmt.hpp Normal file
View File

@ -0,0 +1,9 @@
struct assign_t : stmt_accept_t<assign_t>
{
assign_t(token_t name, expr_t * value)
: name(name), value(value) {}
const token_t name;
const expr_t * value;
};

View File

@ -2,7 +2,10 @@
namespace dsp {
struct assign_t;
struct binary_t;
struct grouping_t;
struct literal_t;
struct unary_t;
struct assign_t;
}

View File

@ -8,7 +8,9 @@ namespace dsp {
template <typename T>
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;
};