Improve CLI
This commit is contained in:
parent
eec2a2c96e
commit
7d38efd106
39
main.go
39
main.go
@ -2,9 +2,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@ -83,11 +86,16 @@ func readEmbedDir(name string, files map[string]string) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
outputFile := flag.String("o", "a.out", "Output file")
|
||||
generateWAT := flag.Bool("wat", false, "Generate WAT instead of WASM")
|
||||
includeStdlib := flag.Bool("stdlib", true, "Include the standard library")
|
||||
flag.Parse()
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
log.Fatalln("Usage: " + os.Args[0] + " <files...>")
|
||||
}
|
||||
|
||||
files := os.Args[1:]
|
||||
files := flag.Args()
|
||||
|
||||
fileSources := make(map[string]string)
|
||||
for _, file := range files {
|
||||
@ -99,11 +107,13 @@ func main() {
|
||||
fileSources[file] = string(content)
|
||||
}
|
||||
|
||||
if *includeStdlib {
|
||||
stdlibFiles := make(map[string]string)
|
||||
readEmbedDir("stdlib", stdlibFiles)
|
||||
for path, file := range stdlibFiles {
|
||||
fileSources["[embedded]/"+path] = file
|
||||
}
|
||||
}
|
||||
|
||||
fileTokens := make(map[string][]LexToken)
|
||||
for file, source := range fileSources {
|
||||
@ -136,7 +146,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Parsed:\n%+#v\n\n", parsed)
|
||||
// log.Printf("Parsed:\n%+#v\n\n", parsed)
|
||||
parsedFiles = append(parsedFiles, parsed)
|
||||
}
|
||||
|
||||
@ -169,6 +179,27 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("WAT: " + wat)
|
||||
os.WriteFile("out.wat", []byte(wat), 0o644)
|
||||
// log.Println("WAT: " + wat)
|
||||
|
||||
if *generateWAT {
|
||||
os.WriteFile(*outputFile, []byte(wat), 0o644)
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.Command("wat2wasm", "-o", *outputFile, "-")
|
||||
|
||||
var input bytes.Buffer
|
||||
input.Write([]byte(wat))
|
||||
|
||||
cmd.Stdin = &input
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user