amath  1.8.5
Simple command line calculator
Parser Class Reference

Encapsulates a recursive descent parser. More...

#include <parser.h>

Collaboration diagram for Parser:

Public Member Functions

 Parser (const char *input)
 
 ~Parser ()
 
SyntaxNodeParse ()
 Parses the input into a syntax tree. More...
 

Private Member Functions

SyntaxNodeParseDefault ()
 
SyntaxNodeTryParseStatement ()
 
SyntaxNodeParseStatement ()
 
SyntaxNodeParseEvaluation ()
 
ExpressionNodeParseExpression ()
 
ExpressionNodeParseAddSubstract ()
 
ExpressionNodeParseFactor ()
 
ExpressionNodeParsePower ()
 
ExpressionNodeParseUnary ()
 
ExpressionNodeParseAtomic ()
 
ExpressionNodeParseIdent ()
 
ExpressionNodeParseNumber ()
 
SyntaxNodeParseFunctionDef ()
 
SyntaxNodeParseHelpStatement ()
 
SyntaxNodeParseDeleteStatement ()
 
SyntaxNodeParseListStatement ()
 
SyntaxNodeParseFileStatement ()
 
SyntaxNodeParseNumeralStatement ()
 
SyntaxNodeParseDigistStatement ()
 
SyntaxNodeParseDrawStatement ()
 
SyntaxNodeParsePromptStatement ()
 
SyntaxNodeParsePrefsStatement ()
 
void GetToken ()
 
void PutToken ()
 
bool Expect (Symbol symbol)
 
TokenPeek () const
 

Private Attributes

Lexerlexer
 
Tokentoken
 
int syntaxError
 
ErrorNodeerrorNode
 

Detailed Description

Encapsulates a recursive descent parser.

More info on recursive descent parsers is available at Wikipedia: https://wikipedia.org/wiki/Recursive_descent_parser

Definition at line 49 of file parser.h.

Constructor & Destructor Documentation

◆ Parser()

Parser::Parser ( const char *  input)
explicit

Definition at line 43 of file parser.cpp.

References errorNode, Lexer::Lexer(), lexer, syntaxError, and token.

Referenced by Evaluator::Evaluate(), ExecuteStatement::Execute(), LoadStatement::Execute(), and PreferencesBase::SetPrefs().

44 {
45  lexer = new Lexer(input);
46  token = nullptr;
47  errorNode = nullptr;
48  syntaxError = 0;
49 }
Encapsulates an lexical analyzer. Provides token for the parser.
Definition: lexer.h:48
Token * token
Definition: parser.h:66
ErrorNode * errorNode
Definition: parser.h:69
Lexer * lexer
Definition: parser.h:65
int syntaxError
Definition: parser.h:68
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~Parser()

Parser::~Parser ( )

Definition at line 51 of file parser.cpp.

References lexer.

52 {
53  delete lexer;
54 }
Lexer * lexer
Definition: parser.h:65

Member Function Documentation

◆ Expect()

bool Parser::Expect ( Symbol  symbol)
private

Definition at line 519 of file parser.cpp.

References Token::GetPos(), GetToken(), Token::symbol, syntaxError, and token.

Referenced by ParseAtomic(), ParseDefault(), ParseDeleteStatement(), ParseDrawStatement(), ParseFileStatement(), ParseFunctionDef(), and ParseIdent().

520 {
521  GetToken();
522  if (token->symbol != symbol)
523  {
524  syntaxError = token->GetPos();
525  return false;
526  }
527 
528  return true;
529 }
Token * token
Definition: parser.h:66
Symbol symbol
Definition: token.h:53
int GetPos() const
Definition: token.cpp:89
void GetToken()
Definition: parser.cpp:494
int syntaxError
Definition: parser.h:68
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetToken()

void Parser::GetToken ( )
private

Definition at line 494 of file parser.cpp.

References Lexer::GetFirstToken(), Token::GetNextToken(), lexer, and token.

Referenced by Expect(), Parse(), ParseAddSubstract(), ParseAtomic(), ParseDeleteStatement(), ParseDigistStatement(), ParseFactor(), ParseHelpStatement(), ParseIdent(), ParseListStatement(), ParseNumber(), ParseNumeralStatement(), ParsePower(), ParsePrefsStatement(), ParsePromptStatement(), ParseStatement(), ParseUnary(), and TryParseStatement().

495 {
496  if (token == nullptr)
497  {
499  }
500  else
501  {
502  token = token->GetNextToken();
503  }
504 }
Token * token
Definition: parser.h:66
Lexer * lexer
Definition: parser.h:65
Token * GetFirstToken() const
Definition: lexer.cpp:62
Token * GetNextToken()
Definition: token.cpp:73
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Parse()

SyntaxNode * Parser::Parse ( )

Parses the input into a syntax tree.

Returns
SyntaxNode* representing a pointer to the top node of the tree. Notice: The caller is responsible for releasing memory.

Definition at line 56 of file parser.cpp.

References StatementBlockNode::Add(), EmptyStatement::EmptyStatement(), ErrorNode::ErrorNode(), Lexer::GetInput(), Token::GetPos(), GetToken(), lexer, PutToken(), StatementBlockNode::StatementBlockNode(), Token::symbol, symdelimiter, symend, token, Lexer::Tokenize(), and TryParseStatement().

Referenced by Evaluator::Evaluate(), ExecuteStatement::Execute(), LoadStatement::Execute(), and PreferencesBase::SetPrefs().

57 {
58  lexer->Tokenize();
59  token = nullptr;
60 
61  StatementBlockNode* block = nullptr;
62  SyntaxNode* result;
63  Token* current;
64 
65  do
66  {
67  result = TryParseStatement();
68 
69  GetToken();
70  if (token->symbol == symdelimiter || (token->symbol == symend && block != nullptr))
71  {
72  if (block == nullptr)
73  {
74  block = new StatementBlockNode();
75  }
76 
77  if (result != nullptr)
78  {
79  block->Add(result);
80  }
81 
82  while (token->symbol == symdelimiter)
83  {
84  GetToken();
85  }
86  }
87  else if (token->symbol != symend)
88  {
89  if (result != nullptr)
90  {
91  delete result;
92  }
93 
94  if (block == nullptr)
95  {
96  block = new StatementBlockNode();
97  }
98 
99  result = new ErrorNode(lexer->GetInput(), token->GetPos());
100  block->Add(result);
101 
102  // Skip until next statement
103  do
104  {
105  GetToken();
106  }
107  while (token->symbol != symdelimiter && token->symbol != symend);
108  }
109 
110  current = token;
111  PutToken();
112  }
113  while (current->symbol != symend);
114 
115  if (block != nullptr)
116  return block;
117 
118  if (result == nullptr)
119  return new EmptyStatement();
120 
121  return result;;
122 }
void Add(SyntaxNode *node)
Definition: nodes.cpp:244
A sequence of statements in a syntax tree.
Definition: nodes.h:151
Token * token
Definition: parser.h:66
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
Lexer * lexer
Definition: parser.h:65
Tokens are created by the Lexical Analyzer and provides an intermediate state for input consumed by t...
Definition: token.h:46
Symbol symbol
Definition: token.h:53
An empty statement.
Definition: empty.h:42
int GetPos() const
Definition: token.cpp:89
Definition: symbol.h:81
char * GetInput() const
Definition: lexer.cpp:57
Represents an error message encapsulated in a syntax tree.
Definition: nodes.h:123
void GetToken()
Definition: parser.cpp:494
void Tokenize()
Definition: lexer.cpp:67
void PutToken()
Definition: parser.cpp:514
SyntaxNode * TryParseStatement()
Definition: parser.cpp:124
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseAddSubstract()

ExpressionNode * Parser::ParseAddSubstract ( )
private

Definition at line 283 of file parser.cpp.

References AdditionNode::AdditionNode(), GetToken(), ParseFactor(), PutToken(), SubtractionNode::SubtractionNode(), Token::symbol, symminus, symplus, and token.

Referenced by ParseExpression().

284 {
285  ExpressionNode* node = ParseFactor();
286 
287  GetToken();
288  while (token->symbol == symplus || token->symbol == symminus)
289  {
290  if (token->symbol == symplus)
291  {
292  node = new AdditionNode(node, ParseFactor());
293  }
294  else if (token->symbol == symminus)
295  {
296  node = new SubtractionNode(node, ParseFactor());
297  }
298 
299  GetToken();
300  }
301 
302  PutToken();
303  return node;
304 }
Token * token
Definition: parser.h:66
Symbol symbol
Definition: token.h:53
Definition: symbol.h:48
ExpressionNode * ParseFactor()
Definition: parser.cpp:306
void GetToken()
Definition: parser.cpp:494
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
void PutToken()
Definition: parser.cpp:514
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseAtomic()

ExpressionNode * Parser::ParseAtomic ( )
private

Definition at line 362 of file parser.cpp.

References AbsoluteNode::AbsoluteNode(), ComplexiNode::ComplexiNode(), EulersNumberNode::EulersNumberNode(), Expect(), FactorialNode::FactorialNode(), Token::GetPos(), GetToken(), InsVariableNode::InsVariableNode(), NumericValueNode::NumericValueNode(), ParseExpression(), ParseIdent(), ParseNumber(), Peek(), PiNode::PiNode(), symabsolute, Token::symbol, syme, symfactorial, symi, symident, symins, symlparen, symnumber, sympi, symrparen, syntaxError, and token.

Referenced by ParseUnary().

363 {
364  ExpressionNode* node;
365 
366  GetToken();
367  if (token->symbol == symlparen)
368  {
369  node = ParseExpression();
370  Expect(symrparen);
371  }
372  else if (token->symbol == symabsolute)
373  {
374  node = new AbsoluteNode(ParseExpression());
376  }
377  else if (token->symbol == symident)
378  {
379  node = ParseIdent();
380  }
381  else if (token->symbol == sympi)
382  {
383  node = new PiNode();
384  }
385  else if (token->symbol == syme)
386  {
387  node = new EulersNumberNode();
388  }
389  else if (token->symbol == symi)
390  {
391  node = new ComplexiNode();
392  }
393  else if (token->symbol == symins)
394  {
395  node = new InsVariableNode();
396  }
397  else if (token->symbol == symnumber)
398  {
399  node = ParseNumber();
400  }
401  else
402  {
403  node = new NumericValueNode();
404  syntaxError = token->GetPos();
405  }
406 
407  if (Peek()->symbol == symfactorial)
408  {
409  GetToken();
410  node = new FactorialNode(node);
411  }
412 
413  return node;
414 }
Use of the square root of -2 in a syntax tree.
Definition: values.h:202
ExpressionNode * ParseExpression()
Definition: parser.cpp:278
Definition: symbol.h:76
Token * token
Definition: parser.h:66
bool Expect(Symbol symbol)
Definition: parser.cpp:519
Token * Peek() const
Definition: parser.cpp:506
Use of PI in a syntax tree.
Definition: values.h:188
Definition: symbol.h:79
Symbol symbol
Definition: token.h:53
int GetPos() const
Definition: token.cpp:89
ExpressionNode * ParseIdent()
Definition: parser.cpp:416
ExpressionNode * ParseNumber()
Definition: parser.cpp:469
Definition: symbol.h:78
Definition: symbol.h:77
void GetToken()
Definition: parser.cpp:494
Use of a numeric value in a syntax tree.
Definition: values.h:150
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
int syntaxError
Definition: parser.h:68
Use of Euler&#39;s number in a syntax tree.
Definition: values.h:174
Use of last result in a syntax tree.
Definition: values.h:135
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseDefault()

