-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (64 loc) · 1.95 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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"github.com/sneaky-potato/goof/lexer"
"github.com/sneaky-potato/goof/types"
"github.com/sneaky-potato/goof/compiler"
)
func callCmd(cmd string, args ...string) {
command := exec.Command(cmd, args...)
var outb, errb bytes.Buffer
command.Stdout = &outb
command.Stderr = &errb
fmt.Printf("%s ", cmd)
fmt.Println(args)
if err := command.Run(); err != nil {
log.Fatal(err)
}
fmt.Printf("%s", outb.String())
}
func usage(program string) {
fmt.Printf("Usage: %s <OPTION> [ARGS]\n", program)
fmt.Println("OPTIONS:")
fmt.Println(" sim <file> Simulate program")
fmt.Println(" com <file> Compile program")
fmt.Println(" SUBOPTIONS:")
fmt.Println(" -r run the program after successful compilation")
fmt.Println(" -skip-type skip static type checking before compilation")
}
func main() {
simCmd := flag.NewFlagSet("sim", flag.ExitOnError)
comCmd := flag.NewFlagSet("com", flag.ExitOnError)
runOnCom := comCmd.Bool("r", false, "run")
skipTypeChecking := comCmd.Bool("skip-type", false, "skip static type checking")
if len(os.Args) < 2 {
fmt.Println("expected subcommand")
os.Exit(1)
}
switch os.Args[1] {
case "sim":
simCmd.Parse(os.Args[2:])
filePath := simCmd.Args()[0]
_ = lexer.LoadProgramFromFile(filePath)
case "com":
comCmd.Parse(os.Args[2:])
filePath := comCmd.Args()[0]
program := lexer.LoadProgramFromFile(filePath)
if *skipTypeChecking == false {
types.TypeCheckingProgram(program)
}
compiler.CompileToAsm("output.asm", program)
callCmd("nasm", "-felf64", "output.asm")
callCmd("ld", "-o", "output", "output.o")
if *runOnCom {
callCmd("./output", comCmd.Args()[1:]...)
}
default:
usage(os.Args[0])
}
}