Skip to content

Commit

Permalink
gox.NewPkg
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Mar 6, 2021
1 parent 5e2145d commit 9390d07
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ gox - Code generator for the Go language

```go
import (
"github.com/goplus/gox/dom"
"github.com/goplus/gox/conv"
"github.com/goplus/gox"
)

var a, b, c *dom.Var
var a, b, c *gox.Var

pkg := dom.NewPkg("main")
pkg := gox.NewPkg("main")

fmt := pkg.Import("fmt")

Expand All @@ -28,7 +27,7 @@ pkg.NewFunc("main").BodyStart(pkg).
Val(fmt.Ref("Println")).Val(a).Val(b).Val(c).Call(3).EndStmt(). // fmt.Println(a, b, c)
End()

conv.WriteFile("./foo.go", pkg)
gox.WriteFile("./foo.go", pkg)
```

This will generate a Go source file named `./foo.go`. The following is its content:
Expand Down
11 changes: 9 additions & 2 deletions dom/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ func (p *Func) BodyStart(pkg *Package) *CodeBuilder {

// Scope type
type Scope struct {
Parent *Scope
parent *Scope
syms map[string]Ref
}

func (p *Scope) initScope(parent *Scope) {
p.parent = parent
p.syms = make(map[string]Ref)
}

// ----------------------------------------------------------------------------
Expand All @@ -96,8 +102,9 @@ func (p *Package) allocIdx() int {
}

// NewPkg func
func NewPkg(name string, opts ...*Options) *Package {
func NewPkg(name string, opts *Options) *Package {
pkg := &Package{}
pkg.initScope(nil)
return pkg
}

Expand Down
11 changes: 5 additions & 6 deletions dom/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"bytes"
"testing"

"github.com/goplus/gox/conv"
"github.com/goplus/gox/dom"
"github.com/goplus/gox"
)

func domTest(t *testing.T, pkg *dom.Package, expected string) {
func domTest(t *testing.T, pkg *gox.Package, expected string) {
var b bytes.Buffer
err := conv.WriteTo(&b, pkg)
err := gox.WriteTo(&b, pkg)
if err != nil {
t.Fatal("conv.WriteTo failed:", err)
}
Expand All @@ -21,9 +20,9 @@ func domTest(t *testing.T, pkg *dom.Package, expected string) {
}

func TestBasic(t *testing.T) {
var a, b, c *dom.Var
var a, b, c *gox.Var

pkg := dom.NewPkg("main")
pkg := gox.NewPkg("main")

fmt := pkg.Import("fmt")

Expand Down
51 changes: 51 additions & 0 deletions gox.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gox

import (
"go/ast"
"go/token"
"io"

"github.com/goplus/gox/conv"
"github.com/goplus/gox/dom"
)

// ----------------------------------------------------------------------------

// Var type
type Var = dom.Var

// Options type
type Options = dom.Options

// Package type
type Package = dom.Package

// NewPkg func
func NewPkg(name string, opts ...*Options) *Package {
var theOpts *Options
if opts != nil {
theOpts = opts[0]
} else {
theOpts = &Options{}
}
return dom.NewPkg(name, theOpts)
}

// ----------------------------------------------------------------------------

// From func
func From(fset *token.FileSet, pkg *dom.Package) (file *ast.File, err error) {
return conv.From(fset, pkg)
}

// WriteTo func
func WriteTo(dst io.Writer, pkg *dom.Package) (err error) {
return conv.WriteTo(dst, pkg)
}

// WriteFile func
func WriteFile(file string, pkg *dom.Package) (err error) {
return conv.WriteFile(file, pkg)
}

// ----------------------------------------------------------------------------

0 comments on commit 9390d07

Please sign in to comment.