SyntaxNode * Parser::ParseDefault ( )
private

Definition at line 237 of file parser.cpp.

References errorNode, Expect(), ParseEvaluation(), ParseFunctionDef(), symassign, symident, symlparen, symrparen, syntaxError, and token.

Referenced by ParseStatement().

238 {
239  Token* temp = token;
240 
241  // Peek tokens
242  bool funcdef = Expect(symident);
243  funcdef = funcdef && Expect(symlparen);
244  funcdef = funcdef && Expect(symident);
245  funcdef = funcdef && Expect(symrparen);
246  funcdef = funcdef && Expect(symassign);
247 
248  // Restart parsing
249  syntaxError = -1;
250  errorNode = nullptr;
251  token = temp;
252 
253  if (funcdef)
254  {
255  return ParseFunctionDef();
256  }
257 
258  return ParseEvaluation();
259 }
SyntaxNode * ParseEvaluation()
Definition: parser.cpp:261
Token * token
Definition: parser.h:66
bool Expect(Symbol symbol)
Definition: parser.cpp:519
ErrorNode * errorNode
Definition: parser.h:69
Tokens are created by the Lexical Analyzer and provides an intermediate state for input consumed by t...
Definition: token.h:46
int syntaxError
Definition: parser.h:68
SyntaxNode * ParseFunctionDef()
Definition: parser.cpp:535
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseDeleteStatement()

SyntaxNode * Parser::ParseDeleteStatement ( )
private

Definition at line 582 of file parser.cpp.

References DeleteStatement::DeleteStatement(), Expect(), Token::GetPos(), Token::GetText(), GetToken(), PutToken(), Token::symbol, symfunction, symident, symlparen, symrparen, symvariable, syntaxError, and token.

Referenced by ParseStatement().

583 {
584  GetToken();
586  {
587  return new DeleteStatement(token->symbol);
588  }
589 
590  if (token->symbol != symident)
591  {
592  syntaxError = token->GetPos();
593  return nullptr;
594  }
595 
596  Token* identToken = token;
597 
598  GetToken();
599  if (token->symbol == symlparen)
600  {
601  Expect(symident);
602  Token* parameter = token;
603  Expect(symrparen);
604  return new DeleteStatement(identToken->GetText(), parameter->GetText());
605  }
606 
607  PutToken();
608  return new DeleteStatement(token->GetText());
609 }
Delete variable or function.
Definition: delete.h:39
Token * token
Definition: parser.h:66
bool Expect(Symbol symbol)
Definition: parser.cpp:519
Tokens are created by the Lexical Analyzer and provides an intermediate state for input consumed by t...
Definition: token.h:46
Symbol symbol
Definition: token.h:53
char * GetText() const
Definition: token.cpp:84
int GetPos() const
Definition: token.cpp:89
void GetToken()
Definition: parser.cpp:494
void PutToken()
Definition: parser.cpp:514
int syntaxError
Definition: parser.h:68
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseDigistStatement()

SyntaxNode * Parser::ParseDigistStatement ( )
private

Definition at line 713 of file parser.cpp.

References DecimalSystem::DecimalSystem(), DigitsStatement::DigitsStatement(), errorNode, ErrorNode::ErrorNode(), Lexer::GetInput(), Number::GetIntegerValue(), Token::GetPos(), Token::GetText(), GetToken(), lexer, NumeralSystem::Parse(), PutToken(), Token::symbol, symdelimiter, symend, symnumber, syntaxError, and token.

Referenced by ParseStatement().

714 {
715  GetToken();
716  if (token->symbol == symdelimiter || token->symbol == symend)
717  {
718  PutToken();
719  return new DigitsStatement();
720  }
721 
722  if (token->symbol != symnumber)
723  {
724  syntaxError = token->GetPos();
725  return nullptr;
726  }
727 
728  NumeralSystem* nsys = new DecimalSystem(0);
729  Number* number = nsys->Parse(token->GetText());
730  int digits = number->GetIntegerValue();
731  delete number;
732  delete nsys;
733 
734  if (digits < 0 || digits > 15)
735  {
736  errorNode = new ErrorNode(
737  lexer->GetInput(),
738  HELPPDIGITS,
739  token->GetText(),
740  token->GetPos());
741 
742  return nullptr;
743  }
744 
745  return new DigitsStatement(digits);
746 }
virtual int GetIntegerValue()=0
Set number of significant digits to show.
Definition: digits.h:39
Token * token
Definition: parser.h:66
ErrorNode * errorNode
Definition: parser.h:69
Lexer * lexer
Definition: parser.h:65
#define HELPPDIGITS
Definition: text.h:84
Definition: numb.h:66
Symbol symbol
Definition: token.h:53
char * GetText() const
Definition: token.cpp:84
int GetPos() const
Definition: token.cpp:89
virtual Number * Parse(const char *text, unsigned int *length, char **end)=0
Definition: symbol.h:81
char * GetInput() const
Definition: lexer.cpp:57
Represents an error message encapsulated in a syntax tree.
Definition: nodes.h:123
void GetToken()
Definition: parser.cpp:494
Base class for all numeral systems.
Definition: ntext.h:49
void PutToken()
Definition: parser.cpp:514
int syntaxError
Definition: parser.h:68
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseDrawStatement()

SyntaxNode * Parser::ParseDrawStatement ( )
private

Definition at line 772 of file parser.cpp.

References DrawStatement::DrawStatement(), Expect(), Token::GetText(), PlotStatement::PlotStatement(), Token::symbol, symident, symlparen, symplot, symrparen, and token.

Referenced by ParseStatement().

773 {
774  Token* statement = token;
775 
776  Expect(symident);
777  Token* identToken = token;
778  Expect(symlparen);
779  Expect(symident);
780  Token* paramToken = token;
781  Expect(symrparen);
782 
783  if (statement->symbol == symplot)
784  return new PlotStatement(identToken->GetText(), paramToken->GetText());
785 
786  return new DrawStatement(identToken->GetText(), paramToken->GetText());
787 }
Token * token
Definition: parser.h:66
bool Expect(Symbol symbol)
Definition: parser.cpp:519
Tokens are created by the Lexical Analyzer and provides an intermediate state for input consumed by t...
Definition: token.h:46
Symbol symbol
Definition: token.h:53
char * GetText() const
Definition: token.cpp:84
Definition: symbol.h:98
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseEvaluation()

SyntaxNode * Parser::ParseEvaluation ( )
private

Definition at line 261 of file parser.cpp.

References EvalStatement::EvalStatement(), ExpressionNode::IsSilent(), ParseExpression(), and SilentStatement::SilentStatement().

Referenced by ParseDefault().

262 {
264  if (exp == nullptr)
265  {
266  return nullptr;
267  }
268 
269  StatementNode* sta = new EvalStatement(exp);
270  if (exp->IsSilent())
271  {
272  sta = new SilentStatement(sta);
273  }
274 
275  return sta;
276 }
ExpressionNode * ParseExpression()
Definition: parser.cpp:278
Base class for all statements in a syntax tree.
Definition: node.h:40
Evaluate arithmetic expression.
Definition: eval.h:39
double exp(double x)
Returns the exponential of x.
Definition: exp.c:138
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
Mutes the output of another statement.
Definition: silent.h:42
virtual bool IsSilent()
Definition: nodes.cpp:114
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseExpression()

ExpressionNode * Parser::ParseExpression ( )
private

Definition at line 278 of file parser.cpp.

References ParseAddSubstract().

Referenced by ParseAtomic(), ParseEvaluation(), ParseFunctionDef(), ParseIdent(), and ParseStatement().

279 {
280  return ParseAddSubstract();
281 }
ExpressionNode * ParseAddSubstract()
Definition: parser.cpp:283
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseFactor()

ExpressionNode * Parser::ParseFactor ( )
private

Definition at line 306 of file parser.cpp.

References DivisionNode::DivisionNode(), GetToken(), MultiplicationNode::MultiplicationNode(), ParsePower(), PutToken(), Token::symbol, symslash, symtimes, and token.

Referenced by ParseAddSubstract().

307 {
308  ExpressionNode* node = ParsePower();
309 
310  GetToken();
311  while (token->symbol == symtimes || token->symbol == symslash)
312  {
313  if (token->symbol == symtimes)
314  {
315  node = new MultiplicationNode(node, ParsePower());
316  }
317  else if (token->symbol == symslash)
318  {
319  node = new DivisionNode(node, ParsePower());
320  }
321 
322  GetToken();
323  }
324 
325  PutToken();
326  return node;
327 }
ExpressionNode * ParsePower()
Definition: parser.cpp:329
Token * token
Definition: parser.h:66
Symbol symbol
Definition: token.h:53
void GetToken()
Definition: parser.cpp:494
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
void PutToken()
Definition: parser.cpp:514
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseFileStatement()

SyntaxNode * Parser::ParseFileStatement ( )
private

Definition at line 627 of file parser.cpp.

References ErrorNode::ErrorNode(), ExecuteStatement::ExecuteStatement(), Expect(), Lexer::GetInput(), Token::GetPos(), Token::GetText(), lexer, LoadStatement::LoadStatement(), SaveStatement::SaveStatement(), ShowStatement::ShowStatement(), Token::symbol, symexecute, symload, symqident, symsave, symshow, and token.

Referenced by ParseStatement().

628 {
629  Token* statement = token;
630 
631  Expect(symqident);
632  Token* identToken = token;
633 
634  if (statement->symbol == symload)
635  return new LoadStatement(identToken->GetText());
636 
637  if (statement->symbol == symsave)
638  return new SaveStatement(identToken->GetText());
639 
640  if (statement->symbol == symexecute)
641  return new ExecuteStatement(identToken->GetText());
642 
643  if (statement->symbol == symshow)
644  return new ShowStatement(identToken->GetText());
645 
646  return new ErrorNode(
647  lexer->GetInput(),
649  statement->GetPos());
650 }
Definition: symbol.h:84
#define EMPTYSTRING
Definition: amath.h:213
Token * token
Definition: parser.h:66
Definition: symbol.h:82
bool Expect(Symbol symbol)
Definition: parser.cpp:519
Lexer * lexer
Definition: parser.h:65
Tokens are created by the Lexical Analyzer and provides an intermediate state for input consumed by t...
Definition: token.h:46
Symbol symbol
Definition: token.h:53
char * GetText() const
Definition: token.cpp:84
int GetPos() const
Definition: token.cpp:89
Definition: symbol.h:85
char * GetInput() const
Definition: lexer.cpp:57
Represents an error message encapsulated in a syntax tree.
Definition: nodes.h:123
#define HELPUERROR
Definition: text.h:79
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseFunctionDef()

SyntaxNode * Parser::ParseFunctionDef ( )
private

Definition at line 535 of file parser.cpp.

References UserFunction::CreateVariable(), errorNode, ErrorNode::ErrorNode(), Expect(), FunctionDefinitionNode::FunctionDefinitionNode(), Program::Functions, FunctionList::GetFunctionDef(), Lexer::GetInput(), Token::GetPos(), Token::GetText(), VariableList::InsertTemporaryVariable(), FunctionList::IsSystemFunction(), lexer, ParseExpression(), VariableList::RemoveTemporaryVariable(), UserFunction::SetExpression(), symassign, symident, symlparen, symrparen, token, and Program::Variables.

Referenced by ParseDefault(), and ParseStatement().

