Skip to content

Instantly share code, notes, and snippets.

@jrziviani
Created October 30, 2017 10:45
Show Gist options
  • Save jrziviani/2cc1aa7bbed5b74c05a90fe0495e1a83 to your computer and use it in GitHub Desktop.
Save jrziviani/2cc1aa7bbed5b74c05a90fe0495e1a83 to your computer and use it in GitHub Desktop.
expression client
class expression_t
{
public:
virtual int accept(expression_visitor &v) = 0;
virtual ~expression_t() {}
};
using expression = unique_ptr<expression_t>;
class type_number : public expression_t
{
int value_;
public:
type_number(int v) :
value_(v)
{
}
type_number(string v) :
value_(stoi(v))
{
}
int get() const
{
return value_;
}
int accept(expression_visitor &v)
{
return v.visit_number(*this);
}
};
class binary_expression : public expression_t
{
expression left_;
expression right_;
char oper_;
public:
binary_expression(expression l,
expression r,
char o) :
left_(move(l)),
right_(move(r)),
oper_(o)
{
}
expression_t *left()
{
return left_.get();
}
expression_t *right()
{
return right_.get();
}
char operation() const
{
return oper_;
}
int accept(expression_visitor &v)
{
return v.visit_binary(*this);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment