-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (123 loc) · 3.83 KB
/
main.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
package main
import (
"fmt"
"github.com/juju/loggo"
"github.com/vsartor/gvm/gvm"
"github.com/vsartor/gvm/gvm/compiler"
"github.com/vsartor/gvm/gvm/vm"
"os"
"path"
"strconv"
"time"
)
var appLogger loggo.Logger
func init() {
appLogger = loggo.GetLogger("gvm")
appLogger.SetLogLevel(loggo.INFO)
}
func currentTimestamp() string {
nanoseconds := time.Now().UnixNano()
return strconv.FormatInt(nanoseconds, 10)
}
func main() {
args := os.Args[1:]
if len(args) == 0 {
appLogger.Criticalf("Expected at least one argument. Call 'gvm help' for usage.\n")
os.Exit(1)
}
// Check early if we're actually doing logging
hasLogging := false
if args[0] == "l" {
gvm.Logger.SetLogLevel(loggo.INFO)
hasLogging = true
} else if args[0] == "L" {
gvm.Logger.SetLogLevel(loggo.DEBUG)
hasLogging = true
}
// Error out in case only a debug flag was passed
if hasLogging && len(args) == 1 {
appLogger.Criticalf("Only received a debug flag.\n")
os.Exit(1)
}
// Remove logging argument from args
if hasLogging {
args = args[1:]
}
// Dispatch into the correct routine
switch runMode := args[0]; runMode {
case "c", "compile":
if len(args) != 3 {
appLogger.Criticalf("Expected two files after 'compile': <source_path>, <object_path>\n")
os.Exit(1)
}
compiler.Compile(args[1], args[2])
case "r", "run":
if len(args) < 2 {
appLogger.Criticalf("Expected one file after 'run': <object_path>\n")
os.Exit(1)
}
vm.Execute(args[1], args[2:])
case "d", "disassemble":
if len(args) != 2 {
appLogger.Criticalf("Expected one file after 'disassemble': <object_path>\n")
os.Exit(1)
}
vm.Disassemble(args[1])
case "D", "debug":
if len(args) < 2 {
appLogger.Criticalf("Expected one file after 'run': <object_path>\n")
os.Exit(1)
}
vm.Debug(args[1], args[2:])
case "cr":
// For composite commands, if output directory is not given write to tmpdir
if len(args) == 2 {
args = append(args, path.Join(os.TempDir(), currentTimestamp()))
}
if len(args) < 3 {
appLogger.Criticalf("Expected two files after 'cr': <source_path>, <object_path>\n")
os.Exit(1)
}
compiler.Compile(args[1], args[2])
vm.Execute(args[2], args[3:])
case "cd":
// For composite commands, if output directory is not given write to tmpdir
if len(args) == 2 {
args = append(args, path.Join(os.TempDir(), currentTimestamp()))
}
if len(args) != 3 {
appLogger.Criticalf("Expected two files after 'cd': <source_path>, <object_path>\n")
os.Exit(1)
}
compiler.Compile(args[1], args[2])
vm.Disassemble(args[2])
case "cD":
// For composite commands, if output directory is not given write to tmpdir
if len(args) == 2 {
args = append(args, path.Join(os.TempDir(), currentTimestamp()))
}
if len(args) < 3 {
appLogger.Criticalf("Expected two files after 'cD': <source_path>, <object_path>\n")
os.Exit(1)
}
compiler.Compile(args[1], args[2])
vm.Debug(args[2], args[3:])
case "h", "help":
fmt.Println("gvm [logging flag] <command> [input file] [output file]")
fmt.Println("Available commands:")
fmt.Println(" help (h) Shows this message.")
fmt.Println(" compile (c) Compiles a file.")
fmt.Println(" run (r) Runs a compiled file.")
fmt.Println(" disassemble (d) Disassembles and pretty prints a compiled file.")
fmt.Println(" debug (D) Runs a compiled file in debug mode.")
fmt.Println(" cd Compiles, disassembles and pretty prints a compiled file.")
fmt.Println(" cr Compiles a file and then runs it.")
fmt.Println(" cD Compiles a file and then runs it in debug mode.")
fmt.Println("Available logging flags:")
fmt.Println(" l Basic logging.")
fmt.Println(" L Verbose logging.")
default:
appLogger.Criticalf("Unknown command '%s'.\n", runMode)
os.Exit(1)
}
}