gisp.go 925 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package main
  2. import (
  3. "github.com/jcla1/gisp/generator"
  4. "github.com/jcla1/gisp/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. a := generator.EvalExprs(p)
  39. fset := token.NewFileSet()
  40. ast.Print(fset, a)
  41. var buf bytes.Buffer
  42. printer.Fprint(&buf, fset, a)
  43. fmt.Printf("%s\n", buf.String())
  44. }
  45. }