536 {
537  Expect(symident);
538  Token* funcToken = token;
539  Expect(symlparen);
540  Expect(symident);
541  Token* funcVariable = token;
542  Expect(symrparen);
543  Expect(symassign);
544 
545  if (Program->Functions->IsSystemFunction(funcToken->GetText()))
546  {
547  errorNode = new ErrorNode(
548  lexer->GetInput(),
549  HELPFUNRDEF,
550  funcToken->GetText(),
551  funcToken->GetPos());
552 
553  return nullptr;
554  }
555 
556  UserFunction* function = Program->Functions->GetFunctionDef(funcToken->GetText());
557  Variable* variable = function->CreateVariable(funcVariable->GetText());
559  function->SetExpression(ParseExpression());
561 
562  // TODO: Move logic to FunctionDefinitionNode
563  return new FunctionDefinitionNode();
564 }
Master control class.
Definition: program.h:55
ExpressionNode * ParseExpression()
Definition: parser.cpp:278
A user defined variable.
Definition: values.h:76
Token * token
Definition: parser.h:66
bool Expect(Symbol symbol)
Definition: parser.cpp:519
ErrorNode * errorNode
Definition: parser.h:69
Lexer * lexer
Definition: parser.h:65
Tokens are created by the Lexical Analyzer and provides an intermediate state for input consumed by t...
Definition: token.h:46
Variable * InsertTemporaryVariable(Variable *variable)
Definition: values.cpp:200
char * GetText() const
Definition: token.cpp:84
int GetPos() const
Definition: token.cpp:89
class FunctionList * Functions
Definition: program.h:78
A syntax node able to define a user defined function.
Definition: funcdef.h:39
#define HELPFUNRDEF
Definition: text.h:82
class VariableList * Variables
Definition: program.h:77
char * GetInput() const
Definition: lexer.cpp:57
Represents an error message encapsulated in a syntax tree.
Definition: nodes.h:123
A user defined function.
Definition: userfunction.h:44
void RemoveTemporaryVariable()
Definition: values.cpp:210
static bool IsSystemFunction(const char *name)
UserFunction * GetFunctionDef(const char *name)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseHelpStatement()

SyntaxNode * Parser::ParseHelpStatement ( )
private

Definition at line 566 of file parser.cpp.

References Token::GetText(), GetToken(), HelpStatement::HelpStatement(), PutToken(), Token::symbol, symdelimiter, symend, symident, and token.

Referenced by ParseStatement().

567 {
568  GetToken();
569 
570  if (token->symbol == symdelimiter || token->symbol == symend)
571  {
572  PutToken();
573  return new HelpStatement();
574  }
575 
576  if (token->symbol == symident)
577  return new HelpStatement(token->GetText());
578 
579  return new HelpStatement(token->symbol);
580 }
Logic related to the help statement.
Definition: help.h:39
Token * token
Definition: parser.h:66
Symbol symbol
Definition: token.h:53
char * GetText() const
Definition: token.cpp:84
Definition: symbol.h:81
void GetToken()
Definition: parser.cpp:494
void PutToken()
Definition: parser.cpp:514
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseIdent()

ExpressionNode * Parser::ParseIdent ( )
private

Definition at line 416 of file parser.cpp.

References AssignmentNode::AssignmentNode(), VariableList::CreateVariable(), errorNode, ErrorNode::ErrorNode(), Expect(), Program::Functions, FunctionList::GetFunctionCall(), Lexer::GetInput(), Token::GetPos(), Token::GetText(), GetToken(), VariableList::GetVariable(), lexer, NumericValueNode::NumericValueNode(), ParseExpression(), PutToken(), symassign, Token::symbol, symlparen, symrparen, token, VariableNode::VariableNode(), and Program::Variables.

Referenced by ParseAtomic().

417 {
418  ExpressionNode* node;
419  Token* identToken = token;
420 
421  GetToken();
422  if (token->symbol == symlparen)
423  {
424  ExpressionNode* parameter = ParseExpression();
425  Expect(symrparen);
426  node = Program->Functions->GetFunctionCall(identToken->GetText(), parameter);
427 
428  if (node == nullptr)
429  {
430  errorNode = new ErrorNode(
431  lexer->GetInput(),
432  HELPFUNNDEF,
433  identToken->GetText(),
434  identToken->GetPos());
435 
436  delete parameter;
437  node = new NumericValueNode();
438  }
439  }
440  else if (token->symbol == symassign)
441  {
442  Variable* var = Program->Variables->CreateVariable(identToken->GetText());
443  node = new AssignmentNode(new VariableNode(var), ParseExpression());
444  }
445  else
446  {
447  PutToken();
449 
450  if (var == nullptr)
451  {
452  errorNode = new ErrorNode(
453  lexer->GetInput(),
454  HELPVARNDEF,
455  identToken->GetText(),
456  identToken->GetPos());
457 
458  node = new NumericValueNode();
459  }
460  else
461  {
462  node = new VariableNode(var);
463  }
464  }
465 
466  return node;
467 }
Master control class.
Definition: program.h:55
ExpressionNode * ParseExpression()
Definition: parser.cpp:278
A user defined variable.
Definition: values.h:76
Token * token
Definition: parser.h:66
bool Expect(Symbol symbol)
Definition: parser.cpp:519
ErrorNode * errorNode
Definition: parser.h:69
Lexer * lexer
Definition: parser.h:65
Tokens are created by the Lexical Analyzer and provides an intermediate state for input consumed by t...
Definition: token.h:46
Variable * GetVariable(const char *name) const
Definition: values.cpp:160
Symbol symbol
Definition: token.h:53
char * GetText() const
Definition: token.cpp:84
int GetPos() const
Definition: token.cpp:89
class FunctionList * Functions
Definition: program.h:78
#define HELPVARNDEF
Definition: text.h:80
Variable * CreateVariable(const char *name)
Definition: values.cpp:172
FunctionNode * GetFunctionCall(const char *function, ExpressionNode *value) const
#define HELPFUNNDEF
Definition: text.h:81
class VariableList * Variables
Definition: program.h:77
char * GetInput() const
Definition: lexer.cpp:57
Represents an error message encapsulated in a syntax tree.
Definition: nodes.h:123
void GetToken()
Definition: parser.cpp:494
Use of a variable in a syntax tree.
Definition: values.h:110
Use of a numeric value in a syntax tree.
Definition: values.h:150
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
void PutToken()
Definition: parser.cpp:514
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseListStatement()

