Commits

Anonymous committed 8aec7c4

keep highest precision on numbers

Comments (0)

Files changed (12)

Cauchy/AST/Expression.cpp

 
 // NumberExpression
 
-namespace Cauchy { // GCC or C++ or both sucks and it is required for the template to be defined
-  namespace AST {
-
-template<>
-ExpressionResultSP NumberExpression<float>::generateValue( GenerationVisitor* _generationVisitor) const
+ExpressionResultSP NumberExpression::generateValue(GenerationVisitor* _generationVisitor) const
 {
-  return _generationVisitor->generateFloat32( m_val, annotation() );
+  return _generationVisitor->generateNumber(m_val, m_numberType, annotation());
 }
 
-template<>
-const Cauchy::Type* NumberExpression<float>::type() const
+const Cauchy::Type* NumberExpression::type() const
 {
-  return Type::defaultType();
+  return Cauchy::Type::defaultType();
 }
 
-template<>
-ExpressionResultSP NumberExpression<std::complex<double> >::generateValue( GenerationVisitor* _generationVisitor) const
+ExpressionResultSP ComplexNumberExpression::generateValue(GenerationVisitor* _generationVisitor) const
 {
-  return _generationVisitor->generateComplex( m_val, annotation() );
+  return _generationVisitor->generateComplexNumber(m_real, m_imag, m_numberType, annotation());
 }
 
-template<>
-const Cauchy::Type* NumberExpression<std::complex<double> >::type() const
+const Cauchy::Type* ComplexNumberExpression::type() const
 {
-  return Type::complexType(Type::defaultType());
-}
-
-template<>
-ExpressionResultSP NumberExpression<cauchy_int32>::generateValue( GenerationVisitor* _generationVisitor) const
-{
-  return _generationVisitor->generateInteger32( m_val, annotation() );
-}
-
-template<>
-const Cauchy::Type* NumberExpression<cauchy_int32>::type() const
-{
-  return Type::defaultType();
-}
-
-template<>
-ExpressionResultSP NumberExpression<bool>::generateValue( GenerationVisitor* _generationVisitor) const
-{
-  return _generationVisitor->generateBoolean( m_val, annotation() );
-}
-
-template<>
-const Type* NumberExpression<bool>::type() const
-{
-  return Type::Logical;
-}
-
-  }
+  return Cauchy::Type::complexType(Cauchy::Type::defaultType());
 }
 
 ExpressionResultSP StringExpression::generateValue( GenerationVisitor* _generationVisitor) const

Cauchy/AST/Expression.h

      * This represent a constant value in the tree. _T_ can be a float, int or bool.
      * @ingroup Cauchy_AST
      */
-    template<typename _T_>
     class NumberExpression : public Expression {
       public:
-        explicit NumberExpression(_T_ val) : m_val(val) { }
-        _T_ value() const { return m_val; }
+        explicit NumberExpression(const String& val, Type::DataType _type) : m_val(val), m_numberType(_type) { }
+        String value() const { return m_val; }
         virtual ExpressionResultSP generateValue( GenerationVisitor* _generationVisitor) const;
         virtual const Type* type() const;
       private:
-        _T_ m_val;
+        String m_val;
+        Type::DataType m_numberType;
+    };
+    class ComplexNumberExpression : public Expression {
+      public:
+        explicit ComplexNumberExpression(const String& real, const String& imag, Type::DataType _type) : m_real(real), m_imag(imag), m_numberType(_type) { }
+        virtual ExpressionResultSP generateValue( GenerationVisitor* _generationVisitor) const;
+        virtual const Type* type() const;
+      private:
+        String m_real, m_imag;
+        Type::DataType m_numberType;
     };
     
     /**

Cauchy/AST/GenerationVisitor.h

 #include <vector>
 #include "ExpressionResult.h"
 #include "Cauchy/StdTypes.h"
+#include "Cauchy/Type.h"
 
 namespace std {
 template<class _Tp >
         /**
          * Generate a float 32 from @p arg1
          */
