Design Pattern Examples
Overview of object-oriented design patterns
Interpreter_Exercise.cpp
Go to the documentation of this file.
1
6
7#include <iostream>
8#include <sstream>
9
10#include "helpers/formatstring.h"
11
13#include "Interpreter_Class.h"
14
15namespace // Anonymous
16{
17 using namespace DesignPatternExamples_cpp;
18
24 std::string _TokensToString(const IntList& tokens)
25 {
26 std::ostringstream output;
27
28 output << "[";
29 for (size_t index = 0; index < tokens.size(); ++index)
30 {
31 output << Helpers::formatstring("%3d", tokens[index]);
32 if (index + 1 < tokens.size())
33 {
34 output << ", ";
35 }
36 }
37 output << "]";
38 return output.str();
39 }
40
41
48 const std::vector<IntList> _sentenceTokenLists{
49 { 39, 18, 17, 27, 2, 7, 101 }, // "What do you say to that?"
50 { 32, 17, 1, 0, 34, 2, 1, 37, 101 }, // "Will you be the one to be there?"
51 { 36, 17, 8, 5, 32, 2, 18, 7, 101 }, // "Would you have a will to do that?"
52 { 11, 12, 17, 9, 36, 12, 1, 6, 20, 100 }, // "For not you I would not be in this."
53 { 26, 27, 7, 21, 36, 17, 27, 10, 101 }, // "We say that but would you say it?"
54 { 23, 28, 32, 26, 32, 18, 10, 100 } // "By her will we will do it."
55 };
56
57} // end namespace Anonmyous
58
59
61{
62
80 // ! [Using Interpreter in C++]
82 {
83 std::cout << std::endl;
84 std::cout << "Interpreter Exercise" << std::endl;
85
86 Interpreter_Class interpreter;
87
88 for (size_t sentenceIndex = 0; sentenceIndex < _sentenceTokenLists.size(); ++sentenceIndex)
89 {
90 const IntList& tokenList = _sentenceTokenLists[sentenceIndex];
91
92 std::string tokensAsString = _TokensToString(tokenList);
93
94 std::string sentence = interpreter.Interpret(tokenList);
95
96 // 50 is a magic number corresponding to the longest token list
97 // expressed as a string. Derived empirically. It makes the
98 // output easier to, er, interpret.
99 std::cout
100 << Helpers::formatstring(" %-50s ==> \"%s\"", tokensAsString.c_str(), sentence.c_str())
101 << std::endl;
102 }
103
104 std::cout << " Done." << std::endl;
105 }
106 // ! [Using Interpreter in C++]
107
108} // end namespace
Declaration of the Interpreter_Class class used in the Interpreter Pattern.
static int * _sentenceTokenLists[]
A list of pre-defined token lists. Each token list represents a single sentence constructed from the ...
static const char * _TokensToString(int *tokens)
Helper function to convert a list of ints to a string representation.
Representation of a simple interpreter.
std::string Interpret(IntList tokens)
Given an array of integer tokens, convert the tokens into a single std::string of space-delimited wor...
Declaration of the Interpreter_Exercise() function as used in the Interpreter Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
void Interpreter_Exercise()
Example of using the Interpreter design pattern.
std::vector< int > IntList
Alias to make it easier to work with a vector of vectors of int.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....