mirror of
https://github.com/RoBaertschi/tt.git
synced 2025-04-18 23:13:29 +00:00
move the main package into main.go and move out all the compile logic into the cmd package
This commit is contained in:
parent
6513c45936
commit
3c3c1fc880
37
cmd/cmd.go
37
cmd/cmd.go
@ -1,8 +1,7 @@
|
||||
package main
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@ -18,6 +17,14 @@ import (
|
||||
"robaertschi.xyz/robaertschi/tt/typechecker"
|
||||
)
|
||||
|
||||
type Arguments struct {
|
||||
Output string
|
||||
Input string
|
||||
OnlyEmitAsm bool
|
||||
}
|
||||
|
||||
// Prefix writer writes a prefix befor a call to an other writer
|
||||
// This Prefix is written befor each new line
|
||||
type prefixWriter struct {
|
||||
output io.Writer
|
||||
outputPrefix []byte
|
||||
@ -66,27 +73,11 @@ func (w *prefixWriter) Write(p []byte) (n int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s [flags] input\nPossible flags:\n", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
func Compile(args Arguments) {
|
||||
output := args.Output
|
||||
input := args.Input
|
||||
onlyEmitAsm := args.OnlyEmitAsm
|
||||
|
||||
var output string
|
||||
flag.StringVar(&output, "o", "", "Output a executable named `file`")
|
||||
flag.StringVar(&output, "output", "", "Output a executable named `file`")
|
||||
onlyEmitAsm := flag.Bool("S", false, "Only emit the asembly file and exit")
|
||||
flag.Parse()
|
||||
|
||||
input := flag.Arg(0)
|
||||
if input == "" {
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if output == "" {
|
||||
output = strings.TrimSuffix(input, filepath.Ext(input))
|
||||
}
|
||||
asmOutputName := strings.TrimSuffix(input, filepath.Ext(input)) + ".asm"
|
||||
|
||||
file, err := os.Open(input)
|
||||
@ -158,7 +149,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
if !*onlyEmitAsm {
|
||||
if !onlyEmitAsm {
|
||||
args := []string{asmOutputName, output}
|
||||
cmd := exec.Command(fasmPath, args...)
|
||||
cmd.Stdout = NewPrefixWriterString(os.Stdout, "fasm output: ")
|
||||
|
36
main.go
Normal file
36
main.go
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"robaertschi.xyz/robaertschi/tt/cmd"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s [flags] input\nPossible flags:\n", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
var output string
|
||||
flag.StringVar(&output, "o", "", "Output a executable named `file`")
|
||||
flag.StringVar(&output, "output", "", "Output a executable named `file`")
|
||||
onlyEmitAsm := flag.Bool("S", false, "Only emit the asembly file and exit")
|
||||
flag.Parse()
|
||||
|
||||
input := flag.Arg(0)
|
||||
if input == "" {
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if output == "" {
|
||||
output = strings.TrimSuffix(input, filepath.Ext(input))
|
||||
}
|
||||
|
||||
cmd.Compile(cmd.Arguments{Output: output, Input: input, OnlyEmitAsm: *onlyEmitAsm})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user