-        virtual ExpressionResultSP generateFloat32(float arg1, const Annotation& _annotation) = 0;
-        virtual ExpressionResultSP generateComplex(std::complex<double> arg1, const Annotation& arg2) = 0;
-        /**
-         * Generate an integer 32 from @p arg1
-         */
-        virtual ExpressionResultSP generateInteger32(cauchy_int32 arg1, const Annotation& _annotation) = 0;
+        virtual ExpressionResultSP generateNumber(const String& arg1, Type::DataType _type, const Annotation& _annotation) = 0;
+        virtual ExpressionResultSP generateComplexNumber(const String& _real, const String& _imag, Type::DataType _type, const Annotation& _annotation) = 0;
         /**
          * Generate a boolean from @p arg1
          */

Cauchy/DeclarationsRegistry.cpp

   std::map< String, VariableDeclaration* > globals;
   std::list< String > searchpathes;
 };
-#include <QtGlobal>
+
 DeclarationsRegistry::DeclarationsRegistry() : d(new Private)
 {
-  Q_UNUSED(d);
 #ifdef _CAUCHY_INCLUDE_DIR_
   addSearchPath(_CAUCHY_INCLUDE_DIR_);
 #endif

Cauchy/EigenBackend/GenerationVisitor.cpp

   }
 }
 
-AST::ExpressionResultSP GenerationVisitor::generateFloat32(float arg1, const AST::Annotation& /*_annotation*/)
+AST::ExpressionResultSP GenerationVisitor::generateNumber(const Cauchy::String& arg1, Cauchy::Type::DataType _type, const AST::Annotation& _annotation)
 {
-  return new ExpressionResult(Cauchy::String::number(arg1), Cauchy::Type::Double);
+  if(_type == Cauchy::Type::INTEGER)
+  {
+    return new ExpressionResult(arg1, Cauchy::Type::Int32);
+  } else {
+    return new ExpressionResult(arg1, Cauchy::Type::Double);
+  }
 }
 
-AST::ExpressionResultSP GenerationVisitor::generateComplex(std::complex<double> arg1, const AST::Annotation& /*_annotation*/)
+
+AST::ExpressionResultSP GenerationVisitor::generateComplexNumber(const Cauchy::String& _real, const Cauchy::String& _imag, Cauchy::Type::DataType _type, const AST::Annotation& _annotation)
 {
-  Cauchy::String r = "std::complex<double>(" + Cauchy::String::number(arg1.real()) + ", " + Cauchy::String::number(arg1.imag()) + ")";
-  return new ExpressionResult(r, Cauchy::Type::Double);
-}
-
-AST::ExpressionResultSP GenerationVisitor::generateInteger32(cauchy_int32 arg1, const AST::Annotation& /*_annotation*/)
-{
-  return new ExpressionResult(Cauchy::String::number(arg1), Cauchy::Type::Int32);
+  Cauchy::String r = "std::complex<double>(" + _real + ", " + _imag + ")";
+  if(_type == Cauchy::Type::INTEGER)
+  {
+    return new ExpressionResult(r, Cauchy::Type::Int32);
+  } else {
+    return new ExpressionResult(r, Cauchy::Type::Double);
+  }
 }
 
 AST::ExpressionResultSP GenerationVisitor::generateBoolean(bool arg1, const AST::Annotation& /*_annotation*/)

Cauchy/EigenBackend/GenerationVisitor.h

         virtual void startFunction(const Cauchy::AST::FunctionDefinition* );
         virtual void declareGlobal(Cauchy::Variable* global);
     public:
