-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_compile.go
67 lines (55 loc) · 1.25 KB
/
cmd_compile.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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/google/subcommands"
"github.com/skx/go.vm/compiler"
"github.com/skx/go.vm/lexer"
)
type compileCmd struct {
}
//
// Glue
//
func (*compileCmd) Name() string { return "compile" }
func (*compileCmd) Synopsis() string { return "Compile a simple.vm program." }
func (*compileCmd) Usage() string {
return `compile :
Compile the given input file to a series of bytecodes.
`
}
//
// Flag setup: no flags
//
func (p *compileCmd) SetFlags(f *flag.FlagSet) {
}
//
// Entry-point.
//
func (p *compileCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
//
// For each file on the command-line we can compile 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))
// Compile it
e := compiler.New(l)
e.Compile()
// Write it out - remove the suffix from the file
name := strings.TrimSuffix(file, filepath.Ext(file))
// Add a .raw suffix to the file.
e.Write(name + ".raw")
}
return subcommands.ExitSuccess
}