amath  1.8.5
Simple command line calculator
Language Class Referenceabstract

#include <language.h>

Inheritance diagram for Language:
Collaboration diagram for Language:

Public Member Functions

 Language ()
 
virtual ~Language ()
 
char * GetText (int id)
 
char * GetHelpText (char *ident)
 
char * GetHelpText (Symbol symbol)
 
Symbol FindKeyword (const char *ident) const
 
virtual char GetFractionPoint ()=0
 
virtual bool CharIsAlNum (unsigned long character)=0
 
virtual bool CharIsAlpha (unsigned long character)=0
 
virtual bool CharIsDigit (unsigned long character)=0
 
virtual bool CharIsPunct (unsigned long character)=0
 
virtual bool CharIsSpace (unsigned long character)=0
 
virtual bool CharIsCntrl (unsigned long character)=0
 
virtual bool CharIsQuote (unsigned long character)
 
virtual bool CharIsBlank (unsigned long character)
 
virtual bool CharIsNewLine (unsigned long character)
 
virtual bool CharIsOperator (unsigned long character)
 
virtual bool StrIsEqualLoc (const char *s1, const char *s2)=0
 
virtual bool Validate (char c)=0
 
void SetAnsiMode (bool value)
 
- Public Member Functions inherited from CharValidator
virtual ~CharValidator ()
 

Protected Member Functions

virtual char * Translate (textdef *def)=0
 
virtual char * Translate (helptextdef *def)=0
 
virtual char * Translate (identhelpdef *def)=0
 

Protected Attributes

keyworddefkeywordsloc
 
unsigned int keywordcount
 
unsigned int textcount
 
unsigned int identcount
 
unsigned int helpcount
 
unsigned int aliascount
 

Private Member Functions

char * FindAlias (const char *ident) const
 
char * UntagText (const char *text)
 

Private Attributes

char * lastText
 
bool ansiMode
 

Detailed Description

Definition at line 40 of file language.h.

Constructor & Destructor Documentation

◆ Language()

Language::Language ( )

Definition at line 76 of file language.cpp.

References aliascount, helpcount, helptexts, identaliases, identcount, identtexts, keywordcount, keywords, keywordsloc, lastText, textcount, and textdefs.

Referenced by StandardLanguage::StandardLanguage().

77 {
78  lastText = nullptr;
79  keywordsloc = nullptr;
80  keywordcount = sizeof(keywords) / sizeof(keyworddef);
81  textcount = sizeof(textdefs) / sizeof(textdef);
82  identcount = sizeof(identtexts) / sizeof(identhelpdef);
83  helpcount = sizeof(helptexts) / sizeof(helptextdef);
84  aliascount = sizeof(identaliases) / sizeof(identalias);
85 }
Character representation of keyword tied with its symbol.
Definition: kword.h:48
unsigned int helpcount
Definition: language.h:73
Definition: amatht.h:33
static const keyworddef keywords[]
Definition: kword.h:55
unsigned int identcount
Definition: language.h:72
unsigned int textcount
Definition: language.h:71
char * lastText
Definition: language.h:79
static const identhelpdef identtexts[]
Definition: ident.h:45
keyworddef * keywordsloc
Definition: language.h:69
unsigned int aliascount
Definition: language.h:74
static const identalias identaliases[]
Definition: functionalias.h:35
unsigned int keywordcount
Definition: language.h:70
static const helptextdef helptexts[]
Definition: help.h:55
static const textdef textdefs[]
Definition: text.h:104
Here is the caller graph for this function:

◆ ~Language()

Language::~Language ( )
virtual

Definition at line 87 of file language.cpp.

References keywordsloc, and lastText.

88 {
89  if (lastText != nullptr)
90  {
91  delete lastText;
92  }
93 
94  if (keywordsloc != nullptr)
95  {
96  delete [] keywordsloc;
97  }
98 }
char * lastText
Definition: language.h:79
keyworddef * keywordsloc
Definition: language.h:69

Member Function Documentation

◆ CharIsAlNum()

virtual bool Language::CharIsAlNum ( unsigned long  character)
pure virtual

Implemented in StandardLanguage.

◆ CharIsAlpha()

virtual bool Language::CharIsAlpha ( unsigned long  character)
pure virtual

Implemented in StandardLanguage.

Referenced by Lexer::GetLiteral().

Here is the caller graph for this function:

◆ CharIsBlank()

bool Language::CharIsBlank ( unsigned long  character)
virtual

Definition at line 232 of file language.cpp.

233 {
234 #if !defined(APPLE)
235  if (character == '\r')
236  {
237  return true;
238  }
239 #endif
240 
241  return (character == ' ' || character == '\t');
242 }

◆ CharIsCntrl()

virtual bool Language::CharIsCntrl ( unsigned long  character)
pure virtual

Implemented in StandardLanguage.

Referenced by Lexer::GetQuotedIdent(), and Lexer::ShouldSkip().

Here is the caller graph for this function:

◆ CharIsDigit()

virtual bool Language::CharIsDigit ( unsigned long  character)
pure virtual

Implemented in StandardLanguage.

Referenced by Lexer::GetLiteral().

Here is the caller graph for this function:

◆ CharIsNewLine()

bool Language::CharIsNewLine ( unsigned long  character)
virtual

Definition at line 244 of file language.cpp.

245 {
246 #if defined(APPLE)
247  return (character == '\r');
248 #else
249  return (character == '\n');
250 #endif
251 }

◆ CharIsOperator()

bool Language::CharIsOperator ( unsigned long  character)
virtual

Definition at line 253 of file language.cpp.

References operatordef::chr, and operators.

254 {
255  static const unsigned int count = sizeof(operators) / sizeof(operatordef);
256  for (unsigned int i = 0; i < count; i++)
257  {
258  if (operators[i].chr == (char)character)
259  {
260  return true;
261  }
262  }
263 
264  return false;
265 }
Character definition of operators.
Definition: operatordefs.h:45
static const operatordef operators[]
Character representation of operators tied with their symbols.
Definition: operatordefs.h:54

◆ CharIsPunct()

virtual bool Language::CharIsPunct ( unsigned long  character)
pure virtual

Implemented in StandardLanguage.

◆ CharIsQuote()

bool Language::CharIsQuote ( unsigned long  character)
virtual

Definition at line 227 of file language.cpp.

228 {
229  return (character == '"');
230 }

◆ CharIsSpace()

virtual bool Language::CharIsSpace ( unsigned long  character)
pure virtual

Implemented in StandardLanguage.

Referenced by Lexer::GetNextToken(), and Lexer::ShouldSkip().

Here is the caller graph for this function:

◆ FindAlias()

char * Language::FindAlias ( const char *  ident) const
private

Definition at line 105 of file language.cpp.

References identalias::alias, aliascount, identalias::ident, identaliases, and StrIsEqual().

Referenced by GetHelpText().

106 {
107  for (unsigned int i = 0; i < aliascount; i++)
108  {
109  if (StrIsEqual(identaliases[i].ident, ident))
110  {
111  return const_cast<char*>(identaliases[i].alias);
112  }
113  }
114 
115  return const_cast<char*>(ident);
116 }
unsigned int aliascount
Definition: language.h:74
static const identalias identaliases[]
Definition: functionalias.h:35
const char * alias
Definition: amatht.h:42
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FindKeyword()

Symbol Language::FindKeyword ( const char *  ident) const

Definition at line 118 of file language.cpp.

References keywordcount, keywords, keywordsloc, keyworddef::name, StrIsEqualLoc(), and keyworddef::symbol.

Referenced by Lexer::FindKeyword().