SyntaxNode * Parser::ParseListStatement ( )
private

Definition at line 611 of file parser.cpp.

References Token::GetPos(), Token::GetText(), GetToken(), ListStatement::ListStatement(), PutToken(), Token::symbol, symdelimiter, symend, symqident, syntaxError, and token.

Referenced by ParseStatement().

612 {
613  GetToken();
614  if (token->symbol == symqident)
615  return new ListStatement(token->GetText());
616 
617  if (token->symbol == symend || token->symbol == symdelimiter)
618  {
619  PutToken();
620  return new ListStatement();
621  }
622 
623  syntaxError = token->GetPos();
624  return nullptr;
625 }
Token * token
Definition: parser.h:66
Symbol symbol
Definition: token.h:53
char * GetText() const
Definition: token.cpp:84
int GetPos() const
Definition: token.cpp:89
Definition: symbol.h:81
void GetToken()
Definition: parser.cpp:494
void PutToken()
Definition: parser.cpp:514
int syntaxError
Definition: parser.h:68
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseNumber()

ExpressionNode * Parser::ParseNumber ( )
private

Definition at line 469 of file parser.cpp.

References ComplexNumber::ComplexNumber(), Number::GetRealValue(), Token::GetText(), GetToken(), Program::Input, NumericValueNode::NumericValueNode(), NumeralSystem::Parse(), PutToken(), Token::symbol, symi, and token.

Referenced by ParseAtomic().

470 {
471  const char* a = token->GetText();
472  Number* number;
473 
474  GetToken();
475  if (token->symbol == symi)
476  {
477  Number* imag = Program->Input->Parse(a);
478  number = new ComplexNumber(0.0, imag->GetRealValue());
479  delete imag;
480  }
481  else
482  {
483  PutToken();
484  number = Program->Input->Parse(a);
485  }
486 
487  return new NumericValueNode(number);
488 }
Master control class.
Definition: program.h:55
Token * token
Definition: parser.h:66
Definition: numb.h:66
Symbol symbol
Definition: token.h:53
char * GetText() const
Definition: token.cpp:84
virtual Number * Parse(const char *text, unsigned int *length, char **end)=0
virtual double GetRealValue()=0
Definition: symbol.h:78
Represent a complex number with 2 components of 15 significant digits.
Definition: cplex.h:46
void GetToken()
Definition: parser.cpp:494
Use of a numeric value in a syntax tree.
Definition: values.h:150
void PutToken()
Definition: parser.cpp:514
class NumeralSystem * Input
Definition: program.h:75
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseNumeralStatement()

SyntaxNode * Parser::ParseNumeralStatement ( )
private

Definition at line 652 of file parser.cpp.

References DecimalSystem::DecimalSystem(), errorNode, ErrorNode::ErrorNode(), Lexer::GetInput(), Number::GetIntegerValue(), Token::GetPos(), Token::GetText(), GetToken(), InputStatement::InputStatement(), lexer, OutputStatement::OutputStatement(), NumeralSystem::Parse(), PutToken(), symbin, Token::symbol, symdec, symdelimiter, symend, symhex, syminput, symnumber, symoct, syntaxError, and token.

Referenced by ParseStatement().

653 {
654  Token* statement = token;
655  unsigned int base;
656 
657  GetToken();
658  switch (token->symbol)
659  {
660  case symend:
661  case symdelimiter:
662  PutToken();
663  return (statement->symbol == syminput)
664  ? static_cast<SyntaxNode*>(new InputStatement())
665  : static_cast<SyntaxNode*>(new OutputStatement());
666  case symbin:
667  base = 2;
668  break;
669  case symoct:
670  base = 8;
671  break;
672  case symdec:
673  base = 10;
674  break;
675  case symhex:
676  base = 16;
677  break;
678  default:
679  base = 0;
680  }
681 
682  if (base == 0 && token->symbol != symnumber)
683  {
684  syntaxError = token->GetPos();
685  return nullptr;
686  }
687 
688  if (base == 0)
689  {
690  NumeralSystem* nsys = new DecimalSystem(0);
691  Number* number = nsys->Parse(token->GetText());
692  base = number->GetIntegerValue();
693  delete number;
694  delete nsys;
695 
696  if (base < 2 || base > 32)
697  {
698  errorNode = new ErrorNode(
699  lexer->GetInput(),
700  HELPPNUMERA,
701  token->GetText(),
702  token->GetPos());
703 
704  return nullptr;
705  }
706  }
707 
708  return (statement->symbol == syminput)
709  ? static_cast<SyntaxNode*>(new InputStatement(base))
710  : static_cast<SyntaxNode*>(new OutputStatement(base));
711 }
virtual int GetIntegerValue()=0
Definition: symbol.h:93
Definition: symbol.h:90
Token * token
Definition: parser.h:66
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
Definition: symbol.h:92
ErrorNode * errorNode
Definition: parser.h:69
Lexer * lexer
Definition: parser.h:65
Tokens are created by the Lexical Analyzer and provides an intermediate state for input consumed by t...
Definition: token.h:46
Definition: numb.h:66
Symbol symbol
Definition: token.h:53
char * GetText() const
Definition: token.cpp:84
Definition: symbol.h:91
int GetPos() const
Definition: token.cpp:89
virtual Number * Parse(const char *text, unsigned int *length, char **end)=0
Change numeral input system.
Definition: input.h:39
Change numeral output system.
Definition: output.h:39
#define HELPPNUMERA
Definition: text.h:83
Definition: symbol.h:81
char * GetInput() const
Definition: lexer.cpp:57
Represents an error message encapsulated in a syntax tree.
Definition: nodes.h:123
void GetToken()
Definition: parser.cpp:494
Base class for all numeral systems.
Definition: ntext.h:49
void PutToken()
Definition: parser.cpp:514
int syntaxError
Definition: parser.h:68
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParsePower()

