gisp.go 852 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "./generator"
  4. "./parser"
  5. "bufio"
  6. "bytes"
  7. "fmt"
  8. "go/ast"
  9. "go/printer"
  10. "go/token"
  11. "io/ioutil"
  12. "os"
  13. )
  14. func args(filename string) {
  15. b, err := ioutil.ReadFile(filename)
  16. if err != nil {
  17. panic(err)
  18. }
  19. p := parser.ParseFromString(filename, string(b)+"\n")
  20. a := generator.GenerateAST(p)
  21. fset := token.NewFileSet()
  22. var buf bytes.Buffer
  23. printer.Fprint(&buf, fset, a)
  24. fmt.Printf("%s\n", buf.String())
  25. }
  26. func main() {
  27. if len(os.Args) > 1 {
  28. args(os.Args[1])
  29. return
  30. }
  31. r := bufio.NewReader(os.Stdin)
  32. for {
  33. fmt.Print(">> ")
  34. line, _, _ := r.ReadLine()
  35. p := parser.ParseFromString("<REPL>", string(line)+"\n")
  36. fmt.Println(p)
  37. a := generator.GenerateAST(p)
  38. fset := token.NewFileSet()
  39. ast.Print(fset, a)
  40. var buf bytes.Buffer
  41. printer.Fprint(&buf, fset, a)
  42. fmt.Printf("%s\n", buf.String())
  43. }
  44. }