119 {
120  for (unsigned int i = 0; i < keywordcount; i++)
121  {
122  if (Program->Language->StrIsEqualLoc(keywords[i].name, ident) ||
123  (keywordsloc != nullptr && Program->Language->StrIsEqualLoc(keywordsloc[i].name, ident)))
124  {
125  return keywords[i].symbol;
126  }
127  }
128 
129  return static_cast<Symbol>(0);
130 }
Master control class.
Definition: program.h:55
const char * name
Definition: kword.h:52
virtual bool StrIsEqualLoc(const char *s1, const char *s2)=0
static const keyworddef keywords[]
Definition: kword.h:55
Symbol symbol
Definition: kword.h:51
class Language * Language
Definition: program.h:71
keyworddef * keywordsloc
Definition: language.h:69
Symbol
Symbols generated by the Lexer.
Definition: symbol.h:41
unsigned int keywordcount
Definition: language.h:70
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFractionPoint()

virtual char Language::GetFractionPoint ( )
pure virtual

Implemented in StandardLanguage.

Referenced by Program::NewPositionalInput(), Program::NewPositionalOutput(), and Program::Program().

Here is the caller graph for this function:

◆ GetHelpText() [1/2]

char * Language::GetHelpText ( char *  ident)

Definition at line 154 of file language.cpp.

References FindAlias(), identhelpdef::ident, identcount, identtexts, StrIsEqual(), Translate(), and UntagText().

Referenced by HelpStatement::Execute().

155 {
156  char* s = FindAlias(ident);
157  identhelpdef* def = nullptr;
158  for (unsigned int i = 0; i < identcount; i++)
159  {
160  if (StrIsEqual(identtexts[i].ident, s))
161  {
162  def = (identhelpdef*)&identtexts[i];
163  break;
164  }
165  }
166 
167  if (def == nullptr)
168  {
169  return (char*)(HELPNOHELP);
170  }
171 
172  char* text = Translate(def);
173  char* untagged = UntagText(text);
174  return untagged;
175 }
char * UntagText(const char *text)
Definition: language.cpp:199
char * FindAlias(const char *ident) const
Definition: language.cpp:105
unsigned int identcount
Definition: language.h:72
static const identhelpdef identtexts[]
Definition: ident.h:45
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
virtual char * Translate(textdef *def)=0
#define HELPNOHELP
Definition: text.h:77
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetHelpText() [2/2]

char * Language::GetHelpText ( Symbol  symbol)

Definition at line 177 of file language.cpp.

References helpcount, helptexts, helptextdef::symbol, Translate(), and UntagText().

Referenced by HelpStatement::Execute().

178 {
179  helptextdef* def = nullptr;
180  for (unsigned int i = 0; i < helpcount; i++)
181  {
182  if (helptexts[i].symbol == symbol)
183  {
184  def = (helptextdef*)&helptexts[i];
185  break;
186  }
187  }
188 
189  if (def == nullptr)
190  {
191  return (char*)(HELPNOHELP);
192  }
193 
194  char* text = Translate(def);
195  char* untagged = UntagText(text);
196  return untagged;
197 }
char * UntagText(const char *text)
Definition: language.cpp:199
unsigned int helpcount
Definition: language.h:73
virtual char * Translate(textdef *def)=0
static const helptextdef helptexts[]
Definition: help.h:55
#define HELPNOHELP
Definition: text.h:77
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetText()

char * Language::GetText ( int  id)

Definition at line 132 of file language.cpp.

References textdef::id, textcount, textdefs, Translate(), and UntagText().

133 {
134  textdef* def = nullptr;
135  for (unsigned int i = 0; i < textcount; i++)
136  {
137  if (textdefs[i].id == id)
138  {
139  def = (textdef*)&textdefs[i];
140  break;
141  }
142  }
143 
144  if (def == nullptr)
145  {
146  return (char*)(HELPNOHELP);
147  }
148 
149  char* text = Translate(def);
150  char* untagged = UntagText(text);
151  return untagged;
152 }
char * UntagText(const char *text)
Definition: language.cpp:199
Definition: amatht.h:33
unsigned int textcount
Definition: language.h:71
virtual char * Translate(textdef *def)=0
#define HELPNOHELP
Definition: text.h:77
static const textdef textdefs[]
Definition: text.h:104
Here is the call graph for this function:

