Skip to content

Commit

Permalink
添加 go2wa 内部命令
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Nov 28, 2023
1 parent fa1358a commit d82536d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
60 changes: 60 additions & 0 deletions internal/app/appgo2wa/appgo2wa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 版权 @2023 凹语言 作者。保留所有权利。

package appgo2wa

import (
"fmt"
"os"

"wa-lang.org/wa/internal/3rdparty/cli"
"wa-lang.org/wa/internal/app/appbase"
"wa-lang.org/wa/internal/format"
"wa-lang.org/wa/internal/parser"
"wa-lang.org/wa/internal/token"
)

var CmdGo2wa = &cli.Command{
Hidden: true,
Name: "go2wa",
Usage: "convert wago to wa",
Action: CmdGo2waAction,
}

func CmdGo2waAction(c *cli.Context) error {
if c.NArg() == 0 {
fmt.Fprintf(os.Stderr, "no input file")
os.Exit(1)
}

code, err := go2wa(c.Args().First())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(code))
return nil
}

func go2wa(filename string) ([]byte, error) {
if !appbase.HasExt(filename, ".go") {
return nil, fmt.Errorf("%q is not Go file", filename)
}

if !appbase.PathExists(filename) {
return nil, fmt.Errorf("%q not found", filename)
}
if !appbase.IsNativeFile(filename) {
return nil, fmt.Errorf("%q must be file", filename)
}

src, _ := os.ReadFile(filename)

fset := token.NewFileSet()
f, err := parser.ParseFile(nil, fset, filename, nil, 0)
if err != nil {
return nil, err
}

f.Name.Name = ""
return format.DevFormat(fset, f, src)
}
2 changes: 2 additions & 0 deletions internal/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"wa-lang.org/wa/internal/app/appdev"
"wa-lang.org/wa/internal/app/appdoc"
"wa-lang.org/wa/internal/app/appfmt"
"wa-lang.org/wa/internal/app/appgo2wa"
"wa-lang.org/wa/internal/app/appinit"
"wa-lang.org/wa/internal/app/applex"
"wa-lang.org/wa/internal/app/applogo"
Expand Down Expand Up @@ -96,6 +97,7 @@ func Main() {
appssa.CmdSsa,

// 待完善的子命令(隐藏)
appgo2wa.CmdGo2wa,
appcir.CmdCir,
appdoc.CmdDoc,
applsp.CmdLsp,
Expand Down
16 changes: 14 additions & 2 deletions internal/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const parserMode = parser.ParseComments
// If src != nil, readSource converts src to a []byte if possible;
// otherwise it returns an error. If src == nil, readSource returns
// the result of reading the file specified by filename.
//
func readSource(vfs fs.FS, filename string, src interface{}) ([]byte, error) {
if src != nil {
switch s := src.(type) {
Expand Down Expand Up @@ -87,7 +86,6 @@ func File(vfs fs.FS, filename string, src interface{}) (text []byte, changed boo
//
// The function may return early (before the entire result is written)
// and return a formatting error, for instance due to an incorrect AST.
//
func Node(dst io.Writer, fset *token.FileSet, node interface{}) error {
// Determine if we have a complete source file (file != nil).
var file *ast.File
Expand Down Expand Up @@ -155,6 +153,20 @@ func SourceFile(src []byte) ([]byte, error) {
return format(fset, file, sourceAdj, indentAdj, src, config)
}

// 从语法树格式化(内部用)
func DevFormat(
fset *token.FileSet,
file *ast.File,
src []byte,
) ([]byte, error) {
indentAdj := 0
sourceAdj := func(src []byte, indent int) []byte {
return src
}

return format(fset, file, sourceAdj, indentAdj, src, config)
}

// 格式化表达式
func SourceExpr(src []byte) ([]byte, error) {
fset := token.NewFileSet()
Expand Down

0 comments on commit d82536d

Please sign in to comment.