compile.awk
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:1k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. # usage: cat pseudocode | sed -f act2num | awk -f compile.awk
  2. #
  3. #
  4. BEGIN { "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. opc = 0 # opcode counter
  10. fpi = 0 # fill pointer for idx array
  11. }
  12. /^;/ { }  # line starting with semicolon is comment 
  13. /^[A-Z]/ { # start of a new action 
  14. emit( 0 )
  15. idx[ ++fpi ] = opc
  16. name[ fpi ] = $1 
  17. emit( $2 )
  18. }
  19. /^[t ]/ { 
  20.         emit( $1 )
  21. }
  22. END {
  23. if ( opl > 8 ) {
  24.     printf("n")
  25. }
  26. printf("t  0n};nn")
  27. printf("static short int pseudo_code_idx [ ] ={n")
  28. opl = 0
  29. emit( 0 )
  30. for( ii = 1; ii <= fpi; ii++ )
  31.    emit( idx[ ii ] )
  32. if ( opl > 8 ) {
  33.     printf("n")
  34. }
  35. printf("t  0n};nn")
  36. printf("#define %-10s t %3d n", "NOP", 0 )
  37. for( ii = 1; ii <= fpi; ii++ )
  38.     printf("#define %-10s t %3d n", name[ ii ], ii )
  39. printf("n")
  40. }
  41. function emit( opcode ){ # Niclaus Wirth
  42. if ( opl > 8 ) {
  43.     printf("n")
  44.     opl = 0
  45. }
  46. opl = opl +1
  47. printf("t%4d,", opcode )
  48. opc++
  49. }