- Timestamp:
- Jul 14, 2024, 5:57:41 PM (5 months ago)
- Branches:
- master
- Children:
- a1a1f37d
- Parents:
- 8315947
- Location:
- libcfa/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/enum.cfa
r8315947 r2e6b2a0 1 1 #include "enum.hfa" 2 2 #include "fstream.hfa" 3 #include <stdlib.h> // qsort4 3 #include <string.h> 5 4 … … 11 10 E lower = lowerBound(); 12 11 // It is okay to overflow as overflow will be theoretically caught by the other bound 13 assert( i <= fromInstance(upper) && i >= fromInstance(lower) 14 && "Not a valid value"); 12 if ( i < fromInstance( lower ) || i > fromInstance( upper ) ) 13 abort( "call to fromInt has index %d outside enumeration range %d-%d", 14 i, fromInstance( lower ), fromInstance( upper ) ); 15 15 return fromInt_unsafe( i ); 16 16 } … … 18 18 E succ( E e ) { 19 19 E upper = upperBound(); 20 assert( (fromInstance(e) < fromInstance(upper)) 21 && "Calling succ() on the last");20 if ( fromInstance( e ) >= fromInstance( upper ) ) 21 abort( "call to succ() exceeds enumeration upper bound of %d", fromInstance( upper ) ); 22 22 return succ_unsafe(e); 23 23 } … … 25 25 E pred( E e ) { 26 26 E lower = lowerBound(); 27 assert( (fromInstance(e) > fromInstance(lower)) 28 && "Calling pred() on the first");27 if ( fromInstance(e) <= fromInstance(lower ) ) 28 abort( "call to pred() exceeds enumeration lower bound of %d", fromInstance( lower ) ); 29 29 return pred_unsafe(e); 30 30 } … … 37 37 } 38 38 39 int scmp( const void * str1, const void * str2 ) {40 return -strcmp( *(char **)str1, *(char **)str2 ); // dscending order41 } // scmp42 43 39 forall( istype & | istream( istype ), E | CfaEnum( E ) ) 44 40 istype & ?|?( istype & is, E & e ) { 45 // printf("here0\n" );41 // fprintf( stderr, "here0\n" ); 46 42 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 47 43 48 44 // Match input enumerator string to enumerator labels. 49 int len = -1; 50 const char * cpy[ 20 /*countof( E )*/ ]; 45 int N = Countof( e ), lnths[N], max = 0; 46 // printf( "N %d\n", N ); 47 // for ( s; E : i; 0~@ ) { 51 48 int i = 0; 52 49 for ( s; E ) { 53 cpy[i] = label( s ); 54 printf( "%s\n", cpy[i] ); 50 lnths[i] = strlen( label( s ) ); 51 if ( lnths[i] > max ) max = lnths[i]; 52 // fprintf( stderr, "%s %d %d\n", label( s ), lnths[i], max ); 55 53 i += 1; 56 } 57 printf( "%d\n", i ); 58 qsort( cpy, i, sizeof(char*), scmp ); 59 i = 0; 60 for ( s; E ) { 61 printf( "%s\n", cpy[i] ); 62 i += 1; 63 } 64 int j = 0; 65 X : for ( s; E ) { 66 len = strlen( cpy[j] ); 67 printf( "%s %d\n", cpy[j], len ); 68 char fmtstr[len + 16]; 69 fmtstr[0] = ' '; // optional leadig whitespace 70 strcpy( &fmtstr[1], cpy[j] ); // copy format and add %n 71 strcpy( &fmtstr[len + 1], "%n" ); 72 printf( "%s\n", fmtstr ); 73 len = -1; 74 // scanf cursor does not move if no match 75 fmt( is, fmtstr, &len ); 76 printf( "%s %d %d\n", fmtstr, len, j ); 77 if ( eof( is ) ) { break; } 78 if ( len != -1 ) { 79 for ( s; E ) { 80 if ( strcmp( label( s ), cpy[j] ) == 0 ) { e = s; break X; } 81 } 82 } 83 j += 1; 54 } // for 55 56 int win = -1; 57 char ch, curr = '\0', prev = '\0'; 58 59 fmt( is, " " ); // skip optional whitespace 60 for ( c; max ) { 61 int args = fmt( is, "%c", &ch ); // read character 62 if ( eof( is ) ) { 63 // fprintf( stderr, "Eof1\n" ); 64 if ( c == 0 ) return is; // no characters read ? 65 clear( is ); // => reset EOF => detect again on next read 66 // fprintf( stderr, "Eof2\n" ); 67 goto W; 68 } 69 if ( args != 1 ) throwResume ExceptionInst( missing_data ); 70 // printf( "read '%c'\n", ch ); 71 for ( i; N ) { 72 // printf( "%d %d %d\n", c, i, lnths[i] ); 73 if ( c < lnths[i] ) { // eligible for this checking round ? 74 char match = label( fromInt( i ) )[c]; // optimization 75 // printf( "%c '%c'\n", match, ch ); 76 if ( (match == ch) && (c == 0 || curr == label( fromInt( i ) )[c - 1]) ) { 77 // printf( "match %d %d %d '%c' '%c' '%c' '%c' 'c'\n", c, i, lnths[i], match, ch, prev, label( fromInt( i ) )[c - 1] ); 78 win = c; 79 prev = curr; 80 curr = ch; 81 break; 82 } // if 83 } // if 84 } else { 85 // fprintf( stderr, "finished win: %d ch: '%c' curr: '%c' prev: '%c'\n", win, ch, curr, prev ); 86 ungetc( ch, is ); 87 if ( win == -1 ) throwResume ExceptionInst( missing_data ); 88 goto W; // break does not work 89 } // for 90 // printf( "\n" ); 91 // } else { 92 // fprintf( stderr, "finished2 %d\n", win ); 93 } // for 94 W :; 95 for ( i; N ) { // scan for winner, must succeed 96 if ( win == lnths[i] - 1 ) { 97 char match = label( fromInt( i ) )[win]; // optimization 98 // printf( "finished1 win: %d i: %d lnth: %d match: '%c' curr: '%c' prev: '%c'\n", win, i, lnths[i], match, curr, prev ); 99 if ( (match == curr) && (win == 0 || prev == label( fromInt( i ) )[win - 1]) ) { 100 e = fromInt( i ); 101 break; 102 } 103 } // if 84 104 } else { 85 //ExceptionInst( missing_data ); 105 // fprintf( stderr, "finished3 %d\n", win ); 106 throwResume ExceptionInst( missing_data ); 86 107 } // for 87 printf( "X %s %d\n", label( e ), len );88 if ( ! eof( is ) && len == -1 ) throwResume ExceptionInst( missing_data );89 90 // if ( eof( is ) ) throwResume ExceptionInst( missing_data );91 // char val[256];92 // int args = fmt( is, "%255s", val );93 // if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data );94 // for ( s; E ) {95 // if ( strcmp(val, label( s )) == 0 ) { e = s; break; }96 // } else {97 // fprintf( stderr, "invalid enumeration constant\n" );98 // abort(); // cannot use abort stream99 // } // for100 108 return is; 101 109 } 102 103 // forall( ostype & | ostream( ostype ), E | CfaEnum( E, quasi_void ) ) {104 // ostype & ?|?( ostype & os, E e ) {105 // return os | label( e );106 // }107 // OSTYPE_VOID_IMPL( E )108 // }109 110 110 111 forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) { … … 114 115 OSTYPE_VOID_IMPL( E ) 115 116 } 116 117 // -
libcfa/src/enum.hfa
r8315947 r2e6b2a0 1 1 #pragma once 2 2 3 #include <assert.h>4 3 #include "iostream.hfa" 5 4
Note: See TracChangeset
for help on using the changeset viewer.