-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
132 lines (104 loc) · 3.39 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
package main
import (
"errors"
"flag"
"fmt"
"mutant/cli"
"mutant/global"
"os"
"path/filepath"
"runtime"
"strings"
)
const (
RELEASECMD = "release"
VERSION = "Version: 2.1.0_dev"
)
func main() {
if len(os.Args) == 1 {
cli.RunRepl(VERSION, false)
return
}
if len(os.Args) == 2 {
if os.Args[1] == "-h" || os.Args[1] == "--help" {
fmt.Println("mutant - an open source, secure by default programming language")
fmt.Println()
fmt.Println("USAGE: mutant COMMAND [OPTIONS...]")
fmt.Println("Options:")
fmt.Println("\tmutant")
fmt.Println("\t\tRun mutant in REPL mode.")
fmt.Println()
fmt.Println("\tmutant -em, --enableMacros")
fmt.Println("\t\tRun mutant in REPL mode with experimental macros support.")
fmt.Println()
fmt.Println("\tmutant -h, --help")
fmt.Println("\t\tShow this help message.")
fmt.Println()
fmt.Println("\tmutant -v, --version")
fmt.Println("\t\tShow version information.")
fmt.Println()
fmt.Println("\tmutant <FILENAME>.mut")
fmt.Println("\t\tCompile mutant source code into mutant bytecode.")
fmt.Println()
fmt.Println("\tmutant <FILENAME>.mu")
fmt.Println("\t\tRun mutant bytecode using mutant VM.")
fmt.Println()
fmt.Println("\tmutant release -src <FILENAME>.mut [-os | -arch]")
fmt.Println("\t\tCompile mutant source code into standalone, independent binary executable.")
fmt.Println("")
fmt.Println("\t\tPossible values for -os: darwin | linux | windows.")
fmt.Println("\t\tPossible values for -arch: amd64 | arm64 | arm | 386 | x86. (386 & x86 have same meaning here)")
return
}
if os.Args[1] == "-em" || os.Args[1] == "enableMacros" {
cli.RunRepl(VERSION, true)
return
}
if os.Args[1] == "-v" || os.Args[1] == "--version" {
fmt.Println(VERSION)
return
}
if strings.HasSuffix(os.Args[1], global.MutantSourceCodeFileExtention) {
cli.CompileCode(os.Args[1], "", "", false)
return
}
if strings.HasSuffix(os.Args[1], global.MutantByteCodeCompiledFileExtension) {
cli.RunCode(os.Args[1])
return
}
}
if len(os.Args) >= 2 && os.Args[1] == RELEASECMD {
src, goos, goarch, err := prepareRelease(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Compiling Release Build....")
cli.CompileCode(src, goos, goarch, true)
return
}
}
func prepareRelease(args []string) (string, string, string, error) {
var goos, goarch, src string
releasecmd := flag.NewFlagSet(RELEASECMD, flag.ExitOnError)
releasecmd.StringVar(&src, "src", "", "Mutant Source Code File Path by using -src flag")
releasecmd.StringVar(&goos, "os", runtime.GOOS, "Use thie flag to specify target OS for cross-compilation by using -os flag")
releasecmd.StringVar(&goarch, "arch", runtime.GOARCH, "Use thie flag to specify target Architecture for cross-compilation by using -arch flag")
if err := releasecmd.Parse(args[2:]); err != nil {
return "", "", "", err
}
if releasecmd.Parsed() {
if src == "" {
return "", "", "", errors.New("mutant source code file path is required, please use -src flag")
}
if !strings.HasSuffix(src, global.MutantSourceCodeFileExtention) {
return "", "", "", errors.New("incorrect file extension, this program only works for mutant source code files")
}
absSrc, err := filepath.Abs(src)
if err != nil {
return "", "", "", err
}
return absSrc, goos, goarch, nil
}
return "", "", "", errors.New("could not parse values")
}