Skip to content

Commit

Permalink
gop/printer.SetDebug
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Sep 23, 2021
1 parent 66dd100 commit 79263dc
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ How do we do this in the Go language?
```go
package main

import "fmt"

func main() {
a := []float64{1, 2, 3.4}
println(a)
fmt.Println(a)
}
```

Expand Down
2 changes: 1 addition & 1 deletion cl/class_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type gmxInfo struct {

var (
gmxTypes = map[string]gmxInfo{
".gmx": {".spx", []string{"github.com/goplus/spx", "math"}},
".gmx": {".spx", []string{"github.com/goplus/spx"}},
}
)

Expand Down
8 changes: 8 additions & 0 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,14 @@ var a = math.Round(1.2)
`)
}

func _TestLocalImport(t *testing.T) {
gopClTest(t, `import "./internal/spx"
var a = spx.TestIntValue
`, `
`)
}

func TestImportUnused(t *testing.T) {
gopClTest(t, `import "fmt"
Expand Down
2 changes: 1 addition & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ func (p *parser) isCommand(x ast.Expr) bool {
case token.IDENT, token.RARROW,
token.STRING, token.INT, token.FLOAT, token.IMAG, token.CHAR, token.RAT:
return true
case token.SUB, token.NOT, token.AND, token.MUL, token.ARROW, token.XOR:
case token.SUB, token.NOT, token.AND, token.MUL, token.ARROW, token.XOR, token.ADD:
if x.End() == p.pos { // x-y
return false
}
Expand Down
4 changes: 4 additions & 0 deletions printer/gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
"github.com/goplus/gop/token"
)

func init() {
printer.SetDebug(printer.DbgFlagAll)
}

func TestNoPkgDecl(t *testing.T) {
var dst bytes.Buffer
fset := token.NewFileSet()
Expand Down
25 changes: 25 additions & 0 deletions printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ import (
"github.com/goplus/gop/token"
)

const (
DbgFlagAll = 1
)

var (
debugFormat bool
)

func SetDebug(flags int) {
if flags != 0 {
debugFormat = true
}
}

// Formatting issues:
// - better comment formatting for /*-style comments at the end of a line (e.g. a declaration)
// when the comment spans multiple lines; if such a comment is just two lines, formatting is
Expand Down Expand Up @@ -1320,6 +1334,11 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
p.stmt(s.Stmt, nextIsRBrace)

case *ast.ExprStmt:
if debugFormat {
if e, ok := s.X.(*ast.CallExpr); ok {
log.Println("==> ExprStmt", e.Fun)
}
}
const depth = 1
p.expr0(s.X, depth)

Expand All @@ -1335,6 +1354,9 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
p.print(s.TokPos, s.Tok)

case *ast.AssignStmt:
if debugFormat {
log.Println("==> AssignStmt", s.Lhs)
}
var depth = 1
if len(s.Lhs) > 1 && len(s.Rhs) > 1 {
depth++
Expand Down Expand Up @@ -1912,6 +1934,9 @@ func (p *printer) distanceFrom(startPos token.Pos, startOutCol int) int {
}

func (p *printer) funcDecl(d *ast.FuncDecl) {
if debugFormat {
log.Println("==> Format Func", d.Name.Name)
}
p.setComment(d.Doc)

if p.unnamedFuncName == d.Name.Name {
Expand Down

0 comments on commit 79263dc

Please sign in to comment.