Skip to content

Commit

Permalink
wa ssa 命令行增加 ast 打印参数
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Apr 12, 2024
1 parent 0869485 commit f90047a
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions internal/app/appssa/appssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"wa-lang.org/wa/internal/3rdparty/cli"
"wa-lang.org/wa/internal/app/appbase"
"wa-lang.org/wa/internal/ast"
"wa-lang.org/wa/internal/loader"
"wa-lang.org/wa/internal/ssa"
)
Expand All @@ -20,6 +21,10 @@ var CmdSsa = &cli.Command{
Flags: []cli.Flag{
appbase.MakeFlag_target(),
appbase.MakeFlag_tags(),
&cli.BoolFlag{
Name: "ast",
Usage: "print ast",
},
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
Expand All @@ -28,7 +33,7 @@ var CmdSsa = &cli.Command{
}

opt := appbase.BuildOptions(c)
err := SSARun(opt, c.Args().First())
err := SSARun(opt, c.Args().First(), c.Bool("ast"))
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand All @@ -37,13 +42,21 @@ var CmdSsa = &cli.Command{
},
}

func SSARun(opt *appbase.Option, filename string) error {
func SSARun(opt *appbase.Option, filename string, printAST bool) error {
cfg := opt.Config()
prog, err := loader.LoadProgram(cfg, filename)
if err != nil {
return err
}

if printAST {
mainPkg := prog.Pkgs[prog.Manifest.MainPkg]
for _, f := range mainPkg.Files {
ast.Print(prog.Fset, f)
}
return nil
}

prog.SSAMainPkg.WriteTo(os.Stdout)

var funcNames []string
Expand Down

0 comments on commit f90047a

Please sign in to comment.