From 745e06ddd2d7e240f6757e5f9679bca49013d266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Thu, 9 Aug 2018 17:07:19 +0200 Subject: [PATCH] [poincare] Fix parser --- poincare/include/poincare.h | 17 ++++++++++++++++ poincare/include/poincare/addition.h | 5 ++++- poincare/include/poincare/symbol.h | 2 +- poincare/src/expression_lexer.l | 13 ++++++++++--- poincare/src/expression_parser.y | 29 ++++++++++++++++++++++------ 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/poincare/include/poincare.h b/poincare/include/poincare.h index 28867d640..ddb662ac8 100644 --- a/poincare/include/poincare.h +++ b/poincare/include/poincare.h @@ -105,6 +105,23 @@ #include #else +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #include #endif diff --git a/poincare/include/poincare/addition.h b/poincare/include/poincare/addition.h index 968ba86be..128d67b55 100644 --- a/poincare/include/poincare/addition.h +++ b/poincare/include/poincare/addition.h @@ -74,7 +74,10 @@ class Addition : public NAryExpression { public: Addition(const AdditionNode * n) : NAryExpression(n) {} Addition() : NAryExpression(TreePool::sharedPool()->createTreeNode()) {} - + Addition(Expression e1, Expression e2) : Addition() { + addChildAtIndexInPlace(e2, 0, 0); + addChildAtIndexInPlace(e1, 0, numberOfChildren()); + } // Expression Expression shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const; Expression shallowBeautify(Context& context, Preferences::AngleUnit angleUnit) const; diff --git a/poincare/include/poincare/symbol.h b/poincare/include/poincare/symbol.h index aad16dd38..8ea26a60d 100644 --- a/poincare/include/poincare/symbol.h +++ b/poincare/include/poincare/symbol.h @@ -88,7 +88,7 @@ public: X3, Y3 = 29 }; - Symbol(const char name) : Expression(TreePool::sharedPool()->createTreeNode()) { + Symbol(const char name = 0) : Expression(TreePool::sharedPool()->createTreeNode()) { node()->setName(name); } Symbol(const SymbolNode * node) : Expression(node) {} diff --git a/poincare/src/expression_lexer.l b/poincare/src/expression_lexer.l index a5e2c4836..890f18264 100644 --- a/poincare/src/expression_lexer.l +++ b/poincare/src/expression_lexer.l @@ -105,10 +105,13 @@ X2 { poincare_expression_yylval.character = Symbol::SpecialSymbols::X2; return S Y2 { poincare_expression_yylval.character = Symbol::SpecialSymbols::Y2; return SYMBOL; } X3 { poincare_expression_yylval.character = Symbol::SpecialSymbols::X3; return SYMBOL; } Y3 { poincare_expression_yylval.character = Symbol::SpecialSymbols::Y3; return SYMBOL; } + /* acos { poincare_expression_yylval.expression = new ArcCosine(); return FUNCTION; } acosh { poincare_expression_yylval.expression = new HyperbolicArcCosine(); return FUNCTION; } abs { poincare_expression_yylval.expression = new AbsoluteValue(); return FUNCTION; } +*/ ans { poincare_expression_yylval.character = Symbol::SpecialSymbols::Ans; return SYMBOL; } + /* arg { poincare_expression_yylval.expression = new ComplexArgument(); return FUNCTION; } asin { poincare_expression_yylval.expression = new ArcSine(); return FUNCTION; } asinh { poincare_expression_yylval.expression = new HyperbolicArcSine(); return FUNCTION; } @@ -151,11 +154,12 @@ tan { poincare_expression_yylval.expression = new Tangent(); return FUNCTION; } tanh { poincare_expression_yylval.expression = new HyperbolicTangent(); return FUNCTION; } trace { poincare_expression_yylval.expression = new MatrixTrace(); return FUNCTION; } transpose { poincare_expression_yylval.expression = new MatrixTranspose(); return FUNCTION; } -undef { poincare_expression_yylval.expression = new Undefined(); return UNDEFINED; } -inf { poincare_expression_yylval.expression = new Undefined(); return UNDEFINED; } + */ +undef { poincare_expression_yylval.expression = Undefined(); return UNDEFINED; } +inf { poincare_expression_yylval.expression = Undefined(); return UNDEFINED; } \x8a { poincare_expression_yylval.character = yytext[0]; return SYMBOL; } \x8d { return EE; } -\x91 { poincare_expression_yylval.expression = new SquareRoot(); return FUNCTION; } + /*\x91 { poincare_expression_yylval.expression = new SquareRoot(); return FUNCTION; }*/ \x8c { poincare_expression_yylval.character = yytext[0]; return SYMBOL; } \x8f { poincare_expression_yylval.character = yytext[0]; return SYMBOL; } \x90 { return STO; } @@ -177,7 +181,10 @@ inf { poincare_expression_yylval.expression = new Undefined(); return UNDEFINED; \, { return COMMA; } \. { return DOT; } \_ { return UNDERSCORE; } + /* \x97 { poincare_expression_yylval.expression = new EmptyExpression(); return EMPTY; } + */ + [ ]+ /* Ignore whitespaces */ . { return UNDEFINED_SYMBOL; } diff --git a/poincare/src/expression_parser.y b/poincare/src/expression_parser.y index 55a545a83..185e61094 100644 --- a/poincare/src/expression_parser.y +++ b/poincare/src/expression_parser.y @@ -7,6 +7,18 @@ %{ #include + +struct YYSTYPE { + Poincare::Expression expression; + Poincare::Symbol symbol; + struct { + char * address; + int length; + } string; + char character; +}; + + /* The lexer manipulates tokens defined by the parser, so we need the following * inclusion order. */ #include "expression_parser.hpp" @@ -20,8 +32,9 @@ void poincare_expression_yyerror(Poincare::Expression * expressionOutput, char c * instead we do provide regular memcpy. Let's instruct Bison to use it. */ #define YYCOPY(To, From, Count) memcpy(To, From, (Count)*sizeof(*(From))) -%} +#if 0 +//TODO move comments /* All symbols (both terminals and non-terminals) may have a value associated * with them. In our case, it's going to be either an Expression (for example, @@ -48,6 +61,10 @@ void poincare_expression_yyerror(Poincare::Expression * expressionOutput, char c } string; char character; } +#endif + +%} + /* The INTEGER token uses the "string" part of the union to store its value */ %token DIGITS @@ -129,9 +146,9 @@ void poincare_expression_yyerror(Poincare::Expression * expressionOutput, char c %type exp; %type number; %type symb; -%type lstData; +/* TODO %type lstData; */ /* MATRICES_ARE_DEFINED */ -%type mtxData; +/* TODO %type mtxData; */ /* FIXME: no destructors, Expressions are GCed */ /* During error recovery, some symbols need to be discarded. We need to tell @@ -180,7 +197,7 @@ mtxData: LEFT_BRACKET lstData RIGHT_BRACKET { $$ = new Poincare::MatrixData($2, * of the exponent digits is above 4 (0.00...-256 times-...01E1256=1E1000 is * accepted and 1000-...256times...-0E10000 = 1E10256, 10256 does not overflow * an int32_t). */ -number : DIGITS { $$ = Poincare::Rational(Poincare::Integer($1.address, false)); } +number : DIGITS { $$ = Poincare::Rational(Poincare::Integer($1.address, $1.length, false)); } /* | DOT DIGITS { $$ = Poincare::Decimal(Poincare::Decimal::mantissa(nullptr, 0, $2.address, $2.length, false), Poincare::Decimal::exponent(nullptr, 0, $2.address, $2.length, nullptr, 0, false)); } @@ -220,7 +237,7 @@ $4->detachOperands(); delete $4; $7->detachOperands(); delete $7; arguments->det ; bang : term { $$ = $1; } - | term BANG { $$ = Poincare::Factorial($1); } + /* | term BANG { $$ = Poincare::Factorial($1); }*/ ; factor : bang { $$ = $1; } @@ -237,7 +254,7 @@ exp : pow { $$ = $1; } | exp MULTIPLY exp { $$ = Poincare::Multiplication($1,$3); } | exp MINUS exp { $$ = Poincare::Subtraction($1,$3); } | MINUS exp %prec UNARY_MINUS { $$ = Poincare::Opposite($2); } - | exp PLUS exp { $$ = Poincare::Addition($1;$3); } + | exp PLUS exp { $$ = Poincare::Addition($1,$3); } ; final_exp : exp { $$ = $1; }