Design Pattern Examples
Overview of object-oriented design patterns
Interpreter_Exercise.c
Go to the documentation of this file.
1
6
7#include <errno.h>
8#include <stdbool.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
15
16//=============================================================================
17//=============================================================================
18
22static int sentenceTokens0[] = { 39, 18, 17, 27, 2, 7, QUESTION, EOL };
23
27static int sentenceTokens1[] = { 32, 17, 1, 0, 34, 2, 1, 37, QUESTION, EOL };
28
32static int sentenceTokens2[] = { 36, 17, 8, 5, 32, 2, 18, 7, QUESTION, EOL };
33
37static int sentenceTokens3[] = { 11, 12, 17, 9, 36, 12, 1, 6, 20, PERIOD, EOL };
38
42static int sentenceTokens4[] = { 26, 27, 7, 21, 36, 17, 27, 10, QUESTION, EOL };
43
47static int sentenceTokens5[] = { 23, 28, 32, 26, 32, 18, 10, PERIOD, EOL };
48
55static int* _sentenceTokenLists[] = {
62 NULL
63};
64
65
73static const char* _TokensToString(int* tokens)
74{
75 static char buffer[256] = { 0 };
76
77 memset(buffer, 0, sizeof(buffer));
78
79 buffer[0] = '[';
80 for (size_t index = 0; tokens[index] != EOL; ++index)
81 {
82 char numBuffer[6] = { 0 };
83 int num_chars = snprintf(numBuffer, sizeof(numBuffer), "%3d", tokens[index]);
84 if (num_chars <= 0)
85 {
86 int errorCode = errno;
87 printf(" Error (%d)! snprintf() failed in _TokensToString(): %s!\n", errorCode,
88 strerror(errorCode));
89 break;
90 }
91 strcat(buffer, numBuffer);
92 if (tokens[index + 1] != EOL)
93 {
94 strcat(buffer, ", ");
95 }
96 }
97 strcat(buffer, "]");
98
99 return buffer;
100}
101
102
103//=============================================================================
104//=============================================================================
105
123// ! [Using Interpreter in C]
125{
126 printf("\nInterpreter Exercise\n");
127
128 for (size_t sentenceIndex = 0; _sentenceTokenLists[sentenceIndex] != NULL; ++sentenceIndex)
129 {
130 int* tokenList = _sentenceTokenLists[sentenceIndex];
131
132 const char* tokensAsString = _TokensToString(tokenList);
133
134 DynamicString sentence = { 0 };
135 bool success = Interpreter_Interpret(tokenList, &sentence);
136
137 if (success)
138 {
139 // 50 is a magic number corresponding to the longest token list
140 // expressed as a string. Derived empirically. It makes the
141 // output easier to, er, interpret.
142 printf(" %-50s ==> \"%s\"\n", tokensAsString, sentence.string);
143 }
144 DynamicString_Clear(&sentence);
145 if (!success)
146 {
147 break;
148 }
149 }
150
151 printf(" Done.\n");
152 // ! [Using Interpreter in C]
153}
static int sentenceTokens2[]
Represents the sentence: "Would you have a will to do that?".
static int * _sentenceTokenLists[]
A list of pre-defined token lists. Each token list represents a single sentence constructed from the ...
void Interpreter_Exercise(void)
Example of using the Interpreter Pattern.
static int sentenceTokens3[]
Represents the sentence: "For not you I would not be in this.".
static int sentenceTokens5[]
Represents the sentence: "By her will we will do it.".
static int sentenceTokens4[]
Represents the sentence: "We say that but would you say it?".
static const char * _TokensToString(int *tokens)
Helper function to convert a list of ints to a string representation.
static int sentenceTokens0[]
Represents the sentence: "What do you say to that?".
static int sentenceTokens1[]
Represents the sentence: "Will you be the one to be there?".
bool Interpreter_Interpret(const int *tokenList, DynamicString *output)
This function is a simple interpreter.
Declaration of the Interpreter_Interpret() function used in the Interpreter Pattern.
@ EOL
Marker for end of a token list.
@ QUESTION
Question mark.
@ PERIOD
Period.
Declaration of the Interpreter_Exercise() function as used in the Interpreter Pattern.
void DynamicString_Clear(DynamicString *string)
Clear a DynamicString object, releasing any allocated memory. Resets to an empty string.
Definition: dynamicstring.c:27
Represents a string that can be grown dynamically.
Definition: dynamicstring.h:16
char * string
The string that can grow.
Definition: dynamicstring.h:17