forked from cosmos72/gomacro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
300 lines (275 loc) · 8.84 KB
/
cmd.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* gomacro - A Go interpreter with Lisp-like macros
*
* Copyright (C) 2017 Massimiliano Ghilardi
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/lgpl>.
*
*
* cmd.go
*
* Created on: Feb 13, 2017
* Author: Massimiliano Ghilardi
*/
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
. "github.com/cosmos72/gomacro/base"
"github.com/cosmos72/gomacro/classic"
)
type Cmd struct {
*classic.Interp
WriteDeclsAndStmtsToFile, OverwriteFiles bool
}
func (cmd *Cmd) Init() {
cmd.Interp = classic.New()
cmd.ParserMode = 0
cmd.Options = OptTrapPanic | OptShowPrompt | OptShowEval | OptShowEvalType // | OptShowAfterMacroExpansion // | OptDebugMacroExpand // | OptDebugQuasiquote // | OptShowEvalDuration // | OptShowAfterParse
cmd.WriteDeclsAndStmtsToFile = false
cmd.OverwriteFiles = false
}
func (cmd *Cmd) Main(args []string) (err error) {
if cmd.Interp == nil {
cmd.Init()
}
ir := cmd.Interp
env := cmd.Env
var set, clear Options
var repl = true
cmd.WriteDeclsAndStmtsToFile = false
cmd.OverwriteFiles = false
for len(args) > 0 {
switch args[0] {
case "-c", "--collect":
env.Options |= OptCollectDeclarations | OptCollectStatements
case "-e", "--expr":
if len(args) > 1 {
repl = false
buf := bytes.NewBufferString(args[1])
buf.WriteByte('\n') // because ReadMultiLine() needs a final '\n'
env.Options |= OptShowEval // set by default, overridden by -s, -v and -vv
env.Options = (env.Options | set) &^ clear
_, err := cmd.EvalReader(buf)
if err != nil {
return err
}
args = args[1:]
}
case "-f", "--force-overwrite":
cmd.OverwriteFiles = true
case "-h", "--help":
return cmd.Usage()
case "-i", "--repl":
repl = false
env.Options |= OptShowPrompt | OptShowEval | OptShowEvalType // set by default, overridden by -s, -v and -vv
env.Options = (env.Options | set) &^ clear
ir.ReplStdin()
case "-m", "--macro-only":
set |= OptMacroExpandOnly
clear &^= OptMacroExpandOnly
case "-n", "--no-trap":
set &^= OptTrapPanic | OptPanicStackTrace
clear |= OptTrapPanic | OptPanicStackTrace
case "-t", "--trap":
set |= OptTrapPanic | OptPanicStackTrace
clear &= OptTrapPanic | OptPanicStackTrace
case "-s", "--silent":
set &^= OptShowEval | OptShowEvalType
clear |= OptShowEval | OptShowEvalType
case "-v", "--verbose":
set = (set | OptShowEval) &^ OptShowEvalType
clear = (clear &^ OptShowEval) | OptShowEvalType
case "-vv", "--very-verbose":
set |= OptShowEval | OptShowEvalType
clear &^= OptShowEval | OptShowEvalType
case "-w", "--write-decls":
cmd.WriteDeclsAndStmtsToFile = true
case "-x", "--exec":
clear |= OptMacroExpandOnly
set &^= OptMacroExpandOnly
default:
arg := args[0]
if len(arg) > 0 && arg[0] == '-' {
fmt.Fprintf(env.Stderr, "gomacro: unrecognized option '%s'.\nTry 'gomacro --help' for more information\n", arg)
return nil
}
repl = false
if cmd.WriteDeclsAndStmtsToFile {
env.Options |= OptCollectDeclarations | OptCollectStatements
}
env.Options &^= OptShowPrompt | OptShowEval | OptShowEvalType // cleared by default, overridden by -s, -v and -vv
env.Options = (env.Options | set) &^ clear
cmd.EvalFileOrDir(arg)
env.Imports, env.Declarations, env.Statements = nil, nil, nil
}
args = args[1:]
}
if repl {
env.Options |= OptShowPrompt | OptShowEval | OptShowEvalType // set by default, overridden by -s, -v and -vv
env.Options = (env.Options | set) &^ clear
ir.ReplStdin()
}
return nil
}
func (cmd *Cmd) Usage() error {
fmt.Fprint(cmd.Env.Stdout, `usage: gomacro [OPTIONS] [files-and-dirs]
Recognized options:
-c, --collect collect declarations and statements, to print them later
-e, --expr EXPR evaluate expression
-f, --force-overwrite option -w will overwrite existing files
-h, --help show this help and exit
-i, --repl interactive. start a REPL after evaluating expression, files and dirs.
default: start a REPL only if no expressions, files or dirs are specified
-m, --macro-only do not execute code, only parse and macroexpand it.
useful to run gomacro as a Go preprocessor
-n, --no-trap do not trap panics in the interpreter
-t, --trap trap panics in the interpreter (default)
-s, --silent silent. do NOT show startup message, prompt, and expressions results.
default when executing files and dirs.
-v, --verbose verbose. show startup message, prompt, and expressions results.
default when executing an expression.
-vv, --very-verbose as -v, and in addition show the type of expressions results.
default when executing a REPL
-w, --write-decls write collected declarations and statements to *.go files.
implies -c
-x, --exec execute parsed code (default). disabled by -m
Options are processed in order, except for -i that is always processed as last.
Collected declarations and statements can be also written to standard output
or to a file with the REPL command :write
`)
return nil
}
func (cmd *Cmd) EvalFilesAndDirs(filesAndDirs ...string) error {
for _, fileOrDir := range filesAndDirs {
err := cmd.EvalFileOrDir(fileOrDir)
if err != nil {
return err
}
}
return nil
}
func (cmd *Cmd) EvalFileOrDir(fileOrDir string) error {
info, err := os.Stat(fileOrDir)
if err != nil {
return err
}
if info.IsDir() {
return cmd.EvalDir(fileOrDir)
} else {
return cmd.EvalFile(fileOrDir)
}
}
func (cmd *Cmd) EvalDir(dirname string) error {
files, err := ioutil.ReadDir(dirname)
if err != nil {
return err
}
for _, file := range files {
filename := file.Name()
if !file.IsDir() && strings.HasSuffix(filename, ".gomacro") {
filename = fmt.Sprintf("%s%c%s", dirname, os.PathSeparator, filename)
err := cmd.EvalFile(filename)
if err != nil {
return err
}
}
}
return nil
}
// use line comments for disclaimer: block comments prevent Go build tags from working
const disclaimer = `// -------------------------------------------------------------
// DO NOT EDIT! this file was generated automatically by gomacro
// Any change will be lost when the file is re-generated
// -------------------------------------------------------------
`
func (cmd *Cmd) EvalFile(filename string) (err error) {
env := cmd.Env
env.Declarations = nil
env.Statements = nil
saveFilename := env.Filename
f, err := os.Open(filename)
if err != nil {
return err
}
defer func() {
f.Close()
env.Filename = saveFilename
}()
env.Filename = filename
var comments string
comments, err = cmd.EvalReader(f)
if err != nil {
return err
}
if cmd.WriteDeclsAndStmtsToFile {
outname := filename
if dot := strings.LastIndexByte(outname, '.'); dot >= 0 {
// sanity check: dot must be in the file name, NOT in its path
if slash := strings.LastIndexByte(outname, os.PathSeparator); slash < dot {
outname = outname[0:dot]
}
}
outname += ".go"
if !cmd.OverwriteFiles {
_, err := os.Stat(outname)
if err == nil {
env.Warnf("file exists already, use -f to force overwriting: %v", outname)
return nil
}
}
env.WriteDeclsToFile(outname, disclaimer, comments)
if env.Options&OptShowEval != 0 {
fmt.Fprintf(env.Stdout, "// processed file: %v\t-> %v\n", filename, outname)
}
}
return nil
}
func (cmd *Cmd) EvalReader(src io.Reader) (comments string, err error) {
defer func() {
if rec := recover(); rec != nil {
switch rec := rec.(type) {
case error:
err = rec
default:
err = errors.New(fmt.Sprint(rec))
}
}
}()
in := bufio.NewReader(src)
ir := cmd.Interp
env := ir.Env
env.Options &^= OptShowPrompt // parsing a file: suppress prompt
env.Line = 0
// perform the first iteration manually, to collect comments
str, firstToken := env.ReadMultiline(in, ReadOptCollectAllComments)
if firstToken >= 0 {
comments = str[0:firstToken]
if firstToken > 0 {
str = str[firstToken:]
env.IncLine(comments)
}
}
if ir.ParseEvalPrint(str, in) {
for ir.ReadParseEvalPrint(in) {
}
}
return comments, nil
}