ExpressionNode * Parser::ParsePower ( )
private

Definition at line 329 of file parser.cpp.

References GetToken(), ParseUnary(), PowerNode::PowerNode(), PutToken(), Token::symbol, sympower, and token.

Referenced by ParseFactor().

330 {
331  ExpressionNode* node = ParseUnary();
332 
333  GetToken();
334  while (token->symbol == sympower)
335  {
336  node = new PowerNode(node, ParseUnary());
337  GetToken();
338  }
339 
340  PutToken();
341  return node;
342 }
Token * token
Definition: parser.h:66
Symbol symbol
Definition: token.h:53
ExpressionNode * ParseUnary()
Definition: parser.cpp:344
void GetToken()
Definition: parser.cpp:494
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
void PutToken()
Definition: parser.cpp:514
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParsePrefsStatement()

SyntaxNode * Parser::ParsePrefsStatement ( )
private

Definition at line 760 of file parser.cpp.

References GetToken(), PrefsStatement::PrefsStatement(), PutToken(), Token::symbol, symload, symsave, and token.

Referenced by ParseStatement().

761 {
762  GetToken();
763  if (token->symbol == symload || token->symbol == symsave)
764  {
765  return new PrefsStatement(token->symbol);
766  }
767 
768  PutToken();
769  return new PrefsStatement();;
770 }
Definition: symbol.h:84
Token * token
Definition: parser.h:66
Symbol symbol
Definition: token.h:53
Load or save preferences.
Definition: prefs.h:40
Definition: symbol.h:85
void GetToken()
Definition: parser.cpp:494
void PutToken()
Definition: parser.cpp:514
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParsePromptStatement()

SyntaxNode * Parser::ParsePromptStatement ( )
private

Definition at line 748 of file parser.cpp.

References Token::GetText(), GetToken(), PromptStatement::PromptStatement(), PutToken(), Token::symbol, symqident, and token.

Referenced by ParseStatement().

749 {
750  GetToken();
751  if (token->symbol == symqident)
752  {
753  return new PromptStatement(token->GetText());
754  }
755 
756  PutToken();
757  return nullptr;
758 }
Token * token
Definition: parser.h:66
Symbol symbol
Definition: token.h:53
char * GetText() const
Definition: token.cpp:84
Set prompt string.
Definition: prompt.h:40
void GetToken()
Definition: parser.cpp:494
void PutToken()
Definition: parser.cpp:514
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseStatement()

SyntaxNode * Parser::ParseStatement ( )
private

Definition at line 156 of file parser.cpp.

References AboutStatement::AboutStatement(), ClearStatement::ClearStatement(), EvalStatement::EvalStatement(), ExitStatement::ExitStatement(), GetToken(), LicenseStatement::LicenseStatement(), ListFunctionsStatement::ListFunctionsStatement(), ListVariablesStatement::ListVariablesStatement(), MemoryStatement::MemoryStatement(), ParseDefault(), ParseDeleteStatement(), ParseDigistStatement(), ParseDrawStatement(), ParseExpression(), ParseFileStatement(), ParseFunctionDef(), ParseHelpStatement(), ParseListStatement(), ParseNumeralStatement(), ParsePrefsStatement(), ParsePromptStatement(), PutToken(), symabout, Token::symbol, symclear, symdef, symdelete, symdigits, symdraw, symeval, symexecute, symexit, symfunction, symhelp, syminput, symlicense, symlist, symload, symmem, symoutput, symplot, symprefs, symprompt, symsave, symshow, symvariable, symversion, token, and VersionStatement::VersionStatement().

Referenced by TryParseStatement().

