diff --git a/poincare/Makefile b/poincare/Makefile index 542dfcdd2..f8b00f669 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -63,6 +63,7 @@ tests += $(addprefix poincare/test/,\ identity.cpp\ integer.cpp\ matrix.cpp\ + parser.cpp\ product.cpp\ power.cpp\ simplify_utils.cpp\ diff --git a/poincare/src/expression_parser.y b/poincare/src/expression_parser.y index 5fe0c1a6e..70077bbb2 100644 --- a/poincare/src/expression_parser.y +++ b/poincare/src/expression_parser.y @@ -112,8 +112,11 @@ mtxData: number: DIGITS { $$ = new Integer($1.address, false); } + | DOT DIGITS { $$ = new Complex(nullptr, 0, false, $2.address, $2.length, nullptr, 0, false); } | DIGITS DOT DIGITS { $$ = new Complex($1.address, $1.length, false, $3.address, $3.length, nullptr, 0, false); } + | DOT DIGITS EE DIGITS { $$ = new Complex(nullptr, 0, false, $2.address, $2.length, $4.address, $4.length, false); } | DIGITS DOT DIGITS EE DIGITS { $$ = new Complex($1.address, $1.length, false, $3.address, $3.length, $5.address, $5.length, false); } + | DOT DIGITS EE MINUS DIGITS { $$ = new Complex(nullptr, 0, false, $2.address, $2.length, $5.address, $5.length, true); } | DIGITS DOT DIGITS EE MINUS DIGITS { $$ = new Complex($1.address, $1.length, false, $3.address, $3.length, $6.address, $6.length, true); } exp: diff --git a/poincare/test/parser.cpp b/poincare/test/parser.cpp new file mode 100644 index 000000000..4d6eed4b6 --- /dev/null +++ b/poincare/test/parser.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +QUIZ_CASE(poincare_parser) { + GlobalContext globalContext; + Expression * a = Expression::parse("1.2*e^(1)"); + float f =1.2f*M_E; + assert(a->approximate(globalContext) == f); + + a = Expression::parse("e^2*e^(1)"); + f = powf(M_E, 2.0f)*M_E; + assert(a->approximate(globalContext) == f); + + a = Expression::parse("2*3^4+2"); + f = 2.0f*powf(3.0f, 4.0f)+2.0f; + assert(a->approximate(globalContext) == f); + + a = Expression::parse("-2*3^4+2"); + f = -2.0f*powf(3.0f, 4.0f)+2.0f; + assert(a->approximate(globalContext) == f); + + a = Expression::parse("-sin(3)*2-3"); + f = -sinf(3.0f)*2.0f-3.0f; + assert(a->approximate(globalContext) == f); + + a = Expression::parse("-.003"); + f = -0.003f; + assert(a->approximate(globalContext) == f); + + char text[10] = {'.', '0', '2', 'E', '2', 0}; + a = Expression::parse(text); + f = 2.0f; + assert(a->approximate(globalContext) == f); +} \ No newline at end of file