- Timestamp:
- Dec 4, 2024, 10:17:49 PM (2 weeks ago)
- Branches:
- master
- Children:
- fc276f3
- Parents:
- 509ec82
- Location:
- libcfa
- Files:
-
- 2 deleted
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/prelude/builtins.c
r509ec82 reae8b37 172 172 }; 173 173 174 forall( E | Serial( E ) ) {174 static inline forall( E | Serial( E ) ) { 175 175 E fromInt( int i ); 176 176 E succ( E e ); … … 179 179 } 180 180 181 182 181 forall( E ) trait CfaEnum { 183 182 const char * label( E e ); … … 188 187 V value( E e ); 189 188 }; 189 190 static inline { 191 forall( E | Serial( E ) ) { 192 E fromInt( int i ) { 193 E upper = upperBound(); 194 E lower = lowerBound(); 195 // It is okay to overflow as overflow will be theoretically caught by the other bound 196 if ( i < fromInstance( lower ) || i > fromInstance( upper ) ) 197 abort( "call to fromInt has index %d outside of enumeration range %d-%d.", 198 i, fromInstance( lower ), fromInstance( upper ) ); 199 return fromInt_unsafe( i ); 200 } 201 202 E succ( E e ) { 203 E upper = upperBound(); 204 if ( fromInstance( e ) >= fromInstance( upper ) ) 205 abort( "call to succ() exceeds enumeration upper bound of %d.", fromInstance( upper ) ); 206 return succ_unsafe(e); 207 } 208 209 E pred( E e ) { 210 E lower = lowerBound(); 211 if ( fromInstance( e ) <= fromInstance(lower ) ) 212 abort( "call to pred() exceeds enumeration lower bound of %d.", fromInstance( lower ) ); 213 return pred_unsafe( e ); 214 } 215 216 int Countof( E ) { 217 E upper = upperBound(); 218 E lower = lowerBound(); 219 return fromInstance( upper ) + fromInstance( lower ) + 1; 220 } 221 } 222 } 223 224 static inline 225 forall( E | CfaEnum(E) | Serial(E) ) { 226 int ?==?( E l, E r ) { return posn( l ) == posn( r ); } // relational operators 227 int ?!=?( E l, E r ) { return posn( l ) != posn( r ); } 228 int ?<?( E l, E r ) { return posn( l ) < posn( r ); } 229 int ?<=?( E l, E r ) { return posn( l ) <= posn( r ); } 230 int ?>?( E l, E r ) { return posn( l ) > posn( r ); } 231 int ?>=?( E l, E r ) { return posn( l ) >= posn( r ); } 232 233 E ++?( E & l ) { // increment operators 234 int pos = posn(l); 235 l = fromInt_unsafe(pos+1); 236 return l; 237 } 238 239 E --?( E & l ) { 240 int pos = posn(l); 241 l = fromInt_unsafe(pos-1); 242 return l; 243 } 244 245 E ?+=? ( E & l, one_t ) { 246 int pos = posn(l); 247 l = fromInt_unsafe(pos+1); 248 return l; 249 } 250 251 E ?-=? ( E & l, one_t ) { 252 int pos = posn(l); 253 l = fromInt_unsafe(pos-1); 254 return l; 255 } 256 257 E ?+=? ( E & l, int i ) { 258 int pos = posn(l); 259 l = fromInt_unsafe(pos+i); 260 return l; 261 } 262 263 E ?-=? ( E & l, int i ) { 264 int pos = posn(l); 265 l = fromInt_unsafe(pos-i); 266 return l; 267 } 268 269 E ?++( E & l ) { 270 int pos = posn(l); 271 l = fromInt_unsafe(pos+1); 272 return fromInt_unsafe(pos); 273 } 274 275 E ?--( E & l ) { 276 int pos = posn(l); 277 l = fromInt_unsafe(pos-1); 278 return fromInt_unsafe(pos); 279 } 280 } 190 281 191 282 // Local Variables: // -
libcfa/src/Makefile.am
r509ec82 reae8b37 85 85 parseconfig.hfa \ 86 86 rational.hfa \ 87 enum.hfa \88 87 stdlib.hfa \ 89 88 strstream.hfa \ -
libcfa/src/iostream.cfa
r509ec82 reae8b37 1202 1202 1203 1203 1204 forall( istype & | istream( istype ), E | CfaEnum( E ) | Serial( E ) ) 1205 istype & ?|?( istype & is, E & e ) { 1206 // fprintf( stderr, "here0\n" ); 1207 if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); 1208 1209 // Match longest input enumerator string to enumerator labels, where enumerator names are unique. 1210 1211 int N = countof( E ), lnths[N], fred = 0; 1212 // printf( "N %d\n", N ); 1213 int r = 0; 1214 // for ( s; E : r; 0~@ ) { 1215 for ( s; E ) { // scan string rows gathering lengths 1216 lnths[r] = strlen( label( s ) ); 1217 if ( lnths[r] > fred ) fred = lnths[r]; 1218 // fprintf( stderr, "%s %d %d\n", label( s ), lnths[r], fred ); 1219 r += 1; 1220 } // for 1221 1222 int mcol = -1; // last match column 1223 char ch, curr = '\0', prev = '\0'; 1224 1225 fmt( is, " " ); // skip optional whitespace 1226 if ( eof( is ) ) throwResume ExceptionInst( end_of_file ); 1227 1228 for ( c; fred ) { // scan columns of the label matix (some columns missing) 1229 int args = fmt( is, "%c", &ch ); // read character 1230 // fprintf( stderr, "fmt args: %d eof: %d\n", args, eof(is) ); 1231 if ( eof( is ) ) { 1232 // fprintf( stderr, "Eof1\n" ); 1233 if ( c == 0 ) return is; // no characters read ? 1234 clear( is ); // => read something => reset EOF => detect again on next read 1235 // fprintf( stderr, "Eof2\n" ); 1236 break; 1237 } // if 1238 if ( args != 1 ) throwResume ExceptionInst( missing_data ); // may be unnecessary since reading single character 1239 1240 // printf( "read '%c'\n", ch ); 1241 for ( r; N ) { // scan enumeration strings for matching character in current column 1242 // printf( "%d %d %d\n", c, r, lnths[r] ); 1243 if ( c < lnths[r] ) { // string long enough for this column check ? 1244 char match = label( fromInt( r ) )[c]; // optimization 1245 // printf( "%c '%c'\n", match, ch ); 1246 // Stop on first match, could be other matches. 1247 if ( (match == ch) && (c == 0 || curr == label( fromInt( r ) )[c - 1]) ) { 1248 // printf( "match %d %d %d '%c' '%c' '%c' '%c' 'c'\n", c, r, lnths[r], match, ch, prev, label( fromInt( r ) )[c - 1] ); 1249 mcol = c; // matching column 1250 prev = curr; // last matching character 1251 curr = ch; // current matching character 1252 break; 1253 } // if 1254 } // if 1255 } else { 1256 // fprintf( stderr, "finished mcol: %d ch: '%c' curr: '%c' prev: '%c'\n", mcol, ch, curr, prev ); 1257 ungetc( ch, is ); // push back last unmatching character 1258 if ( mcol == -1 ) throwResume ExceptionInst( missing_data ); // no matching character in first column 1259 break; 1260 } // for 1261 // printf( "\n" ); 1262 // } else { 1263 // fprintf( stderr, "finished2 %d\n", mcol ); 1264 } // for 1265 1266 for ( c; N ) { // scan enumeration strings of length "mcol" for match 1267 if ( mcol == lnths[c] - 1 ) { 1268 char match = label( fromInt( c ) )[mcol]; // optimization 1269 // printf( "finished1 mcol: %d c: %d lnth: %d match: '%c' curr: '%c' prev: '%c'\n", mcol, c, lnths[c], match, curr, prev ); 1270 if ( (match == curr) && (mcol == 0 || prev == label( fromInt( c ) )[mcol - 1]) ) { 1271 e = fromInt( c ); 1272 break; 1273 } // if 1274 } // if 1275 } else { 1276 // fprintf( stderr, "finished3 %d\n", mcol ); 1277 throwResume ExceptionInst( missing_data ); // no match in this column 1278 } // for 1279 return is; 1280 } 1281 1282 forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) { 1283 ostype & ?|?( ostype & os, E e ) { 1284 return os | label( e ); 1285 } 1286 OSTYPE_VOID_IMPL( os, E ) 1287 } 1288 1204 1289 // Local Variables: // 1205 1290 // tab-width: 4 // -
libcfa/src/iostream.hfa
r509ec82 reae8b37 507 507 } // distribution 508 508 509 forall( istype & | istream( istype ), E | CfaEnum( E ) | Serial(E) ) 510 istype & ?|?( istype &, E & ); 511 512 forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) { 513 ostype & ?|?( ostype &, E ); 514 OSTYPE_VOID( E ); 515 } 516 509 517 // Local Variables: // 510 518 // tab-width: 4 // -
libcfa/src/rational.cfa
r509ec82 reae8b37 21 21 22 22 // Arithmetic, Relational 23 24 forall( T | arithmetic( T ) ) { 23 forall( T | Simple(T) ) { 25 24 // helper routines 26 27 25 // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce 28 26 // rationals. alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm … … 44 42 return gcd( abs( n ), d ); // simplify 45 43 } // simplify 46 44 } 45 46 forall( T | arithmetic( T ) ) { 47 47 // constructors 48 48 … … 197 197 198 198 forall( T ) { 199 forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } | arithmetic( T) )199 forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } | Simple(T) ) 200 200 istype & ?|?( istype & is, rational(T) & r ) { 201 201 is | r.numerator | r.denominator; -
libcfa/src/rational.hfa
r509ec82 reae8b37 78 78 79 79 // I/O 80 forall(T | multiplicative(T) | equality(T)) 81 trait Simple { 82 int ?<?( T, T ); 83 }; 80 84 81 85 forall( T ) { 82 forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } | arithmetic( T) )86 forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } | Simple(T) ) 83 87 istype & ?|?( istype &, rational(T) & ); 84 88
Note: See TracChangeset
for help on using the changeset viewer.