amath  1.8.5
Simple command line calculator
Evaluator Class Reference

#include <evaluator.h>

Collaboration diagram for Evaluator:

Public Member Functions

 Evaluator (const char *input)
 
 ~Evaluator ()
 
void Evaluate () const
 
char * GetResult () const
 

Private Attributes

const char * input
 
CharBufferoutput
 

Detailed Description

Definition at line 35 of file evaluator.h.

Constructor & Destructor Documentation

◆ Evaluator()

Evaluator::Evaluator ( const char *  input)
explicit

Definition at line 36 of file evaluator.cpp.

References CharBuffer::CharBuffer(), input, and output.

Referenced by StandardProgram::Start().

37 {
38  this->input = input;
39  output = new CharBuffer();
40 }
CharBuffer * output
Definition: evaluator.h:45
const char * input
Definition: evaluator.h:44
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:44
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~Evaluator()

Evaluator::~Evaluator ( )

Definition at line 42 of file evaluator.cpp.

References output.

43 {
44  if (output != nullptr)
45  {
46  delete output;
47  }
48 }
CharBuffer * output
Definition: evaluator.h:45

Member Function Documentation

◆ Evaluate()

void Evaluator::Evaluate ( ) const

Definition at line 50 of file evaluator.cpp.

References CharBuffer::ClearAndCopy(), CharBuffer::ClearBuffer(), SyntaxNode::Execute(), Optimizer::GetRoot(), input, Optimizer::Optimize(), Optimizer::Optimizer(), output, Parser::Parse(), and Parser::Parser().

Referenced by StandardProgram::Start().

51 {
53 
54  Parser* parser = new Parser(input);
55  SyntaxNode* node = parser->Parse();
56  delete parser;
57 
58 #if defined(DEBUGTREE)
59  Viewer* viewer = new Viewer(node);
60  const char* vout = viewer->GetTree();
61  output->EnsureGrowth(StrLen(vout));
62  output->Append(vout);
63  delete viewer;
64 #endif
65 
66  Optimizer* optimizer = new Optimizer(node);
67  optimizer->Optimize();
68  node = optimizer->GetRoot();
69  delete optimizer;
70 
71 #if defined(DEBUGTREE)
72  viewer = new Viewer(node);
73  vout = viewer->GetTree();
74  output->EnsureGrowth(StrLen(vout));
75  output->Append(vout);
76  delete viewer;
77 
78  const char* out = node->Execute();
79  output->EnsureGrowth(StrLen(out));
80  output->Append(out);
81 #endif
82 
83 #ifndef DEBUGTREE
84  const char* out = node->Execute();
85  output->ClearAndCopy(out);
86 #endif
87 
88  delete node;
89 }
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
void ClearBuffer()
Release memory in buffer.
Definition: charbuf.cpp:65
void Append(const char *source)
Definition: charbuf.cpp:262
void Optimize() const
Definition: optimizer.cpp:49
virtual char * Execute()=0
CharBuffer * output
Definition: evaluator.h:45
SyntaxNode * GetRoot() const
Definition: optimizer.cpp:44
void EnsureGrowth(unsigned int size)
Definition: charbuf.cpp:169
const char * input
Definition: evaluator.h:44
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
void ClearAndCopy(const char *source)
Release memory, allocate and copy source.
Definition: charbuf.cpp:81
SyntaxNode * Parse()
Parses the input into a syntax tree.
Definition: parser.cpp:56
Encapsulates a recursive descent parser.
Definition: parser.h:49
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetResult()

char * Evaluator::GetResult ( ) const

Definition at line 91 of file evaluator.cpp.

References CharBuffer::GetString(), and output.

Referenced by StandardProgram::Start().

92 {
93  return output->GetString();
94 }
char * GetString() const
Definition: charbuf.cpp:306
CharBuffer * output
Definition: evaluator.h:45
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ input

const char* Evaluator::input
private

Definition at line 44 of file evaluator.h.

Referenced by Evaluate(), and Evaluator().

◆ output

CharBuffer* Evaluator::output
private

Definition at line 45 of file evaluator.h.

Referenced by Evaluate(), Evaluator(), GetResult(), and ~Evaluator().


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