sym.awk 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # parse the following:
  2. # : word :: assembler-name
  3. # \ word :: assembler-name
  4. # code word :: assembler-name
  5. # also tries to handle : constant variable create
  6. # and exclude them from inside definitions
  7. BEGIN {
  8. _ord_init()
  9. delete done
  10. compiling = 0
  11. print "\\ generated file - do not modify"
  12. print
  13. print "only forth"
  14. print "also meta-words definitions"
  15. print "meta-compiler"
  16. print
  17. }
  18. # from the gnu/gawk manual
  19. function _ord_init( low, high, i, t)
  20. {
  21. low = sprintf("%c", 7) # BEL is ascii 7
  22. if (low == "\a") { # regular ascii
  23. low = 0
  24. high = 127
  25. } else if (sprintf("%c", 128 + 7) == "\a") {
  26. # ascii, mark parity
  27. low = 128
  28. high = 255
  29. } else { # ebcdic(!)
  30. low = 0
  31. high = 255
  32. }
  33. for (i = low; i <= high; i++) {
  34. t = sprintf("%c", i)
  35. _ord_[t] = i
  36. }
  37. }
  38. function ord(str, c)
  39. {
  40. # only first character is of interest
  41. c = substr(str, 1, 1)
  42. return _ord_[c]
  43. }
  44. function chr(c)
  45. {
  46. # force c to be numeric by adding 0
  47. return sprintf("%c", c + 0)
  48. }
  49. function asm_name(word, name) {
  50. if ("" == name) {
  51. for (i = 1; i <= length(word); ++i) {
  52. c = tolower(substr(word, i, 1))
  53. if ( "-" == c) {
  54. c = "_"
  55. } else if (c !~ /[[:alnum:]]/) {
  56. c = sprintf("_%02x", ord(c))
  57. }
  58. name = name c
  59. }
  60. } else {
  61. name = tolower(name)
  62. gsub("-", "_", name)
  63. }
  64. return name
  65. }
  66. /[[:space:]]::[[:space:]]/ {
  67. word = tolower($2)
  68. name = tolower($4)
  69. if (!done[word]) {
  70. print ": " word " output-symbol\" " asm_name(word, name) "\" ;"
  71. done[word] = 1
  72. }
  73. }
  74. /;[[:space:]]+immediate/ {
  75. print " immediate"
  76. }
  77. # also detects start of compiled definition
  78. # to avoid printing entries for defining words used
  79. # inside colon definitions
  80. /^[[:space:]]*(:|create|variable)[[:space:]]/ {
  81. def = tolower($1)
  82. word = tolower($2)
  83. if (!done[word] && !compiling) {
  84. print ": " word " output-symbol\" " asm_name(word, "") "\" ;"
  85. done[word] = 1
  86. }
  87. if (":" == def) {
  88. compiling = 1
  89. }
  90. }
  91. /^([[:space:]]*|.*[[:space:]])constant[[:space:]]/ {
  92. word = gensub(/^(.*[[:space:]]|[[:space:]]*)constant[[:space:]]+([^[:space:]]+)([[:space:]]*|[[:space:]].*)$/, "\\2", 1)
  93. word = tolower(word)
  94. if (!done[word] && !compiling) {
  95. print ": " word " output-symbol\" " asm_name(word, "") "\" ;"
  96. done[word] = 1
  97. }
  98. }
  99. # this also detects assemble comments
  100. # hopefully this does not matter
  101. /^([[:space:]]*|.*[[:space:]]);([[:space:]]*|[[:space:]].*)$/ {
  102. compiling = 0
  103. }