main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package main
  2. import (
  3. "os"
  4. "io"
  5. "fmt"
  6. "reflect"
  7. "io/ioutil"
  8. "kumachan/checker"
  9. "kumachan/loader"
  10. "kumachan/transformer"
  11. "kumachan/parser/syntax"
  12. "kumachan/parser/scanner"
  13. "kumachan/parser"
  14. . "kumachan/error"
  15. )
  16. func check (err error) {
  17. if (err != nil) {
  18. panic(err)
  19. }
  20. }
  21. func debug_parser(file io.Reader, name string, root string) {
  22. var code_bytes, e = ioutil.ReadAll(file)
  23. check(e)
  24. var code_string = string(code_bytes)
  25. var code = []rune(code_string)
  26. var tokens, info, _ = scanner.Scan(code)
  27. fmt.Println("Tokens:")
  28. for i, token := range tokens {
  29. fmt.Printf(
  30. "(%v) at [%v, %v](%v, %v) %v: %v\n",
  31. i,
  32. token.Span.Start,
  33. token.Span.End,
  34. info[token.Span.Start].Row,
  35. info[token.Span.Start].Col,
  36. syntax.Id2Name[token.Id],
  37. string(token.Content),
  38. )
  39. }
  40. var _, exists = syntax.Name2Id[root]
  41. if !exists {
  42. panic("invalid root syntax unit " + root)
  43. }
  44. var tree, err = parser.Parse(code, root, name)
  45. fmt.Println("------------------------------------------------------")
  46. fmt.Println("AST Nodes:")
  47. parser.PrintBareTree(tree.Nodes)
  48. fmt.Println("------------------------------------------------------")
  49. fmt.Println("AST:")
  50. parser.PrintTree(tree)
  51. if err != nil {
  52. fmt.Println(err.Message())
  53. } else {
  54. fmt.Println("------------------------------------------------------")
  55. fmt.Println("Transformed:")
  56. transformer.PrintNode(reflect.ValueOf(transformer.Transform(tree)))
  57. // fmt.Printf("%+v\n", transformer.Transform(tree))
  58. }
  59. }
  60. func debug_loader() (*loader.Module, loader.Index) {
  61. if len(os.Args) != 2 {
  62. panic("invalid arguments")
  63. }
  64. var path = os.Args[1]
  65. var mod, idx, err = loader.LoadEntry(path)
  66. if err != nil {
  67. fmt.Fprintf(os.Stderr, "%s\n", err.Error())
  68. os.Exit(127)
  69. } else {
  70. fmt.Println("Modules loaded, no errors occurred.")
  71. }
  72. return mod, idx
  73. }
  74. func debug_checker(mod *loader.Module, idx loader.Index) {
  75. var _, _, errs = checker.TypeCheck(mod, idx)
  76. if errs != nil {
  77. var messages = make([]ErrorMessage, len(errs))
  78. for i, e := range errs {
  79. messages[i] = e.Message()
  80. }
  81. var msg = MsgFailedToCompile(errs[0], messages)
  82. fmt.Fprintf(os.Stderr, "%s\n", msg.String())
  83. os.Exit(126)
  84. } else {
  85. fmt.Println("Type check finished, no errors occurred.")
  86. }
  87. }
  88. func main () {
  89. // debug_parser(os.Stdin, "[eval]", "module")
  90. var mod, idx = debug_loader()
  91. debug_checker(mod, idx)
  92. }