diff --git a/ion/src/shared/keyboard.c b/ion/src/shared/keyboard.c index f17ebe790..ebecb25cf 100644 --- a/ion/src/shared/keyboard.c +++ b/ion/src/shared/keyboard.c @@ -5,7 +5,7 @@ char charForKey[ION_NUMBER_OF_KEYS] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', - '7', '8', '9', 'S', 'T', + '7', '8', '9', '(', ')', '4', '5', '6', '*', '/', '1', '2', '3', '+', '-', '0', '.', 'x', '7', 'X' diff --git a/poincare/Makefile b/poincare/Makefile index 42c79640f..d18f442cd 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -1,5 +1,5 @@ SFLAGS += -Ipoincare/include -objs += $(addprefix poincare/src/, addition.o context.o expression.o float.o integer.o fraction.o expression_lexer.o expression_parser.o product.o symbol.o) +objs += $(addprefix poincare/src/, addition.o context.o expression.o float.o integer.o fraction.o expression_lexer.o expression_parser.o power.o product.o symbol.o) objs += $(addprefix poincare/src/layout/, expression_layout.o fraction_layout.o horizontal_layout.o string_layout.o) tests += $(addprefix poincare/test/, integer.cpp) diff --git a/poincare/include/poincare/addition.h b/poincare/include/poincare/addition.h index 0d2ddac4c..7039f59d2 100644 --- a/poincare/include/poincare/addition.h +++ b/poincare/include/poincare/addition.h @@ -10,7 +10,8 @@ class Addition : public Expression { ExpressionLayout * createLayout(ExpressionLayout * parent) override; float approximate(Context& context) override; private: - Expression * m_children[2]; + Expression * m_left; + Expression * m_right; }; #endif diff --git a/poincare/include/poincare/power.h b/poincare/include/poincare/power.h index c8649e393..295067ff3 100644 --- a/poincare/include/poincare/power.h +++ b/poincare/include/poincare/power.h @@ -6,12 +6,12 @@ class Power : public Expression { public: Power(Expression * base, Expression * exponent); - virtual Expression ** children(); - virtual float approximate(); -// protected: - virtual void layout(); + ~Power(); + ExpressionLayout * createLayout(ExpressionLayout * parent) override; + float approximate(Context& context) override; private: - Expression * m_children[3]; + Expression * m_base; + Expression * m_exponent; }; #endif diff --git a/poincare/src/addition.cpp b/poincare/src/addition.cpp index 596af7be0..60781950a 100644 --- a/poincare/src/addition.cpp +++ b/poincare/src/addition.cpp @@ -2,19 +2,19 @@ #include "layout/horizontal_layout.h" Addition::Addition(Expression * first_operand, Expression * second_operand) { - m_children[0] = first_operand; - m_children[1] = second_operand; + m_left = first_operand; + m_right = second_operand; } Addition::~Addition() { - delete m_children[1]; - delete m_children[0]; + delete m_left; + delete m_right; } float Addition::approximate(Context& context) { - return m_children[0]->approximate(context) + m_children[1]->approximate(context); + return m_left->approximate(context) + m_right->approximate(context); } ExpressionLayout * Addition::createLayout(ExpressionLayout * parent) { - return new HorizontalLayout(parent, m_children[0], '+', m_children[1]); + return new HorizontalLayout(parent, m_left, '+', m_right); } diff --git a/poincare/src/expression_lexer.l b/poincare/src/expression_lexer.l index c884d9e3c..b1f2249b1 100644 --- a/poincare/src/expression_lexer.l +++ b/poincare/src/expression_lexer.l @@ -53,6 +53,8 @@ \* { return(MULTIPLY); } \^ { return(POW); } \+ { return(PLUS); } +\( { return(LEFT_PARENTHESIS); } +\) { return(RIGHT_PARENTHESIS); } [A-Za-z]+ { yylval->string = yytext; return(SYMBOL); } %% diff --git a/poincare/src/expression_parser.y b/poincare/src/expression_parser.y index 909ddc484..757316843 100644 --- a/poincare/src/expression_parser.y +++ b/poincare/src/expression_parser.y @@ -63,28 +63,12 @@ void poincare_expression_yyerror(yyscan_t scanner, Expression ** expressionOutpu %token POW %token PLUS +%token LEFT_PARENTHESIS +%token RIGHT_PARENTHESIS + /* The "exp" symbol uses the "expression" part of the union. */ %type exp; -/* - //Expression * expression; - //CSSSelector * selector; -%union { - int value; - char * string; -} - -%token - -%right COMBINATOR -%token IDENT -%token HASH -%token DOT - -%type Expression; -*/ - - %% Root: @@ -98,66 +82,12 @@ exp: | exp DIVIDE exp { $$ = new Fraction($1,$3); } | exp MULTIPLY exp { $$ = new Product($1,$3); } | exp PLUS exp { $$ = new Addition($1,$3); } -/* | exp POW exp { $$ = new Power($1,$3); } */ + | exp POW exp { $$ = new Power($1,$3); } + | LEFT_PARENTHESIS exp RIGHT_PARENTHESIS { $$ = $2 } ; -/* - -Root: - Selector { - *selector = $1; - //printf("ROOT! : %p\n", $1); - } - -Selector: - { - //$$ = new CSSSelector(); - //printf("New selector at %p\n", $$); - } - | IDENT { - //$$ = new CSSSelector(); - //$$->setNameRequirement(*$1); - //delete $1; - //printf("Create NodeSpecWithName %s at %p\n", $1, $$); - } - | Selector DOT IDENT { - //$1->addClassRequirement(*$3); - //delete $3; - //printf("AddClassToNodeSpec %s\n", $3); - } - | Selector HASH IDENT { - //$1->setIdentifierRequirement(*$3); - //delete $3; - //printf("AddIdToNodeSpec : %s\n", $3); - } - | Selector COMBINATOR Selector { - //$3->setParentSelectorWithCombinator($1, awe_css_combinator_from_string(*$2)); - //printf("Combine with \"%s\"\n", $2->c_str()); - //delete $2; - } - ; - -*/ %% void poincare_expression_yyerror(yyscan_t scanner, Expression ** expressionOutput, char const *msg) { // Handle the error! } - -/* - -CSSSelector::Combinator awe_css_combinator_from_string(std::string string) { - for (char& c : string) { - switch (c) { - case '>': - return CSSSelector::Combinator::DIRECT; - } - } - return CSSSelector::Combinator::ANY; -} - -void awe_css_yyerror(yyscan_t scanner, CSSSelector ** selector, char const *msg) { - printf("ERROR %s\n",msg); -} -*/ - diff --git a/poincare/src/integer.cpp b/poincare/src/integer.cpp index 092bcddda..d9d5d38df 100644 --- a/poincare/src/integer.cpp +++ b/poincare/src/integer.cpp @@ -170,7 +170,7 @@ Integer Integer::add(const Integer &other, bool inverse_other_negative) const { if (m_negative == other_negative) { return usum(other, false, m_negative); } else { - /* The signs are different, this is in fact a substraction + /* The signs are different, this is in fact a subtraction * s = this+other = (abs(this)-abs(other) OR abs(other)-abs(this)) * 1/abs(this)>abs(other) : s = sign*udiff(this, other) * 2/abs(other)>abs(this) : s = sign*udiff(other, this) diff --git a/poincare/src/power.cpp b/poincare/src/power.cpp index fa1e9a2d5..e68890198 100644 --- a/poincare/src/power.cpp +++ b/poincare/src/power.cpp @@ -1,35 +1,21 @@ #include -#include +#include "layout/horizontal_layout.h" -#define BASE m_children[0] -#define EXPONENT m_children[1] - -Power::Power(Expression * base, Expression * exponent) { - m_children[0] = base; - m_children[1] = exponent; - m_children[2] = NULL; +Power::Power(Expression * base, Expression * exponent) : + m_base(base), + m_exponent(exponent) { } -Expression ** Power::children() { - return m_children; +Power::~Power() { + delete m_exponent; + delete m_base; } -void Power::layout() { - m_frame.width = BASE->m_frame.width + EXPONENT->m_frame.width; - m_frame.height = BASE->m_frame.height + EXPONENT->m_frame.height; - - BASE->m_frame.origin = { - .x = 0, - .y = EXPONENT->m_frame.height - }; - - EXPONENT->m_frame.origin = { - .x = BASE->m_frame.width, - .y = 0 - }; +float Power::approximate(Context& context) { + // TODO: do this for real + return 1; } -float Power::approximate() { - // TODO: Do it.. - return 1.0f; +ExpressionLayout * Power::createLayout(ExpressionLayout * parent) { + return new HorizontalLayout(parent, m_base, '^', m_exponent); }