From 340a1aa10d992e36fd833d7aa03d51094f00ba16 Mon Sep 17 00:00:00 2001 From: chai2010 Date: Sat, 22 Jul 2023 14:26:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=20wa=20=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 +- internal/app/app_utils.go | 40 -------- internal/app/main.go | 148 +++++++++++++-------------- waroot/examples/Makefile | 12 +-- waroot/examples/arduino-wat/Makefile | 2 +- waroot/examples/arduino/Makefile | 2 +- waroot/examples/expr/Makefile | 2 +- waroot/examples/misc/Makefile | 24 ++--- waroot/examples/surface/Makefile | 2 +- 9 files changed, 98 insertions(+), 138 deletions(-) delete mode 100644 internal/app/app_utils.go diff --git a/Makefile b/Makefile index 3a0a6813..46b7a86a 100644 --- a/Makefile +++ b/Makefile @@ -26,8 +26,8 @@ ci-test-all: go run main.go test std @echo "== std ok ==" - go run main.go ./waroot/hello.wa - cd waroot && go run ../main.go hello.wa + go run main.go run ./waroot/hello.wa + cd waroot && go run ../main.go run hello.wa make -C ./waroot/examples ci-test-all diff --git a/internal/app/app_utils.go b/internal/app/app_utils.go deleted file mode 100644 index e4f9efc2..00000000 --- a/internal/app/app_utils.go +++ /dev/null @@ -1,40 +0,0 @@ -// 版权 @2023 凹语言 作者。保留所有权利。 - -package app - -import ( - "bytes" - "errors" - "io" - "os" - "strings" -) - -func (p *App) readSource(filename string, src interface{}) ([]byte, error) { - if src != nil { - switch s := src.(type) { - case string: - return []byte(s), nil - case []byte: - return s, nil - case *bytes.Buffer: - if s != nil { - return s.Bytes(), nil - } - case io.Reader: - d, err := io.ReadAll(s) - return d, err - } - return nil, errors.New("invalid source") - } - - d, err := os.ReadFile(filename) - return d, err -} - -func (p *App) isWaFile(path string) bool { - if fi, err := os.Lstat(path); err == nil && fi.Mode().IsRegular() { - return strings.HasSuffix(strings.ToLower(path), ".wa") - } - return false -} diff --git a/internal/app/main.go b/internal/app/main.go index 1ed88730..0f4797ee 100644 --- a/internal/app/main.go +++ b/internal/app/main.go @@ -28,11 +28,6 @@ func Main() { cliApp.Version = waroot.GetVersion() cliApp.Flags = []cli.Flag{ - &cli.StringFlag{ - Name: "target", - Usage: fmt.Sprintf("set target os (%s)", strings.Join(config.WaOS_List, "|")), - Value: config.WaOS_Default, - }, &cli.BoolFlag{ Name: "debug", Aliases: []string{"d"}, @@ -46,10 +41,6 @@ func Main() { } cliApp.Before = func(c *cli.Context) error { - if !config.CheckWaOS(c.String("target")) { - fmt.Printf("unknown target: %s\n", c.String("target")) - os.Exit(1) - } if c.Bool("debug") { config.SetDebugMode() } @@ -59,12 +50,9 @@ func Main() { return nil } - // 没有参数时对应 run 命令 + // 没有参数时显示 help 信息 cliApp.Action = func(c *cli.Context) error { - if c.NArg() < 1 { - cli.ShowAppHelpAndExit(c, 0) - } - cliRun(c) + cli.ShowAppHelpAndExit(c, 0) return nil } @@ -90,9 +78,40 @@ func Main() { return nil }, }, + + { + Name: "play", + Usage: "the Wa playground", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "http", + Value: ":2023", + Usage: "set http address", + }, + &cli.StringFlag{ + Name: "target", + Usage: fmt.Sprintf("set target os (%s)", strings.Join(config.WaOS_List, "|")), + Value: config.WaOS_Default, + }, + &cli.StringFlag{ + Name: "tags", + Usage: "set build tags", + }, + }, + Action: func(c *cli.Context) error { + waApp := NewApp(build_Options(c)) + err := waApp.Playground(c.String("http")) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + return nil + }, + }, + { Name: "init", - Usage: "init a sketch wa module", + Usage: "init a sketch Wa module", ArgsUsage: "app-name", Flags: []cli.Flag{ &cli.StringFlag{ @@ -124,25 +143,6 @@ func Main() { return nil }, }, - { - Name: "run", - Usage: "compile and run Wa program", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "target", - Usage: fmt.Sprintf("set target os (%s)", strings.Join(config.WaOS_List, "|")), - Value: config.WaOS_Default, - }, - &cli.StringFlag{ - Name: "tags", - Usage: "set build tags", - }, - }, - Action: func(c *cli.Context) error { - cliRun(c) - return nil - }, - }, { Name: "build", Usage: "compile Wa source code", @@ -218,9 +218,41 @@ func Main() { }, }, { - Hidden: true, - Name: "test", - Usage: "test packages", + Name: "run", + Usage: "compile and run Wa program", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "target", + Usage: fmt.Sprintf("set target os (%s)", strings.Join(config.WaOS_List, "|")), + Value: config.WaOS_Default, + }, + &cli.StringFlag{ + Name: "tags", + Usage: "set build tags", + }, + }, + Action: func(c *cli.Context) error { + cliRun(c) + return nil + }, + }, + { + Name: "fmt", + Usage: "format Wa source code file", + Action: func(c *cli.Context) error { + waApp := NewApp(new(Option)) + err := waApp.Fmt(c.Args().First()) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + return nil + }, + }, + + { + Name: "test", + Usage: "test Wa packages", Flags: []cli.Flag{ &cli.StringFlag{ Name: "target", @@ -246,7 +278,7 @@ func Main() { { Hidden: true, Name: "native", - Usage: "compile wa source code to native executable", + Usage: "compile Wa source code to native executable", Flags: []cli.Flag{ &cli.StringFlag{ Name: "output", @@ -374,40 +406,7 @@ func Main() { return nil }, }, - { - Name: "fmt", - Usage: "format Wa source code file", - Action: func(c *cli.Context) error { - waApp := NewApp(build_Options(c)) - err := waApp.Fmt(c.Args().First()) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - return nil - }, - }, - { - Name: "play", - Usage: "run wa playground server", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "http", - Value: ":2023", - Usage: "set http address", - }, - }, - Action: func(c *cli.Context) error { - waApp := NewApp(build_Options(c)) - err := waApp.Playground(c.String("http")) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - return nil - }, - }, { Hidden: true, Name: "doc", @@ -465,8 +464,9 @@ func Main() { }, { - Name: "lsp", - Usage: "run wa lang server", + Hidden: true, + Name: "lsp", + Usage: "run Wa langugage server", Flags: []cli.Flag{ &cli.StringFlag{ Name: "log-file", @@ -482,7 +482,7 @@ func Main() { { Name: "logo", - Usage: "print wa-lang text format logo", + Usage: "print Wa text format logo", Flags: []cli.Flag{ &cli.BoolFlag{ Name: "more", diff --git a/waroot/examples/Makefile b/waroot/examples/Makefile index 48db9c21..88861753 100644 --- a/waroot/examples/Makefile +++ b/waroot/examples/Makefile @@ -23,12 +23,12 @@ ci-test-all: # snake cd ./snake && make publish - go run ../../main.go copy.wa - go run ../../main.go eq.wa - go run ../../main.go interface_named.wa - go run ../../main.go short-var.wa - go run ../../main.go strbytes.wa - go run ../../main.go struct.wa + go run ../../main.go run copy.wa + go run ../../main.go run eq.wa + go run ../../main.go run interface_named.wa + go run ../../main.go run short-var.wa + go run ../../main.go run strbytes.wa + go run ../../main.go run struct.wa @echo "== examples ok ==" diff --git a/waroot/examples/arduino-wat/Makefile b/waroot/examples/arduino-wat/Makefile index cf9d0583..07ab9ad1 100644 --- a/waroot/examples/arduino-wat/Makefile +++ b/waroot/examples/arduino-wat/Makefile @@ -1,7 +1,7 @@ # 版权 @2022 凹语言 作者。保留所有权利。 default: - go run ../../../main.go -target=arduino hello.wat + go run ../../../main.go run -target=arduino hello.wat clean: -rm *.wasm diff --git a/waroot/examples/arduino/Makefile b/waroot/examples/arduino/Makefile index 0c9729c7..81cb533a 100644 --- a/waroot/examples/arduino/Makefile +++ b/waroot/examples/arduino/Makefile @@ -1,5 +1,5 @@ default: - go run ../../../main.go -target=arduino app.wa + go run ../../../main.go run -target=arduino app.wa build: go run ../../../main.go build -target=arduino app.wa diff --git a/waroot/examples/expr/Makefile b/waroot/examples/expr/Makefile index f5f758f1..0bca6cfe 100644 --- a/waroot/examples/expr/Makefile +++ b/waroot/examples/expr/Makefile @@ -2,7 +2,7 @@ default: go run ../../../main.go yacc -l -p=expr -c="copyright.txt" -o="y.wa" expr.y - go run ../../../main.go y.wa + go run ../../../main.go run y.wa clean: -rm y.* a.out* diff --git a/waroot/examples/misc/Makefile b/waroot/examples/misc/Makefile index 8bf290fb..22031c4c 100644 --- a/waroot/examples/misc/Makefile +++ b/waroot/examples/misc/Makefile @@ -1,18 +1,18 @@ # 版权 @2023 凹语言 作者。保留所有权利。 default: - go run ../../../main.go array.wa - go run ../../../main.go array_1.wa - go run ../../../main.go closure.wa - go run ../../../main.go global.wa - go run ../../../main.go heart.wa - go run ../../../main.go multi_ret.wa - #go run ../../../main.go native_test.wa - go run ../../../main.go prime.wa - go run ../../../main.go ref.wa - go run ../../../main.go slice.wa - go run ../../../main.go string.wa - go run ../../../main.go struct.wa + go run ../../../main.go run array.wa + go run ../../../main.go run array_1.wa + go run ../../../main.go run closure.wa + go run ../../../main.go run global.wa + go run ../../../main.go run heart.wa + go run ../../../main.go run multi_ret.wa + #go run ../../../main.go run native_test.wa + go run ../../../main.go run prime.wa + go run ../../../main.go run ref.wa + go run ../../../main.go run slice.wa + go run ../../../main.go run string.wa + go run ../../../main.go run struct.wa clean: diff --git a/waroot/examples/surface/Makefile b/waroot/examples/surface/Makefile index 1f275017..71b86d81 100644 --- a/waroot/examples/surface/Makefile +++ b/waroot/examples/surface/Makefile @@ -1,4 +1,4 @@ default: - go run ../../../main.go surface.wa + go run ../../../main.go run surface.wa clean: