Skip to content

Commit cba22db

Browse files
committed
github.com/goplus/gox
1 parent 970ff17 commit cba22db

File tree

7 files changed

+367
-2
lines changed

7 files changed

+367
-2
lines changed

README.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
1-
# gox
2-
Code generator for the Go language
1+
gox - Code generator for the Go language
2+
========
3+
4+
[![Build Status](https://travis-ci.org/goplus/gox.png?branch=master)](https://travis-ci.org/goplus/gox)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/goplus/gox)](https://goreportcard.com/report/github.com/goplus/gox)
6+
[![GitHub release](https://img.shields.io/github/v/tag/goplus/gox.svg?label=release)](https://github.com/goplus/gox/releases)
7+
[![Coverage Status](https://codecov.io/gh/goplus/gox/branch/master/graph/badge.svg)](https://codecov.io/gh/goplus/gox)
8+
[![GoDoc](https://img.shields.io/badge/godoc-reference-teal.svg)](https://pkg.go.dev/mod/github.com/goplus/gox)
9+
10+
## Tutorials
11+
12+
```go
13+
import (
14+
"github.com/goplus/gox/dom"
15+
"github.com/goplus/gox/conv"
16+
)
17+
18+
var a, b, c *dom.Var
19+
20+
pkg := dom.NewPkg("main")
21+
22+
fmt := pkg.Import("fmt")
23+
24+
pkg.NewFunc("main").BodyStart(pkg).
25+
NewVar("a", &a).NewVar("b", &b).NewVar("c", &c). // type of variables will be auto detected
26+
VarRef(a).VarRef(b).Const("Hi").Const(3).Assign(2).EndStmt(). // a, b = "Hi", 3
27+
VarRef(c).Val(b).Assign(1).EndStmt(). // c = b
28+
Val(fmt.Ref("Println")).Val(a).Val(b).Val(c).Call(3).EndStmt(). // fmt.Println(a, b, c)
29+
End()
30+
31+
conv.WriteFile("./foo.go", pkg)
32+
```
33+
34+
This will generate a Go source file named `./foo.go`. The following is its content:
35+
36+
```go
37+
package main
38+
39+
import (
40+
"fmt"
41+
)
42+
43+
func main() {
44+
var a string
45+
var b int
46+
var c int
47+
a, b = "Hi", 3
48+
c = b
49+
fmt.Println(a, b, c)
50+
}
51+
```

conv/goconv.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package goconv
2+
3+
import (
4+
"go/ast"
5+
"go/format"
6+
"go/token"
7+
"io"
8+
"os"
9+
10+
"github.com/goplus/gox/dom"
11+
)
12+
13+
// ----------------------------------------------------------------------------
14+
15+
// From func
16+
func From(fset *token.FileSet, pkg *dom.Package) (file *ast.File, err error) {
17+
return
18+
}
19+
20+
// WriteTo func
21+
func WriteTo(dst io.Writer, pkg *dom.Package) (err error) {
22+
fset := token.NewFileSet()
23+
file, err := From(fset, pkg)
24+
if err != nil {
25+
return
26+
}
27+
return format.Node(dst, fset, file)
28+
}
29+
30+
// WriteFile func
31+
func WriteFile(file string, pkg *dom.Package) (err error) {
32+
f, err := os.Create(file)
33+
if err != nil {
34+
return
35+
}
36+
defer f.Close()
37+
return WriteTo(f, pkg)
38+
}
39+
40+
// ----------------------------------------------------------------------------

dom/codebuild.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package dom
2+
3+
// ----------------------------------------------------------------------------
4+
5+
// Code type
6+
type Code struct {
7+
Tokens []Token
8+
}
9+
10+
// ----------------------------------------------------------------------------
11+
12+
// CodeBuilder type
13+
type CodeBuilder struct {
14+
code *Code
15+
}
16+
17+
// NewVar func
18+
func (p *CodeBuilder) NewVar(name string, pv **Var) *CodeBuilder {
19+
return p
20+
}
21+
22+
// VarRef func
23+
func (p *CodeBuilder) VarRef(v *Var) *CodeBuilder {
24+
return p
25+
}
26+
27+
// Val func
28+
func (p *CodeBuilder) Val(v Ref) *CodeBuilder {
29+
return p
30+
}
31+
32+
// Const func
33+
func (p *CodeBuilder) Const(v interface{}) *CodeBuilder {
34+
return p
35+
}
36+
37+
// Assign func
38+
func (p *CodeBuilder) Assign(n int) *CodeBuilder {
39+
return p
40+
}
41+
42+
// Call func
43+
func (p *CodeBuilder) Call(n int) *CodeBuilder {
44+
return p
45+
}
46+
47+
// Defer func
48+
func (p *CodeBuilder) Defer(n int) *CodeBuilder {
49+
return p
50+
}
51+
52+
// Go func
53+
func (p *CodeBuilder) Go() *CodeBuilder {
54+
return p
55+
}
56+
57+
// EndStmt func
58+
func (p *CodeBuilder) EndStmt() *CodeBuilder {
59+
return p
60+
}
61+
62+
// End func
63+
func (p *CodeBuilder) End() {
64+
}
65+
66+
// ----------------------------------------------------------------------------

dom/package.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package dom
2+
3+
// ----------------------------------------------------------------------------
4+
5+
// Type type
6+
type Type interface {
7+
}
8+
9+
// ----------------------------------------------------------------------------
10+
11+
// Param type
12+
type Param struct {
13+
name string
14+
typ Type
15+
idx int
16+
}
17+
18+
// Name func
19+
func (p *Param) Name() string {
20+
return p.name
21+
}
22+
23+
// Type func
24+
func (p *Param) Type() Type {
25+
return p.typ
26+
}
27+
28+
// Index func
29+
func (p *Param) Index() int {
30+
return p.idx
31+
}
32+
33+
// ----------------------------------------------------------------------------
34+
35+
// Var type
36+
type Var struct {
37+
Param
38+
}
39+
40+
// SetType func
41+
func (p *Var) SetType(typ Type) *Var {
42+
if p.typ != nil {
43+
if p.typ != typ {
44+
panic("TODO")
45+
}
46+
} else {
47+
p.typ = typ
48+
}
49+
return p
50+
}
51+
52+
// ----------------------------------------------------------------------------
53+
54+
// Func type
55+
type Func struct {
56+
name string
57+
in []*Param
58+
out []*Param
59+
body *Code
60+
}
61+
62+
// SetResults func
63+
func (p *Func) SetResults(out ...*Param) *Func {
64+
return p
65+
}
66+
67+
// BodyStart func
68+
func (p *Func) BodyStart(pkg *Package) *CodeBuilder {
69+
return &CodeBuilder{}
70+
}
71+
72+
// ----------------------------------------------------------------------------
73+
74+
// Scope type
75+
type Scope struct {
76+
Parent *Scope
77+
}
78+
79+
// ----------------------------------------------------------------------------
80+
81+
// Package type
82+
type Package struct {
83+
Scope
84+
gidx int
85+
}
86+
87+
func (p *Package) allocIdx() int {
88+
p.gidx++
89+
return p.gidx
90+
}
91+
92+
// NewPkg func
93+
func NewPkg(name string) *Package {
94+
pkg := &Package{}
95+
return pkg
96+
}
97+
98+
// MaxIndex func
99+
func (p *Package) MaxIndex() int {
100+
return p.gidx + 1
101+
}
102+
103+
// Import func
104+
func (p *Package) Import(name, path string) *PkgRef {
105+
return &PkgRef{}
106+
}
107+
108+
// NewVar func
109+
func (p *Package) NewVar(name string, typ Type) *Var {
110+
return &Var{}
111+
}
112+
113+
// NewParam func
114+
func (p *Package) NewParam(name string, typ Type) *Param {
115+
return &Param{name: name, typ: typ, idx: p.allocIdx()}
116+
}
117+
118+
// NewFunc func
119+
func (p *Package) NewFunc(name string, params ...*Param) *Func {
120+
return &Func{}
121+
}
122+
123+
// ----------------------------------------------------------------------------

dom/pkgref.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dom
2+
3+
// ----------------------------------------------------------------------------
4+
5+
// Ref type
6+
type Ref interface {
7+
}
8+
9+
// ----------------------------------------------------------------------------
10+
11+
// PkgRef type
12+
type PkgRef struct {
13+
}
14+
15+
// Ref func
16+
func (p *PkgRef) Ref(name string) Ref {
17+
return nil
18+
}
19+
20+
// ----------------------------------------------------------------------------

dom/token.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package dom
2+
3+
// ----------------------------------------------------------------------------
4+
5+
// Token type
6+
type Token interface {
7+
tokenNode()
8+
}
9+
10+
// VarRefToken type
11+
type VarRefToken struct {
12+
Var *Var
13+
}
14+
15+
// VarToken type
16+
type VarToken struct {
17+
Var *Var
18+
}
19+
20+
// ConstToken type
21+
type ConstToken struct {
22+
Value interface{}
23+
}
24+
25+
// AssignToken type
26+
type AssignToken struct {
27+
N int
28+
}
29+
30+
// FuncToken type
31+
type FuncToken struct {
32+
Func Ref
33+
}
34+
35+
// CallToken type
36+
type CallToken struct {
37+
NArg int
38+
}
39+
40+
// DeferToken type
41+
type DeferToken struct {
42+
}
43+
44+
// GoToken type
45+
type GoToken struct {
46+
}
47+
48+
// EndStmtToken type
49+
type EndStmtToken struct {
50+
}
51+
52+
// ----------------------------------------------------------------------------
53+
54+
func (p *VarRefToken) tokenNode() {}
55+
func (p *VarToken) tokenNode() {}
56+
func (p *ConstToken) tokenNode() {}
57+
func (p *AssignToken) tokenNode() {}
58+
func (p *FuncToken) tokenNode() {}
59+
func (p *CallToken) tokenNode() {}
60+
func (p *DeferToken) tokenNode() {}
61+
func (p *GoToken) tokenNode() {}
62+
func (p *EndStmtToken) tokenNode() {}
63+
64+
// ----------------------------------------------------------------------------

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/goplus/gox
2+
3+
go 1.14

0 commit comments

Comments
 (0)