Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Interpreter pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Example == <!-- Wikipedia is not a list of examples. Do not add examples from favorite programming languages here; this page exists to explain the design pattern, not to show how it interacts with subtleties of every language extant. Feel free to add examples at: http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Interpreter --> This C++11 implementation is based on the pre C++98 sample code in the book. <syntaxhighlight lang="c++"> #include <iostream> #include <map> #include <cstring> class Context; class BooleanExp { public: BooleanExp() = default; virtual ~BooleanExp() = default; virtual bool evaluate(Context&) = 0; virtual BooleanExp* replace(const char*, BooleanExp&) = 0; virtual BooleanExp* copy() const = 0; }; class VariableExp; class Context { public: Context() :m() {} bool lookup(const VariableExp* key) { return m.at(key); } void assign(VariableExp* key, bool value) { m[key] = value; } private: std::map<const VariableExp*, bool> m; }; class VariableExp : public BooleanExp { public: VariableExp(const char* name_) :name(nullptr) { name = strdup(name_); } virtual ~VariableExp() = default; virtual bool evaluate(Context& aContext) { return aContext.lookup(this); } virtual BooleanExp* replace( const char* name_, BooleanExp& exp ) { if (0 == strcmp(name_, name)) { return exp.copy(); } else { return new VariableExp(name); } } virtual BooleanExp* copy() const { return new VariableExp(name); } VariableExp(const VariableExp&) = delete; // rule of three VariableExp& operator=(const VariableExp&) = delete; private: char* name; }; class AndExp : public BooleanExp { public: AndExp(BooleanExp* op1, BooleanExp* op2) :operand1(nullptr), operand2(nullptr) { operand1 = op1; operand2 = op2; } virtual ~AndExp() = default; virtual bool evaluate(Context& aContext) { return operand1->evaluate(aContext) && operand2->evaluate(aContext); } virtual BooleanExp* replace(const char* name_, BooleanExp& exp) { return new AndExp( operand1->replace(name_, exp), operand2->replace(name_, exp) ); } virtual BooleanExp* copy() const { return new AndExp(operand1->copy(), operand2->copy()); } AndExp(const AndExp&) = delete; // rule of three AndExp& operator=(const AndExp&) = delete; private: BooleanExp* operand1; BooleanExp* operand2; }; int main() { BooleanExp* expression; Context context; VariableExp* x = new VariableExp("X"); VariableExp* y = new VariableExp("Y"); expression = new AndExp(x, y); context.assign(x, false); context.assign(y, true); bool result = expression->evaluate(context); std::cout << result << '\n'; context.assign(x, true); context.assign(y, true); result = expression->evaluate(context); std::cout << result << '\n'; } </syntaxhighlight> The program output is: <syntaxhighlight lang="c++"> 0 1 </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)