Changes in / [6ce67ce:6ed1d4b]
- Location:
- src
- Files:
-
- 2 deleted
- 24 edited
-
GenPoly/Box.cc (modified) (16 diffs)
-
ResolvExpr/CastCost.cc (modified) (3 diffs)
-
ResolvExpr/Resolver.cc (modified) (3 diffs)
-
SymTab/Validate.cc (modified) (5 diffs)
-
Tests/Makefile (modified) (2 diffs)
-
examples/abs.c (modified) (2 diffs)
-
examples/alloc.c (modified) (3 diffs)
-
examples/ato.c (deleted)
-
examples/fstream_test.c (modified) (2 diffs)
-
examples/hello.c (modified) (2 diffs)
-
examples/identity.c (modified) (3 diffs)
-
examples/minmax.c (modified) (2 diffs)
-
examples/quad.c (modified) (2 diffs)
-
examples/quoted_keyword.c (modified) (2 diffs)
-
examples/random.c (modified) (1 diff)
-
examples/searchsort.c (deleted)
-
examples/square.c (modified) (2 diffs)
-
examples/sum.c (modified) (2 diffs)
-
examples/swap.c (modified) (2 diffs)
-
examples/twice.c (modified) (2 diffs)
-
examples/vector_test.c (modified) (2 diffs)
-
libcfa/fstream (modified) (2 diffs)
-
libcfa/fstream.c (modified) (2 diffs)
-
libcfa/iostream (modified) (3 diffs)
-
libcfa/iostream.c (modified) (7 diffs)
-
libcfa/stdlib.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
r6ce67ce r6ed1d4b 73 73 /// Makes a new temporary array holding the offsets of the fields of `type`, and returns a new variable expression referencing it 74 74 Expression *makeOffsetArray( StructInstType *type ); 75 /// Pass the extra type parameters from polymorphic generic arguments or return types into a function application76 void passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes );77 75 /// passes extra type parameters into a polymorphic function application 78 void passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType,std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );76 void passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); 79 77 /// wraps a function application with a new temporary for the out-parameter return value 80 78 Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ); … … 400 398 } 401 399 402 void Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) { 403 Type *polyBase = hasPolyBase( parmType, exprTyVars ); 404 if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) { 405 std::string sizeName = sizeofName( polyBase ); 406 if ( seenTypes.count( sizeName ) ) return; 407 408 arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) ); 409 arg++; 410 arg = appExpr->get_args().insert( arg, new AlignofExpr( argBaseType->clone() ) ); 411 arg++; 412 if ( dynamic_cast< StructInstType* >( polyBase ) ) { 413 if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) { 414 arg = appExpr->get_args().insert( arg, makeOffsetArray( argBaseStructType ) ); 415 arg++; 416 } else { 417 throw SemanticError( "Cannot pass non-struct type for generic struct" ); 418 } 419 } 420 421 seenTypes.insert( sizeName ); 422 } 423 } 424 425 void Pass1::passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { 400 void Pass1::passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { 426 401 // pass size/align for type variables 427 402 for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) { … … 449 424 std::list< Expression* >::const_iterator fnArg = arg; 450 425 std::set< std::string > seenTypes; //< names for generic types we've seen 451 452 // a polymorphic return type may need to be added to the argument list453 if ( polyRetType ) {454 Type *concRetType = replaceWithConcrete( appExpr, polyRetType );455 passArgTypeVars( appExpr, polyRetType, concRetType, arg, exprTyVars, seenTypes );456 }457 458 // add type information args for presently unseen types in parameter list459 426 for ( ; fnParm != funcType->get_parameters().end() && fnArg != appExpr->get_args().end(); ++fnParm, ++fnArg ) { 460 VariableExpr *fnArgBase = getBaseVar( *fnArg ); 461 if ( ! fnArgBase || fnArgBase->get_results().empty() ) continue; 462 passArgTypeVars( appExpr, (*fnParm)->get_type(), fnArgBase->get_results().front(), arg, exprTyVars, seenTypes ); 427 Type *polyBase = hasPolyBase( (*fnParm)->get_type(), exprTyVars ); 428 if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) { 429 std::string sizeName = sizeofName( polyBase ); 430 if ( seenTypes.count( sizeName ) ) continue; 431 432 VariableExpr *fnArgBase = getBaseVar( *fnArg ); 433 assert( fnArgBase && ! fnArgBase->get_results().empty() ); 434 Type *argBaseType = fnArgBase->get_results().front(); 435 arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) ); 436 arg++; 437 arg = appExpr->get_args().insert( arg, new AlignofExpr( argBaseType->clone() ) ); 438 arg++; 439 if ( dynamic_cast< StructInstType* >( polyBase ) ) { 440 if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) { 441 arg = appExpr->get_args().insert( arg, makeOffsetArray( argBaseStructType ) ); 442 arg++; 443 } else { 444 throw SemanticError( "Cannot pass non-struct type for generic struct" ); 445 } 446 } 447 448 seenTypes.insert( sizeName ); 449 } 463 450 } 464 451 } … … 483 470 ObjectDecl *newObj = makeTemporary( retType->clone() ); 484 471 Expression *paramExpr = new VariableExpr( newObj ); 485 486 // If the type of the temporary is not polymorphic, box temporary by taking its address; 487 // otherwise the temporary is already boxed and can be used directly. 472 // If the type of the temporary is not polymorphic, box temporary by taking its address; otherwise the 473 // temporary is already boxed and can be used directly. 488 474 if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) { 489 475 paramExpr = new AddressExpr( paramExpr ); … … 534 520 assert( env ); 535 521 Type *concrete = replaceWithConcrete( appExpr, polyType ); 536 // add out-parameter for return value537 522 return addRetParam( appExpr, function, concrete, arg ); 538 523 } … … 557 542 assert( ! arg->get_results().empty() ); 558 543 if ( isPolyType( param, exprTyVars ) ) { 559 if ( isPolyType( arg->get_results().front() ) ) {560 // if the argument's type is polymorphic, we don't need to box again!544 if ( dynamic_cast< TypeInstType *>( arg->get_results().front() ) ) { 545 // if the argument's type is a type parameter, we don't need to box again! 561 546 return; 562 547 } else if ( arg->get_results().front()->get_isLvalue() ) { … … 637 622 assert( arg ); 638 623 if ( isPolyType( realParam->get_type(), tyVars ) ) { 639 if ( ! isPolyType( arg->get_type() )) {624 if ( dynamic_cast<TypeInstType *>(arg->get_type()) == NULL ) { 640 625 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); 641 626 deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) ); … … 932 917 std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin(); 933 918 934 TyVarMap exprTyVars; 935 makeTyVarMap( function, exprTyVars ); 936 ReferenceToType *polyRetType = 0; 937 938 if ( polyRetType = isPolyRet( function ) ) { 939 ret = addPolyRetParam( appExpr, function, polyRetType, arg ); 919 if ( ReferenceToType *polyType = isPolyRet( function ) ) { 920 ret = addPolyRetParam( appExpr, function, polyType, arg ); 940 921 } else if ( needsAdapter( function, scopeTyVars ) ) { 941 922 // std::cerr << "needs adapter: "; … … 949 930 arg = appExpr->get_args().begin(); 950 931 951 passTypeVars( appExpr, polyRetType, arg, exprTyVars ); 932 TyVarMap exprTyVars; 933 makeTyVarMap( function, exprTyVars ); 934 935 passTypeVars( appExpr, arg, exprTyVars ); 952 936 addInferredParams( appExpr, function, arg, exprTyVars ); 953 937 … … 1008 992 } 1009 993 1010 /// Wraps a function declaration in a new pointer-to-function variable expression1011 VariableExpr *wrapFunctionDecl( DeclarationWithType *functionDecl ) {1012 // line below cloned from FixFunction.cc1013 ObjectDecl *functionObj = new ObjectDecl( functionDecl->get_name(), functionDecl->get_storageClass(), functionDecl->get_linkage(), 0,1014 new PointerType( Type::Qualifiers(), functionDecl->get_type()->clone() ), 0 );1015 functionObj->set_mangleName( functionDecl->get_mangleName() );1016 return new VariableExpr( functionObj );1017 }1018 1019 994 Statement * Pass1::mutate( ReturnStmt *returnStmt ) { 1020 995 if ( retval && returnStmt->get_expr() ) { … … 1032 1007 1033 1008 // find assignment operator for (polymorphic) return type 1034 ApplicationExpr *assignExpr= 0;1009 DeclarationWithType *assignDecl = 0; 1035 1010 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) { 1036 // find assignment operator for type variable1037 1011 std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() ); 1038 1012 if ( assignIter == assignOps.end() ) { 1039 1013 throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() ); 1040 1014 } // if 1041 assign Expr = new ApplicationExpr( new VariableExpr( assignIter->second ) );1015 assignDecl = assignIter->second; 1042 1016 } else if ( ReferenceToType *refType = dynamic_cast< ReferenceToType *>( retval->get_type() ) ) { 1043 // find assignment operator for generic type1044 1017 ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = scopedAssignOps.find( refType->get_name() ); 1045 1018 if ( assignIter == scopedAssignOps.end() ) { 1046 1019 throw SemanticError( "Attempt to return dtype or ftype generic object in ", returnStmt->get_expr() ); 1047 1020 } 1048 1049 // wrap it up in an application expression1050 1021 DeclarationWithType *functionDecl = assignIter->second; 1051 assignExpr = new ApplicationExpr( wrapFunctionDecl( functionDecl ) ); 1052 assignExpr->set_env( env->clone() ); 1053 1054 // find each of its needed secondary assignment operators 1055 std::list< Expression* > &tyParams = refType->get_parameters(); 1056 std::list< TypeDecl* > &forallParams = functionDecl->get_type()->get_forall(); 1057 std::list< Expression* >::const_iterator tyIt = tyParams.begin(); 1058 std::list< TypeDecl* >::const_iterator forallIt = forallParams.begin(); 1059 for ( ; tyIt != tyParams.end() && forallIt != forallParams.end(); ++tyIt, ++forallIt ) { 1060 if ( (*forallIt)->get_kind() != TypeDecl::Any ) continue; // skip types with no assign op (ftype/dtype) 1061 1062 std::list< DeclarationWithType* > &asserts = (*forallIt)->get_assertions(); 1063 assert( ! asserts.empty() && "Type param needs assignment operator assertion" ); 1064 DeclarationWithType *actualDecl = asserts.front(); 1065 ReferenceToType *actualType = isAssignment( actualDecl ); 1066 assert( actualType && "First assertion of type with assertions should be assignment operator" ); 1067 TypeExpr *formalTypeExpr = dynamic_cast< TypeExpr* >( *tyIt ); 1068 assert( formalTypeExpr && "type parameters must be type expressions" ); 1069 Type *formalType = formalTypeExpr->get_type(); 1070 assignExpr->get_env()->add( actualType->get_name(), formalType ); 1071 1072 DeclarationWithType *assertAssign = 0; 1073 if ( TypeInstType *formalTypeInstType = dynamic_cast< TypeInstType* >( formalType ) ) { 1074 std::map< std::string, DeclarationWithType *>::const_iterator assertAssignIt = assignOps.find( formalTypeInstType->get_name() ); 1075 if ( assertAssignIt == assignOps.end() ) { 1076 throw SemanticError( "No assignment operation found for ", formalTypeInstType ); 1077 } 1078 assertAssign = assertAssignIt->second; 1079 //assignExpr->get_env()->add( formalTypeInstType->get_name(), actualType ); 1080 } else if ( ReferenceToType *formalReferenceType = dynamic_cast< ReferenceToType* >( formalType ) ) { 1081 ScopedMap< std::string, DeclarationWithType *>::const_iterator assertAssignIt = scopedAssignOps.find( formalReferenceType->get_name() ); 1082 if ( assertAssignIt == scopedAssignOps.end() ) { 1083 throw SemanticError( "No assignment operation found for ", formalReferenceType ); 1084 } 1085 assertAssign = assertAssignIt->second; 1086 } else assert( false && "returning polymorphic types with non struct/polymorphic parameters not yet supported" ); 1087 1088 1089 assignExpr->get_inferParams()[ actualDecl->get_uniqueId() ] 1090 = ParamEntry( assertAssign->get_uniqueId(), assertAssign->get_type()->clone(), actualDecl->get_type()->clone(), wrapFunctionDecl( assertAssign ) ); 1091 } 1022 // line below cloned from FixFunction.cc 1023 assignDecl = new ObjectDecl( functionDecl->get_name(), functionDecl->get_storageClass(), functionDecl->get_linkage(), 0, 1024 new PointerType( Type::Qualifiers(), functionDecl->get_type()->clone() ), 0 ); 1025 assignDecl->set_mangleName( functionDecl->get_mangleName() ); 1092 1026 } 1093 assert( assign Expr);1027 assert( assignDecl ); 1094 1028 1095 1029 // replace return statement with appropriate assignment to out parameter 1030 ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignDecl ) ); 1096 1031 Expression *retParm = new NameExpr( retval->get_name() ); 1097 1032 retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) ); … … 1245 1180 } 1246 1181 1247 // add size/align for generic parametertypes to parameter list1182 // add size/align for generic types to parameter list 1248 1183 std::set< std::string > seenTypes; // sizeofName for generic types we've seen 1249 1184 for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) { … … 1360 1295 1361 1296 if ( DeclarationWithType *declWithType = dynamic_cast< DeclarationWithType* >( *decl ) ) { 1362 if ( memberDecl->get_mangleName().empty() || declWithType->get_mangleName().empty() 1363 || memberDecl->get_mangleName() == declWithType->get_mangleName() ) return i; 1297 if ( memberDecl->get_mangleName() == declWithType->get_mangleName() ) return i; 1364 1298 else continue; 1365 1299 } else return i; … … 1407 1341 if ( ! objectType ) return memberExpr; 1408 1342 1409 Expression *newMemberExpr = 0;1410 1343 if ( StructInstType *structType = dynamic_cast< StructInstType* >( objectType ) ) { 1411 1344 // look up offset index … … 1417 1350 fieldLoc->get_args().push_back( makeDerefdVar( varExpr->clone(), varDepth ) ); 1418 1351 fieldLoc->get_args().push_back( makeOffsetIndex( objectType, i ) ); 1419 newMemberExpr = fieldLoc; 1420 } else if ( dynamic_cast< UnionInstType* >( objectType ) ) { 1352 1353 delete memberExpr; 1354 return fieldLoc; 1355 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( objectType ) ) { 1421 1356 // union members are all at offset zero, so build appropriately-dereferenced variable 1422 newMemberExpr = makeDerefdVar( varExpr->clone(), varDepth ); 1357 Expression *derefdVar = makeDerefdVar( varExpr->clone(), varDepth ); 1358 delete memberExpr; 1359 return derefdVar; 1423 1360 } else return memberExpr; 1424 assert( newMemberExpr );1425 1426 // wrap pointer members in appropriate cast1427 if ( dynamic_cast< PointerType* >( memberExpr->get_member()->get_type() ) ) {1428 CastExpr *ptrCastExpr = new CastExpr( newMemberExpr, new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ) ) );1429 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );1430 derefExpr->get_args().push_back( ptrCastExpr );1431 newMemberExpr = derefExpr;1432 }1433 1434 delete memberExpr;1435 return newMemberExpr;1436 1361 } 1437 1362 … … 1454 1379 delete offsetofExpr; 1455 1380 return offsetInd; 1456 } else if ( dynamic_cast< UnionInstType* >( ty ) ) {1381 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( ty ) ) { 1457 1382 // all union members are at offset zero 1458 1383 delete offsetofExpr; -
src/ResolvExpr/CastCost.cc
r6ce67ce r6ed1d4b 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 06:57:43 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Tue Feb 2 15:34:36 201613 // Update Count : 711 // Last Modified By : Rob Schluntz 12 // Last Modified On : Mon Oct 05 14:48:45 2015 13 // Update Count : 5 14 14 // 15 15 … … 69 69 PointerType *destAsPointer = dynamic_cast< PointerType* >( dest ); 70 70 if ( destAsPointer && basicType->isInteger() ) { 71 //cost = Cost( 1, 0, 0 ); 72 cost = Cost::infinity; 71 cost = Cost( 1, 0, 0 ); 73 72 } else { 74 73 ConversionCost::visit( basicType ); … … 88 87 cost = Cost( 0, 0, 1 ); 89 88 } else if ( castResult < 0 ) { 90 cost = Cost::infinity; 91 //cost = Cost( 1, 0, 0 ); 89 cost = Cost( 1, 0, 0 ); 92 90 } // if 93 91 } // if 94 92 } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { 95 93 if ( destAsBasic->isInteger() ) { 96 //cost = Cost( 1, 0, 0 ); 97 cost = Cost::infinity; 94 cost = Cost( 1, 0, 0 ); 98 95 } // if 99 96 } -
src/ResolvExpr/Resolver.cc
r6ce67ce r6ed1d4b 10 10 // Created On : Sun May 17 12:17:01 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Feb 9 21:57:52 201613 // Update Count : 17 912 // Last Modified On : Fri Jul 24 17:33:54 2015 13 // Update Count : 178 14 14 // 15 15 … … 322 322 BasicType::SignedInt); 323 323 } else { 324 DeclarationWithType * decl = lookupId( n);324 DeclarationWithType * decl = lookupId(n); 325 325 initContext = decl->get_type(); 326 326 } … … 344 344 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) { 345 345 if ( isCharType( pt->get_base() ) ) { 346 // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello"; 346 // strip cast if we're initializing a char[] with a char *, e.g. 347 // char x[] = "hello"; 347 348 CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); 348 349 singleInit->set_value( ce->get_arg() ); -
src/SymTab/Validate.cc
r6ce67ce r6ed1d4b 382 382 // it's not a semantic error if the struct is not found, just an implicit forward declaration 383 383 if ( st ) { 384 //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );384 assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() ); 385 385 structInst->set_baseStruct( st ); 386 386 } // if … … 659 659 } 660 660 661 /// Creates a new type decl that's the same as src, but renamed and with only the ?=? assertion (for complete types only)662 TypeDecl *cloneAndRename( TypeDecl *src, const std::string &name ) {663 TypeDecl *dst = new TypeDecl( name, src->get_storageClass(), 0, src->get_kind() );664 665 if ( src->get_kind() == TypeDecl::Any ) {666 // just include assignment operator assertion667 TypeInstType *assignParamType = new TypeInstType( Type::Qualifiers(), name, dst );668 FunctionType *assignFunctionType = new FunctionType( Type::Qualifiers(), false );669 assignFunctionType->get_returnVals().push_back(670 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, assignParamType->clone(), 0 ) );671 assignFunctionType->get_parameters().push_back(672 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), assignParamType->clone() ), 0 ) );673 assignFunctionType->get_parameters().push_back(674 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, assignParamType, 0 ) );675 FunctionDecl *assignAssert = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, assignFunctionType, 0, false, false );676 dst->get_assertions().push_back( assignAssert );677 }678 679 return dst;680 }681 682 661 Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting ) { 683 662 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false ); … … 687 666 std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters(); 688 667 std::list< Expression* > structParams; // List of matching parameters to put on types 689 TypeSubstitution genericSubs; // Substitutions to make to member types of struct690 668 for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) { 691 669 isGeneric = true; 692 TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name());670 TypeDecl *typeParam = (*param)->clone(); 693 671 assignType->get_forall().push_back( typeParam ); 694 TypeInstType *newParamType = new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ); 695 genericSubs.add( (*param)->get_name(), newParamType ); 696 structParams.push_back( new TypeExpr( newParamType ) ); 672 structParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) ); 697 673 } 698 674 … … 725 701 } 726 702 727 if ( isGeneric ) { 728 // rewrite member type in terms of the type variables on this operator 729 DeclarationWithType *fixedMember = dwt->clone(); 730 genericSubs.apply( fixedMember ); 731 732 // assign to both destination and return value 733 if ( ArrayType *array = dynamic_cast< ArrayType * >( fixedMember->get_type() ) ) { 734 makeArrayAssignment( srcParam, dstParam, fixedMember, array, back_inserter( assignDecl->get_statements()->get_kids() ) ); 735 makeArrayAssignment( srcParam, returnVal, fixedMember, array, back_inserter( assignDecl->get_statements()->get_kids() ) ); 736 } else { 737 makeScalarAssignment( srcParam, dstParam, fixedMember, back_inserter( assignDecl->get_statements()->get_kids() ) ); 738 makeScalarAssignment( srcParam, returnVal, fixedMember, back_inserter( assignDecl->get_statements()->get_kids() ) ); 739 } // if 703 if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) { 704 makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) ); 705 if ( isGeneric ) makeArrayAssignment( srcParam, returnVal, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) ); 740 706 } else { 741 // assign to destination 742 if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) { 743 makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) ); 744 } else { 745 makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) ); 746 } // if 707 makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) ); 708 if ( isGeneric ) makeScalarAssignment( srcParam, returnVal, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) ); 747 709 } // if 748 710 } // if … … 762 724 for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) { 763 725 isGeneric = true; 764 TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name());726 TypeDecl *typeParam = (*param)->clone(); 765 727 assignType->get_forall().push_back( typeParam ); 766 728 unionParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) ); -
src/Tests/Makefile
r6ce67ce r6ed1d4b 8 8 OUTPUTS = ${addprefix ${OUTPUTDIR}/,${EXAMPLES:.c=.txt}} 9 9 10 #.SILENT :10 .SILENT : 11 11 12 12 all : … … 19 19 20 20 ${OUTPUTDIR}/%.txt : %.c ${CFA} Makefile 21 -${CFA} -n ${CFAOPT} $< > $@ 2>&121 -${CFA} -n ${CFAOPT} < $< > $@ 2>&1 22 22 23 23 ${OUTPUTDIR}/report : ${OUTPUTS} ${EXPECTDIR} -
src/examples/abs.c
r6ce67ce r6ed1d4b 10 10 // Created On : Thu Jan 28 18:26:16 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 09:32:04201613 // Update Count : 4 412 // Last Modified On : Wed Feb 3 11:14:58 2016 13 // Update Count : 43 14 14 // 15 15 … … 18 18 19 19 int main( void ) { 20 ofstream *sout = ofstream_stdout(); 21 20 22 char ch = -65; 21 23 sout | "char\t\t\t" | ch | "\tabs " | abs( ch ) | endl; -
src/examples/alloc.c
r6ce67ce r6ed1d4b 11 11 // Created On : Wed Feb 3 07:56:22 2016 12 12 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Wed Feb 17 11:43:23201614 // Update Count : 4013 // Last Modified On : Wed Feb 3 16:32:04 2016 14 // Update Count : 38 15 15 // 16 16 … … 27 27 28 28 int main( void ) { 29 ofstream * sout = ofstream_stdout(); 30 29 31 size_t size = 10; 30 32 int * p; … … 98 100 free( x ); 99 101 #endif 102 free( sout ); 100 103 } 101 104 -
src/examples/fstream_test.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 11:45:43 201613 // Update Count : 4 312 // Last Modified On : Tue Jan 26 17:11:33 2016 13 // Update Count : 42 14 14 // 15 15 … … 17 17 18 18 int main( void ) { 19 ofstream *sout = ofstream_stdout(); 20 ifstream *sin = ifstream_stdin(); 19 21 int nombre; 20 22 sout | "Entrez un nombre, s'il vous plaît:\n"; -
src/examples/hello.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 12:11:45201613 // Update Count : 812 // Last Modified On : Tue Jan 26 17:11:49 2016 13 // Update Count : 7 14 14 // 15 15 … … 17 17 18 18 int main() { 19 ofstream *sout = ofstream_stdout(); 20 ifstream *sin = ifstream_stdin(); 19 21 sout | "Bonjour au monde!\n"; 20 22 } -
src/examples/identity.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 12:17:32201613 // Update Count : 1012 // Last Modified On : Tue Jan 26 17:11:58 2016 13 // Update Count : 8 14 14 // 15 15 … … 22 22 23 23 int main() { 24 ofstream *sout = ofstream_stdout(); 24 25 sout | "char\t\t\t" | identity( 'z' ) | endl; 25 26 sout | "signed int\t\t" | identity( 4 ) | endl; … … 29 30 sout | "signed long long int\t" | identity( 4ll ) | endl; 30 31 sout | "unsigned long long int\t" | identity( 4ull ) | endl; 31 sout | "float\t\t\t" | identity( 4. 1f ) | endl;32 sout | "double\t\t\t" | identity( 4. 1) | endl;33 sout | "long double\t\t" | identity( 4. 1l ) | endl;32 sout | "float\t\t\t" | identity( 4.0f ) | endl; 33 sout | "double\t\t\t" | identity( 4.0 ) | endl; 34 sout | "long double\t\t" | identity( 4.0l ) | endl; 34 35 } 35 36 -
src/examples/minmax.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 12:17:53201613 // Update Count : 4 712 // Last Modified On : Wed Feb 3 11:14:49 2016 13 // Update Count : 46 14 14 // 15 15 … … 18 18 19 19 int main( void ) { 20 ofstream *sout = ofstream_stdout(); 20 21 // char does not have less or greater than. 21 22 int ?<?( char op1, char op2 ) { return (int)op1 < (int)op2; } -
src/examples/quad.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 12:19:24201613 // Update Count : 612 // Last Modified On : Tue Jan 26 17:13:48 2016 13 // Update Count : 5 14 14 // 15 15 … … 27 27 28 28 int main() { 29 ofstream *sout = ofstream_stdout(); 29 30 int N = 2; 30 31 sout | "result of quad of " | N | " is " | quad( N ) | endl; -
src/examples/quoted_keyword.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 12:19:45201613 // Update Count : 912 // Last Modified On : Tue Jan 26 17:13:58 2016 13 // Update Count : 8 14 14 // 15 15 … … 28 28 29 29 int main() { 30 ofstream *sout = ofstream_stdout(); 30 31 sout | `catch` + st.`type` + st.`struct` + `throw` | endl; 31 32 } -
src/examples/random.c
r6ce67ce r6ed1d4b 7 7 8 8 int main() { 9 ofstream *sout = ofstream_stdout(); 10 9 11 randseed( getpid() ); // set random seed 10 12 -
src/examples/square.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 12:21:58201613 // Update Count : 2 612 // Last Modified On : Tue Jan 26 17:14:16 2016 13 // Update Count : 25 14 14 // 15 15 … … 23 23 int main() { 24 24 #if 0 25 ofstream *sout = ofstream_stdout(); 25 26 sout | "result of squaring 9 is " | endl; 26 27 -
src/examples/sum.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Feb 16 23:49:31201613 // Update Count : 1 8912 // Last Modified On : Fri Feb 5 16:47:44 2016 13 // Update Count : 139 14 14 // 15 15 … … 33 33 34 34 // Required to satisfy sumable as char does not have addition. 35 const char 0; 36 char ?+?( char t1, char t2 ) { return (int)t1 + t2; } // cast forces integer addition, otherwise recursion 37 char ?+=?( char *t1, char t2 ) { *t1 = *t1 + t2; return *t1; } 38 char ++?( char *t ) { *t += 1; return *t; } 39 char ?++( char *t ) { char temp = *t; *t += 1; return temp; } 35 // const char 0; 36 // char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion 37 // char ++?( char *op ) { *op += 1; return *op; } 38 // char ?++( char *op ) { char temp = *op; *op += 1; return temp; } 40 39 41 40 int main( void ) { 42 41 const int low = 5, High = 15, size = High - low; 42 ofstream *sout = ofstream_stdout(); 43 #if 0 43 44 44 char s = 0, a[size], v = low; 45 char s = 0, a[size]; 46 char v = low; 45 47 for ( int i = 0; i < size; i += 1, v += 1 ) { 46 48 s += v; 47 49 a[i] = v; 48 } // for50 } 49 51 sout | "sum from " | low | " to " | High | " is " 50 52 | (int)sum( size, a ) | ", check " | (int)s | endl; 51 53 52 int s = 0, a[size], v = low; 54 int s = 0, a[size]; 55 int v = low; 53 56 for ( int i = 0; i < size; i += 1, v += 1 ) { 54 57 s += (int)v; 55 58 a[i] = (int)v; 56 } // for59 } 57 60 sout | "sum from " | low | " to " | High | " is " 58 61 | sum( size, (int *)a ) | ", check " | (int)s | endl; 59 62 60 float s = 0.0, a[size], v = low / 10.0; 63 float s = 0.0, a[size]; 64 float v = low / 10.0; 61 65 for ( int i = 0; i < size; i += 1, v += 0.1f ) { 62 66 s += (float)v; 63 67 a[i] = (float)v; 64 } // for68 } 65 69 sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is " 66 70 | sum( size, (float *)a ) | ", check " | (float)s | endl; 71 #endif 72 double s = 0, a[size]; 73 double v = low / 10.0; 67 74 68 double s = 0, a[size], v = low / 10.0;69 75 for ( int i = 0; i < size; i += 1, v += 0.1 ) { 70 76 s += (double)v; 71 77 a[i] = (double)v; 72 } // for78 } 73 79 sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is " 74 80 | sum( size, (double *)a ) | ", check " | (double)s | endl; 75 81 76 struct S { int i, j; } 0 = { 0, 0 }, 1 = { 1, 1 }; 77 S ?+?( S t1, S t2 ) { S s = { t1.i + t2.i, t1.j + t2.j }; return s; } 78 S ?+=?( S *t1, S t2 ) { *t1 = *t1 + t2; return *t1; } 79 S ++?( S *t ) { *t += 1; return *t; } 80 S ?++( S *t ) { S temp = *t; *t += 1; return temp; } 81 ofstream * ?|?( ofstream * os, S v ) { return os | v.i | ' ' | v.j; } 82 83 S s = 0, a[size], v = { low, low }; 84 for ( int i = 0; i < size; i += 1, v += (S)1 ) { 85 s += (S)v; 86 a[i] = (S)v; 87 } // for 88 sout | "sum from " | low | " to " | High | " is " 89 | sum( size, (S *)a ) | ", check " | (S)s | endl; 82 // struct S { int i, j; } sarr[size]; 83 // struct S 0 = { 0, 0 }; 84 // struct S 1 = { 1, 1 }; 85 // S ?+?( S t1, S t2 ) { S s = { t1.i + t1.j, t2.i + t2.j }; return s; } 86 // S ?+=?( S *t1, S t2 ) { *t1 = *t1 + t2; return *t1; } 87 // S ++?( S *t ) { *t += 1; return *t; } 88 // S ?++( S *t ) { S temp = *t; *t += 1; return temp; } 89 // sum( size, sarr ); 90 90 } // main 91 91 -
src/examples/swap.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 12:22:12201613 // Update Count : 6 412 // Last Modified On : Wed Feb 3 11:14:04 2016 13 // Update Count : 63 14 14 // 15 15 … … 18 18 19 19 int main( void ) { 20 ofstream *sout = ofstream_stdout(); 21 20 22 char c1 = 'a', c2 = 'b'; 21 23 sout | "char\t\t\t" | c1 | ' ' | c2 | "\t\t\tswap "; -
src/examples/twice.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 12:23:25201613 // Update Count : 1 312 // Last Modified On : Tue Jan 26 17:14:44 2016 13 // Update Count : 12 14 14 // 15 15 … … 27 27 char ?++( char *op ) { char temp = *op; *op += 1; return temp; } 28 28 29 ofstream *sout = ofstream_stdout(); 29 30 sout | twice( 'a' ) | ' ' | twice( 1 ) | ' ' | twice( 3.2 ) | endl; 30 31 } -
src/examples/vector_test.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 12:23:55201613 // Update Count : 1 812 // Last Modified On : Tue Jan 26 17:14:52 2016 13 // Update Count : 17 14 14 // 15 15 … … 20 20 21 21 int main( void ) { 22 ofstream *sout = ofstream_stdout(); 23 ifstream *sin = ifstream_stdin(); 22 24 vector_int vec = vector_int_allocate(); 23 25 -
src/libcfa/fstream
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 14:02:01 201613 // Update Count : 2212 // Last Modified On : Wed Jan 27 23:47:41 2016 13 // Update Count : 3 14 14 // 15 15 … … 19 19 #include "iostream" 20 20 21 typedef struct ofstream ofstream; 22 21 23 // implement context ostream 22 struct ofstream; 24 ofstream *write( ofstream *, const char *, streamsize_type ); 25 int fail( ofstream * ); 23 26 24 int fail( ofstream * os ); 25 int flush( ofstream * os ); 26 void open( ofstream ** os, const char * name, const char * mode ); 27 void close( ofstream * os ); 28 ofstream * write( ofstream * os, const char * data, streamsize_type size ); 27 ofstream *ofstream_stdout(); 28 ofstream *ofstream_stderr(); 29 ofstream *ofstream_fromfile( const char *name ); 30 void ofstream_close( ofstream *os ); 29 31 30 extern ofstream * sout, * serr;32 typedef struct ifstream ifstream; 31 33 32 34 // implement context istream 33 struct ifstream; 35 ifstream *read( ifstream *, char *, streamsize_type ); 36 ifstream *unread( ifstream *, char ); 37 int fail( ifstream * ); 38 int eof( ifstream * ); 34 39 35 int fail( ifstream * is ); 36 int eof( ifstream * is ); 37 void open( ifstream ** is, const char * name, const char * mode ); 38 void close( ifstream * is ); 39 ifstream * get( ifstream * is, int * data ); 40 ifstream * read( ifstream * is, char * data, streamsize_type size ); 41 ifstream * ungetc( ifstream * is, char c ); 42 43 extern ifstream *sin; 40 ifstream *ifstream_stdin(); 41 ifstream *ifstream_fromfile( const char *name ); 44 42 45 43 #endif // __FSTREAM_H__ -
src/libcfa/fstream.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 14:03:05201613 // Update Count : 7612 // Last Modified On : Tue Jan 26 17:12:59 2016 13 // Update Count : 6 14 14 // 15 15 … … 23 23 struct ofstream { 24 24 FILE *file; 25 int fail; 25 26 }; 26 27 27 #define IO_MSG "I/O error " 28 29 int fail( ofstream * os ) { 30 return ferror( os->file ); 31 } // fail 32 33 int flush( ofstream * os ) { 34 return fflush( os->file ); 35 } // flush 36 37 void open( ofstream ** os, const char * name, const char * mode ) { 38 FILE *t = fopen( name, mode ); 39 if ( t == 0 ) { // do not change unless successful 40 perror( IO_MSG "open output" ); 41 exit( EXIT_FAILURE ); 42 } // if 43 (*os)->file = t; 44 } // open 45 46 void close( ofstream * os ) { 47 if ( os->file == stdout || os->file == stderr ) return; 48 49 if ( fclose( os->file ) == EOF ) { 50 perror( IO_MSG "close output" ); 51 } // if 52 } // close 53 54 ofstream * write( ofstream * os, const char * data, streamsize_type size ) { 55 if ( fail( os ) ) { 56 fprintf( stderr, "attempt write I/O on failed stream\n" ); 57 exit( EXIT_FAILURE ); 58 } // if 59 60 if ( fwrite( data, 1, size, os->file ) != size ) { 61 perror( IO_MSG "write" ); 62 exit( EXIT_FAILURE ); 28 ofstream *write( ofstream *os, const char *data, streamsize_type size ) { 29 if ( ! os->fail ) { 30 fwrite( data, size, 1, os->file ); 31 os->fail = ferror( os->file ); 63 32 } // if 64 33 return os; 65 34 } // write 66 35 67 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) }; 68 ofstream *sout = &soutFile; 69 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) }; 70 ofstream *serr = &serrFile; 36 int fail( ofstream *os ) { 37 return os->fail; 38 } // fail 71 39 72 //--------------------------------------- 40 static ofstream *make_ofstream() { 41 ofstream *new_stream = malloc( sizeof( ofstream ) ); 42 new_stream->fail = 0; 43 return new_stream; 44 } // make_ofstream 45 46 ofstream *ofstream_stdout() { 47 ofstream *stdout_stream = make_ofstream(); 48 stdout_stream->file = stdout; 49 return stdout_stream; 50 } // ofstream_stdout 51 52 ofstream *ofstream_stderr() { 53 ofstream *stderr_stream = make_ofstream(); 54 stderr_stream->file = stderr; 55 return stderr_stream; 56 } // ofstream_stderr 57 58 ofstream *ofstream_fromfile( const char *name ) { 59 ofstream *file_stream = make_ofstream(); 60 file_stream->file = fopen( name, "w" ); 61 file_stream->fail = file_stream->file == 0; 62 return file_stream; 63 } 64 65 void ofstream_close( ofstream *os ) { 66 if ( os->file != stdout && os->file != stderr ) { 67 os->fail = fclose( os->file ); 68 } 69 free( os ); 70 } 73 71 74 72 struct ifstream { 75 73 FILE *file; 74 int fail; 75 int eof; 76 76 }; 77 77 78 int fail( ifstream * is ) { 79 return ferror( is->file ); 80 } // fail 78 ifstream *read( ifstream *is, char *data, streamsize_type size ) { 79 if ( ! is->fail && ! is->eof ) { 80 fread( data, size, 1, is->file ); 81 is->fail = ferror( is->file ); 82 is->eof = feof( is->file ); 83 } 84 return is; 85 } 86 87 ifstream *unread( ifstream *is, char c ) { 88 if ( ! is->fail ) { 89 if ( ! EOF == ungetc( c, is->file ) ) { 90 is->fail = 1; 91 } 92 } 93 return is; 94 } 81 95 82 int eof( ifstream *is ) {83 return feof( is->file );84 } // eof96 int fail( ifstream *is ) { 97 return is->fail; 98 } 85 99 86 ifstream * get( ifstream * is, int * data ) { 87 if ( fscanf( is->file, "%d", data ) == EOF ) { 88 if ( ferror( is->file ) ) { 89 fprintf( stderr, "invalid int read\n" ); 90 exit( EXIT_FAILURE ); 91 } // if 92 } // if 93 return is; 94 } // read 100 int eof( ifstream *is ) { 101 return is->eof; 102 } 95 103 96 ifstream * read( ifstream * is, char * data, streamsize_type size ) { 97 if ( fail( is ) ) { 98 fprintf( stderr, "attempt read I/O on failed stream\n" ); 99 exit( EXIT_FAILURE ); 100 } // if 104 static ifstream *make_ifstream() { 105 ifstream *new_stream = malloc( sizeof( ifstream ) ); 106 new_stream->fail = 0; 107 new_stream->eof = 0; 108 return new_stream; 109 } 101 110 102 if ( fread( data, size, 1, is->file ) == 0 ) { 103 perror( IO_MSG "read" ); 104 exit( EXIT_FAILURE ); 105 } // if 106 return is; 107 } // read 108 109 ifstream *ungetc( ifstream * is, char c ) { 110 if ( fail( is ) ) { 111 fprintf( stderr, "attempt ungetc I/O on failed stream\n" ); 112 exit( EXIT_FAILURE ); 113 } // if 111 ifstream *ifstream_stdin() { 112 ifstream *stdin_stream = make_ifstream(); 113 stdin_stream->file = stdin; 114 return stdin_stream; 115 } 114 116 115 if ( ungetc( c, is->file ) == EOF ) { 116 perror( IO_MSG "ungetc" ); 117 exit( EXIT_FAILURE ); 118 } // if 119 return is; 120 } // ungetc 121 122 void open( ifstream ** is, const char * name, const char * mode ) { 123 FILE *t = fopen( name, mode ); 124 if ( t == 0 ) { // do not change unless successful 125 perror( IO_MSG "open input" ); 126 exit( EXIT_FAILURE ); 127 } // if 128 (*is)->file = t; 129 } // open 130 131 void close( ifstream * is ) { 132 if ( is->file == stdin ) return; 133 134 if ( fclose( is->file ) == EOF ) { 135 perror( IO_MSG "close input" ); 136 } // if 137 } // close 138 139 static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) }; 140 ifstream *sin = &sinFile; 117 ifstream *ifstream_fromfile( const char *name ) { 118 ifstream *file_stream = make_ifstream(); 119 file_stream->file = fopen( name, "r" ); 120 file_stream->fail = file_stream->file == 0; 121 return file_stream; 122 } 141 123 142 124 // Local Variables: // -
src/libcfa/iostream
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 14:04:24201613 // Update Count : 3212 // Last Modified On : Fri Jan 29 15:50:36 2016 13 // Update Count : 29 14 14 // 15 15 … … 22 22 23 23 context ostream( dtype ostype ) { 24 ostype *write( ostype *, const char *, streamsize_type ); 24 25 int fail( ostype * ); 25 int flush( ostype * );26 ostype * write( ostype *, const char *, streamsize_type );27 26 }; 27 28 28 context writeable( type T ) { 29 29 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, T ); … … 52 52 53 53 // writes the range [begin, end) to the given stream 54 forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), dtype os_type | ostream( os_type ) ) 54 forall( type elt_type | writeable( elt_type ), 55 type iterator_type | iterator( iterator_type, elt_type ), 56 dtype os_type | ostream( os_type ) ) 55 57 void write( iterator_type begin, iterator_type end, os_type *os ); 56 58 57 forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), dtype os_type | ostream( os_type ) ) 59 forall( type elt_type | writeable( elt_type ), 60 type iterator_type | iterator( iterator_type, elt_type ), 61 dtype os_type | ostream( os_type ) ) 58 62 void write_reverse( iterator_type begin, iterator_type end, os_type *os ); 59 63 60 // ---------------------------------------64 //****************************************************************************** 61 65 62 66 context istream( dtype istype ) { 67 istype *read( istype *, char *, streamsize_type ); 68 istype *unread( istype *, char ); 63 69 int fail( istype * ); 64 70 int eof( istype * ); 65 istype * get( istype *, int * );66 istype * read( istype *, char *, streamsize_type );67 istype * ungetc( istype *, char );68 71 }; 69 72 -
src/libcfa/iostream.c
r6ce67ce r6ed1d4b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 17 14:19:56201613 // Update Count : 7612 // Last Modified On : Mon Feb 1 14:20:30 2016 13 // Update Count : 60 14 14 // 15 15 … … 19 19 #include <stdio.h> 20 20 #include <string.h> // strlen 21 #include <float.h> // DBL_DIG, LDBL_DIG22 21 #include <complex.h> // creal, cimag 23 22 } … … 73 72 ostype * ?|?( ostype *os, double d ) { 74 73 char buffer[32]; 75 return write( os, buffer, sprintf( buffer, "% .*lg", DBL_DIG, d ) );74 return write( os, buffer, sprintf( buffer, "%g", d ) ); 76 75 } // ?|? 77 76 … … 79 78 ostype * ?|?( ostype *os, long double d ) { 80 79 char buffer[32]; 81 return write( os, buffer, sprintf( buffer, "% .*Lg", LDBL_DIG, d ) );80 return write( os, buffer, sprintf( buffer, "%Lg", d ) ); 82 81 } // ?|? 83 82 … … 111 110 forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) ) 112 111 retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) { 113 return manip( os);112 return manip(os); 114 113 } 115 114 116 115 forall( dtype ostype | ostream( ostype ) ) 117 116 ostype * endl( ostype * os ) { 118 os | "\n";119 flush( os ); 120 return os;117 os | "\n"; 118 // flush 119 return os; 121 120 } // endl 122 121 123 //--------------------------------------- 124 125 forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), 122 forall( type elt_type | writeable( elt_type ), 123 type iterator_type | iterator( iterator_type, elt_type ), 126 124 dtype os_type | ostream( os_type ) ) 127 125 void write( iterator_type begin, iterator_type end, os_type *os ) { 128 void print( elt_type i ) { os | i | ' '; } 126 void print( elt_type i ) { 127 os | i | ' '; 128 } 129 129 for_each( begin, end, print ); 130 130 } // ?|? 131 131 132 forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), 132 forall( type elt_type | writeable( elt_type ), 133 type iterator_type | iterator( iterator_type, elt_type ), 133 134 dtype os_type | ostream( os_type ) ) 134 135 void write_reverse( iterator_type begin, iterator_type end, os_type *os ) { … … 137 138 } // ?|? 138 139 139 //---------------------------------------140 140 141 141 forall( dtype istype | istream( istype ) ) … … 146 146 forall( dtype istype | istream( istype ) ) 147 147 istype * ?|?( istype *is, int *ip ) { 148 return get( is, ip ); 148 char cur; 149 150 // skip some whitespace 151 do { 152 is | &cur; 153 if ( fail( is ) || eof( is ) ) return is; 154 } while ( !( cur >= '0' && cur <= '9' ) ); 155 156 // accumulate digits 157 *ip = 0; 158 while ( cur >= '0' && cur <= '9' ) { 159 *ip = *ip * 10 + ( cur - '0' ); 160 is | &cur; 161 if ( fail( is ) || eof( is ) ) return is; 162 } 163 164 unread( is, cur ); 165 return is; 149 166 } // ?|? 150 167 -
src/libcfa/stdlib.c
r6ce67ce r6ed1d4b 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Feb 10 15:45:56201613 // Update Count : 1 4012 // Last Modified On : Fri Feb 5 15:41:24 2016 13 // Update Count : 128 14 14 // 15 15 … … 23 23 #include <string.h> // memset 24 24 #include <malloc.h> // malloc_usable_size 25 #include <stdio.h> 25 26 #include <math.h> // fabsf, fabs, fabsl 26 27 #include <complex.h> // _Complex_I, cabsf, cabs, cabsl … … 105 106 long int ato( const char * ptr ) { 106 107 long int li; 107 if ( sscanf( ptr, "%ld", &li ) == EOF ) {} // check return code108 if ( sscanf( ptr, "%ld", &li ) == EOF ) {}; // check return code 108 109 return li; 109 110 } 110 111 unsigned long int ato( const char * ptr ) { 111 112 unsigned long int uli; 112 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {} // check return code113 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}; // check return code 113 114 return uli; 114 115 } 115 116 long long int ato( const char * ptr ) { 116 117 long long int lli; 117 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {} // check return code118 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}; // check return code 118 119 return lli; 119 120 } 120 121 unsigned long long int ato( const char * ptr ) { 121 122 unsigned long long int ulli; 122 if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {} // check return code123 if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}; // check return code 123 124 return ulli; 124 125 } 125 126 float ato( const char * ptr ) { 126 127 float f; 127 if ( sscanf( ptr, "%f", &f ) == EOF ) {} // check return code128 if ( sscanf( ptr, "%f", &f ) == EOF ) {}; // check return code 128 129 return f; 129 130 } 130 131 double ato( const char * ptr ) { 131 132 double d; 132 if ( sscanf( ptr, "%lf", &d ) == EOF ) {} // check return code133 if ( sscanf( ptr, "%lf", &d ) == EOF ) {}; // check return code 133 134 return d; 134 135 } 135 136 long double ato( const char * ptr ) { 136 137 long double ld; 137 if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {} // check return code 138 printf( "FRED " ); 139 if ( sscanf( ptr, "%.32Lf", &ld ) == EOF ) {}; // check return code 138 140 return ld; 139 141 } 140 142 float _Complex ato( const char * ptr ) { 141 143 float re, im; 142 if ( sscanf( ptr, "%g%g i", &re, &im ) == EOF ) {}// check return code144 if ( sscanf( ptr, "%g%g", &re, &im ) == EOF ) {}; // check return code 143 145 return re + im * _Complex_I; 144 146 } 145 147 double _Complex ato( const char * ptr ) { 146 148 double re, im; 147 if ( sscanf( ptr, "% lf%lfi", &re, &im ) == EOF ) {}// check return code149 if ( sscanf( ptr, "%.16lg%.16lg", &re, &im ) == EOF ) {}; // check return code 148 150 return re + im * _Complex_I; 149 151 } 150 152 long double _Complex ato( const char * ptr ) { 151 153 long double re, im; 152 if ( sscanf( ptr, "% Lf%Lfi", &re, &im ) == EOF ) {}// check return code154 if ( sscanf( ptr, "%.32Lg%.32Lg", &re, &im ) == EOF ) {}; // check return code 153 155 return re + im * _Complex_I; 154 156 }
Note:
See TracChangeset
for help on using the changeset viewer.