-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (67 loc) · 1.36 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
package main
import (
"flag"
"io/ioutil"
"os"
"strings"
"github.com/lollipopkit/gommon/log"
"github.com/lollipopkit/lk/compiler/parser"
. "github.com/lollipopkit/lk/json"
"github.com/lollipopkit/lk/repl"
"github.com/lollipopkit/lk/state"
)
var (
args = []string{}
)
func main() {
ast := flag.Bool("a", false, "Write AST Tree Json")
compile := flag.Bool("c", false, "Compile file")
flag.Parse()
args = flag.Args()
if len(args) == 0 {
repl.Repl()
return
}
fPath := args[0]
if *ast {
writeAst(fPath)
} else if *compile {
state.Compile(fPath)
} else {
if strings.HasSuffix(fPath, ".lk") || strings.HasSuffix(fPath, ".lkc") {
runVM(fPath)
} else {
log.Yellow("Can't run file without suffix '.lk(c)':\n" + fPath)
}
}
}
func writeAst(path string) {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Red(err.Error())
os.Exit(1)
}
block := parser.Parse(string(data), path)
j, err := Json.MarshalIndent(block, "", " ")
if err != nil {
log.Red(err.Error())
os.Exit(1)
}
err = ioutil.WriteFile(path+".ast.json", j, 0644)
if err != nil {
log.Red(err.Error())
os.Exit(1)
}
}
func runVM(path string) {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Red("[run] can't read file: " + err.Error())
os.Exit(1)
}
ls := state.New()
defer ls.CatchAndPrint(false)
ls.OpenLibs()
ls.Load(data, path, "bt")
ls.Call(0, -1)
}