Changeset 7ef4438 for doc/theses/mike_brooks_MMath/programs
- Timestamp:
- Oct 29, 2024, 12:50:56 PM (5 months ago)
- Branches:
- master
- Children:
- b699a61
- Parents:
- 63cf80e (diff), d0296db6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- doc/theses/mike_brooks_MMath/programs
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified doc/theses/mike_brooks_MMath/programs/hello-accordion.cfa ¶
r63cf80e r7ef4438 2 2 #include <stdlib.hfa> 3 3 #include <array.hfa> 4 #include < locale.h> // setlocale4 #include <string.hfa> 5 5 6 6 … … 8 8 9 9 10 forall( [C], [S] ) $\C{// Class size, Students in class}$10 forall( [C], [S] ) $\C{// num courses, num students}$ 11 11 struct School { 12 @array ( int, C )@ classIds; $\C{// nested VLAs}$13 @array ( int, S )@ studentIds;14 @array ( int, C, S )@preferences; $\C{// multidimensional}$12 @array@( string, C ) course_codes; $\C{// nested VLAs}$ 13 @array@( string, S ) student_ids; 14 @array@( int, C, S ) preferences; $\C{// multidimensional}$ 15 15 }; 16 17 18 19 // TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?)20 21 forall( [C], [S] )22 void ?{}( School( C, S ) & this ) {}23 24 forall( [C], [S] )25 void ^?{}( School( C, S ) & this ) {}26 16 27 17 … … 39 29 40 30 forall( [C], [S] ) 41 void init( @School( C, S ) & classes@, int class, int student, int pref ) with( classes ) { 42 classIds[class] = class; $\C{// handle dynamic offsets of fields within structure}$ 43 studentIds[student] = student; 44 preferences[class][student] = pref; 31 int getPref( @School( C, S ) & school@, int is, int pref ) { 32 for ( ic; C ) { 33 int curPref = @school.preferences@[ic][is]; $\C{// offset calculation implicit}$ 34 if ( curPref == pref ) return ic; 35 } 36 assert( false ); 45 37 } 46 38 … … 56 48 57 49 50 int main() { 51 int nc, ns; 52 sin | nc | " courses,"; 53 sin | ns | " students"; 54 @School( nc, ns ) school;@ 55 // ... elided: read data into school 58 56 59 57 60 int main() { 61 int classes, students; 62 sin | classes | students; 63 @School( classes, students ) school;@ 64 int class, student, preference; 65 // read data into school calling init 66 // for each student's class/preferences 67 try { 68 for ( ) { 69 sin | class | student | preference; 70 init( school, class, student, preference ); 71 } 72 } catch( end_of_file * ) {} 73 for ( s; students ) { 74 sout | "student" | s | nonl; 75 for ( c; classes ) { 76 sout | school.preferences[c][s] | nonl; 58 59 60 { string sv; 61 int iv; 62 // headers' row 63 sin | "\nc\\s"; 64 for ( is ; ns ) { 65 // column label 66 sin | sv; 67 school.student_ids[is] = sv; 68 } 69 // body rows 70 for ( ic ; nc ) { 71 // row label 72 sin | sv; 73 school.course_codes[ic] = sv; 74 for ( is ; ns ) { 75 // matrix item 76 sin | iv; 77 school.preferences[ic][is] = iv; 78 } 79 } 80 } 81 82 83 84 85 86 87 88 89 90 for ( is; ns ) { 91 sout | school.student_ids[is] | ": " | nonl; 92 for ( pref; 1 ~= nc ) { 93 int ic = getPref(school, is, pref); 94 sout | school.course_codes[ ic ] | nonl; 77 95 } 78 96 sout | nl; … … 81 99 82 100 83 84 /*85 $\$$ cat school186 2 287 0 0 188 1 0 789 0 1 1290 1 1 1391 $\$$ a.out < school192 student 0 1 793 student 1 12 1394 95 $\$$ cat school296 3 397 0 0 198 1 0 799 2 0 8100 0 1 12101 1 1 13102 2 1 14103 0 2 26104 1 2 27105 2 2 28106 $\$$ a.out < school2107 student 0 1 7 8108 student 1 12 13 14109 student 2 26 27 28110 */111 112 101 // Local Variables: // 113 102 // compile-command: "sed -f sedcmd hello-accordion.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" // -
TabularUnified doc/theses/mike_brooks_MMath/programs/hello-array.cfa ¶
r63cf80e r7ef4438 49 49 */ 50 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #ifdef SHOWERR1 65 51 66 void fred() { 67 68 69 70 array( float, @10@ ) x; 71 array( float, @20@ ) y; 72 f( x, x ); $\C[0.5in]{// ok}$ 73 f( y, y ); $\C{// ok}$ 74 f( x, y ); $\C{// error}\CRT$ 75 76 77 78 79 } 80 81 82 83 84 85 86 87 88 89 90 forall( [M], [N] ) 91 void bad( array(float, M) &x, 92 array(float, N) &y ) { 93 f( x, x ); $\C[0.5in]{// ok}$ 94 f( y, y ); $\C{// ok}$ 95 96 f( x, y ); $\C{// error}\CRT$ 97 } 98 99 #endif 100 101 102 103 104 105 106 107 108 109 110 forall( [M], [N] ) 111 void bad_fixed( array( float, M ) & x, 112 array( float, N ) & y ) { 113 f( x, x ); $\C[0.5in]{// ok}$ 114 f( y, y ); $\C{// ok}$ 115 if ( M == N ) 116 f( x, @(array( float, M ) &)@y ); $\C{// ok}$ 117 } 118 119 120 121 122 123 124 void fred_ok_only() { 52 125 array( float, @10@ ) x; 53 126 array( float, @20@ ) y; … … 57 130 } 58 131 59 #ifdef SHOWERR160 forall( [M], [N] )61 void bad( array(float, M) &x, array(float, N) &y ) {62 f( x, x ); $\C[1.5in]{// ok}$63 f( y, y ); $\C{// ok}$64 f( x, y ); $\C{// error}\CRT$65 }66 #endif67 68 69 132 70 133 forall( [M], [N] ) 71 void bad_fixed( array( float, M ) & x, array( float, N ) & y ) { 72 if ( M == N ) 73 f( x, @(array( float, M ) &)@y ); $\C{// cast y to matching type}$ 134 void bad_ok_only( array(float, M) &x, 135 array(float, N) &y ) { 136 f( x, x ); 137 f( y, y ); 138 139 // f( x, y ); 74 140 } 141 75 142 76 143 // Local Variables: // -
TabularUnified doc/theses/mike_brooks_MMath/programs/school1 ¶
r63cf80e r7ef4438 1 2 2 2 0 0 1 3 1 0 7 4 0 1 12 5 1 1 13 1 3 courses, 2 students 2 c\s 90111111 90222222 3 ENGL101 3 2 4 PHYS101 1 3 5 CHEM101 2 1 -
TabularUnified doc/theses/mike_brooks_MMath/programs/school2 ¶
r63cf80e r7ef4438 1 3 3 2 0 0 1 3 1 0 7 4 2 0 8 5 0 1 12 6 1 1 13 7 2 1 14 8 0 2 26 9 1 2 27 10 2 2 28 1 5 courses, 3 students 2 c\s 90111111 90222222 90333333 3 ENGL101 3 2 3 4 PHYS101 1 3 4 5 CHEM101 2 1 5 6 PHIL101 4 5 2 7 MATH499 5 4 1
Note: See TracChangeset
for help on using the changeset viewer.