Adds the power and parentheses to the parser.

Also removes some dead code and adds buttons in order to have parentehses.

Change-Id: Iaf6d678f765bb6a1c3405818738c51661aa43e12
This commit is contained in:
Felix Raimundo
2016-03-16 11:52:54 +01:00
committed by Félix Raimundo
parent 15d91052f2
commit b33eb99067
7 changed files with 27 additions and 109 deletions

View File

@@ -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'

View File

@@ -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)

View File

@@ -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

View File

@@ -53,6 +53,8 @@
\* { return(MULTIPLY); }
\^ { return(POW); }
\+ { return(PLUS); }
\( { return(LEFT_PARENTHESIS); }
\) { return(RIGHT_PARENTHESIS); }
[A-Za-z]+ { yylval->string = yytext; return(SYMBOL); }
%%

View File

@@ -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 <expression> exp;
/*
//Expression * expression;
//CSSSelector * selector;
%union {
int value;
char * string;
}
%token
%right <string> COMBINATOR
%token <string> IDENT
%token HASH
%token DOT
%type <selector> 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);
}
*/

View File

@@ -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)

View File

@@ -1,35 +1,21 @@
#include <poincare/power.h>
#include <string.h>
#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);
}