- Timestamp:
- Dec 15, 2015, 4:09:13 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 4389966
- Parents:
- b0b958a
- Location:
- src
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
rb0b958a rcf16f94 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Rob Schluntz12 // Last Modified On : T hu Nov 26 17:01:55201513 // Update Count : 19111 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Dec 15 15:30:31 2015 13 // Update Count : 215 14 14 // 15 15 … … 53 53 public: 54 54 Pass1(); 55 virtual Expression * mutate( ApplicationExpr *appExpr );56 virtual Expression * mutate( AddressExpr *addrExpr );57 virtual Expression * mutate( UntypedExpr *expr );58 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );59 virtual TypeDecl * mutate( TypeDecl *typeDecl );60 virtual Expression * mutate( CommaExpr *commaExpr );61 virtual Expression * mutate( ConditionalExpr *condExpr );62 virtual Statement * mutate(ReturnStmt *catchStmt);63 virtual Type * mutate( PointerType *pointerType );64 virtual Type * mutate( FunctionType *pointerType );55 virtual Expression * mutate( ApplicationExpr *appExpr ); 56 virtual Expression * mutate( AddressExpr *addrExpr ); 57 virtual Expression * mutate( UntypedExpr *expr ); 58 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ); 59 virtual TypeDecl * mutate( TypeDecl *typeDecl ); 60 virtual Expression * mutate( CommaExpr *commaExpr ); 61 virtual Expression * mutate( ConditionalExpr *condExpr ); 62 virtual Statement * mutate( ReturnStmt *returnStmt ); 63 virtual Type * mutate( PointerType *pointerType ); 64 virtual Type * mutate( FunctionType *functionType ); 65 65 66 66 virtual void doBeginScope(); … … 190 190 if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) { 191 191 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) { 192 name = typeInst->get_name(); 193 return true; 192 if ( TypeInstType *typeInst2 = dynamic_cast< TypeInstType *>( funType->get_parameters().back()->get_type() ) ) { 193 if ( typeInst->get_name() == typeInst2->get_name() ) { 194 name = typeInst->get_name(); 195 return true; 196 } // if 197 } // if 194 198 } // if 195 199 } // if … … 340 344 341 345 Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) { 342 if ( useRetval ) { 343 assert( retval ); 344 arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) ); 345 arg++; 346 } else { 347 ObjectDecl *newObj = makeTemporary( retType->clone() ); 348 Expression *paramExpr = new VariableExpr( newObj ); 349 if ( ! isPolyType( newObj->get_type(), env, scopeTyVars ) ) { 350 paramExpr = new AddressExpr( paramExpr ); 351 } // if 352 arg = appExpr->get_args().insert( arg, paramExpr ); 353 arg++; 354 /// stmtsToAdd.push_back( new ExprStmt( noLabels, appExpr ) ); 355 CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) ); 356 commaExpr->set_env( appExpr->get_env() ); 357 appExpr->set_env( 0 ); 358 return commaExpr; 359 } // if 360 return appExpr; 346 // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous. 347 // if ( useRetval ) { 348 // assert( retval ); 349 // arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) ); 350 // arg++; 351 // } else { 352 353 // Create temporary to hold return value of polymorphic function and produce that temporary as a result 354 // using a comma expression. Possibly change comma expression into statement expression "{}" for multiple 355 // return values. 356 ObjectDecl *newObj = makeTemporary( retType->clone() ); 357 Expression *paramExpr = new VariableExpr( newObj ); 358 // If the type of the temporary is not polymorphic, box temporary by taking its address; otherwise the 359 // temporary is already boxed and can be used directly. 360 if ( ! isPolyType( newObj->get_type(), env, scopeTyVars ) ) { 361 paramExpr = new AddressExpr( paramExpr ); 362 } // if 363 arg = appExpr->get_args().insert( arg, paramExpr ); // add argument to function call 364 arg++; 365 // Build a comma expression to call the function and emulate a normal return. 366 CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) ); 367 commaExpr->set_env( appExpr->get_env() ); 368 appExpr->set_env( 0 ); 369 return commaExpr; 370 // } // if 371 // return appExpr; 361 372 } 362 373 … … 413 424 Type *newType = formal->clone(); 414 425 std::list< FunctionType *> functions; 415 // instead of functions needing adapters, this really ought to look for 416 // any function mentioning apolymorphic type426 // instead of functions needing adapters, this really ought to look for any function mentioning a 427 // polymorphic type 417 428 findAndReplaceFunction( newType, functions, tyVars, needsAdapter ); 418 429 if ( ! functions.empty() ) { … … 852 863 Expression *Pass1::mutate( AddressExpr *addrExpr ) { 853 864 assert( ! addrExpr->get_arg()->get_results().empty() ); 865 866 bool needs = false; 867 if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) { 868 if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), env, scopeTyVars ) ) { 869 if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { 870 if ( name->get_name() == "*?" ) { 871 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->get_args().front() ) ) { 872 assert( ! appExpr->get_function()->get_results().empty() ); 873 PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() ); 874 assert( pointer ); 875 FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() ); 876 assert( function ); 877 needs = needsAdapter( function, scopeTyVars ); 878 } // if 879 } // if 880 } // if 881 } // if 882 } // if 854 883 addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) ); 855 if ( isPolyType( addrExpr->get_arg()->get_results().front(), env, scopeTyVars ) ) {884 if ( isPolyType( addrExpr->get_arg()->get_results().front(), env, scopeTyVars ) || needs ) { 856 885 Expression *ret = addrExpr->get_arg(); 857 886 delete ret->get_results().front(); … … 865 894 } 866 895 867 Statement * Pass1::mutate(ReturnStmt *retStmt) { 868 // a cast expr on a polymorphic return value is either redundant or invalid 869 while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( retStmt->get_expr() ) ) { 870 retStmt->set_expr( castExpr->get_arg() ); 871 retStmt->get_expr()->set_env( castExpr->get_env() ); 872 castExpr->set_env( 0 ); 873 castExpr->set_arg( 0 ); 874 delete castExpr; 875 } 876 if ( retval && retStmt->get_expr() ) { 877 assert( ! retStmt->get_expr()->get_results().empty() ); 878 if ( retStmt->get_expr()->get_results().front()->get_isLvalue() ) { 879 /// retStmt->set_expr( mutateExpression( retStmt->get_expr() ) ); 880 TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ); 881 assert( typeInst ); 882 std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() ); 883 if ( assignIter == assignOps.end() ) { 884 throw SemanticError( "Attempt to return dtype or ftype object in ", retStmt->get_expr() ); 885 } // if 886 ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) ); 887 Expression *retParm = new NameExpr( retval->get_name() ); 888 retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) ); 889 assignExpr->get_args().push_back( retParm ); 890 assignExpr->get_args().push_back( retStmt->get_expr() ); 891 stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) ); 892 } else { 893 useRetval = true; 894 stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( retStmt->get_expr() ) ) ); 895 useRetval = false; 896 } // if 897 retStmt->set_expr( 0 ); 896 Statement * Pass1::mutate( ReturnStmt *returnStmt ) { 897 if ( retval && returnStmt->get_expr() ) { 898 assert( ! returnStmt->get_expr()->get_results().empty() ); 899 // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous. 900 // if ( returnStmt->get_expr()->get_results().front()->get_isLvalue() ) { 901 // a cast expr on a polymorphic return value is either redundant or invalid 902 while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( returnStmt->get_expr() ) ) { 903 returnStmt->set_expr( castExpr->get_arg() ); 904 returnStmt->get_expr()->set_env( castExpr->get_env() ); 905 castExpr->set_env( 0 ); 906 castExpr->set_arg( 0 ); 907 delete castExpr; 908 } // while 909 TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ); 910 assert( typeInst ); 911 std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() ); 912 if ( assignIter == assignOps.end() ) { 913 throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() ); 914 } // if 915 ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) ); 916 Expression *retParm = new NameExpr( retval->get_name() ); 917 retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) ); 918 assignExpr->get_args().push_back( retParm ); 919 assignExpr->get_args().push_back( returnStmt->get_expr() ); 920 stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) ); 921 // } else { 922 // std::cerr << "THOMAS " << std::endl; 923 // useRetval = true; 924 // stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( returnStmt->get_expr() ) ) ); 925 // useRetval = false; 926 // } // if 927 returnStmt->set_expr( 0 ); 898 928 } else { 899 ret Stmt->set_expr( mutateExpression( retStmt->get_expr() ) );900 } // if 901 return ret Stmt;929 returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) ); 930 } // if 931 return returnStmt; 902 932 } 903 933 … … 1097 1127 if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) { 1098 1128 if ( isPolyVal( objectDecl->get_type(), scopeTyVars ) ) { 1099 // change initialization of a polymorphic value object 1100 // to allocate storage with alloca 1129 // change initialization of a polymorphic value object to allocate storage with alloca 1101 1130 TypeInstType *typeInst = dynamic_cast< TypeInstType *>( objectDecl->get_type() ); 1102 1131 assert( typeInst ); -
src/GenPoly/GenPoly.cc
rb0b958a rcf16f94 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Rob Schluntz12 // Last Modified On : Tue Nov 24 15:23:08201513 // Update Count : 1 111 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Dec 1 15:18:54 2015 13 // Update Count : 12 14 14 // 15 15 … … 21 21 22 22 namespace GenPoly { 23 // A function needs an adapter if it returns a polymorphic value or if any of its 24 // parameters have polymorphic type 23 // A function needs an adapter if it returns a polymorphic value or if any of its parameters have polymorphic type 25 24 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { 26 25 if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { -
src/GenPoly/Lvalue.cc
rb0b958a rcf16f94 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 07:41:33 201513 // Update Count : 112 // Last Modified On : Tue Dec 15 15:33:13 2015 13 // Update Count : 3 14 14 // 15 15 … … 120 120 if ( retval && retStmt->get_expr() ) { 121 121 assert( ! retStmt->get_expr()->get_results().empty() ); 122 while ( CastExpr *castExpr = dynamic_cast< CastExpr* >( retStmt->get_expr() ) ) {123 retStmt->set_expr( castExpr->get_arg() );124 retStmt->get_expr()->set_env( castExpr->get_env() );125 castExpr->set_env( 0 );126 castExpr->set_arg( 0 );127 delete castExpr;128 } // while129 122 if ( retStmt->get_expr()->get_results().front()->get_isLvalue() ) { 123 // ***** Code Removal ***** because casts may be stripped already 124 125 // strip casts because not allowed to take address of cast 126 // while ( CastExpr *castExpr = dynamic_cast< CastExpr* >( retStmt->get_expr() ) ) { 127 // retStmt->set_expr( castExpr->get_arg() ); 128 // retStmt->get_expr()->set_env( castExpr->get_env() ); 129 // castExpr->set_env( 0 ); 130 // castExpr->set_arg( 0 ); 131 // delete castExpr; 132 // } // while 130 133 retStmt->set_expr( new AddressExpr( retStmt->get_expr()->acceptMutator( *this ) ) ); 131 134 } else { -
src/InitTweak/RemoveInit.cc
rb0b958a rcf16f94 7 7 // RemoveInit.cc -- 8 8 // 9 // Author : Ro dolfo G. Esteves9 // Author : Rob Schluntz 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:39:32201513 // Update Count : 1 12 // Last Modified On : Tue Dec 15 15:37:26 2015 13 // Update Count : 15 14 14 // 15 15 … … 26 26 const std::list<Label> noLabels; 27 27 } 28 29 class RemoveInit : public Mutator { 30 public: 31 RemoveInit(); 32 virtual ObjectDecl * mutate(ObjectDecl *objDecl); 33 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ); 34 35 virtual Statement * mutate( ReturnStmt * returnStmt ); 36 37 virtual CompoundStmt * mutate(CompoundStmt * compoundStmt); 38 39 protected: 40 std::list< Statement* > stmtsToAddBefore; 41 std::list< Statement* > stmtsToAddAfter; 42 void mutateStatementList( std::list< Statement* > &statements ); 43 44 std::list<DeclarationWithType*> returnVals; 45 UniqueName tempNamer; 46 std::string funcName; 47 }; 28 48 29 49 void tweak( std::list< Declaration * > translationUnit ) { … … 32 52 } 33 53 54 RemoveInit::RemoveInit() : tempNamer( "_retVal" ) {} 55 34 56 void RemoveInit::mutateStatementList( std::list< Statement* > &statements ) { 35 57 for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { … … 38 60 } // if 39 61 *i = (*i)->acceptMutator( *this ); 62 if ( ! stmtsToAddBefore.empty() ) { 63 statements.splice( i, stmtsToAddBefore ); 64 } // if 40 65 } // for 41 66 if ( ! stmtsToAddAfter.empty() ) { … … 49 74 } 50 75 51 // in the case where an object has an initializer and a polymorphic type, insert an assignment 52 // immediately after the declaration. This will (seemingly) cause the later phases to do the right 53 // thing with the assignment 76 // in the case where an object has an initializer and a polymorphic type, insert an assignment immediately after the 77 // declaration. This will (seemingly) cause the later phases to do the right thing with the assignment 54 78 ObjectDecl *RemoveInit::mutate( ObjectDecl *objDecl ) { 55 79 if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) { … … 63 87 return objDecl; 64 88 } 89 90 Statement *RemoveInit::mutate( ReturnStmt *returnStmt ) { 91 // update for multiple return values 92 assert( returnVals.size() == 0 || returnVals.size() == 1 ); 93 // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address 94 // is being returned 95 if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) { 96 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), 0 ); 97 stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) ); 98 99 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); 100 assign->get_args().push_back( new AddressExpr (new NameExpr( newObj->get_name() ) ) ); 101 assign->get_args().push_back( returnStmt->get_expr() ); 102 stmtsToAddBefore.push_back(new ExprStmt(noLabels, assign)); 103 104 returnStmt->set_expr( new VariableExpr( newObj ) ); 105 } // if 106 return returnStmt; 107 } 108 109 DeclarationWithType* RemoveInit::mutate( FunctionDecl *functionDecl ) { 110 std::list<DeclarationWithType*> oldReturnVals = returnVals; 111 std::string oldFuncName = funcName; 112 113 FunctionType * type = functionDecl->get_functionType(); 114 returnVals = type->get_returnVals(); 115 funcName = functionDecl->get_name(); 116 DeclarationWithType * decl = Mutator::mutate( functionDecl ); 117 returnVals = oldReturnVals; 118 funcName = oldFuncName; 119 return decl; 120 } 65 121 } // namespace InitTweak 66 122 -
src/InitTweak/RemoveInit.h
rb0b958a rcf16f94 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:40:11201513 // Update Count : 112 // Last Modified On : Fri Nov 27 17:00:47 2015 13 // Update Count : 2 14 14 // 15 15 … … 27 27 /// Adds assignment statements for polymorphic type initializers 28 28 void tweak( std::list< Declaration * > translationUnit ); 29 30 class RemoveInit : public Mutator {31 public:32 // RemoveInit();33 virtual ObjectDecl *mutate(ObjectDecl *objDecl);34 virtual CompoundStmt *mutate(CompoundStmt *compoundStmt);35 protected:36 std::list< Statement* > stmtsToAddAfter;37 void mutateStatementList( std::list< Statement* > &statements );38 };39 29 } // namespace 40 30 -
src/SynTree/PointerType.cc
rb0b958a rcf16f94 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 10:15:16201513 // Update Count : 212 // Last Modified On : Tue Dec 15 15:39:10 2015 13 // Update Count : 5 14 14 // 15 15 … … 20 20 PointerType::PointerType( const Type::Qualifiers &tq, Type *base ) 21 21 : Type( tq ), base( base ), dimension( 0 ), isVarLen( false ), isStatic( false ) { 22 base->set_isLvalue( false );23 22 } 24 23 25 24 PointerType::PointerType( const Type::Qualifiers &tq, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) 26 25 : Type( tq ), base( base ), dimension( dimension ), isVarLen( isVarLen ), isStatic( isStatic ) { 27 base->set_isLvalue( false );28 26 } 29 27 -
src/SynTree/Type.h
rb0b958a rcf16f94 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Nov 2 0 12:54:09201513 // Update Count : 1 512 // Last Modified On : Fri Nov 27 17:54:31 2015 13 // Update Count : 16 14 14 // 15 15 … … 188 188 virtual ~FunctionType(); 189 189 190 std::list<DeclarationWithType*> & get_returnVals() { return returnVals; }191 std::list<DeclarationWithType*> & get_parameters() { return parameters; }190 std::list<DeclarationWithType*> & get_returnVals() { return returnVals; } 191 std::list<DeclarationWithType*> & get_parameters() { return parameters; } 192 192 bool get_isVarArgs() { return isVarArgs; } 193 193 void set_isVarArgs( bool newValue ) { isVarArgs = newValue; } -
src/examples/fstream_test.c
rb0b958a rcf16f94 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:13:43201513 // Update Count : 212 // Last Modified On : Mon Nov 23 14:43:32 2015 13 // Update Count : 40 14 14 // 15 15 … … 20 20 ifstream *sin = ifstream_stdin(); 21 21 int nombre; 22 sout << "Appuyez un nombre, s'il vous plâit:\n"; 23 sin >> &nombre; 24 sout << "Vous avez appuyé: " << nombre << "\n"; 22 sout | "Entrez un nombre, s'il vous plaît:\n"; 23 sin | &nombre; 24 sout | "Vous avez entré " | nombre | " stocké à l'adresse " | &nombre | endl; 25 sout | "nombre " | nombre | " est " 26 | (nombre > 0 ? "plus grand que" : 27 nombre == 0 ? "égal à" : "moins de") 28 | " zéro" | endl; 29 30 sout | "Entrez trois nombres, s'il vous plaît:\n"; 31 int i, j, k; 32 sin | &i | &j | &k; 33 sout | "Vous avez entré " | "i:" | i | " j:" | j | " k:" | k | endl; 34 35 sout | 3 | ' ' | 3.5 | ' ' | 'a' | ' ' | "abc" | endl; 25 36 } 26 37 -
src/examples/hello.c
rb0b958a rcf16f94 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Nov 20 16:02:50201513 // Update Count : 312 // Last Modified On : Sun Nov 22 17:40:37 2015 13 // Update Count : 5 14 14 // 15 15 … … 19 19 ofstream *sout = ofstream_stdout(); 20 20 ifstream *sin = ifstream_stdin(); 21 sout << "Bonjour au monde!\n"; 22 sout << 3 << " " << 3.5 << " " << 'a' << " " << "abc" << "\n"; 23 int i, j, k; 24 sin >> &i >> &j >> &k; 25 sout << "i:" << i << " j:" << j << " k:" << k << "\n"; 21 sout | "Bonjour au monde!\n"; 26 22 } 27 23 -
src/examples/iostream.c
rb0b958a rcf16f94 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Nov 20 13:19:19201513 // Update Count : 912 // Last Modified On : Mon Dec 7 23:08:02 2015 13 // Update Count : 24 14 14 // 15 15 … … 21 21 22 22 forall( dtype ostype | ostream( ostype ) ) 23 ostype * ? <<?( ostype *os, char c ) {23 ostype * ?|?( ostype *os, char c ) { 24 24 return write( os, &c, 1 ); 25 } // ? <<?25 } // ?|? 26 26 27 27 forall( dtype ostype | ostream( ostype ) ) 28 ostype * ? <<?( ostype *os, int i ) {28 ostype * ?|?( ostype *os, int i ) { 29 29 char buffer[32]; // larger than the largest integer 30 30 sprintf( buffer, "%d", i ); 31 31 return write( os, buffer, strlen( buffer ) ); 32 } // ? <<?32 } // ?|? 33 33 34 34 forall( dtype ostype | ostream( ostype ) ) 35 ostype * ? <<?( ostype *os, double d ) {35 ostype * ?|?( ostype *os, double d ) { 36 36 char buffer[32]; // larger than the largest double 37 37 sprintf( buffer, "%g", d ); 38 38 return write( os, buffer, strlen( buffer ) ); 39 } // ? <<?39 } // ?|? 40 40 41 41 forall( dtype ostype | ostream( ostype ) ) 42 ostype * ? <<?( ostype *os, const char *cp ) {42 ostype * ?|?( ostype *os, const char *cp ) { 43 43 return write( os, cp, strlen( cp ) ); 44 } // ? <<?44 } // ?|? 45 45 46 46 forall( dtype ostype | ostream( ostype ) ) 47 ostype * ? <<?( ostype *os, const void *p ) {47 ostype * ?|?( ostype *os, const void *p ) { 48 48 char buffer[32]; // larger than the largest pointer 49 49 sprintf( buffer, "%p", p ); 50 50 return write( os, buffer, strlen( buffer ) ); 51 } // ?<<? 51 } // ?|? 52 53 54 forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) ) 55 retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) { 56 return manip(os); 57 } 58 59 forall( dtype ostype | ostream( ostype ) ) 60 ostype * endl( ostype * os ) { 61 os | "\n"; 62 // flush 63 return os; 64 } // endl 52 65 53 66 forall( type elt_type | writeable( elt_type ), … … 56 69 void write( iterator_type begin, iterator_type end, os_type *os ) { 57 70 void print( elt_type i ) { 58 os << i <<' ';71 os | i | ' '; 59 72 } 60 73 for_each( begin, end, print ); 61 } // ? <<?74 } // ?|? 62 75 63 76 forall( type elt_type | writeable( elt_type ), … … 65 78 dtype os_type | ostream( os_type ) ) 66 79 void write_reverse( iterator_type begin, iterator_type end, os_type *os ) { 67 void print( elt_type i ) { 68 os << i << ' '; 69 } 80 void print( elt_type i ) { os | i | ' '; } 70 81 for_each_reverse( begin, end, print ); 71 } // ? <<?82 } // ?|? 72 83 73 84 74 85 forall( dtype istype | istream( istype ) ) 75 istype * ? >>?( istype *is, char *cp ) {86 istype * ?|?( istype *is, char *cp ) { 76 87 return read( is, cp, 1 ); 77 } // ? >>?88 } // ?|? 78 89 79 90 forall( dtype istype | istream( istype ) ) 80 istype * ? >>?( istype *is, int *ip ) {91 istype * ?|?( istype *is, int *ip ) { 81 92 char cur; 82 93 83 94 // skip some whitespace 84 95 do { 85 is >>&cur;96 is | &cur; 86 97 if ( fail( is ) || eof( is ) ) return is; 87 98 } while ( !( cur >= '0' && cur <= '9' ) ); … … 91 102 while ( cur >= '0' && cur <= '9' ) { 92 103 *ip = *ip * 10 + ( cur - '0' ); 93 is >>&cur;104 is | &cur; 94 105 if ( fail( is ) || eof( is ) ) return is; 95 106 } … … 97 108 unread( is, cur ); 98 109 return is; 99 } // ? >>?110 } // ?|? 100 111 101 112 // Local Variables: // -
src/examples/iostream.h
rb0b958a rcf16f94 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Nov 19 17:56:51201513 // Update Count : 512 // Last Modified On : Mon Nov 23 14:15:25 2015 13 // Update Count : 17 14 14 // 15 15 … … 27 27 28 28 context writeable( type T ) { 29 forall( dtype ostype | ostream( ostype ) ) ostype * ? <<?( ostype *, T );29 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, T ); 30 30 }; 31 31 32 32 // implement writable for some intrinsic types 33 33 34 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, char ); 35 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, int ); 36 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, double ); 37 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, const char * ); 38 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, void * ); 34 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, char ); 35 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, int ); 36 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, double ); 37 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, const char * ); 38 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, const void * ); 39 40 forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) ) retostype * ?|?( ostype *os, retostype * (* manip)(ostype*) ); 41 forall( dtype ostype | ostream( ostype ) ) ostype * endl( ostype * ); 39 42 40 43 // writes the range [begin, end) to the given stream … … 49 52 void write_reverse( iterator_type begin, iterator_type end, os_type *os ); 50 53 54 //****************************************************************************** 51 55 52 56 context istream( dtype istype ) { … … 58 62 59 63 context readable( type T ) { 60 forall( dtype istype | istream( istype ) ) istype * ? <<?( istype *, T );64 forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, T ); 61 65 }; 62 66 63 67 forall( dtype istype | istream( istype ) ) 64 istype * ? >>?( istype *, char * );68 istype * ?|?( istype *, char * ); 65 69 66 70 forall( dtype istype | istream( istype ) ) 67 istype * ? >>?( istype *, int * );71 istype * ?|?( istype *, int * ); 68 72 69 73 #endif // IOSTREAM_H -
src/examples/sum.c
rb0b958a rcf16f94 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Sep 28 15:09:55201513 // Update Count : 11 812 // Last Modified On : Sat Nov 21 18:08:18 2015 13 // Update Count : 119 14 14 // 15 15 … … 53 53 } 54 54 sout << "sum from " << low << " to " << High << " is " 55 << (int)sum( size, a ) << ", check " << (int)s << "\n";55 << (int)sum( size, a ) << ", check " << (int)s << endl; 56 56 57 57 int s = 0, a[size]; … … 62 62 } 63 63 sout << "sum from " << low << " to " << High << " is " 64 << sum( size, (int *)a ) << ", check " << (int)s << "\n";64 << sum( size, (int *)a ) << ", check " << (int)s << endl; 65 65 66 66 double s = 0.0, a[size]; … … 72 72 printf( "%g\n", sum( size, (double *)a ) ); 73 73 // sout << "sum from " << low / 10.0 << " to " << High / 10.0 << " is " 74 // << sum( size, (double *)a ) << ", check " << (double)s << "\n";74 // << sum( size, (double *)a ) << ", check " << (double)s << endl; 75 75 76 76 float s = 0.0, a[size]; … … 82 82 printf( "%g\n", sum( size, (float *)a ) ); 83 83 // sout << "sum from " << low / 10.0 << " to " << High / 10.0 << " is " 84 // << sum( size, (float *)a ) << ", check " << (float)s << "\n";84 // << sum( size, (float *)a ) << ", check " << (float)s << endl; 85 85 } 86 86 -
src/examples/vector_test.c
rb0b958a rcf16f94 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Nov 19 17:54:34201513 // Update Count : 912 // Last Modified On : Tue Dec 15 16:02:56 2015 13 // Update Count : 13 14 14 // 15 15 … … 27 27 int num; 28 28 29 sout << "enter N elements and C-d on a separate line:\n";29 sout | "enter N elements and C-d on a separate line:" | endl; 30 30 for ( ;; ) { 31 sin >>#31 sin | # 32 32 if ( fail( sin ) || eof( sin ) ) break; 33 33 append( &vec, num ); … … 35 35 // write out the numbers 36 36 37 sout << "Array elements:\n";37 sout | "Array elements:" | endl; 38 38 write( begin( vec ), end( vec ), sout ); 39 sout << "\n";39 sout | endl; 40 40 41 sout << "Array elements reversed:\n";41 sout | "Array elements reversed:" | endl; 42 42 write_reverse( begin( vec ), end( vec ), sout ); 43 sout << "\n";43 sout | endl; 44 44 } 45 45
Note: See TracChangeset
for help on using the changeset viewer.