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

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

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include "helpers/titlecase.h"
#include "Interpreter_Interpreter.h"
Include dependency graph for Interpreter_Interpreter.c:

Go to the source code of this file.

Functions

static const char * _InterpretToken (int token)
 Helper function to convert the token into its corresponding word or punctuation mark.
 
bool Interpreter_Interpret (const int *tokenList, DynamicString *output)
 This function is a simple interpreter.
 

Variables

static const char * _commonwords []
 The 40 most common words in English (in order but that doesn't really matter here). A token is nothing more than an index into this list.
 

Detailed Description

Implementation of the Interpreter_Interpret() function used in the Interpreter Pattern.

Definition in file Interpreter_Interpreter.c.

Function Documentation

◆ _InterpretToken()

static const char * _InterpretToken ( int  token)
static

Helper function to convert the token into its corresponding word or punctuation mark.

Parameters
tokenThe token to interpret.
Returns
Returns a pointer to the corresponding word or punctuation. If the token is not recognized, the pointer returns "<UNKNOWN TOKEN #>", where # is the token value.

Definition at line 72 of file Interpreter_Interpreter.c.

References _commonwords, PERIOD, and QUESTION.

Referenced by Interpreter_Interpret().

◆ Interpreter_Interpret()

bool Interpreter_Interpret ( const int *  tokenList,
DynamicString output 
)

This function is a simple interpreter.

The interpreter takes an array of integer tokens and converts each token into a word or punctuation mark. The interpreter then arranges the words into a space-separated list in a single string. In other words, the tokens are converted into a sentence, with the first word capitalized and no space between the last two "words" under the assumption the last word is actually a punctuation mark.

Interpreter Rules:

  1. Each token must be in the range of 0 through 39 (maximum number of words known by the interpreter) or must be 100 ('.') or 101 ('?').
  2. The word corresponding to the first token is always capitalized.
  3. A single space appears between each word.
  4. No space appears between the last two tokens.
Parameters
tokenListList of integer tokens to be interpreted. The list is assumed to be terminated by -1 (EOL).
outputA DynamicString object returning the sentence created through interpreting the tokens. Call DynamicString_Clear() when done with the string.
Returns
Returns true if the token list was interpreted successfully; otherwise, returns false, indicating an out of memory condition (or a NULL argument.

Definition at line 117 of file Interpreter_Interpreter.c.

References _InterpretToken(), DynamicString_Append(), EOL, and titlecase().

Referenced by Interpreter_Exercise().

Variable Documentation

◆ _commonwords

const char* _commonwords[]
static

The 40 most common words in English (in order but that doesn't really matter here). A token is nothing more than an index into this list.

Definition at line 20 of file Interpreter_Interpreter.c.

Referenced by _InterpretToken().