-      virtual Cauchy::AST::ExpressionResultSP generateFloat32(float arg1, const Cauchy::AST::Annotation& _annotation);
-      virtual Cauchy::AST::ExpressionResultSP generateComplex(std::complex< double > arg1, const Cauchy::AST::Annotation&);
-      virtual Cauchy::AST::ExpressionResultSP generateInteger32(cauchy_int32 arg1, const Cauchy::AST::Annotation& _annotation);
+      virtual Cauchy::AST::ExpressionResultSP generateNumber(const Cauchy::String& arg1, Cauchy::Type::DataType _type, const Cauchy::AST::Annotation& _annotation);
+      virtual Cauchy::AST::ExpressionResultSP generateComplexNumber(const Cauchy::String& _real, const Cauchy::String& _imag, Cauchy::Type::DataType _type, const Cauchy::AST::Annotation& _annotation);
       virtual Cauchy::AST::ExpressionResultSP generateBoolean(bool arg1, const Cauchy::AST::Annotation& _annotation);
       virtual Cauchy::AST::ExpressionResultSP generateMatrixExpression(const Cauchy::Type* _type, int size1, int size2, const std::list< Cauchy::AST::ExpressionResultSP >& results, const Cauchy::AST::Annotation&);
       virtual Cauchy::AST::ExpressionResultSP generateVariable(Cauchy::Variable* var, Cauchy::AST::ExpressionResultSP idx1, Cauchy::AST::ExpressionResultSP idx2, const Cauchy::AST::Annotation& annotation);
   }
   if(complex)
   {
-    return Token( identifierStr.toFloat(), true, line(), initial_col );
+    return Token( Token::COMPLEX_CONSTANT, identifierStr.toFloat(), line(), initial_col );
   } else if(integer)
   {
     unget();
-    return Token( identifierStr.toInt(), line(), initial_col );
+    return Token( Token::INTEGER_CONSTANT, identifierStr.toInt(), line(), initial_col );
   } else {
     unget();
-    return Token( identifierStr.toFloat(), false, line(), initial_col );
+    return Token( Token::FLOAT_CONSTANT, identifierStr.toFloat(), line(), initial_col );
   }
 }
 

Cauchy/MathMLBackend/GenerationVisitor.cpp

   // We do not care about that in the MathML generator
 }
 
-Cauchy::AST::ExpressionResultSP GenerationVisitor::generateFloat32(float arg1, const Cauchy::AST::Annotation& /*_annotation*/)
+Cauchy::AST::ExpressionResultSP GenerationVisitor::generateNumber(const Cauchy::String& arg1, Cauchy::Type::DataType _type, const Cauchy::AST::Annotation& _annotation)
 {
-  return new ExpressionResult("<mn>" + Cauchy::String::number(arg1) + "</mn>");
+  return new ExpressionResult("<mn>" + arg1 + "</mn>");
 }
 
-Cauchy::AST::ExpressionResultSP GenerationVisitor::generateComplex(std::complex< double > arg1, const Cauchy::AST::Annotation&)
+Cauchy::AST::ExpressionResultSP GenerationVisitor::generateComplexNumber(const Cauchy::String& _real, const Cauchy::String& _imag, Cauchy::Type::DataType _type, const Cauchy::AST::Annotation& _annotation)
 {
-  return new ExpressionResult("<mn>" + Cauchy::String::number(arg1.real()) + "</mn><mo>+</mo><mn>"
-                              + Cauchy::String::number(arg1.imag()) + "</mn><mo>&InvisibleTimes;</mo><mi>i</mi>");
-}
-
-Cauchy::AST::ExpressionResultSP GenerationVisitor::generateInteger32(cauchy_int32 arg1, const Cauchy::AST::Annotation& /*_annotation*/)
-{
-  return new ExpressionResult("<mn>" + Cauchy::String::number(arg1) + "</mn>");
+  return new ExpressionResult("<mn>" + _real + "</mn><mo>+</mo><mn>"
+                              + _imag + "</mn><mo>&InvisibleTimes;</mo><mi>i</mi>");
 }
 
 Cauchy::AST::ExpressionResultSP GenerationVisitor::generateBoolean(bool arg1, const Cauchy::AST::Annotation& /*_annotation*/)

Cauchy/MathMLBackend/GenerationVisitor.h

         virtual void startFunction(const Cauchy::AST::FunctionDefinition* );
         virtual void declareGlobal(Cauchy::Variable* global);
     public:
