Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Interpreter pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Interpreter pattern
(section)
Add topic