提交 2ba6c2a9 编写于 作者: chai2010's avatar chai2010

修复并导出格式化函数

上级 782cab0d
......@@ -9,6 +9,7 @@ import (
"github.com/wa-lang/wa/internal/backends/compiler_wat"
"github.com/wa-lang/wa/internal/backends/target_spec"
"github.com/wa-lang/wa/internal/config"
"github.com/wa-lang/wa/internal/format"
"github.com/wa-lang/wa/internal/loader"
"github.com/wa-lang/wa/internal/logger"
)
......@@ -115,3 +116,12 @@ func BuildVFS(vfs *config.PkgVFS, appPkg string, target Machine) (wat []byte, er
watOut, err := compiler_wat.New().Compile(prog, target)
return []byte(watOut), err
}
// 格式化代码
func FormatCode(filename, code string) (string, error) {
data, err := format.File(nil, filename, code)
if err != nil {
return "", err
}
return string(data), nil
}
// 版权 @2022 凹语言 作者。保留所有权利。
package api_test
import (
"fmt"
"github.com/wa-lang/wa/api"
)
func ExampleFormatCode() {
s, _ := api.FormatCode("hello.wa", "fn add(a i32, b i32) i32 {return a+b}")
fmt.Println(s)
// Output:
// fn add(a: i32, b: i32) => i32 { return a + b }
}
......@@ -10,5 +10,5 @@ fn main() {
}
fn add(a: i32, b: i32) => i32 {
return a+b
return a + b
}
......@@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
package astutil
import (
......
......@@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
package astutil_test
import (
......
......@@ -4,6 +4,9 @@
// To avoid a cyclic dependency with github.com/wa-lang/wa/internal/parser, this file is in a separate package.
//go:build ignore
// +build ignore
package ast_test
import (
......
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !windows
// +build !windows
//go:build ignore
// +build ignore
package format
......
......@@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
package printer_test
import (
......
......@@ -363,6 +363,7 @@ func (p *printer) parameters(fields *ast.FieldList) {
// by a linebreak call after a type, or in the next multi-line identList
// will do the right thing.
p.identList(par.Names, ws == indent)
p.print(token.COLON)
p.print(blank)
}
// parameter type
......@@ -393,6 +394,9 @@ func (p *printer) signature(params, result *ast.FieldList) {
if n > 0 {
// result != nil
p.print(blank)
p.print(token.ARROW)
p.print(blank)
if n == 1 && result.List[0].Names == nil {
// single anonymous result; no ()'s
p.expr(stripParensAlways(result.List[0].Type))
......@@ -1459,11 +1463,14 @@ func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
switch s := spec.(type) {
case *ast.ImportSpec:
p.setComment(s.Doc)
p.expr(sanitizeImportPath(s.Path))
if s.Name != nil {
p.expr(s.Name)
p.print(blank)
p.print(token.ARROW)
p.print(blank)
p.expr(s.Name)
}
p.expr(sanitizeImportPath(s.Path))
p.setComment(s.Comment)
p.print(s.EndPos)
......@@ -1474,6 +1481,7 @@ func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
p.setComment(s.Doc)
p.identList(s.Names, doIndent) // always present
if s.Type != nil {
p.print(token.COLON)
p.print(blank)
p.expr(s.Type)
}
......
......@@ -5,8 +5,8 @@
// This file implements a simple printer performance benchmark:
// go test -bench=BenchmarkPrint
//go:build !windows
// +build !windows
//go:build ignore
// +build ignore
package printer
......
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !windows
// +build !windows
//go:build ignore
// +build ignore
package printer
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册