Skip to content

Commit

Permalink
added support for symbols, and respective tests
Browse files Browse the repository at this point in the history
  • Loading branch information
srajangarg committed Jan 20, 2016
1 parent 6d3b995 commit 1c23093
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
43 changes: 34 additions & 9 deletions symengine/parser.cpp
Expand Up @@ -39,16 +39,24 @@ class expressionParser
{
RCP<const Basic> result;
bool result_set = false;
bool expr_is_symbol = false;
std::string expr;

for (uint iter = l; iter < h; ++iter)
{
if (OPERATORS.find(s[iter]) != OPERATORS.end())
if (is_operator(s, iter))
{
if (s[iter] != '(')
{
if (!result_set)
result = integer(std::stoi(expr));

{
if (expr_is_symbol)
result = symbol(expr);
else
result = integer(std::stoi(expr));
}
}

switch(s[iter])
{
case '+':
Expand Down Expand Up @@ -79,13 +87,23 @@ class expressionParser
continue;
}
result_set = true;
expr_is_symbol = false;
}
else
{
expr += s[iter];

int ascii = s[iter] - '0';
if (ascii < 0 or ascii > 9)
expr_is_symbol = true;

if (iter == h-1)
result = integer(std::stoi(expr));
{
if (expr_is_symbol)
result = symbol(expr);
else
result = integer(std::stoi(expr));
}
}
}

Expand All @@ -95,7 +113,6 @@ class expressionParser
RCP<const Basic> parse(std::string &s)
{
std::string copy;
char x;
std::stack<uint> rBracket;
std::stack<std::pair<int, uint> > opStack;

Expand All @@ -113,9 +130,9 @@ class expressionParser

for (int i = newLength-1; i >= 0; i--)
{
x = copy[i];
if (OPERATORS.find(x) != OPERATORS.end())
{
if (is_operator(copy, i))
{
char x = copy[i];
if(x == '(')
{
while(opStack.top().second != rBracket.top())
Expand All @@ -127,7 +144,7 @@ class expressionParser
else if(x == ')')
{
opStack.push(std::make_pair(opPrecedence[x], i));
rBracket.push(i);
rBracket.push(i);
}
else
{
Expand All @@ -141,6 +158,14 @@ class expressionParser
}
return parse_string(copy, 0, newLength);
}

bool is_operator(std::string& s, int iter)
{
if (iter >= 0 and iter < (int)s.length())
if (OPERATORS.find(s[iter]) != OPERATORS.end())
return true;
return false;
}
};

} // SymEngine
41 changes: 40 additions & 1 deletion symengine/tests/basic/test_parser.cpp
Expand Up @@ -42,7 +42,7 @@ TEST_CASE("Parsing: integers, basic operations", "[parser]")
s = "((3)+(1*0))";
res = p.parse(s);
REQUIRE(eq(*res, *integer(3)));

s = "((2))*(1+(2*3))";
res = p.parse(s);
REQUIRE(eq(*res, *integer(14)));
Expand Down Expand Up @@ -83,3 +83,42 @@ TEST_CASE("Parsing: integers, basic operations", "[parser]")
res = p.parse(s);
REQUIRE(eq(*res, *add(integer(9), div(integer(-5), integer(4)))));
}

TEST_CASE("Parsing: symbols", "[parser]")
{
std::string s;
expressionParser p;
RCP<const Basic> res;
RCP<const Basic> x = symbol("x");
RCP<const Basic> y = symbol("y");
RCP<const Basic> w = symbol("w1");
RCP<const Basic> l = symbol("l0ngn4me");

s = "x + 2*y";
res = p.parse(s);
REQUIRE(eq(*res, *add(x, mul(integer(2), y))));

s = "w1*y";
res = p.parse(s);
REQUIRE(eq(*res, *mul(w ,y)));

s = "x^(3+w1)-2/y";
res = p.parse(s);
REQUIRE(eq(*res, *add(pow(x, add(integer(3), w)), div(integer(-2), y))));

s = "l0ngn4me - w1*y + 2^(x)";
res = p.parse(s);
REQUIRE(eq(*res, *add(add(l, neg(mul(w, y))), pow(integer(2), x))));

s = "4*x/8 - (w1*y)";
res = p.parse(s);
REQUIRE(eq(*res, *add(neg(mul(w, y)), div(x, integer(2)))));

s = "3*y + (2*y)";
res = p.parse(s);
REQUIRE(eq(*res, *mul(y, integer(5))));

s = "3*y/(1+x)";
res = p.parse(s);
REQUIRE(eq(*res, *div(mul(y, integer(3)), add(x, integer(1)))));
}

0 comments on commit 1c23093

Please sign in to comment.