◆ SetAnsiMode()

void Language::SetAnsiMode ( bool  value)

Definition at line 100 of file language.cpp.

References ansiMode.

Referenced by Program::SetAnsiMode().

101 {
102  ansiMode = value;
103 }
bool ansiMode
Definition: language.h:80
Here is the caller graph for this function:

◆ StrIsEqualLoc()

virtual bool Language::StrIsEqualLoc ( const char *  s1,
const char *  s2 
)
pure virtual

Implemented in StandardLanguage.

Referenced by FindKeyword().

Here is the caller graph for this function:

◆ Translate() [1/3]

virtual char* Language::Translate ( textdef def)
protectedpure virtual

Implemented in StandardLanguage.

Referenced by GetText().

Here is the caller graph for this function:

◆ Translate() [2/3]

virtual char* Language::Translate ( helptextdef def)
protectedpure virtual

Implemented in StandardLanguage.

Referenced by GetHelpText().

Here is the caller graph for this function:

◆ Translate() [3/3]

virtual char* Language::Translate ( identhelpdef def)
protectedpure virtual

Implemented in StandardLanguage.

Referenced by GetHelpText().

Here is the caller graph for this function:

◆ UntagText()

char * Language::UntagText ( const char *  text)
private

Definition at line 199 of file language.cpp.

References ansiMode, ansiTags, emptyTags, lastText, MemCopy(), StrLen(), and Untag().

Referenced by GetHelpText(), and GetText().

200 {
201  if (lastText != nullptr)
202  {
203  delete lastText;
204  lastText = nullptr;
205  }
206 
207  if (text == nullptr)
208  {
209  return nullptr;
210  }
211 
212  unsigned int count = sizeof(ansiTags) / sizeof(texttag);
213  char* untagged = new char[StrLen(text) * 2];
214  texttag* tags = ansiMode
215  ? (texttag*)ansiTags
216  : (texttag*)emptyTags;
217  Untag(untagged, text, tags, count);
218 
219  unsigned int len = StrLen(untagged) + 1;
220  lastText = new char[len];
221  MemCopy(lastText, untagged, len);
222  delete [] untagged;
223 
224  return lastText;
225 }
static const texttag ansiTags[]
Definition: language.cpp:42
void Untag(char *destination, const char *source, texttag tags[], unsigned int tagcount)
Definition: untag.c:32
Definition: amathc.h:45
static const texttag emptyTags[]
Definition: language.cpp:61
char * lastText
Definition: language.h:79
void MemCopy(void *destination, const void *source, unsigned int length)
Copy a block of memory, handling overlap.
Definition: memcpy.c:75
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
bool ansiMode
Definition: language.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Validate()

virtual bool Language::Validate ( char  c)
pure virtual

Implements CharValidator.

Implemented in StandardLanguage.

Member Data Documentation

◆ aliascount

unsigned int Language::aliascount
protected

Definition at line 74 of file language.h.

Referenced by FindAlias(), and Language().

◆ ansiMode

bool Language::ansiMode
private

Definition at line 80 of file language.h.

Referenced by SetAnsiMode(), and UntagText().

◆ helpcount

unsigned int Language::helpcount
protected

Definition at line 73 of file language.h.

Referenced by GetHelpText(), and Language().

◆ identcount

unsigned int Language::identcount
protected

Definition at line 72 of file language.h.

Referenced by GetHelpText(), and Language().

◆ keywordcount

unsigned int Language::keywordcount
protected

Definition at line 70 of file language.h.

Referenced by FindKeyword(), and Language().

◆ keywordsloc

keyworddef* Language::keywordsloc
protected

Definition at line 69 of file language.h.

Referenced by FindKeyword(), Language(), and ~Language().

◆ lastText

char* Language::lastText
private

Definition at line 79 of file language.h.

Referenced by Language(), UntagText(), and ~Language().

◆ textcount

unsigned int Language::textcount
protected

Definition at line 71 of file language.h.

Referenced by GetText(), and Language().


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