-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (71 loc) · 2.11 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"lxa/binchunk"
"lxa/compiler"
"lxa/runner"
"os"
)
var (
CLUA bool
GOLUA bool
DEBUG bool
PARSE bool
COMPILE bool
PROGNAME string
)
func init() {
PROGNAME = os.Args[0]
flag.BoolVar(&COMPILE, "c", false, "compile lxa file to lua bytecode")
flag.BoolVar(&DEBUG, "g", false, "enable verbose logging and tracing")
flag.BoolVar(&PARSE, "p", false, "parse and print lua bytecode only")
flag.BoolVar(&GOLUA, "golua", false, "use inner golua vm for excuting")
flag.BoolVar(&CLUA, "clua", false, "use inner official clua 5.3.5 vm for excuting")
flag.Parse()
}
func main() {
if len(os.Args) > 1 {
for _, filename := range flag.Args() {
chunk, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "reading %s: %s", filename, err)
}
if PARSE {
if binchunk.IsBinaryChunk(chunk) {
runner.ParseBinary(chunk)
} else {
fmt.Fprintf(os.Stderr, "parsinging %s: %s", filename, "is not a lua bytecode file")
}
continue
}
var data []byte
if binchunk.IsBinaryChunk(chunk) {
data = chunk
} else {
proto := compiler.Compile(string(chunk), filename)
data = binchunk.Dump(proto)
}
if COMPILE {
ioutil.WriteFile(filename+".luac", data, 0666)
} else {
if GOLUA && !CLUA || DEBUG {
runner.GoRunBinary(data, filename, DEBUG)
} else {
runner.CRunBinary(data, PROGNAME)
}
}
}
} else {
fmt.Println("Oops! No input files given.")
fmt.Println("Lxa 0.2.6 2020.04.03 Copyright (C) 2020 xaxys.")
fmt.Println("usage:", PROGNAME, "[options] [script]")
fmt.Println("avaliable options are:")
fmt.Println(" -c ", "Compile a lxa file to lua bytecode without running")
fmt.Println(" -g ", "Enable verbose logging and tracing (golua vm only)")
fmt.Println(" -p ", "Parse and Print lua bytecode without running")
fmt.Println(" -golua", "Use inner golua vm for excuting (several stdlib unsupported yet)")
fmt.Println(" -clua ", "Use inner official clua 5.3.5 vm for excuting (default vm)")
}
}