-
Notifications
You must be signed in to change notification settings - Fork 1
/
vilmos.go
133 lines (121 loc) · 2.62 KB
/
vilmos.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
package main
import (
"errors"
"fmt"
"log"
"os"
inter "github.com/Vinetwigs/vilmos/v2/interpreter"
"github.com/urfave/cli/v2"
)
const (
version string = "2.1.1"
usage string = "Official vilmos language interpreter"
)
var (
ErrorNoImage = errors.New("error: no specified image")
ErrorWrongCommand = errors.New("error: wrong command")
)
func main() {
var (
debug bool
configPath string
maxSize int
instructionSize int
imagePath string
)
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"V", "v"},
Usage: "Shows installed version",
}
app := &cli.App{
Name: "vilmos",
Version: version,
Authors: []*cli.Author{
{
Name: "Vinetwigs",
Email: "github.com/Vinetwigs",
},
},
Usage: usage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Value: false,
Usage: "enable debug mode",
Destination: &debug,
},
&cli.StringFlag{
Name: "config",
Aliases: []string{"conf", "c"},
Usage: "load configuration from `FILE_PATH` for custom color codes",
Value: "",
Destination: &configPath,
},
&cli.IntFlag{
Name: "max_size",
Aliases: []string{"m"},
Usage: "set max memory `SIZE`",
Value: -1,
Destination: &maxSize,
},
&cli.IntFlag{
Name: "instruction_size",
Aliases: []string{"is", "size", "s"},
Usage: "set instruction `SIZE`",
Value: 1,
Destination: &instructionSize,
},
&cli.StringFlag{
Name: "input",
Aliases: []string{"i"},
Usage: "input `FILE_PATH`",
Value: "",
Destination: &imagePath,
},
},
Action: func(c *cli.Context) error {
if imagePath != "" {
i := inter.NewInterpreter(debug, maxSize, instructionSize)
if configPath != "" {
err := inter.LoadConfigs(configPath)
if err != nil {
logError(err)
}
}
err := i.LoadImage(imagePath)
if err != nil {
logError(err)
}
i.Run()
} else {
logError(ErrorNoImage)
}
return nil
},
Commands: []*cli.Command{
{
Name: "version",
Aliases: []string{"v"},
Usage: "show installed version",
Action: func(c *cli.Context) error {
fmt.Printf("vilmos %s\n", version)
return nil
},
},
},
CommandNotFound: func(c *cli.Context, command string) {
logError(ErrorWrongCommand)
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func logError(e error) {
fmt.Printf("\n")
log.Println("\033[31m" + e.Error() + "\033[0m")
os.Exit(2)
}