1# usage: cat pseudocode | sed -f act2num | awk -f compile.awk 2# 3# 4BEGIN { "date" | getline 5 today = $0 6 printf("\n/* this file generated on %s */\n", today ) 7 printf("\nstatic char pseudo_code [ ] = { \n" ) 8 opl = 0 # op codes on the current line 9 10 opc = 0 # opcode counter 11 fpi = 0 # fill pointer for idx array 12} 13 14/^;/ { } # line starting with semicolon is comment 15 16/^[A-Z]/ { # start of a new action 17 emit( 0 ) 18 idx[ ++fpi ] = opc 19 name[ fpi ] = $1 20 emit( $2 ) 21} 22 23/^[\t ]/ { 24 emit( $1 ) 25} 26 27END { 28 if ( opl > 8 ) { 29 printf("\n") 30 } 31 printf("\t 0\n};\n\n") 32 printf("static short int pseudo_code_idx [ ] ={\n") 33 opl = 0 34 emit( 0 ) 35 for( ii = 1; ii <= fpi; ii++ ) 36 emit( idx[ ii ] ) 37 if ( opl > 8 ) { 38 printf("\n") 39 } 40 printf("\t 0\n};\n\n") 41 42 printf("#define %-10s \t %3d \n", "NOP", 0 ) 43 for( ii = 1; ii <= fpi; ii++ ) 44 printf("#define %-10s \t %3d \n", name[ ii ], ii ) 45 printf("\n") 46} 47 48function emit( opcode ){ # Niclaus Wirth 49 if ( opl > 8 ) { 50 printf("\n") 51 opl = 0 52 } 53 opl = opl +1 54 printf("\t%4d,", opcode ) 55 opc++ 56} 57 58