157 {
158  SyntaxNode* res;
159 
160  GetToken();
161  switch (token->symbol)
162  {
163  case symhelp:
164  res = ParseHelpStatement();
165  break;
166  case symdelete:
167  res = ParseDeleteStatement();
168  break;
169  case symdef:
170  res = ParseFunctionDef();
171  break;
172 #if defined(UNIX) || defined(HAIKU) || defined(AMIGA)
173  case symlist:
174  res = ParseListStatement();
175  break;
176 #endif
177  case symshow:
178  case symload:
179  case symsave:
180  case symexecute:
181  res = ParseFileStatement();
182  break;
183  case syminput:
184  case symoutput:
185  res = ParseNumeralStatement();
186  break;
187  case symprompt:
188  res = ParsePromptStatement();
189  break;
190  case symprefs:
191  res = ParsePrefsStatement();
192  break;
193  case symdigits:
194  res = ParseDigistStatement();
195  break;
196  case symdraw:
197  case symplot:
198  res = ParseDrawStatement();
199  break;
200  case symabout:
201  res = new AboutStatement();
202  break;
203  case symlicense:
204  res = new LicenseStatement();
205  break;
206  case symversion:
207  res = new VersionStatement();
208  break;
209  case symexit:
210  res = new ExitStatement();
211  break;
212 #if defined(AMIGA) || defined(TERMIOS) || defined(WINDOWS)
213  case symclear:
214  res = new ClearStatement();
215  break;
216 #endif
217  case symmem:
218  res = new MemoryStatement();
219  break;
220  case symvariable:
221  res = new ListVariablesStatement();
222  break;
223  case symfunction:
224  res = new ListFunctionsStatement();
225  break;
226  case symeval:
227  res = new EvalStatement(ParseExpression());
228  break;
229  default:
230  PutToken();
231  res = ParseDefault();
232  }
233 
234  return res;
235 }
Definition: symbol.h:84
ExpressionNode * ParseExpression()
Definition: parser.cpp:278
Token * token
Definition: parser.h:66
Evaluate arithmetic expression.
Definition: eval.h:39
SyntaxNode * ParseDefault()
Definition: parser.cpp:237
Definition: symbol.h:82
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
SyntaxNode * ParsePromptStatement()
Definition: parser.cpp:748
SyntaxNode * ParseDeleteStatement()
Definition: parser.cpp:582
Definition: symbol.h:83
Symbol symbol
Definition: token.h:53
A syntax node able to list all user defined functions.
Definition: funclist.h:39
Definition: symbol.h:98
SyntaxNode * ParseListStatement()
Definition: parser.cpp:611
Definition: symbol.h:85
SyntaxNode * ParseDigistStatement()
Definition: parser.cpp:713
Definition: symbol.h:67
Definition: symbol.h:99
SyntaxNode * ParseHelpStatement()
Definition: parser.cpp:566
Exit program.
Definition: exit.h:39
Show version string.
Definition: version.h:39
Statement to list all user defined variables.
Definition: values.h:99
Show memory usage.
Definition: memory.h:39
SyntaxNode * ParseDrawStatement()
Definition: parser.cpp:772
Clear the console window.
Definition: clear.h:39
Show license text.
Definition: license.h:39
Definition: symbol.h:62
void GetToken()
Definition: parser.cpp:494
Definition: symbol.h:94
SyntaxNode * ParsePrefsStatement()
Definition: parser.cpp:760
Definition: symbol.h:61
void PutToken()
Definition: parser.cpp:514
SyntaxNode * ParseFileStatement()
Definition: parser.cpp:627
SyntaxNode * ParseNumeralStatement()
Definition: parser.cpp:652
Show about text.
Definition: about.h:39
Definition: symbol.h:66
SyntaxNode * ParseFunctionDef()
Definition: parser.cpp:535
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseUnary()

ExpressionNode * Parser::ParseUnary ( )
private

Definition at line 344 of file parser.cpp.

References GetToken(), ParseAtomic(), PutToken(), Token::symbol, symminus, token, and UnaryNode::UnaryNode().

Referenced by ParsePower().

345 {
346  ExpressionNode* node;
347 
348  GetToken();
349  if (token->symbol == symminus)
350  {
351  node = new UnaryNode(ParseAtomic());
352  }
353  else
354  {
355  PutToken();
356  node = ParseAtomic();
357  }
358 
359  return node;
360 }
Token * token
Definition: parser.h:66
Symbol symbol
Definition: token.h:53
ExpressionNode * ParseAtomic()
Definition: parser.cpp:362
void GetToken()
Definition: parser.cpp:494
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
void PutToken()
Definition: parser.cpp:514
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Peek()

Token * Parser::Peek ( ) const
private

Definition at line 506 of file parser.cpp.

References Lexer::GetFirstToken(), Token::GetNextToken(), lexer, and token.

Referenced by ParseAtomic().

507 {
508  if (token == nullptr)
509  return lexer->GetFirstToken();
510 
511  return token->GetNextToken();
512 }
Token * token
Definition: parser.h:66
Lexer * lexer
Definition: parser.h:65
Token * GetFirstToken() const
Definition: lexer.cpp:62
Token * GetNextToken()
Definition: token.cpp:73
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PutToken()

void Parser::PutToken ( )
private

Definition at line 514 of file parser.cpp.

References Token::GetLastToken(), and token.

Referenced by Parse(), ParseAddSubstract(), ParseDeleteStatement(), ParseDigistStatement(), ParseFactor(), ParseHelpStatement(), ParseIdent(), ParseListStatement(), ParseNumber(), ParseNumeralStatement(), ParsePower(), ParsePrefsStatement(), ParsePromptStatement(), ParseStatement(), ParseUnary(), and TryParseStatement().

515 {
516  token = token->GetLastToken();
517 }
Token * token
Definition: parser.h:66
Token * GetLastToken() const
Definition: token.cpp:68
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TryParseStatement()

SyntaxNode * Parser::TryParseStatement ( )
private

Definition at line 124 of file parser.cpp.

References errorNode, ErrorNode::ErrorNode(), Lexer::GetInput(), GetToken(), lexer, ParseStatement(), PutToken(), Token::symbol, symdelimiter, symend, syntaxError, and token.

Referenced by Parse().

125 {
126  GetToken();
127  if (token->symbol == symend || token->symbol == symdelimiter)
128  {
129  PutToken();
130  return nullptr;
131  }
132  PutToken();
133 
134  errorNode = nullptr;
135  syntaxError = -1;
136  SyntaxNode* result = ParseStatement();
137 
138  if (errorNode == nullptr && syntaxError != -1)
139  {
141  }
142 
143  if (errorNode != nullptr)
144  {
145  delete result;
146  result = errorNode;
147  }
148 
149  return result;
150 }
Token * token
Definition: parser.h:66
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
ErrorNode * errorNode
Definition: parser.h:69
Lexer * lexer
Definition: parser.h:65
Symbol symbol
Definition: token.h:53
Definition: symbol.h:81
SyntaxNode * ParseStatement()
Definition: parser.cpp:156
char * GetInput() const
Definition: lexer.cpp:57
Represents an error message encapsulated in a syntax tree.
Definition: nodes.h:123
void GetToken()
Definition: parser.cpp:494
void PutToken()
Definition: parser.cpp:514
int syntaxError
Definition: parser.h:68
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ errorNode

ErrorNode* Parser::errorNode
private

◆ lexer

◆ syntaxError

◆ token


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