forked from ark-lang/ark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (96 loc) · 2.52 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
package main
import (
"fmt"
"os"
"os/exec"
"time"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/ark-lang/ark/codegen"
"github.com/ark-lang/ark/codegen/LLVMCodegen"
"github.com/ark-lang/ark/doc"
"github.com/ark-lang/ark/lexer"
"github.com/ark-lang/ark/parser"
"github.com/ark-lang/ark/util"
)
const (
VERSION = "0.0.2"
AUTHOR = "The Ark Authors"
)
var startTime time.Time
func main() {
startTime = time.Now()
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case buildCom.FullCommand():
ccArgs := []string{}
if *buildStatic {
ccArgs = append(ccArgs, "-static")
}
build(*buildInputs, *buildOutput, *buildCodegen, ccArgs)
printFinishedMessage(startTime, buildCom.FullCommand(), len(*buildInputs))
if *buildRun {
run(*buildOutput)
}
case docgenCom.FullCommand():
docgen(*docgenInputs, *docgenDir)
printFinishedMessage(startTime, docgenCom.FullCommand(), len(*docgenInputs))
}
}
func printFinishedMessage(startTime time.Time, command string, numFiles int) {
dur := time.Since(startTime)
fmt.Printf("%s (%d file(s), %.2fms)\n",
util.TEXT_GREEN+util.TEXT_BOLD+fmt.Sprintf("Finished %s", command)+util.TEXT_RESET,
numFiles, float32(dur.Nanoseconds())/1000000)
}
func setupErr(err string, stuff ...interface{}) {
fmt.Printf(util.TEXT_RED+util.TEXT_BOLD+"Setup error:"+util.TEXT_RESET+" %s\n",
fmt.Sprintf(err, stuff...))
os.Exit(util.EXIT_FAILURE_SETUP)
}
func parseFiles(files []string) []*parser.Module {
sourcefiles := make([]*lexer.Sourcefile, 0)
for _, file := range files {
input, err := lexer.NewSourcefile(file)
if err != nil {
setupErr("%s", err.Error())
}
sourcefiles = append(sourcefiles, input)
}
for _, file := range sourcefiles {
file.Tokens = lexer.Lex(file.Contents, file.Filename, *verbose)
}
parsedFiles := make([]*parser.Module, 0)
for _, file := range sourcefiles {
parsedFiles = append(parsedFiles, parser.Parse(file, *verbose))
}
return parsedFiles
}
func build(input []string, output string, cg string, ccArgs []string) {
parsedFiles := parseFiles(input)
if cg != "none" {
var gen codegen.Codegen
switch cg {
case "llvm":
gen = &LLVMCodegen.Codegen{
OutputName: output,
CCArgs: ccArgs,
}
default:
panic("whoops")
}
gen.Generate(parsedFiles, *verbose)
}
}
func run(output string) {
cmd := exec.Command("./" + output)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
func docgen(input []string, dir string) {
gen := &doc.Docgen{
Input: parseFiles(input),
Dir: dir,
}
gen.Generate(*verbose)
}