-
Notifications
You must be signed in to change notification settings - Fork 0
/
NonTerminalExpressions.cpp
50 lines (45 loc) · 1.12 KB
/
NonTerminalExpressions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "NonTerminalExpressions.h"
void ParenthesisExpression::Interpret(Context context)
{
std::stack<unsigned> indices_of_separators;
unsigned i=0;
if(*context.GetBegin()!=*_parenthesis->_leftSeparator)
{
throw std::invalid_argument("syntax error: missing " + std::string(_parenthesis->_leftSeparator,1));
}
else
{
indices_of_separators.push(i);
}
i++;
const char *cntxt_ptr = context.GetBegin()+i;
bool found_end = false;
for(;context.GetBegin()+i!=context.GetEnd()&&found_end==false;i++)
{
if(*cntxt_ptr == *_parenthesis->_leftSeparator)
{
indices_of_separators.push(i);
}
else if(*cntxt_ptr == *_parenthesis->_rightSeparator)
{
if(!indices_of_separators.empty())
indices_of_separators.pop();
else
found_end = true;
}
}
if(found_end)
{
_nonterminalExpression->Interpret(Context(context.GetBegin()+1,context.GetBegin()+i));
_end = context.GetBegin()+i;
}
else
throw std::invalid_argument("syntax error: missing " + std::string(_parenthesis->_rightSeparator,1));
}
void ParenthesisExpression::UpdateContext(Context &context)
{
if(_end != NULL)
{
context.SetBegin(_end+1);
}
}