Design Pattern Examples
Overview of object-oriented design patterns
Interpreter_Exercise.c File Reference

Implementation of the Interpreter_Exercise() function as used in the Interpreter Pattern. More...

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Interpreter_Interpreter.h"
#include "Interpreter_Exercise.h"
Include dependency graph for Interpreter_Exercise.c:

Go to the source code of this file.

Functions

static const char * _TokensToString (int *tokens)
 Helper function to convert a list of ints to a string representation.
 
void Interpreter_Exercise (void)
 Example of using the Interpreter Pattern.
 

Variables

static int sentenceTokens0 [] = { 39, 18, 17, 27, 2, 7, QUESTION, EOL }
 Represents the sentence: "What do you say to that?".
 
static int sentenceTokens1 [] = { 32, 17, 1, 0, 34, 2, 1, 37, QUESTION, EOL }
 Represents the sentence: "Will you be the one to be there?".
 
static int sentenceTokens2 [] = { 36, 17, 8, 5, 32, 2, 18, 7, QUESTION, EOL }
 Represents the sentence: "Would you have a will to do that?".
 
static int sentenceTokens3 [] = { 11, 12, 17, 9, 36, 12, 1, 6, 20, PERIOD, EOL }
 Represents the sentence: "For not you I would not be in this.".
 
static int sentenceTokens4 [] = { 26, 27, 7, 21, 36, 17, 27, 10, QUESTION, EOL }
 Represents the sentence: "We say that but would you say it?".
 
static int sentenceTokens5 [] = { 23, 28, 32, 26, 32, 18, 10, PERIOD, EOL }
 Represents the sentence: "By her will we will do it.".
 
static int * _sentenceTokenLists []
 A list of pre-defined token lists. Each token list represents a single sentence constructed from the 40 most common words in the English language. I don't use all 40 words, though; that would be silly.
 

Detailed Description

Implementation of the Interpreter_Exercise() function as used in the Interpreter Pattern.

Definition in file Interpreter_Exercise.c.

Function Documentation

◆ _TokensToString()

static const char * _TokensToString ( int *  tokens)
static

Helper function to convert a list of ints to a string representation.

Parameters
tokensArray of ints to work with. Assumed to be terminated with a -1 (EOL).
Returns
A string representation of the integer list. This is a pointer to a static buffer and is valid only until the next call to this function.

Definition at line 73 of file Interpreter_Exercise.c.

References EOL.

Referenced by DesignPatternExamples_cpp::Interpreter_Exercise(), and Interpreter_Exercise().

◆ Interpreter_Exercise()

void Interpreter_Exercise ( void  )

Example of using the Interpreter Pattern.

The interpreter is instantiated then fed a series of arrays containing integer tokens. Each token represents a single word or punctuation mark. The interpreter converts that array of tokens to an actual sentence by interpreting the meaning of the tokens.

This is a very simple interpreter that handles the first token in a special way and supports punctuation. It is an example of a linear interpreter where tokens can appear in any order (it's up to the creator of the token list to make sure the outcome makes any sense).

The output shows the token list followed by the sentence produced from the tokens.

Definition at line 124 of file Interpreter_Exercise.c.

References _sentenceTokenLists, _TokensToString(), DynamicString_Clear(), Interpreter_Interpret(), and DynamicString::string.

Variable Documentation

◆ _sentenceTokenLists

int* _sentenceTokenLists[]
static
Initial value:
= {
NULL
}
static int sentenceTokens2[]
Represents the sentence: "Would you have a will to do that?".
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 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?".

A list of pre-defined token lists. Each token list represents a single sentence constructed from the 40 most common words in the English language. I don't use all 40 words, though; that would be silly.

Definition at line 55 of file Interpreter_Exercise.c.

Referenced by DesignPatternExamples_cpp::Interpreter_Exercise(), and Interpreter_Exercise().

◆ sentenceTokens0

int sentenceTokens0[] = { 39, 18, 17, 27, 2, 7, QUESTION, EOL }
static

Represents the sentence: "What do you say to that?".

Definition at line 22 of file Interpreter_Exercise.c.

◆ sentenceTokens1

int sentenceTokens1[] = { 32, 17, 1, 0, 34, 2, 1, 37, QUESTION, EOL }
static

Represents the sentence: "Will you be the one to be there?".

Definition at line 27 of file Interpreter_Exercise.c.

◆ sentenceTokens2

int sentenceTokens2[] = { 36, 17, 8, 5, 32, 2, 18, 7, QUESTION, EOL }
static

Represents the sentence: "Would you have a will to do that?".

Definition at line 32 of file Interpreter_Exercise.c.

◆ sentenceTokens3

int sentenceTokens3[] = { 11, 12, 17, 9, 36, 12, 1, 6, 20, PERIOD, EOL }
static

Represents the sentence: "For not you I would not be in this.".

Definition at line 37 of file Interpreter_Exercise.c.

◆ sentenceTokens4

int sentenceTokens4[] = { 26, 27, 7, 21, 36, 17, 27, 10, QUESTION, EOL }
static

Represents the sentence: "We say that but would you say it?".

Definition at line 42 of file Interpreter_Exercise.c.

◆ sentenceTokens5

int sentenceTokens5[] = { 23, 28, 32, 26, 32, 18, 10, PERIOD, EOL }
static

Represents the sentence: "By her will we will do it.".

Definition at line 47 of file Interpreter_Exercise.c.