-      virtual Cauchy::AST::ExpressionResultSP generateFloat32(float arg1, const Cauchy::AST::Annotation& _annotation);
-      virtual Cauchy::AST::ExpressionResultSP generateComplex(std::complex< double > arg1, const Cauchy::AST::Annotation&);
-      virtual Cauchy::AST::ExpressionResultSP generateInteger32(cauchy_int32 arg1, const Cauchy::AST::Annotation& _annotation);
+      virtual Cauchy::AST::ExpressionResultSP generateNumber(const Cauchy::String& arg1, Cauchy::Type::DataType _type, const Cauchy::AST::Annotation& _annotation);
+      virtual Cauchy::AST::ExpressionResultSP generateComplexNumber(const Cauchy::String& _real, const Cauchy::String& _imag, Cauchy::Type::DataType _type, const Cauchy::AST::Annotation& _annotation);
       virtual Cauchy::AST::ExpressionResultSP generateBoolean(bool arg1, const Cauchy::AST::Annotation& _annotation);
       virtual Cauchy::AST::ExpressionResultSP generateMatrixExpression(const Cauchy::Type* _type, int size1, int size2, const std::list< Cauchy::AST::ExpressionResultSP >& results, const Cauchy::AST::Annotation&);
       virtual Cauchy::AST::ExpressionResultSP generateVariable(Cauchy::Variable* var, Cauchy::AST::ExpressionResultSP idx1, Cauchy::AST::ExpressionResultSP idx2, const Cauchy::AST::Annotation& annotation);

Cauchy/Parser.cpp

   {
     case Token::INTEGER_CONSTANT:
     {
-      int v = d->currentToken.i;
       getNextToken();
-      return new AST::NumberExpression<cauchy_int32>( v );
+      return new AST::NumberExpression( d->currentToken.string, Type::INTEGER );
     }
     case Token::FLOAT_CONSTANT:
     {
-      float v = d->currentToken.f;
       getNextToken();
-      return new AST::NumberExpression<float>( v );
+      return new AST::NumberExpression( d->currentToken.string, Type::DOUBLE );
     }
     case Token::COMPLEX_CONSTANT:
     {
-      float v = d->currentToken.f;
       getNextToken();
-      return new AST::NumberExpression<std::complex<double> >( std::complex<double>(0, v) );
+      return new AST::ComplexNumberExpression( "0", d->currentToken.string, Cauchy::Type::DOUBLE );
     }
     case Token::STRING_CONSTANT:
     {

Cauchy/Token_p.cpp

   CAUCHY_ASSERT( _type == STRING_CONSTANT or _type == IDENTIFIER or _type == COMMENT );
 }
 
-Token::Token(int _i, int _line, int _column) : type(INTEGER_CONSTANT), line(_line), column(_column), i(_i)
-{
-}
-
-Token::Token(float _f, bool complex, int _line, int _column) : type(complex ? COMPLEX_CONSTANT : FLOAT_CONSTANT), line(_line), column(_column), f(_f)
-{
-}
-
 bool Token::isConstant() const
 {
   return type == Token::INTEGER_CONSTANT or type == Token::FLOAT_CONSTANT or type == Token::COMPLEX_CONSTANT or type == Token::STRING_CONSTANT;
 #define _CAUCHY_TOKEN_H_
 
 #include <Cauchy/String.h>
+#include "Type.h"
 #include <sstream>
 
 namespace Cauchy {
     int column;
     /// String or identifier name
     String string;
-    /// Integer value
-    int i;
-    /// Floating point value
-    float f;
     Token();
     /**
       * Creates a token of the given type
       */
     Token(Type _type, int _line, int _column);
     /**
-      * Creates an identifier or a string constant
+      * Creates an identifier or a string constant or a number constant
       */
     Token(Type _type, const String& _string, int _line, int _column);
-    /**
-      * Creates an integer constant
-      */
-    Token(int _i, int _line, int _column);
-    /**
-      * Creates a floating point constant
-      */
-    Token(float _f, bool complex, int _line, int _column);
     bool isExpressionTerminal();
     bool isConstant() const;
     bool isBinaryOperator() const;