Skip to content

Commit 3e39eed

Browse files
committed
针对开发者添加 wat2c 测试子命令入口
1 parent 85088e7 commit 3e39eed

File tree

5 files changed

+87
-55
lines changed

5 files changed

+87
-55
lines changed

internal/app/appwasm2wat/appwasm2wat.go

-53
This file was deleted.

internal/app/appwat2c/appwat2c.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// 版权 @2023 凹语言 作者。保留所有权利。
2+
3+
package appwat2c
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"strings"
9+
10+
"wa-lang.org/wa/internal/3rdparty/cli"
11+
"wa-lang.org/wa/internal/wat/watutil"
12+
)
13+
14+
var CmdWat2c = &cli.Command{
15+
Hidden: true,
16+
Name: "wat2c",
17+
Usage: "convert a WebAssembly text file to a C source and header",
18+
ArgsUsage: "<file.wat>",
19+
Flags: []cli.Flag{
20+
&cli.StringFlag{
21+
Name: "output",
22+
Aliases: []string{"o"},
23+
Usage: "set output file",
24+
},
25+
},
26+
Action: func(c *cli.Context) error {
27+
if c.NArg() == 0 {
28+
fmt.Fprintf(os.Stderr, "no input file")
29+
os.Exit(1)
30+
}
31+
32+
infile := c.Args().First()
33+
outfile := c.String("output")
34+
if outfile == "" {
35+
outfile = infile
36+
if n1, n2 := len(outfile), len(".wat"); n1 > n2 {
37+
if s := outfile[n1-n2:]; strings.EqualFold(s, ".wat") {
38+
outfile = outfile[:n1-n2]
39+
}
40+
}
41+
outfile += ".c"
42+
}
43+
if !strings.HasSuffix(outfile, ".c") {
44+
outfile += ".c"
45+
}
46+
47+
source, err := os.ReadFile(infile)
48+
if err != nil {
49+
fmt.Println(err)
50+
os.Exit(1)
51+
}
52+
53+
code, header, err := watutil.Wat2C(infile, source)
54+
if err != nil {
55+
os.WriteFile(outfile, code, 0666)
56+
fmt.Println(err)
57+
os.Exit(1)
58+
}
59+
60+
err = os.WriteFile(outfile, code, 0666)
61+
if err != nil {
62+
fmt.Println(err)
63+
os.Exit(1)
64+
}
65+
66+
err = os.WriteFile(outfile+".h", header, 0666)
67+
if err != nil {
68+
fmt.Println(err)
69+
os.Exit(1)
70+
}
71+
72+
return nil
73+
},
74+
}

internal/wat/watutil/wat2c.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// 版权 @2024 凹语言 作者。保留所有权利。
2+
3+
package watutil
4+
5+
import "fmt"
6+
7+
func Wat2C(filename string, source []byte) (code, header []byte, err error) {
8+
err = fmt.Errorf("wat2c: TODO")
9+
return
10+
}

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"wa-lang.org/wa/internal/app/apprun"
3030
"wa-lang.org/wa/internal/app/appssa"
3131
"wa-lang.org/wa/internal/app/apptest"
32-
"wa-lang.org/wa/internal/app/appwasm2wat"
32+
"wa-lang.org/wa/internal/app/appwat2c"
3333
"wa-lang.org/wa/internal/app/appwat2wasm"
3434
"wa-lang.org/wa/internal/app/appwatstrip"
3535
"wa-lang.org/wa/internal/app/appyacc"
@@ -105,8 +105,8 @@ func main() {
105105
appast.CmdAst,
106106
appssa.CmdSsa,
107107
appwat2wasm.CmdWat2wasm,
108-
appwasm2wat.CmdWasm2wat,
109108
appwatstrip.CmdWatStrip,
109+
appwat2c.CmdWat2c,
110110

111111
// 待完善的子命令(隐藏)
112112
appgo2wa.CmdGo2wa,

waroot/changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- (dev)
44
- 完善 `wa lsp` 子命令 (TODO)
5+
- 支持 `wat2c` 命令(TODO)
56
- v0.17.0 (2024-10-??)
67
- 恢复 Arduino Nano 33 支持
78
- 预定义常量统一用大写字母, 并增加 `__COLUMN__` 常量

0 commit comments

Comments
 (0)