forked from rcarmo/gisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
238 lines (191 loc) · 5.81 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package generator
import (
"bytes"
"fmt"
"go/ast"
"go/printer"
"go/token"
"io/ioutil"
log "github.com/sirupsen/logrus"
"github.com/zylisp/zylisp/parser"
)
var anyType = makeSelectorExpr(ast.NewIdent("core"), ast.NewIdent("Any"))
// GenerateAST takes a collection of parser nodes and returns an AST file.
func GenerateAST(tree []parser.Node) *ast.File {
f := &ast.File{Name: ast.NewIdent("main")}
decls := make([]ast.Decl, 0, len(tree))
if len(tree) < 1 {
return f
}
// you can only have (ns ...) as the first form
if isNSDecl(tree[0]) {
name, imports := getNamespace(tree[0].(*parser.CallNode))
f.Name = name
if imports != nil {
decls = append(decls, imports)
}
tree = tree[1:]
}
decls = append(decls, generateDecls(tree)...)
f.Decls = decls
return f
}
func generateDecls(tree []parser.Node) []ast.Decl {
decls := make([]ast.Decl, len(tree))
for i, node := range tree {
if node.Type() != parser.NodeCall {
// XXX Can we log an error and return an emptuy decls? What would that
// mean ...?
log.Panic(MissingCallNodeError)
}
decls[i] = evalDeclNode(node.(*parser.CallNode))
}
return decls
}
func evalDeclNode(node *parser.CallNode) ast.Decl {
// Let's just assume that all top-level functions called will be "def"
if node.Callee.Type() != parser.NodeIdent {
log.Error(CalleeIndentifierMismatchError)
// panic(CalleeIndentifierMismatchError)
// XXX will not panic'ing here break something?
return nil
}
callee := node.Callee.(*parser.IdentNode)
switch callee.Ident {
case "def":
return evalDef(node)
case "defn":
return evalDefn(node)
}
return nil
}
func evalDefn(node *parser.CallNode) ast.Decl {
log.Debugf("Got: %#v", node)
log.Error("Not implemented")
return nil
}
func evalDef(node *parser.CallNode) ast.Decl {
if len(node.Args) < 2 {
// XXX Could we log an error and return a custom decl?
log.Panicf(MissingAssgnmentArgsError, node.Args[0])
}
val := EvalExpr(node.Args[1])
fn, ok := val.(*ast.FuncLit)
ident := makeIdomaticIdent(node.Args[0].(*parser.IdentNode).Ident)
if ok {
if ident.Name == "main" {
mainable(fn)
}
return makeFunDeclFromFuncLit(ident, fn)
}
return makeGeneralDecl(token.VAR,
[]ast.Spec{makeValueSpec([]*ast.Ident{ident}, []ast.Expr{val}, nil)})
}
func isNSDecl(node parser.Node) bool {
if node.Type() != parser.NodeCall {
return false
}
call := node.(*parser.CallNode)
if call.Callee.(*parser.IdentNode).Ident != "ns" {
return false
}
if len(call.Args) < 1 {
return false
}
return true
}
func getNamespace(node *parser.CallNode) (*ast.Ident, ast.Decl) {
return getPackageName(node), getImports(node)
}
func getPackageName(node *parser.CallNode) *ast.Ident {
if node.Args[0].Type() != parser.NodeIdent {
// XXX How does a type mismatch between these two occur? Could we return an
// error here instead? Or maybe return some sort of fallback/default
// package?
log.Panic(NSPackageTypeMismatch)
}
return ast.NewIdent(node.Args[0].(*parser.IdentNode).Ident)
}
func checkNSArgs(node *parser.CallNode) bool {
if node.Callee.Type() != parser.NodeIdent {
return false
}
if callee := node.Callee.(*parser.IdentNode); callee.Ident != "ns" {
return false
}
return true
}
// GenerateASTFromLispFile takes a ZYLISP file and generates the Go AST for it,
// as an AST file set.
func GenerateASTFromLispFile(filename string) (*token.FileSet, *ast.File) {
b, err := ioutil.ReadFile(filename)
if err != nil {
// XXX let's improve the error handling here ... maybe an empty fset? and
// nil a (AST)?
log.Panic(err)
}
fset := token.NewFileSet()
p := parser.ParseFromString(filename, string(b)+"\n")
a := GenerateAST(p)
return fset, a
}
// GenerateASTFromLispString takes a ZYLISP string and generates the Go AST for
// it and returns both a token file set for it as well as its AST expressions.
func GenerateASTFromLispString(data string) (*token.FileSet, []ast.Expr) {
fset := token.NewFileSet()
p := parser.ParseFromString("<REPL>", data+"\n")
a := EvalExprs(p)
return fset, a
}
// PrintASTFromFile takes a filename, generates the Go AST for it, and then
// prints that AST.
func PrintASTFromFile(filename string) {
fset, a := GenerateASTFromLispFile(filename)
ast.Print(fset, a)
}
// WriteASTFromFile takes an input file and an uutput file, reading Lisp from
// the former and writing Go AST to the latter.
func WriteASTFromFile(fromFile string, toFile string) {
var buf bytes.Buffer
fset, a := GenerateASTFromLispFile(fromFile)
ast.Fprint(&buf, fset, a, nil)
err := ioutil.WriteFile(toFile, buf.Bytes(), 0644)
if err != nil {
log.Error(err)
}
}
// PrintASTFromLispString takes Lisp data in the form of a string, parses it,
// and prints the Go AST for it.
func PrintASTFromLispString(data string) {
fset, a := GenerateASTFromLispString(data)
ast.Print(fset, a)
}
// WriteGoFromFile takes an input file and an uutput file, reading Lisp from
// the former and writing the corresponding Go for it to the latter.
func WriteGoFromFile(fromFile string, toFile string) {
var buf bytes.Buffer
fset, a := GenerateASTFromLispFile(fromFile)
printer.Fprint(&buf, fset, a)
err := ioutil.WriteFile(toFile, buf.Bytes(), 0644)
// XXX let's improve the error handling here ...
if err != nil {
log.Error(err)
log.Debug("Tried writing to file:", toFile)
}
}
// PrintGoFromFile takes a filename, generates the Go code for it, and then
// prints the Go.
func PrintGoFromFile(filename string) {
var buf bytes.Buffer
fset, a := GenerateASTFromLispFile(filename)
printer.Fprint(&buf, fset, a)
fmt.Printf("%s\n", buf.String())
}
// PrintGoFromLispString takes Lisp data in the form of a string, parses it,
// and prints the generated Go for it.
func PrintGoFromLispString(data string) {
var buf bytes.Buffer
fset, a := GenerateASTFromLispString(data)
printer.Fprint(&buf, fset, a)
fmt.Printf("%s\n", buf.String())
}