完善对 w2 文件的支持

This commit is contained in:
chai2010
2025-10-03 07:42:35 +08:00
parent 6479f4cc8a
commit c3bc85c516
8 changed files with 18 additions and 7 deletions

View File

@@ -40,7 +40,7 @@ func PrintAST(filename string) error {
if appbase.IsNativeFile(filename, ".wat") {
return PrintWatAST(filename)
}
if !appbase.HasExt(filename, ".wa", ".wz") {
if !appbase.HasExt(filename, ".wa", ".wz", ".w2") {
return fmt.Errorf("%q is not Wa file", filename)
}

View File

@@ -125,7 +125,7 @@ func BuildApp(opt *appbase.Option, input, outfile string) (mainFunc string, wasm
}
// 只编译 wa/wz 文件, 输出路径相同, 后缀名调整
if appbase.HasExt(input, ".wa", ".wz") {
if appbase.HasExt(input, ".wa", ".wz", ".w2") {
prog, _, watOutput, err := buildWat(opt, input)
if err != nil {
fmt.Println(err)

View File

@@ -83,7 +83,7 @@ func CmdRunAction(c *cli.Context) error {
}
var opt = appbase.BuildOptions(c)
if appbase.HasExt(input, ".wa") {
if appbase.HasExt(input, ".wa", ".wz", ".w2") {
// 执行单个 wa 脚本, 避免写磁盘
opt.RunFileMode = true
}

View File

@@ -60,7 +60,7 @@ func (p *_Loader) LoadProgram(appPath string) (*Program, error) {
logger.Tracef(&config.EnableTrace_loader, "cfg: %+v", p.cfg)
logger.Tracef(&config.EnableTrace_loader, "appPath: %s", appPath)
if isWaFile(appPath) || isWzFile(appPath) {
if isWaFile(appPath) || isWzFile(appPath) || isW2File(appPath) {
vfs, manifest, err := loadProgramFileMeta(&p.cfg, appPath, nil)
if err != nil {
logger.Tracef(&config.EnableTrace_loader, "err: %v", err)

View File

@@ -256,3 +256,10 @@ func isWzFile(path string) bool {
}
return false
}
func isW2File(path string) bool {
if fi, err := os.Lstat(path); err == nil && fi.Mode().IsRegular() {
return strings.HasSuffix(strings.ToLower(path), ".w2")
}
return false
}

View File

@@ -2239,7 +2239,7 @@ func (p *parser) parseStmt() (s ast.Stmt) {
switch p.tok {
case token.CONST, token.TYPE, token.VAR:
// TODO(chai2010): var declaration not allowed in func body
s = &ast.DeclStmt{Decl: p.parseDecl(stmtStart)}
s = &ast.DeclStmt{Decl: p.parseDecl(stmtStart, false)}
case
// tokens that may start an expression
token.IDENT, token.INT, token.FLOAT, token.IMAG, token.CHAR, token.STRING, token.FUNC, token.LPAREN, // operands
@@ -2560,7 +2560,7 @@ func (p *parser) parseFuncDecl(keyword token.Token) *ast.FuncDecl {
return decl
}
func (p *parser) parseDecl(sync map[token.Token]bool) ast.Decl {
func (p *parser) parseDecl(sync map[token.Token]bool, isPkgScope bool) ast.Decl {
if p.trace {
defer un(trace(p, "Declaration"))
}
@@ -2642,7 +2642,7 @@ func (p *parser) parseFile() *ast.File {
if p.mode&ImportsOnly == 0 {
// rest of package body
for p.tok != token.EOF {
decls = append(decls, p.parseDecl(declStart))
decls = append(decls, p.parseDecl(declStart, true))
}
}
}

View File

@@ -370,6 +370,10 @@ func init() {
for i := keyword_beg + 1; i < keyword_end; i++ {
keywords[tokens[i]] = i
}
for i := w2_keyword_beg + 1; i < w2_keyword_end; i++ {
keywords[tokens[i]] = i
}
}
// Lookup maps an identifier to its keyword token or IDENT (if not a keyword).