maketab.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * This file is part of Jehanne.
  3. *
  4. * Copyright (C) 2016 Giacomo Tesio <giacomo@tesio.it>
  5. *
  6. * Jehanne is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 2 of the License.
  9. *
  10. * Jehanne is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * this program makes the table to link function names
  20. * and type indices that is used by execute() in run.c.
  21. * it finds the indices in y.tab.h, produced by yacc.
  22. */
  23. package main
  24. import (
  25. "bufio"
  26. "log"
  27. "os"
  28. "strings"
  29. "strconv"
  30. "text/template"
  31. )
  32. type Flist struct {
  33. Token string
  34. Name string
  35. Pname string
  36. }
  37. var flist = []Flist {
  38. { "PROGRAM", "program", "" },
  39. { "BOR", "boolop", " || " },
  40. { "AND", "boolop", " && " },
  41. { "NOT", "boolop", " !" },
  42. { "NE", "relop", " != " },
  43. { "EQ", "relop", " == " },
  44. { "LE", "relop", " <= " },
  45. { "LT", "relop", " < " },
  46. { "GE", "relop", " >= " },
  47. { "GT", "relop", " > " },
  48. { "ARRAY", "array", "" },
  49. { "INDIRECT", "indirect", "$(" },
  50. { "SUBSTR", "substr", "substr" },
  51. { "SUB", "sub", "sub" },
  52. { "GSUB", "gsub", "gsub" },
  53. { "INDEX", "sindex", "sindex" },
  54. { "SPRINTF", "awksprintf", "sprintf" },
  55. { "ADD", "arith", " + " },
  56. { "MINUS", "arith", " - " },
  57. { "MULT", "arith", " * " },
  58. { "DIVIDE", "arith", " / " },
  59. { "MOD", "arith", " % " },
  60. { "UMINUS", "arith", " -" },
  61. { "POWER", "arith", " **" },
  62. { "PREINCR", "incrdecr", "++" },
  63. { "POSTINCR", "incrdecr", "++" },
  64. { "PREDECR", "incrdecr", "--" },
  65. { "POSTDECR", "incrdecr", "--" },
  66. { "CAT", "cat", " " },
  67. { "PASTAT", "pastat", "" },
  68. { "PASTAT2", "dopa2", "" },
  69. { "MATCH", "matchop", " ~ " },
  70. { "NOTMATCH", "matchop", " !~ " },
  71. { "MATCHFCN", "matchop", "matchop" },
  72. { "INTEST", "intest", "intest" },
  73. { "PRINTF", "awkprintf", "printf" },
  74. { "PRINT", "printstat", "print" },
  75. { "CLOSE", "closefile", "closefile" },
  76. { "DELETE", "awkdelete", "awkdelete" },
  77. { "SPLIT", "split", "split" },
  78. { "ASSIGN", "assign", " = " },
  79. { "ADDEQ", "assign", " += " },
  80. { "SUBEQ", "assign", " -= " },
  81. { "MULTEQ", "assign", " *= " },
  82. { "DIVEQ", "assign", " /= " },
  83. { "MODEQ", "assign", " %= " },
  84. { "POWEQ", "assign", " ^= " },
  85. { "CONDEXPR", "condexpr", " ?: " },
  86. { "IF", "ifstat", "if(" },
  87. { "WHILE", "whilestat", "while(" },
  88. { "FOR", "forstat", "for(" },
  89. { "DO", "dostat", "do" },
  90. { "IN", "instat", "instat" },
  91. { "NEXT", "jump", "next" },
  92. { "NEXTFILE", "jump", "nextfile" },
  93. { "EXIT", "jump", "exit" },
  94. { "BREAK", "jump", "break" },
  95. { "CONTINUE", "jump", "continue" },
  96. { "RETURN", "jump", "ret" },
  97. { "BLTIN", "bltin", "bltin" },
  98. { "CALL", "call", "call" },
  99. { "ARG", "arg", "arg" },
  100. { "VARNF", "getnf", "NF" },
  101. { "GETLINE", "getline", "getline" },
  102. { "", "", "" },
  103. }
  104. type Proctab struct {
  105. Code int
  106. Name string
  107. Func string
  108. }
  109. func getf(token string) Flist {
  110. for i := range flist {
  111. if token == flist[i].Token {
  112. return flist[i]
  113. }
  114. }
  115. return Flist{"","nullproc",""}
  116. }
  117. func main() {
  118. file, err := os.Open("y.tab.h")
  119. if err != nil {
  120. log.Fatal(err)
  121. }
  122. defer file.Close()
  123. proctab := make([]Proctab, 0)
  124. scanner := bufio.NewScanner(file)
  125. for scanner.Scan() {
  126. s := scanner.Text()
  127. if strings.Contains(s, "#define") {
  128. tokens := strings.Split(s, " ");
  129. i, err := strconv.Atoi(tokens[2])
  130. if err == nil {
  131. f := getf(tokens[1])
  132. proctab = append(proctab, Proctab{i, tokens[1], f.Name})
  133. }
  134. }
  135. }
  136. if err := scanner.Err(); err != nil {
  137. log.Fatal(err)
  138. }
  139. tmpl, err := template.New("proctab.c").Parse(`/* automatically generated by maketab */
  140. #include <u.h>
  141. #include <lib9.h>
  142. #include <bio.h>
  143. #include "awk.h"
  144. #include "y.tab.h"
  145. static char *printname[{{len .}}] = {
  146. {{ range . }} (char *) "{{.Name}}", /* {{.Code}} */
  147. {{ end }}
  148. };
  149. Cell *(*proctab[{{len .}}])(Node **, int) = {
  150. {{ range . }} {{.Func}}, /* {{.Name}} */
  151. {{ end }}
  152. };
  153. char *
  154. tokname(int n)
  155. {
  156. static char buf[100];
  157. if (n < FIRSTTOKEN || n > LASTTOKEN) {
  158. sprint(buf, "token %d", n);
  159. return buf;
  160. }
  161. return printname[n-FIRSTTOKEN];
  162. }
  163. `)
  164. if err != nil {
  165. log.Fatal(err)
  166. }
  167. err = tmpl.Execute(os.Stdout, proctab)
  168. if err != nil {
  169. log.Fatal(err)
  170. }
  171. }