generator.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package generator
  2. import (
  3. "github.com/jcla1/gisp/parser"
  4. "fmt"
  5. "go/ast"
  6. "go/token"
  7. )
  8. var anyType = makeSelectorExpr(ast.NewIdent("core"), ast.NewIdent("Any"))
  9. func GenerateAST(tree []parser.Node) *ast.File {
  10. f := &ast.File{Name: ast.NewIdent("main")}
  11. decls := make([]ast.Decl, 0, len(tree))
  12. if len(tree) < 1 {
  13. return f
  14. }
  15. // you can only have (ns ...) as the first form
  16. if isNSDecl(tree[0]) {
  17. name, imports := getNamespace(tree[0].(*parser.CallNode))
  18. f.Name = name
  19. if imports != nil {
  20. decls = append(decls, imports)
  21. }
  22. tree = tree[1:]
  23. }
  24. decls = append(decls, generateDecls(tree)...)
  25. f.Decls = decls
  26. return f
  27. }
  28. func generateDecls(tree []parser.Node) []ast.Decl {
  29. decls := make([]ast.Decl, len(tree))
  30. for i, node := range tree {
  31. if node.Type() != parser.NodeCall {
  32. panic("expected call node in root scope!")
  33. }
  34. decls[i] = evalDeclNode(node.(*parser.CallNode))
  35. }
  36. return decls
  37. }
  38. func evalDeclNode(node *parser.CallNode) ast.Decl {
  39. // Let's just assume that all top-level functions called will be "def"
  40. if node.Callee.Type() != parser.NodeIdent {
  41. panic("expecting call to identifier (i.e. def, defconst, etc.)")
  42. }
  43. callee := node.Callee.(*parser.IdentNode)
  44. switch callee.Ident {
  45. case "def":
  46. return evalDef(node)
  47. }
  48. return nil
  49. }
  50. func evalDef(node *parser.CallNode) ast.Decl {
  51. if len(node.Args) < 2 {
  52. panic(fmt.Sprintf("expecting expression to be assigned to variable: %q", node.Args[0]))
  53. }
  54. val := EvalExpr(node.Args[1])
  55. fn, ok := val.(*ast.FuncLit)
  56. ident := makeIdomaticIdent(node.Args[0].(*parser.IdentNode).Ident)
  57. if ok {
  58. if ident.Name == "main" {
  59. mainable(fn)
  60. }
  61. return makeFunDeclFromFuncLit(ident, fn)
  62. } else {
  63. return makeGeneralDecl(token.VAR, []ast.Spec{makeValueSpec([]*ast.Ident{ident}, []ast.Expr{val}, nil)})
  64. }
  65. }
  66. func isNSDecl(node parser.Node) bool {
  67. if node.Type() != parser.NodeCall {
  68. return false
  69. }
  70. call := node.(*parser.CallNode)
  71. if call.Callee.(*parser.IdentNode).Ident != "ns" {
  72. return false
  73. }
  74. if len(call.Args) < 1 {
  75. return false
  76. }
  77. return true
  78. }
  79. func getNamespace(node *parser.CallNode) (*ast.Ident, ast.Decl) {
  80. return getPackageName(node), getImports(node)
  81. }
  82. func getPackageName(node *parser.CallNode) *ast.Ident {
  83. if node.Args[0].Type() != parser.NodeIdent {
  84. panic("ns package name is not an identifier!")
  85. }
  86. return ast.NewIdent(node.Args[0].(*parser.IdentNode).Ident)
  87. }
  88. func checkNSArgs(node *parser.CallNode) bool {
  89. if node.Callee.Type() != parser.NodeIdent {
  90. return false
  91. }
  92. if callee := node.Callee.(*parser.IdentNode); callee.Ident != "ns" {
  93. return false
  94. }
  95. return true
  96. }