-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcmd_dump.go
60 lines (50 loc) · 1.08 KB
/
cmd_dump.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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"github.com/google/subcommands"
"github.com/skx/go.vm/compiler"
"github.com/skx/go.vm/lexer"
)
type dumpCmd struct {
}
//
// Glue
//
func (*dumpCmd) Name() string { return "dump" }
func (*dumpCmd) Synopsis() string { return "Show the lexed output of the given program." }
func (*dumpCmd) Usage() string {
return `dump :
Demonstrate how our lexer performed by dumping the given input file, as a
stream of tokens.
`
}
//
// Flag setup: no flags
//
func (p *dumpCmd) SetFlags(f *flag.FlagSet) {
}
//
// Entry-point.
//
func (p *dumpCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
//
// For each file on the command-line we can dump it.
//
for _, file := range f.Args() {
// Read the file.
input, err := ioutil.ReadFile(file)
if err != nil {
fmt.Printf("Error reading %s - %s\n", file, err.Error())
return subcommands.ExitFailure
}
// Lex it
l := lexer.New(string(input))
// Dump it
e := compiler.New(l)
e.Dump()
}
return subcommands.ExitSuccess
}