Design Pattern Examples
Overview of object-oriented design patterns
Interpreter_Class Class Reference

Representation of a simple interpreter. More...

Collaboration diagram for Interpreter_Class:
Collaboration graph

Public Member Functions

string Interpret (int[] tokens)
 Given an array of integer tokens, convert the tokens into a single string of space-delimited words, following simple rules of formatting.
 

Private Member Functions

string _InterpretToken (int token)
 Helper method to convert the token into its corresponding word or punctuation mark.
 

Static Private Attributes

const int PERIOD = 100
 
const int QUESTION = 101
 
static string[] _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

Representation of a simple interpreter.

This 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.

Definition at line 31 of file Interpreter_Class.cs.

Member Function Documentation

◆ _InterpretToken()

string _InterpretToken ( int  token)
inlineprivate

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

Parameters
tokenThe token to interpret.
Returns

A string containing the corresponding word or punctuation. If the token is not recognized, the string returns "<UNKNOWN TOKEN #>", where

Definition at line 93 of file Interpreter_Class.cs.

References Interpreter_Class._commonwords, Interpreter_Class.PERIOD, and Interpreter_Class.QUESTION.

Referenced by Interpreter_Class.Interpret().

◆ Interpret()

string Interpret ( int[]  tokens)
inline

Given an array of integer tokens, convert the tokens into a single string of space-delimited words, following simple rules of formatting.

Parameters
tokensThe list of integers to interpret.
Returns
A string containing a sentence built from the tokens.

Definition at line 131 of file Interpreter_Class.cs.

References Interpreter_Class._InterpretToken().

Referenced by Interpreter_Exercise.Run().

Member Data Documentation

◆ _commonwords

string [] _commonwords
staticprivate

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 40 of file Interpreter_Class.cs.

Referenced by Interpreter_Class._InterpretToken().

◆ PERIOD

const int PERIOD = 100
staticprivate

Definition at line 33 of file Interpreter_Class.cs.

Referenced by Interpreter_Class._InterpretToken().

◆ QUESTION

const int QUESTION = 101
staticprivate

Definition at line 34 of file Interpreter_Class.cs.

Referenced by Interpreter_Class._InterpretToken().


The documentation for this class was generated from the following file: