From 5c62505554389cce45b20fca8d7584d534b5135e Mon Sep 17 00:00:00 2001 From: chai2010 Date: Wed, 7 Sep 2022 22:58:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=91=BD=E4=BB=A4=E8=A1=8C,?= =?UTF-8?q?=20=E5=8E=BB=E6=8E=89=E5=AF=B9=20clang=20=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/wa.yml | 29 ++--- internal/app/wabt_wat2wasm.go | 1 + main.go | 213 +++++++++++++++------------------- 3 files changed, 104 insertions(+), 139 deletions(-) diff --git a/.github/workflows/wa.yml b/.github/workflows/wa.yml index 1f1da1f..4d93058 100644 --- a/.github/workflows/wa.yml +++ b/.github/workflows/wa.yml @@ -19,32 +19,25 @@ jobs: uses: actions/setup-go@v2 with: go-version: 1.17 + - name: Setup Wasmer uses: wasmerio/setup-wasmer@v1 - - name: Set up Clang - uses: egor-tensin/setup-clang@v1 - with: - version: latest - platform: x64 - - run: wasmer -V - run: go version - run: go env - - run: clang --version - - run: go test -v -coverprofile=profile.cov ./... - run: go install - run: wa -h - run: wa hello.wa - - run: wa run _examples/hello - run: wa run _examples/prime + - run: wa run hello.wa - - run: wa wasm hello.wa + - run: wa build -o a.out.wat hello.wa - run: wasmer a.out.wat - uses: shogo82148/actions-goveralls@v1 @@ -74,11 +67,11 @@ jobs: - run: go install - run: wa -h - #- run: wa hello.wa - #- run: wa run _examples/hello - #- run: wa run _examples/prime + - run: wa hello.wa + - run: wa run _examples/prime + - run: wa run hello.wa - - run: wa wasm hello.wa + - run: wa build -o a.out.wat hello.wa - run: wasmer a.out.wat @@ -105,9 +98,9 @@ jobs: - run: go install - run: wa -h - #- run: wa hello.wa - #- run: wa run _examples/hello - #- run: wa run _examples/prime + - run: wa hello.wa + - run: wa run _examples/prime + - run: wa run hello.wa - - run: wa wasm hello.wa + - run: wa build -o a.out.wat hello.wa - run: wasmer a.out.wat diff --git a/internal/app/wabt_wat2wasm.go b/internal/app/wabt_wat2wasm.go index f1a5c3d..9b86a7b 100644 --- a/internal/app/wabt_wat2wasm.go +++ b/internal/app/wabt_wat2wasm.go @@ -30,6 +30,7 @@ func RunWasm(filename string) (stdoutStderr []byte, err error) { if stdoutStderr, err = RunWat2Wasm(filename, "-o", dst); err != nil { return stdoutStderr, err } + defer os.Remove(dst) } wasmBytes, err := os.ReadFile(dst) diff --git a/main.go b/main.go index b318aa6..3a0f2e9 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,9 @@ import ( "fmt" "os" "os/exec" - "runtime" "runtime/debug" + "strings" + "time" "github.com/wa-lang/wa/internal/3rdparty/cli" "github.com/wa-lang/wa/internal/app" @@ -25,15 +26,20 @@ func main() { return info.Main.Version } } - return "(devel)" + return "devel:" + time.Now().Format("2006-01-02+15:04:05") }() cliApp.Flags = []cli.Flag{ - &cli.StringFlag{Name: "os", Usage: "set target OS", Value: runtime.GOOS}, - &cli.StringFlag{Name: "arch", Usage: "set target Arch", Value: runtime.GOARCH}, - &cli.StringFlag{Name: "clang", Usage: "set clang"}, - &cli.BoolFlag{Name: "debug", Aliases: []string{"d"}, Usage: "set debug mode"}, - &cli.StringFlag{Name: "trace", Aliases: []string{"t"}, Usage: "set trace mode (*|app|compiler|loader)"}, + &cli.BoolFlag{ + Name: "debug", + Aliases: []string{"d"}, + Usage: "set debug mode", + }, + &cli.StringFlag{ + Name: "trace", + Aliases: []string{"t"}, + Usage: "set trace mode (*|app|compiler|loader)", + }, } cliApp.Before = func(c *cli.Context) error { @@ -101,39 +107,50 @@ func main() { return nil }, }, - { - Name: "fmt", - Usage: "format Wa package sources", - Action: func(c *cli.Context) error { - waApp := app.NewApp(build_Options(c)) - err := waApp.Fmt(c.Args().First()) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - return nil - }, - }, { Name: "run", Usage: "compile and run Wa program", + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "html", + Usage: "output html", + }, + }, Action: func(c *cli.Context) error { if c.NArg() == 0 { fmt.Fprintf(os.Stderr, "no input file") os.Exit(1) } - waApp := app.NewApp(build_Options(c)) - data, err := waApp.Run(c.Args().First(), nil) - if len(data) != 0 { - fmt.Print(string(data)) + ctx := app.NewApp(build_Options(c)) + output, err := ctx.WASM(c.Args().First()) + if err != nil { + fmt.Println(err) + os.Exit(1) } - if errx, ok := err.(*exec.ExitError); ok { - os.Exit(errx.ExitCode()) + + outfile := "a.out.wat" + if !c.Bool("debug") { + defer os.Remove(outfile) } + + if err = os.WriteFile(outfile, []byte(output), 0666); err != nil { + fmt.Println(err) + os.Exit(1) + } + + stdoutStderr, err := app.RunWasm(outfile) if err != nil { + if len(stdoutStderr) > 0 { + fmt.Println(string(stdoutStderr)) + } fmt.Println(err) + os.Exit(1) } + if len(stdoutStderr) > 0 { + fmt.Println(string(stdoutStderr)) + } + return nil }, }, @@ -146,40 +163,46 @@ func main() { Aliases: []string{"o"}, Usage: "set output file", }, + &cli.BoolFlag{ + Name: "html", + Usage: "output html", + }, }, Action: func(c *cli.Context) error { + outfile := c.String("output") + if c.NArg() == 0 { fmt.Fprintf(os.Stderr, "no input file") os.Exit(1) } - opt := build_Options(c) + ctx := app.NewApp(build_Options(c)) + output, err := ctx.WASM(c.Args().First()) + if err != nil { + fmt.Println(err) + os.Exit(1) + } - outfile := c.String("output") - if outfile == "" { - outfile = "a.out" - if opt.TargetOS == "windows" { - outfile += ".exe" + if outfile != "" && outfile != "-" { + if !strings.HasSuffix(outfile, ".wat") { + outfile += ".wat" + } + err := os.WriteFile(outfile, []byte(output), 0666) + if err != nil { + fmt.Println(err) + os.Exit(1) } + } else { + fmt.Println(string(output)) } - waApp := app.NewApp(opt) - data, err := waApp.Build(c.Args().First(), nil, outfile) - if len(data) != 0 { - fmt.Print(string(data)) - } - if errx, ok := err.(*exec.ExitError); ok { - os.Exit(errx.ExitCode()) - } - if err != nil { - fmt.Println(err) - } return nil }, }, { - Name: "lex", - Usage: "lex Wa source code and print token list", + Hidden: true, + Name: "lex", + Usage: "lex Wa source code and print token list", Action: func(c *cli.Context) error { if c.NArg() == 0 { fmt.Fprintf(os.Stderr, "no input file") @@ -196,8 +219,9 @@ func main() { }, }, { - Name: "ast", - Usage: "parse Wa source code and print ast", + Hidden: true, + Name: "ast", + Usage: "parse Wa source code and print ast", Action: func(c *cli.Context) error { if c.NArg() == 0 { fmt.Fprintf(os.Stderr, "no input file") @@ -214,8 +238,9 @@ func main() { }, }, { - Name: "ssa", - Usage: "print Wa ssa code", + Hidden: true, + Name: "ssa", + Usage: "print Wa ssa code", Action: func(c *cli.Context) error { if c.NArg() == 0 { fmt.Fprintf(os.Stderr, "no input file") @@ -232,8 +257,9 @@ func main() { }, }, { - Name: "cir", - Usage: "print cir code", + Hidden: true, + Name: "cir", + Usage: "print cir code", Action: func(c *cli.Context) error { if c.NArg() == 0 { fmt.Fprintf(os.Stderr, "no input file") @@ -250,8 +276,9 @@ func main() { }, }, { - Name: "asm", - Usage: "parse Wa and print ouput assembly code", + Hidden: true, + Name: "asm", + Usage: "parse Wa and print ouput assembly code", Action: func(c *cli.Context) error { if c.NArg() == 0 { fmt.Fprintf(os.Stderr, "no input file") @@ -268,76 +295,23 @@ func main() { }, }, { - Name: "wasm", - Usage: "parse Wa and print ouput WebAssembly text", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "output", - Aliases: []string{"o"}, - Usage: "set output file", - Value: "a.out.wat", - }, - &cli.BoolFlag{ - Name: "run", - Usage: "run wat or wasm", - }, - }, + Name: "test", + Usage: "test packages", Action: func(c *cli.Context) error { - outfile := c.String("output") - - if c.NArg() == 0 { - fmt.Fprintf(os.Stderr, "no input file") - os.Exit(1) - } - - ctx := app.NewApp(build_Options(c)) - output, err := ctx.WASM(c.Args().First()) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - if outfile != "" && outfile != "-" { - err := os.WriteFile(outfile, []byte(output), 0666) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - } else { - if c.Bool("run") { - outfile = "a.out.wat" - err := os.WriteFile(outfile, []byte(output), 0666) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - } else { - fmt.Println(string(output)) - } - } - - if c.Bool("run") { - stdoutStderr, err := app.RunWasm(outfile) - if err != nil { - if len(stdoutStderr) > 0 { - fmt.Println(string(stdoutStderr)) - } - fmt.Println(err) - os.Exit(1) - } - if len(stdoutStderr) > 0 { - fmt.Println(string(stdoutStderr)) - } - } - + fmt.Println("TODO") return nil }, }, { - Name: "test", - Usage: "test packages", + Name: "fmt", + Usage: "format Wa package sources", Action: func(c *cli.Context) error { - fmt.Println("TODO") + waApp := app.NewApp(build_Options(c)) + err := waApp.Fmt(c.Args().First()) + if err != nil { + fmt.Println(err) + os.Exit(1) + } return nil }, }, @@ -376,9 +350,6 @@ func main() { func build_Options(c *cli.Context) *app.Option { return &app.Option{ - Debug: c.Bool("debug"), - TargetOS: c.String("os"), - TargetArch: c.String("arch"), - Clang: c.String("clang"), + Debug: c.Bool("debug"), } } -- GitLab