main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "runtime/pprof"
  8. "strings"
  9. "github.com/zhemao/glisp/extensions"
  10. "github.com/zhemao/glisp/interpreter"
  11. )
  12. var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
  13. var memprofile = flag.String("memprofile", "", "write mem profile to file")
  14. var exitOnFailure = flag.Bool("exitonfail", false,
  15. "exit on failure instead of starting repl")
  16. var countFuncCalls = flag.Bool("countcalls", false,
  17. "count how many times each function is run")
  18. var precounts map[string]int
  19. var postcounts map[string]int
  20. func CountPreHook(env *glisp.Glisp, name string, args []glisp.Sexp) {
  21. precounts[name] += 1
  22. }
  23. func CountPostHook(env *glisp.Glisp, name string, retval glisp.Sexp) {
  24. postcounts[name] += 1
  25. }
  26. func getLine(reader *bufio.Reader) (string, error) {
  27. line := make([]byte, 0)
  28. for {
  29. linepart, hasMore, err := reader.ReadLine()
  30. if err != nil {
  31. return "", err
  32. }
  33. line = append(line, linepart...)
  34. if !hasMore {
  35. break
  36. }
  37. }
  38. return string(line), nil
  39. }
  40. func isBalanced(str string) bool {
  41. parens := 0
  42. squares := 0
  43. for _, c := range str {
  44. switch c {
  45. case '(':
  46. parens++
  47. case ')':
  48. parens--
  49. case '[':
  50. squares++
  51. case ']':
  52. squares--
  53. }
  54. }
  55. return parens == 0 && squares == 0
  56. }
  57. func getExpression(reader *bufio.Reader) (string, error) {
  58. fmt.Printf("> ")
  59. line, err := getLine(reader)
  60. if err != nil {
  61. return "", err
  62. }
  63. for !isBalanced(line) {
  64. fmt.Printf(">> ")
  65. nextline, err := getLine(reader)
  66. if err != nil {
  67. return "", err
  68. }
  69. line += "\n" + nextline
  70. }
  71. return line, nil
  72. }
  73. func processDumpCommand(env *glisp.Glisp, args []string) {
  74. if len(args) == 0 {
  75. env.DumpEnvironment()
  76. } else {
  77. err := env.DumpFunctionByName(args[0])
  78. if err != nil {
  79. fmt.Println(err)
  80. }
  81. }
  82. }
  83. func repl(env *glisp.Glisp) {
  84. fmt.Printf("glisp version %s\n", glisp.Version())
  85. fmt.Printf("glispext version %s\n", glispext.Version())
  86. reader := bufio.NewReader(os.Stdin)
  87. for {
  88. line, err := getExpression(reader)
  89. if err != nil {
  90. fmt.Println(err)
  91. os.Exit(-1)
  92. }
  93. parts := strings.Split(line, " ")
  94. if len(parts) == 0 {
  95. continue
  96. }
  97. if parts[0] == "quit" {
  98. break
  99. }
  100. if parts[0] == "dump" {
  101. processDumpCommand(env, parts[1:])
  102. continue
  103. }
  104. expr, err := env.EvalString(line)
  105. if err != nil {
  106. fmt.Print(env.GetStackTrace(err))
  107. env.Clear()
  108. continue
  109. }
  110. if expr != glisp.SexpNull {
  111. fmt.Println(expr.SexpString())
  112. }
  113. }
  114. }
  115. func runScript(env *glisp.Glisp, fname string) {
  116. file, err := os.Open(fname)
  117. if err != nil {
  118. fmt.Println(err)
  119. os.Exit(-1)
  120. }
  121. defer file.Close()
  122. err = env.LoadFile(file)
  123. if err != nil {
  124. fmt.Println(err)
  125. os.Exit(-1)
  126. }
  127. _, err = env.Run()
  128. if *countFuncCalls {
  129. fmt.Println("Pre:")
  130. for name, count := range precounts {
  131. fmt.Printf("\t%s: %d\n", name, count)
  132. }
  133. fmt.Println("Post:")
  134. for name, count := range postcounts {
  135. fmt.Printf("\t%s: %d\n", name, count)
  136. }
  137. }
  138. if err != nil {
  139. fmt.Print(env.GetStackTrace(err))
  140. if *exitOnFailure {
  141. os.Exit(-1)
  142. }
  143. repl(env)
  144. }
  145. }
  146. func main() {
  147. env := glisp.NewGlisp()
  148. env.ImportEval()
  149. glispext.ImportRandom(env)
  150. glispext.ImportTime(env)
  151. glispext.ImportChannels(env)
  152. glispext.ImportCoroutines(env)
  153. glispext.ImportRegex(env)
  154. flag.Parse()
  155. if *cpuprofile != "" {
  156. f, err := os.Create(*cpuprofile)
  157. if err != nil {
  158. fmt.Println(err)
  159. os.Exit(-1)
  160. }
  161. err = pprof.StartCPUProfile(f)
  162. if err != nil {
  163. fmt.Println(err)
  164. os.Exit(-1)
  165. }
  166. defer pprof.StopCPUProfile()
  167. }
  168. precounts = make(map[string]int)
  169. postcounts = make(map[string]int)
  170. if *countFuncCalls {
  171. env.AddPreHook(CountPreHook)
  172. env.AddPostHook(CountPostHook)
  173. }
  174. args := flag.Args()
  175. if len(args) > 0 {
  176. runScript(env, args[0])
  177. } else {
  178. repl(env)
  179. }
  180. if *memprofile != "" {
  181. f, err := os.Create(*memprofile)
  182. if err != nil {
  183. fmt.Println(err)
  184. os.Exit(-1)
  185. }
  186. defer f.Close()
  187. err = pprof.Lookup("heap").WriteTo(f, 1)
  188. if err != nil {
  189. fmt.Println(err)
  190. os.Exit(-1)
  191. }
  192. }
  193. }