Changes in / [3da470c:224e52f]


Ignore:
Location:
src
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r3da470c r224e52f  
    1010// Created On       : Sat May 16 13:17:07 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr  8 15:43:05 2016
    13 // Update Count     : 296
     12// Last Modified On : Sun Mar 13 12:34:38 2016
     13// Update Count     : 272
    1414//
    1515
     
    2222
    2323#include "ParseNode.h"
    24 #include "TypeData.h"
    2524#include "SynTree/Constant.h"
    2625#include "SynTree/Expression.h"
    27 #include "SynTree/Declaration.h"
    2826#include "Common/UnimplementedError.h"
    2927#include "parseutility.h"
     
    874872}
    875873
    876 
    877 CompoundLiteralNode::CompoundLiteralNode( DeclarationNode *type, InitializerNode *kids ) : type( type ), kids( kids ) {}
    878 CompoundLiteralNode::CompoundLiteralNode( const CompoundLiteralNode &other ) : ExpressionNode( other ), type( other.type ), kids( other.kids ) {}
    879 
    880 CompoundLiteralNode::~CompoundLiteralNode() {
    881         delete kids;
    882         delete type;
    883 }
    884 
    885 CompoundLiteralNode *CompoundLiteralNode::clone() const {
    886         return new CompoundLiteralNode( *this );
    887 }
    888 
    889 void CompoundLiteralNode::print( std::ostream &os, int indent ) const {
    890         os << string( indent,' ' ) << "CompoundLiteralNode:" << endl;
    891 
    892         os << string( indent + 2, ' ' ) << "type:" << endl;
    893         if ( type != 0 )
    894                 type->print( os, indent + 4 );
    895 
    896         os << string( indent + 2, ' ' ) << "initialization:" << endl;
    897         if ( kids != 0 )
    898                 kids->printList( os, indent + 4 );
    899 }
    900 
    901 void CompoundLiteralNode::printOneLine( std::ostream &os, int indent ) const {
    902         os << "( ";
    903         if ( type ) type->print( os );
    904         os << ", ";
    905         if ( kids ) kids->printOneLine( os );
    906         os << ") ";
    907 }
    908 
    909 Expression *CompoundLiteralNode::build() const {
    910         Declaration * newDecl = type->build();                          // compound literal type
    911         if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
    912                 return new CompoundLiteralExpr( newDeclWithType->get_type(), kids->build() );
    913         // these types do not have associated type information
    914         } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
    915                 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), kids->build() );
    916         } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
    917                 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), kids->build() );
    918         } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
    919                 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), kids->build() );
    920         } else {
    921                 assert( false );
    922         } // if
    923 }
    924 
    925 
    926874ExpressionNode *flattenCommas( ExpressionNode *list ) {
    927875        if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( list ) ) {
  • src/Parser/ParseNode.h

    r3da470c r224e52f  
    1010// Created On       : Sat May 16 13:28:16 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr  8 16:27:20 2016
    13 // Update Count     : 205
     12// Last Modified On : Wed Mar  2 17:26:35 2016
     13// Update Count     : 190
    1414//
    1515
     
    538538};
    539539
    540 class CompoundLiteralNode : public ExpressionNode {
    541   public:
    542         CompoundLiteralNode( DeclarationNode *type, InitializerNode *kids );
    543         CompoundLiteralNode( const CompoundLiteralNode &type );
    544         ~CompoundLiteralNode();
    545 
    546         virtual CompoundLiteralNode *clone() const;
    547 
    548         DeclarationNode *get_type() const { return type; }
    549         CompoundLiteralNode *set_type( DeclarationNode *t ) { type = t; return this; }
    550 
    551         InitializerNode *get_initializer() const { return kids; }
    552         CompoundLiteralNode *set_initializer( InitializerNode *k ) { kids = k; return this; }
    553 
    554         void print( std::ostream &, int indent = 0 ) const;
    555         void printOneLine( std::ostream &, int indent = 0 ) const;
    556 
    557         virtual Expression *build() const;
    558   private:
    559         DeclarationNode *type;
    560         InitializerNode *kids;
    561 };
    562 
    563540template< typename SynTreeType, typename NodeType >
    564541void buildList( const NodeType *firstNode, std::list< SynTreeType *> &outputList ) {
  • src/Parser/parser.yy

    r3da470c r224e52f  
    1010// Created On       : Sat Sep  1 20:22:55 2001
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr  8 16:21:55 2016
    13 // Update Count     : 1508
     12// Last Modified On : Thu Mar 24 16:16:16 2016
     13// Update Count     : 1498
    1414//
    1515
     
    372372                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::DecrPost ), $1 ); }
    373373        | '(' type_name_no_function ')' '{' initializer_list comma_opt '}' // C99
    374                 { $$ = new CompoundLiteralNode( $2, new InitializerNode( $5, true ) ); }
     374                { $$ = 0; }
    375375        | postfix_expression '{' argument_expression_list '}' // CFA
    376376                {
  • src/SymTab/AddVisit.h

    r3da470c r224e52f  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 16:14:32 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Apr  7 14:42:21 2016
    13 // Update Count     : 5
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Tue Jul 14 12:26:17 2015
     13// Update Count     : 4
    1414//
    1515
     
    2727
    2828        template< typename Visitor >
     29        inline void addVisitStatement( Statement *stmt, Visitor &visitor ) {
     30                maybeAccept( stmt, visitor );
     31///   if ( ! declsToAdd.empty() ) {
     32///     CompoundStmt *compound = new CompoundStmt( noLabels );
     33///     compound->get_kids().push_back( stmt );
     34///     addDecls( declsToAdd, compound->get_kids(), compound->get_kids().end() );
     35///   }
     36        }
     37
     38        template< typename Visitor >
    2939        inline void addVisit(CompoundStmt *compoundStmt, Visitor &visitor) {
    3040                addVisitStatementList( compoundStmt->get_kids(), visitor );
     41        }
     42
     43        template< typename Visitor >
     44        inline void addVisit(IfStmt *ifStmt, Visitor &visitor) {
     45                addVisitStatement( ifStmt->get_thenPart(), visitor );
     46                addVisitStatement( ifStmt->get_elsePart(), visitor );
     47                maybeAccept( ifStmt->get_condition(), visitor );
     48        }
     49
     50        template< typename Visitor >
     51        inline void addVisit(WhileStmt *whileStmt, Visitor &visitor) {
     52                addVisitStatement( whileStmt->get_body(), visitor );
     53                maybeAccept( whileStmt->get_condition(), visitor );
     54        }
     55
     56        template< typename Visitor >
     57        inline void addVisit(ForStmt *forStmt, Visitor &visitor) {
     58                addVisitStatement( forStmt->get_body(), visitor );
     59                acceptAll( forStmt->get_initialization(), visitor );
     60                maybeAccept( forStmt->get_condition(), visitor );
     61                maybeAccept( forStmt->get_increment(), visitor );
    3162        }
    3263
     
    4374        }
    4475
    45         // template< typename Visitor >
    46         // inline void addVisit(CaseStmt *caseStmt, Visitor &visitor) {
    47         //      addVisitStatementList( caseStmt->get_statements(), visitor );
    48         //      maybeAccept( caseStmt->get_condition(), visitor );
    49         // }
     76        template< typename Visitor >
     77        inline void addVisit(CaseStmt *caseStmt, Visitor &visitor) {
     78                addVisitStatementList( caseStmt->get_statements(), visitor );
     79                maybeAccept( caseStmt->get_condition(), visitor );
     80        }
     81
     82        template< typename Visitor >
     83        inline void addVisit(CatchStmt *cathStmt, Visitor &visitor) {
     84                addVisitStatement( cathStmt->get_body(), visitor );
     85                maybeAccept( cathStmt->get_decl(), visitor );
     86        }
    5087} // namespace SymTab
    5188
  • src/SymTab/Validate.cc

    r3da470c r224e52f  
    1010// Created On       : Sun May 17 21:50:04 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Apr  7 16:45:30 2016
    13 // Update Count     : 243
     12// Last Modified On : Wed Mar  2 17:31:39 2016
     13// Update Count     : 226
    1414//
    1515
     
    4040#include <list>
    4141#include <iterator>
    42 #include "Common/utility.h"
    43 #include "Common/UniqueName.h"
    4442#include "Validate.h"
    4543#include "SynTree/Visitor.h"
    4644#include "SynTree/Mutator.h"
    4745#include "SynTree/Type.h"
    48 #include "SynTree/Expression.h"
    4946#include "SynTree/Statement.h"
    5047#include "SynTree/TypeSubstitution.h"
     
    5249#include "FixFunction.h"
    5350// #include "ImplementationType.h"
    54 #include "GenPoly/DeclMutator.h"
     51#include "Common/utility.h"
     52#include "Common/UniqueName.h"
    5553#include "AddVisit.h"
    5654#include "MakeLibCfa.h"
     
    7270
    7371                virtual void visit( CompoundStmt *compoundStmt );
     72                virtual void visit( IfStmt *ifStmt );
     73                virtual void visit( WhileStmt *whileStmt );
     74                virtual void visit( ForStmt *forStmt );
    7475                virtual void visit( SwitchStmt *switchStmt );
    7576                virtual void visit( ChooseStmt *chooseStmt );
    76                 // virtual void visit( CaseStmt *caseStmt );
     77                virtual void visit( CaseStmt *caseStmt );
     78                virtual void visit( CatchStmt *catchStmt );
    7779          private:
    7880                HoistStruct();
     
    142144
    143145                virtual void visit( CompoundStmt *compoundStmt );
     146                virtual void visit( IfStmt *ifStmt );
     147                virtual void visit( WhileStmt *whileStmt );
     148                virtual void visit( ForStmt *forStmt );
    144149                virtual void visit( SwitchStmt *switchStmt );
    145150                virtual void visit( ChooseStmt *chooseStmt );
    146                 // virtual void visit( CaseStmt *caseStmt );
     151                virtual void visit( CaseStmt *caseStmt );
     152                virtual void visit( CatchStmt *catchStmt );
    147153
    148154                AutogenerateRoutines() : functionNesting( 0 ) {}
     
    160166                /// and return something if the return type is non-void.
    161167                static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
     168
    162169          private:
    163170                virtual void visit( FunctionDecl * functionDecl );
     
    195202        };
    196203
    197         class CompoundLiteral : public GenPoly::DeclMutator {
    198                 DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass;
    199 
    200                 virtual DeclarationWithType * mutate( ObjectDecl *objectDecl );
    201                 virtual Expression *mutate( CompoundLiteralExpr *compLitExpr );
    202         };
    203 
    204204        void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
    205205                Pass1 pass1;
    206206                Pass2 pass2( doDebug, 0 );
    207207                Pass3 pass3( 0 );
    208                 CompoundLiteral compoundliteral;
    209 
    210208                EliminateTypedef::eliminateTypedef( translationUnit );
    211209                HoistStruct::hoistStruct( translationUnit );
     
    213211                acceptAll( translationUnit, pass2 );
    214212                ReturnChecker::checkFunctionReturns( translationUnit );
    215                 mutateAll( translationUnit, compoundliteral );
    216213                AutogenerateRoutines::autogenerateRoutines( translationUnit );
    217214                acceptAll( translationUnit, pass3 );
     
    295292        }
    296293
     294        void HoistStruct::visit( IfStmt *ifStmt ) {
     295                addVisit( ifStmt, *this );
     296        }
     297
     298        void HoistStruct::visit( WhileStmt *whileStmt ) {
     299                addVisit( whileStmt, *this );
     300        }
     301
     302        void HoistStruct::visit( ForStmt *forStmt ) {
     303                addVisit( forStmt, *this );
     304        }
     305
    297306        void HoistStruct::visit( SwitchStmt *switchStmt ) {
    298307                addVisit( switchStmt, *this );
     
    303312        }
    304313
    305         // void HoistStruct::visit( CaseStmt *caseStmt ) {
    306         //      addVisit( caseStmt, *this );
    307         // }
     314        void HoistStruct::visit( CaseStmt *caseStmt ) {
     315                addVisit( caseStmt, *this );
     316        }
     317
     318        void HoistStruct::visit( CatchStmt *cathStmt ) {
     319                addVisit( cathStmt, *this );
     320        }
    308321
    309322        void Pass1::visit( EnumDecl *enumDecl ) {
     
    861874        }
    862875
     876        void AutogenerateRoutines::visit( IfStmt *ifStmt ) {
     877                visitStatement( ifStmt );
     878        }
     879
     880        void AutogenerateRoutines::visit( WhileStmt *whileStmt ) {
     881                visitStatement( whileStmt );
     882        }
     883
     884        void AutogenerateRoutines::visit( ForStmt *forStmt ) {
     885                visitStatement( forStmt );
     886        }
     887
    863888        void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) {
    864889                visitStatement( switchStmt );
     
    869894        }
    870895
    871         // void AutogenerateRoutines::visit( CaseStmt *caseStmt ) {
    872         //      visitStatement( caseStmt );
    873         // }
     896        void AutogenerateRoutines::visit( CaseStmt *caseStmt ) {
     897                visitStatement( caseStmt );
     898        }
     899
     900        void AutogenerateRoutines::visit( CatchStmt *cathStmt ) {
     901                visitStatement( cathStmt );
     902        }
    874903
    875904        void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) {
     
    10511080        }
    10521081
    1053         DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) {
    1054                 storageclass = objectDecl->get_storageClass();
    1055                 DeclarationWithType * temp = Mutator::mutate( objectDecl );
    1056                 storageclass = DeclarationNode::NoStorageClass;
    1057                 return temp;
    1058         }
    1059 
    1060         Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) {
    1061                 // transform [storage_class] ... (struct S){ 3, ... };
    1062                 // into [storage_class] struct S temp =  { 3, ... };
    1063                 static UniqueName indexName( "_compLit" );
    1064 
    1065                 ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageclass, LinkageSpec::C, 0, compLitExpr->get_type(), compLitExpr->get_initializer() );
    1066                 compLitExpr->set_type( 0 );
    1067                 compLitExpr->set_initializer( 0 );
    1068                 delete compLitExpr;
    1069                 DeclarationWithType * newtempvar = mutate( tempvar );
    1070                 addDeclaration( newtempvar );                                   // add modified temporary to current block
    1071                 return new VariableExpr( newtempvar );
    1072         }
    10731082} // namespace SymTab
    10741083
  • src/SynTree/Expression.cc

    r3da470c r224e52f  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr  8 17:16:23 2016
    13 // Update Count     : 40
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Dec 09 14:10:29 2015
     13// Update Count     : 34
    1414//
    1515
     
    2222
    2323#include "Type.h"
    24 #include "Initializer.h"
    2524#include "Expression.h"
    2625#include "Declaration.h"
     
    465464
    466465
    467 CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : type( type ), initializer( initializer ) {
    468         add_result( type->clone() );
    469 }
    470 
    471 CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), type( maybeClone( other.type ) ), initializer( maybeClone( other.initializer ) ) {}
    472 
    473 CompoundLiteralExpr::~CompoundLiteralExpr() {
    474         delete initializer;
    475         delete type;
    476 }
    477 
    478 void CompoundLiteralExpr::print( std::ostream &os, int indent ) const {
    479         os << "Compound Literal Expression: " << std::endl;
    480         if ( type ) type->print( os, indent + 2 );
    481         if ( initializer ) initializer->print( os, indent + 2 );
    482 }
    483 
    484466
    485467std::ostream & operator<<( std::ostream & out, Expression * expr ) {
  • src/SynTree/Expression.h

    r3da470c r224e52f  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr  8 17:18:06 2016
    13 // Update Count     : 21
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Dec 09 14:10:21 2015
     13// Update Count     : 19
    1414//
    1515
     
    577577};
    578578
    579 /// CompoundLiteralExpr represents a C99 'compound literal'
    580 class CompoundLiteralExpr : public Expression {
    581   public:
    582         CompoundLiteralExpr( Type * type, Initializer * initializer );
    583         CompoundLiteralExpr( const CompoundLiteralExpr &other );
    584         ~CompoundLiteralExpr();
    585 
    586         Type * get_type() const { return type; }
    587         void set_type( Type * t ) { type = t; }
    588 
    589         Initializer * get_initializer() const { return initializer; }
    590         void set_initializer( Initializer * i ) { initializer = i; }
    591 
    592         virtual CompoundLiteralExpr *clone() const { return new CompoundLiteralExpr( *this ); }
    593         virtual void accept( Visitor &v ) { v.visit( this ); }
    594         virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
    595         virtual void print( std::ostream &os, int indent = 0 ) const;
    596   private:
    597         Type * type;
    598         Initializer * initializer;
    599 };
    600 
    601579std::ostream & operator<<( std::ostream & out, Expression * expr );
    602580
  • src/SynTree/Mutator.cc

    r3da470c r224e52f  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr  1 18:05:16 2016
    13 // Update Count     : 16
     12// Last Modified On : Wed Mar  2 17:28:20 2016
     13// Update Count     : 12
    1414//
    1515
     
    342342}
    343343
    344 Expression *Mutator::mutate( CompoundLiteralExpr *compLitExpr ) {
    345         mutateAll( compLitExpr->get_results(), *this );
    346         compLitExpr->set_type( maybeMutate( compLitExpr->get_type(), *this ) );
    347         compLitExpr->set_initializer( maybeMutate( compLitExpr->get_initializer(), *this ) );
    348         return compLitExpr;
    349 }
    350 
    351344Type *Mutator::mutate( VoidType *voidType ) {
    352345        mutateAll( voidType->get_forall(), *this );
  • src/SynTree/Mutator.h

    r3da470c r224e52f  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr  1 17:26:56 2016
    13 // Update Count     : 10
     12// Last Modified On : Wed Mar  2 17:33:11 2016
     13// Update Count     : 9
    1414//
    1515#include <cassert>
     
    7777        virtual Expression* mutate( AsmExpr *asmExpr );
    7878        virtual Expression* mutate( UntypedValofExpr *valofExpr );
    79         virtual Expression* mutate( CompoundLiteralExpr *compLitExpr );
    8079
    8180        virtual Type* mutate( VoidType *basicType );
  • src/SynTree/SynTree.h

    r3da470c r224e52f  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr  1 16:47:44 2016
    13 // Update Count     : 5
     12// Last Modified On : Wed Mar  2 17:29:00 2016
     13// Update Count     : 4
    1414//
    1515
     
    8282class AsmExpr;
    8383class UntypedValofExpr;
    84 class CompoundLiteralExpr;
    8584
    8685class Type;
  • src/SynTree/Visitor.cc

    r3da470c r224e52f  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr  1 18:05:13 2016
    13 // Update Count     : 18
     12// Last Modified On : Wed Mar  2 17:29:23 2016
     13// Update Count     : 16
    1414//
    1515
     
    289289}
    290290
    291 void Visitor::visit( CompoundLiteralExpr *compLitExpr ) {
    292         acceptAll( compLitExpr->get_results(), *this );
    293         maybeAccept( compLitExpr->get_type(), *this );
    294         maybeAccept( compLitExpr->get_initializer(), *this );
    295 }
    296 
    297291void Visitor::visit( VoidType *voidType ) {
    298292        acceptAll( voidType->get_forall(), *this );
  • src/SynTree/Visitor.h

    r3da470c r224e52f  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr  1 17:26:55 2016
    13 // Update Count     : 7
     12// Last Modified On : Wed Mar  2 17:33:35 2016
     13// Update Count     : 6
    1414//
    1515
     
    7777        virtual void visit( AsmExpr *asmExpr );
    7878        virtual void visit( UntypedValofExpr *valofExpr );
    79         virtual void visit( CompoundLiteralExpr *compLitExpr );
    8079
    8180        virtual void visit( VoidType *basicType );
  • src/examples/rational.c

    r3da470c r224e52f  
    66// file "LICENCE" distributed with Cforall.
    77//
    8 // rational.c -- test rational number package
     8// rational.c --
    99//
    1010// Author           : Peter A. Buhr
    1111// Created On       : Mon Mar 28 08:43:12 2016
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Fri Apr  8 11:27:48 2016
    14 // Update Count     : 21
     13// Last Modified On : Thu Apr  7 17:25:44 2016
     14// Update Count     : 20
    1515//
    1616
  • src/libcfa/rational

    r3da470c r224e52f  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // rational -- Rational numbers are numbers written as a ratio, i.e., as a fraction, where the numerator (top number)
    8 //     and the denominator (bottom number) are whole numbers. When creating and computing with rational numbers, results
    9 //     are constantly reduced to keep the numerator and denominator as small as possible.
     7// rational --
    108//
    119// Author           : Peter A. Buhr
    1210// Created On       : Wed Apr  6 17:56:25 2016
    1311// Last Modified By : Peter A. Buhr
    14 // Last Modified On : Fri Apr  8 11:38:27 2016
    15 // Update Count     : 15
     12// Last Modified On : Thu Apr  7 17:23:36 2016
     13// Update Count     : 9
    1614//
    1715
    1816#include "iostream"
    1917
    20 // implementation
    2118struct Rational {
    2219        long int numerator, denominator;                                        // invariant: denominator > 0
    2320}; // Rational
    2421
    25 // constants
    2622extern struct Rational 0;
    2723extern struct Rational 1;
    2824
    29 // constructors
    30 Rational rational();
    31 Rational rational( long int n );
    32 Rational rational( long int n, long int d );
    33 
    34 // getter/setter for numerator/denominator
     25long int gcd( long int a, long int b );
     26long int simplify( long int *n, long int *d );
     27Rational rational();                                                                    // constructor
     28Rational rational( long int n );                                                // constructor
     29Rational rational( long int n, long int d );                    // constructor
    3530long int numerator( Rational r );
    3631long int numerator( Rational r, long int n );
    37 long int denominator( Rational r );
    3832long int denominator( Rational r, long int d );
    39 
    40 // comparison
    4133int ?==?( Rational l, Rational r );
    4234int ?!=?( Rational l, Rational r );
     
    4537int ?>?( Rational l, Rational r );
    4638int ?>=?( Rational l, Rational r );
    47 
    48 // arithmetic
    4939Rational -?( Rational r );
    5040Rational ?+?( Rational l, Rational r );
     
    5242Rational ?*?( Rational l, Rational r );
    5343Rational ?/?( Rational l, Rational r );
    54 
    55 // conversion
    5644double widen( Rational r );
    5745Rational narrow( double f, long int md );
    58 
    59 // I/O
    6046forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * );
    6147forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational );
  • src/libcfa/rational.c

    r3da470c r224e52f  
    1111// Created On       : Wed Apr  6 17:54:28 2016
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Fri Apr  8 15:39:17 2016
    14 // Update Count     : 17
     13// Last Modified On : Thu Apr  7 17:28:03 2016
     14// Update Count     : 12
    1515//
    1616
     
    2323} // extern
    2424
    25 
    26 // constants
    27 
    2825struct Rational 0 = {0, 1};
    2926struct Rational 1 = {1, 1};
    3027
    31 
    32 // helper
    33 
    34 // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals.
    35 static long int gcd( long int a, long int b ) {
     28// Calculate the greatest common denominator of two numbers, the first of which may be negative.  It is used to reduce
     29// rationals.
     30
     31long int gcd( long int a, long int b ) {
    3632    for ( ;; ) {                                                                                // Euclid's algorithm
    3733                long int r = a % b;
     
    4339} // gcd
    4440
    45 static long int simplify( long int *n, long int *d ) {
     41long int simplify( long int *n, long int *d ) {
    4642    if ( *d == 0 ) {
    4743                serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl;
     
    5248} // Rationalnumber::simplify
    5349
    54 
    55 // constructors
    56 
    57 Rational rational() {
    58     return (Rational){ 0, 1 };
    59 //      Rational t = { 0, 1 };
    60 //      return t;
     50Rational rational() {                                                                   // constructor
     51//    r = (Rational){ 0, 1 };
     52        Rational t = { 0, 1 };
     53        return t;
    6154} // rational
    6255
    63 Rational rational( long int n ) {
    64     return (Rational){ n, 1 };
    65 //      Rational t = { n, 1 };
    66 //      return t;
     56Rational rational( long int n ) {                                               // constructor
     57//    r = (Rational){ n, 1 };
     58        Rational t = { n, 1 };
     59        return t;
    6760} // rational
    6861
    69 Rational rational( long int n, long int d ) {
     62Rational rational( long int n, long int d ) {                   // constructor
    7063    long int t = simplify( &n, &d );                                    // simplify
    7164//    r = (Rational){ n / t, d / t };
     
    7366        return t;
    7467} // rational
    75 
    76 
    77 // getter/setter for numerator/denominator
    7868
    7969long int numerator( Rational r ) {
     
    8979} // numerator
    9080
    91 long int denominator( Rational r ) {
    92     return r.denominator;
    93 } // denominator
    94 
    9581long int denominator( Rational r, long int d ) {
    9682    long int prev = r.denominator;
     
    10187} // denominator
    10288
    103 
    104 // comparison
    105 
    10689int ?==?( Rational l, Rational r ) {
    10790    return l.numerator * r.denominator == l.denominator * r.numerator;
     
    127110    return ! ( l < r );
    128111} // ?>=?
    129 
    130 
    131 // arithmetic
    132112
    133113Rational -?( Rational r ) {
     
    169149    return t;
    170150} // ?/?
    171 
    172 
    173 // conversion
    174151
    175152double widen( Rational r ) {
     
    211188} // narrow
    212189
    213 
    214 // I/O
    215 
    216190forall( dtype istype | istream( istype ) )
    217191istype * ?|?( istype *is, Rational *r ) {
Note: See TracChangeset for help on using the changeset viewer.