From 7d38efd106f883f47b98a2666d03a64de3abed2f Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Fri, 29 Mar 2024 15:43:55 +0100 Subject: [PATCH] Improve CLI --- main.go | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index c6d5784..33072c1 100644 --- a/main.go +++ b/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 := os.Args[1:] + files := flag.Args() fileSources := make(map[string]string) for _, file := range files { @@ -99,10 +107,12 @@ func main() { fileSources[file] = string(content) } - stdlibFiles := make(map[string]string) - readEmbedDir("stdlib", stdlibFiles) - for path, file := range stdlibFiles { - fileSources["[embedded]/"+path] = file + if *includeStdlib { + stdlibFiles := make(map[string]string) + readEmbedDir("stdlib", stdlibFiles) + for path, file := range stdlibFiles { + fileSources["[embedded]/"+path] = file + } } fileTokens := make(map[string][]LexToken) @@ -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) + } }