mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
Poincare: Put the AST printing in its own file
Change-Id: I1793fa2057098f31a549faf6bf39e64c333b5b06
This commit is contained in:
@@ -9,9 +9,6 @@ class Addition : public CommutativeOperation {
|
||||
Type type() override;
|
||||
float operateApproximatevelyOn(float a, float b) override;
|
||||
Expression * clone() override;
|
||||
#ifdef DEBUG
|
||||
int getPrintableVersion(char* txt) override;
|
||||
#endif
|
||||
protected:
|
||||
char operatorChar() override;
|
||||
};
|
||||
|
||||
@@ -9,9 +9,6 @@ class Cosine : public Function {
|
||||
float approximate(Context& context) override;
|
||||
Type type() override;
|
||||
Expression * clone() override;
|
||||
#ifdef DEBUG
|
||||
int getPrintableVersion(char* txt) override;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,12 +4,6 @@
|
||||
#include <poincare/expression_layout.h>
|
||||
#include <kandinsky.h>
|
||||
|
||||
#ifdef DEBUG
|
||||
#define PRINT_AST(e) e->__printAst(0)
|
||||
#else
|
||||
#define PRINT_AST(e) ((void)0)
|
||||
#endif
|
||||
|
||||
class Context;
|
||||
|
||||
class Expression {
|
||||
@@ -48,13 +42,6 @@ class Expression {
|
||||
virtual Type type() = 0;
|
||||
|
||||
virtual float approximate(Context& context) = 0;
|
||||
/*private:
|
||||
void forEachChild(ExpressionAction);
|
||||
*/
|
||||
#ifdef DEBUG
|
||||
virtual int getPrintableVersion(char* txt) = 0;
|
||||
void __printAst(int level=0);
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -13,9 +13,6 @@ class Float : public LeafExpression {
|
||||
Type type() override;
|
||||
Expression * clone() override;
|
||||
bool valueEquals(Expression * e) override;
|
||||
#ifdef DEBUG
|
||||
int getPrintableVersion(char* txt) override;
|
||||
#endif
|
||||
private:
|
||||
float m_float;
|
||||
};
|
||||
|
||||
@@ -10,9 +10,6 @@ class Fraction : public BinaryOperation {
|
||||
float approximate(Context& context) override;
|
||||
Type type() override;
|
||||
Expression * clone() override;
|
||||
#ifdef DEBUG
|
||||
int getPrintableVersion(char* txt) override;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -46,9 +46,6 @@ class Integer : public LeafExpression {
|
||||
uint16_t m_numberOfDigits; // In base native_uint_max
|
||||
bool m_negative;
|
||||
|
||||
#ifdef DEBUG
|
||||
int getPrintableVersion(char* txt) override;
|
||||
#endif
|
||||
/*
|
||||
// TODO: Small-int optimization
|
||||
union {
|
||||
|
||||
@@ -10,9 +10,6 @@ class Power : public BinaryOperation {
|
||||
float approximate(Context& context) override;
|
||||
Type type() override;
|
||||
Expression * clone() override;
|
||||
#ifdef DEBUG
|
||||
int getPrintableVersion(char* txt) override;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -9,9 +9,6 @@ class Product : public CommutativeOperation {
|
||||
Type type() override;
|
||||
float operateApproximatevelyOn(float a, float b) override;
|
||||
Expression * clone() override;
|
||||
#ifdef DEBUG
|
||||
int getPrintableVersion(char* txt) override;
|
||||
#endif
|
||||
protected:
|
||||
char operatorChar() override;
|
||||
};
|
||||
|
||||
@@ -9,9 +9,6 @@ class Sine : public Function {
|
||||
float approximate(Context& context) override;
|
||||
Type type() override;
|
||||
Expression * clone() override;
|
||||
#ifdef DEBUG
|
||||
int getPrintableVersion(char* txt) override;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,9 +10,6 @@ class Subtraction : public BinaryOperation {
|
||||
float approximate(Context& context) override;
|
||||
Type type() override;
|
||||
Expression * clone() override;
|
||||
#ifdef DEBUG
|
||||
int getPrintableVersion(char* txt) override;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -12,9 +12,6 @@ class Symbol : public LeafExpression {
|
||||
Type type() override;
|
||||
Expression * clone() override;
|
||||
bool valueEquals(Expression * e) override;
|
||||
#ifdef DEBUG
|
||||
int getPrintableVersion(char* txt) override;
|
||||
#endif
|
||||
private:
|
||||
char * m_name;
|
||||
};
|
||||
|
||||
@@ -9,9 +9,6 @@ class Tangent : public Function {
|
||||
float approximate(Context& context) override;
|
||||
Type type() override;
|
||||
Expression * clone() override;
|
||||
#ifdef DEBUG
|
||||
int getPrintableVersion(char* txt) override;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -16,10 +16,3 @@ float Addition::operateApproximatevelyOn(float a, float b) {
|
||||
char Addition::operatorChar() {
|
||||
return '+';
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
int Addition::getPrintableVersion(char* txt) {
|
||||
txt[0] = '+';
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -13,13 +13,3 @@ float Cosine::approximate(Context& context) {
|
||||
// FIXME: use cosine obviously.
|
||||
return m_arg->approximate(context);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
int Cosine::getPrintableVersion(char* txt) {
|
||||
const char* printable = "cos";
|
||||
for(int i=0; printable[i]!='\0'; i++) {
|
||||
txt[i] = printable[i];
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -7,11 +7,6 @@ extern "C" {
|
||||
|
||||
#include "simplify/simplification_rules.h"
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#endif
|
||||
|
||||
int poincare_expression_yyparse(yyscan_t scanner, Expression ** expressionOutput);
|
||||
|
||||
Expression::~Expression() {
|
||||
@@ -38,8 +33,6 @@ Expression * Expression::simplify() {
|
||||
const Simplification * simplification = (simplifications + i); // Pointer arithmetics
|
||||
Expression * simplified = simplification->simplify(result);
|
||||
if (simplified != nullptr) {
|
||||
std::cout << "simplification " << i << " returned a non null pointer" << std::endl;
|
||||
PRINT_AST(simplified);
|
||||
simplification_pass_was_useful = true;
|
||||
if (result != this) {
|
||||
delete result;
|
||||
@@ -70,18 +63,3 @@ bool Expression::valueEquals(Expression * e) {
|
||||
* -riden. */
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
void Expression::__printAst(int level) {
|
||||
char txt[255];
|
||||
for (int i(0); i<level; i++) {
|
||||
txt[i] = ' ';
|
||||
}
|
||||
int offset = getPrintableVersion(&txt[level]);
|
||||
txt[level + offset] = '\0';
|
||||
std::cout << txt << std::endl;
|
||||
for (int i(0); i < numberOfOperands(); i++) {
|
||||
operand(i)->__printAst(level + 2);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
54
poincare/src/expression_debug.cpp
Normal file
54
poincare/src/expression_debug.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <poincare/expression.h>
|
||||
#include <poincare/context.h>
|
||||
#include <iostream>
|
||||
|
||||
void print_expression(Expression * e, int indentationLevel) {
|
||||
if (indentationLevel>0) {
|
||||
for (int i=0; i<indentationLevel-1; i++) {
|
||||
std::cout << " ";
|
||||
}
|
||||
std::cout << "|-";
|
||||
}
|
||||
Context context;
|
||||
switch (e->type()) {
|
||||
case Expression::Type::Addition:
|
||||
std::cout << "Addition";
|
||||
break;
|
||||
case Expression::Type::Cosine:
|
||||
std::cout << "Cosine";
|
||||
break;
|
||||
case Expression::Type::Float:
|
||||
std::cout << "Float()";
|
||||
break;
|
||||
case Expression::Type::Integer:
|
||||
std::cout << "Integer(";
|
||||
std::cout << e->approximate(context);
|
||||
std::cout << ")";
|
||||
break;
|
||||
case Expression::Type::Fraction:
|
||||
std::cout << "Fraction";
|
||||
break;
|
||||
case Expression::Type::Power:
|
||||
std::cout << "Power";
|
||||
break;
|
||||
case Expression::Type::Product:
|
||||
std::cout << "Product";
|
||||
break;
|
||||
case Expression::Type::Sine:
|
||||
std::cout << "Sine";
|
||||
break;
|
||||
case Expression::Type::Subtraction:
|
||||
std::cout << "Subtraction";
|
||||
break;
|
||||
case Expression::Type::Symbol:
|
||||
std::cout << "Symbol";
|
||||
break;
|
||||
case Expression::Type::Tangent:
|
||||
std::cout << "Tangent";
|
||||
break;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
for (int i=0; i<e->numberOfOperands(); i++) {
|
||||
print_expression(e->operand(i), indentationLevel+1);
|
||||
}
|
||||
}
|
||||
3
poincare/src/expression_debug.h
Normal file
3
poincare/src/expression_debug.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#include <poincare/expression.h>
|
||||
|
||||
void print_expression(Expression * e, int indentationLevel = 0);
|
||||
@@ -33,13 +33,3 @@ bool Float::valueEquals(Expression * e) {
|
||||
assert(e->type() == Expression::Type::Float);
|
||||
return m_float == ((Float *)e)->m_float;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
int Float::getPrintableVersion(char* txt) {
|
||||
const char* printable = "float number";
|
||||
for(int i=0; printable[i]!='\0'; i++) {
|
||||
txt[i] = printable[i];
|
||||
}
|
||||
return strlen(printable);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -18,10 +18,3 @@ float Fraction::approximate(Context& context) {
|
||||
Expression::Type Fraction::type() {
|
||||
return Expression::Type::Fraction;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
int Fraction::getPrintableVersion(char* txt) {
|
||||
txt[0] = '/';
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -344,30 +344,6 @@ ExpressionLayout * Integer::createLayout() {
|
||||
return new StringLayout(buffer, size);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
int Integer::getPrintableVersion(char* txt) {
|
||||
Integer base = Integer(10);
|
||||
Division d = Division(*this, base);
|
||||
int size = 0;
|
||||
while (!(d.m_remainder == Integer((native_int_t)0))) {
|
||||
assert(size<255); //TODO: malloc an extra buffer
|
||||
char c = char_from_digit(d.m_remainder.m_digits[0]);
|
||||
txt[size++] = c;
|
||||
d = Division(d.m_quotient, base);
|
||||
}
|
||||
txt[size] = 0;
|
||||
|
||||
// Flip the string
|
||||
for (int i=0, j=size-1 ; i < j ; i++, j--) {
|
||||
char c = txt[i];
|
||||
txt[i] = txt[j];
|
||||
txt[j] = c;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool Integer::valueEquals(Expression * e) {
|
||||
assert(e->type() == Expression::Type::Integer);
|
||||
return (*this == *(Integer *)e); // FIXME: Remove operator overloading
|
||||
|
||||
@@ -17,10 +17,3 @@ Expression::Type Power::type() {
|
||||
ExpressionLayout * Power::createLayout() {
|
||||
return new ExponentLayout(m_operands[0]->createLayout(), m_operands[1]->createLayout());
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
int Power::getPrintableVersion(char* txt) {
|
||||
txt[0] = '^';
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
#include <poincare/integer.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
int __print_ast(Expression* expression, int offset) {
|
||||
// TODO print my current node.
|
||||
char txt[offset+1] = {' '};
|
||||
text[offset] = '\0';
|
||||
cout << txt;
|
||||
|
||||
// Print my children.
|
||||
for (int i(0); i<expression->m_operands(); i++) {
|
||||
int tmp = __print_ast(expression->operand(i), offset);
|
||||
offset += tmp;
|
||||
}
|
||||
|
||||
return 3;
|
||||
}
|
||||
@@ -19,10 +19,3 @@ Expression::Type Product::type() {
|
||||
char Product::operatorChar() {
|
||||
return '*';
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
int Product::getPrintableVersion(char* txt) {
|
||||
txt[0] = '*';
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -13,13 +13,3 @@ float Sine::approximate(Context& context) {
|
||||
// FIXME: use sine obviously.
|
||||
return m_arg->approximate(context);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
int Sine::getPrintableVersion(char* txt) {
|
||||
const char* printable = "sin";
|
||||
for(int i=0; printable[i]!='\0'; i++) {
|
||||
txt[i] = printable[i];
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -25,10 +25,3 @@ ExpressionLayout * Subtraction::createLayout() {
|
||||
children_layouts[2] = m_operands[1]->createLayout();
|
||||
return new HorizontalLayout(children_layouts, 3);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
int Subtraction::getPrintableVersion(char* txt) {
|
||||
txt[0] = '-';
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -38,12 +38,3 @@ bool Symbol::valueEquals(Expression * e) {
|
||||
assert(e->type() == Expression::Type::Symbol);
|
||||
return (strcmp(m_name, ((Symbol *)e)->m_name) == 0);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
int Symbol::getPrintableVersion(char* txt) {
|
||||
for(int i=0; m_name[i]!='\0'; i++) {
|
||||
txt[i] = m_name[i];
|
||||
}
|
||||
return strlen(m_name);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -13,13 +13,3 @@ float Tangent::approximate(Context& context) {
|
||||
// FIXME: use tangent obviously.
|
||||
return m_arg->approximate(context);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
int Tangent::getPrintableVersion(char* txt) {
|
||||
const char* printable = "tan";
|
||||
for(int i=0; printable[i]!='\0'; i++) {
|
||||
txt[i] = printable[i];
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -2,39 +2,45 @@
|
||||
#include <poincare.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef DEBUG
|
||||
#define DUMP_EXPRESSIONS_TO_STD_OUT 1
|
||||
#if DUMP_EXPRESSIONS_TO_STD_OUT
|
||||
#include "../src/expression_debug.h"
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#endif
|
||||
|
||||
void assert_simplifies_to(const char * input_string, const char * expected_string) {
|
||||
#if DUMP_EXPRESSIONS_TO_STD_OUT
|
||||
cout << "---- Simplification Run ----" << endl;
|
||||
cout << input_string << " -> " << expected_string << endl;
|
||||
#endif
|
||||
//Expression* tab[3] = {new Integer(1), new Integer(2), new Integer(3)};
|
||||
//Expression* input = new Addition(tab, 3, false);
|
||||
Expression * input = Expression::parse(input_string);
|
||||
assert(input != nullptr);
|
||||
#ifdef DEBUG
|
||||
#if DUMP_EXPRESSIONS_TO_STD_OUT
|
||||
cout << "Input = " << endl;
|
||||
PRINT_AST(input);
|
||||
print_expression(input);
|
||||
#endif
|
||||
|
||||
Expression * simplified = input->simplify();
|
||||
assert(simplified != nullptr);
|
||||
#ifdef DEBUG
|
||||
#if DUMP_EXPRESSIONS_TO_STD_OUT
|
||||
cout << "Simplified = " << endl;
|
||||
PRINT_AST(simplified);
|
||||
print_expression(simplified);
|
||||
#endif
|
||||
|
||||
Expression * expected = Expression::parse(expected_string);
|
||||
assert(expected != nullptr);
|
||||
#ifdef DEBUG
|
||||
#if DUMP_EXPRESSIONS_TO_STD_OUT
|
||||
cout << "Expected = " << endl;
|
||||
PRINT_AST(expected);
|
||||
print_expression(expected);
|
||||
#endif
|
||||
|
||||
assert(simplified->isIdenticalTo(expected));
|
||||
delete expected;
|
||||
delete simplified;
|
||||
delete input;
|
||||
#ifdef DEBUG
|
||||
cout << "----" << endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
QUIZ_CASE(poincare_simplify_addition_integer) {
|
||||
|
||||
Reference in New Issue
Block a user