Skip to content

Commit

Permalink
Fixed a bunch of tests.
Browse files Browse the repository at this point in the history
Part of #11.
  • Loading branch information
tcard committed Apr 1, 2016
1 parent fa39eed commit 76110b9
Show file tree
Hide file tree
Showing 42 changed files with 362 additions and 200 deletions.
2 changes: 1 addition & 1 deletion sgo/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (l *ExprList) End() token.Pos {
}

func NewExprList(list ...Expr) *ExprList {
return &ExprList{List: list, EntangledPos: -1}
return &ExprList{List: list, EntangledPos: 0}
}

// All statement nodes implement the Stmt interface.
Expand Down
6 changes: 3 additions & 3 deletions sgo/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func (c *converter) convertExprList(v *ast.ExprList) {
return
}
for i, expr := range v.List {
if i == v.EntangledPos {
if i == v.EntangledPos-1 {
c.putChunks(int(expr.Pos())-1, c.src[c.lastChunkEnd:int(v.List[i-1].End())-c.base-1], []byte(", "))
}
c.convertExpr(expr)
Expand Down Expand Up @@ -718,7 +718,7 @@ func (c *converter) convertReturnStmt(v *ast.ReturnStmt) {
if v == nil {
return
}
if v.Results.EntangledPos == 0 {
if v.Results.EntangledPos == 1 {
// return \ err
resultsLen := c.lastFunc.Results().Len()
results := make([][]byte, 0, resultsLen)
Expand Down Expand Up @@ -754,7 +754,7 @@ func (c *converter) convertReturnStmt(v *ast.ReturnStmt) {
for _, v := range v.Results.List {
c.convertExpr(v)
}
if v.Results.EntangledPos > 0 {
if v.Results.EntangledPos > 1 {
// return x, y, z \
chunk := ", nil"
if c.lastFunc.Results().Entangled().Type() == types.Typ[types.Bool] {
Expand Down
7 changes: 3 additions & 4 deletions sgo/doc/example_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build disabled

// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Expand All @@ -8,12 +6,13 @@ package doc_test

import (
"bytes"
"strings"
"testing"

"github.com/tcard/sgo/sgo/doc"
"github.com/tcard/sgo/sgo/format"
"github.com/tcard/sgo/sgo/parser"
"github.com/tcard/sgo/sgo/token"
"strings"
"testing"
)

const exampleTestFile = `
Expand Down
2 changes: 0 additions & 2 deletions sgo/doc/synopsis_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build disabled

// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Expand Down
7 changes: 3 additions & 4 deletions sgo/format/format_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build disabled

// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Expand All @@ -8,11 +6,12 @@ package format

import (
"bytes"
"github.com/tcard/sgo/sgo/parser"
"github.com/tcard/sgo/sgo/token"
"io/ioutil"
"strings"
"testing"

"github.com/tcard/sgo/sgo/parser"
"github.com/tcard/sgo/sgo/token"
)

const testfile = "format_test.go"
Expand Down
14 changes: 11 additions & 3 deletions sgo/importer/example_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// +build disabled

package importer_test

import (
"os"

"github.com/tcard/sgo/sgo/ast"
"github.com/tcard/sgo/sgo/types"

"github.com/tcard/sgo/sgo/importer"
"github.com/tcard/sgo/sgo/parser"
"github.com/tcard/sgo/sgo/printer"
Expand All @@ -23,7 +24,14 @@ func ExampleConvertAST() {
}
`, parser.ParseComments)

importer.ConvertAST(a)
info := &types.Info{
Defs: map[*ast.Ident]types.Object{},
Uses: map[*ast.Ident]types.Object{},
}
cfg := &types.Config{}
cfg.Check("", fset, []*ast.File{a}, info)

importer.ConvertAST(a, info, nil)

printer.Fprint(os.Stdout, fset, a)
// Output:
Expand Down
11 changes: 11 additions & 0 deletions sgo/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ func (imp *importer) Import(path string) (*types.Package, error) {
return imported, nil
}

if path == "unsafe" {
gopkg, err := goimporter.Default().Import(path)
if err != nil {
return nil, err
}
conv := &converter{gopkg: gopkg}
conv.convert()
imp.imported[path] = conv.ret
return conv.ret, nil
}

buildPkg, err := build.Import(path, "", build.ImportComment)
if err != nil {
return nil, err
Expand Down
7 changes: 3 additions & 4 deletions sgo/parser/error_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build disabled

// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Expand All @@ -25,13 +23,14 @@
package parser

import (
"github.com/tcard/sgo/sgo/scanner"
"github.com/tcard/sgo/sgo/token"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
"testing"

"github.com/tcard/sgo/sgo/scanner"
"github.com/tcard/sgo/sgo/token"
)

const testdata = "testdata"
Expand Down
3 changes: 1 addition & 2 deletions sgo/parser/example_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build disabled

// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Expand All @@ -8,6 +6,7 @@ package parser_test

import (
"fmt"

"github.com/tcard/sgo/sgo/parser"
"github.com/tcard/sgo/sgo/token"
)
Expand Down
16 changes: 8 additions & 8 deletions sgo/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,21 +575,21 @@ func (p *parser) parseExprList(lhs bool) *ast.ExprList {

list := ast.NewExprList()
if p.tok == token.BACKSL {
list.EntangledPos = 0
list.EntangledPos = 1
p.next()
}

list.List = append(list.List, p.parseExpr(lhs))
for p.tok == token.COMMA || (list.EntangledPos == -1 && p.tok == token.BACKSL) {
list.List = append(list.List, p.checkExpr(p.parseExpr(lhs)))
for p.tok == token.COMMA || (list.EntangledPos == 0 && p.tok == token.BACKSL) {
prevTok := p.tok
p.next()
if prevTok == token.BACKSL {
list.EntangledPos = len(list.List)
list.EntangledPos = len(list.List) + 1
if p.tok == token.SEMICOLON || p.tok == token.RBRACE {
break
}
}
list.List = append(list.List, p.parseExpr(lhs))
list.List = append(list.List, p.checkExpr(p.parseExpr(lhs)))
}

return list
Expand Down Expand Up @@ -851,8 +851,8 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
for {
list.List = append(list.List, p.parseVarType(ellipsisOk))
if p.tok != token.COMMA {
if list.EntangledPos == -1 && p.tok == token.BACKSL {
list.EntangledPos = len(list.List)
if list.EntangledPos == 0 && p.tok == token.BACKSL {
list.EntangledPos = len(list.List) + 1
}
break
}
Expand Down Expand Up @@ -2364,7 +2364,7 @@ func (p *parser) parseValueSpec(doc *ast.CommentGroup, keyword token.Token, iota
p.error(pos, "missing variable type or initialization")
}
case token.CONST:
if values == nil && (iota == 0 || typ != nil) {
if (values == nil || len(values.List) == 0) && (iota == 0 || typ != nil) {
p.error(pos, "missing constant value")
}
}
Expand Down
2 changes: 0 additions & 2 deletions sgo/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build disabled

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Expand Down
5 changes: 2 additions & 3 deletions sgo/parser/performance_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// +build disabled

// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package parser

import (
"github.com/tcard/sgo/sgo/token"
"io/ioutil"
"testing"

"github.com/tcard/sgo/sgo/token"
)

var src = readFile("parser.go")
Expand Down
2 changes: 0 additions & 2 deletions sgo/parser/short_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build disabled

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Expand Down
7 changes: 3 additions & 4 deletions sgo/printer/example_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build disabled

// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Expand All @@ -9,12 +7,13 @@ package printer_test
import (
"bytes"
"fmt"
"strings"
"testing"

"github.com/tcard/sgo/sgo/ast"
"github.com/tcard/sgo/sgo/parser"
"github.com/tcard/sgo/sgo/printer"
"github.com/tcard/sgo/sgo/token"
"strings"
"testing"
)

// Dummy test function so that godoc does not use the entire file as example.
Expand Down
29 changes: 11 additions & 18 deletions sgo/printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ func (p *printer) exprList(prev0 token.Pos, list *ast.ExprList, depth int, mode
// use position of expression following the comma as
// comma position for correct comment placement
p.print(x.Pos(), token.COMMA, blank)
} else if i == list.EntangledPos {
} else if i == list.EntangledPos-1 {
p.print(" ")
p.print(x.Pos(), token.BACKSL, blank)
}
p.expr0(x, depth)
}
if list.EntangledPos == len(list.List) {
if list.EntangledPos == len(list.List)+1 {
p.print(" ")
p.print(token.BACKSL, blank)
}
Expand Down Expand Up @@ -303,12 +303,7 @@ func (p *printer) parameters(fields *ast.FieldList) {
if !needsLinebreak {
p.print(par.Pos())
}
if par == fields.Entangled {
p.print(" ")
p.print(token.BACKSL)
} else {
p.print(token.COMMA)
}
p.print(token.COMMA)
}
// separator if needed (linebreak or blank)
if needsLinebreak && p.linebreak(parLineBeg, 0, ws, true) {
Expand Down Expand Up @@ -347,15 +342,12 @@ func (p *printer) parameters(fields *ast.FieldList) {
}

func (p *printer) signature(params, result *ast.FieldList) {
if params != nil {
if params != nil && len(params.List) != 0 {
p.parameters(params)
} else {
p.print(token.LPAREN, token.RPAREN)
}
n := result.NumFields()
if result != nil && result.Entangled != nil {
n++
}
if n > 0 {
// result != nil
p.print(blank)
Expand Down Expand Up @@ -901,6 +893,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
p.expr(x.Value)

case *ast.OptionalType:
p.print(token.QUEST)
p.expr(x.Elt)

default:
Expand Down Expand Up @@ -1170,7 +1163,7 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {

case *ast.ReturnStmt:
p.print(token.RETURN)
if s.Results != nil {
if s.Results != nil && len(s.Results.List) != 0 {
p.print(blank)
// Use indentList heuristic to make corner cases look
// better (issue 1207). A more systematic approach would
Expand Down Expand Up @@ -1216,7 +1209,7 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
}

case *ast.CaseClause:
if s.List != nil {
if s.List != nil && len(s.List.List) != 0 {
p.print(token.CASE, blank)
p.exprList(s.Pos(), s.List, 1, 0, s.Colon)
} else {
Expand Down Expand Up @@ -1334,7 +1327,7 @@ func keepTypeColumn(specs []ast.Spec) []bool {
var keepType bool
for i, s := range specs {
t := s.(*ast.ValueSpec)
if t.Values != nil {
if t.Values != nil && len(t.Values.List) != 0 {
if i0 < 0 {
// start of a run of ValueSpecs with non-nil Values
i0 = i
Expand Down Expand Up @@ -1370,7 +1363,7 @@ func (p *printer) valueSpec(s *ast.ValueSpec, keepType bool) {
if s.Type != nil {
p.expr(s.Type)
}
if s.Values != nil {
if s.Values != nil && len(s.Values.List) != 0 {
p.print(vtab, token.ASSIGN, blank)
p.exprList(token.NoPos, s.Values, 1, 0, token.NoPos)
extraTabs--
Expand Down Expand Up @@ -1452,7 +1445,7 @@ func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
p.print(blank)
p.expr(s.Type)
}
if s.Values != nil {
if s.Values != nil && len(s.Values.List) != 0 {
p.print(blank, token.ASSIGN, blank)
p.exprList(token.NoPos, s.Values, 1, 0, token.NoPos)
}
Expand Down Expand Up @@ -1628,7 +1621,7 @@ func (p *printer) distanceFrom(from token.Pos) int {
func (p *printer) funcDecl(d *ast.FuncDecl) {
p.setComment(d.Doc)
p.print(d.Pos(), token.FUNC, blank)
if d.Recv != nil {
if d.Recv != nil && len(d.Recv.List) != 0 {
p.parameters(d.Recv) // method: print receiver
p.print(blank)
}
Expand Down
5 changes: 3 additions & 2 deletions sgo/printer/performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ package printer

import (
"bytes"
"github.com/tcard/sgo/sgo/ast"
"github.com/tcard/sgo/sgo/parser"
"io"
"io/ioutil"
"log"
"testing"

"github.com/tcard/sgo/sgo/ast"
"github.com/tcard/sgo/sgo/parser"
)

var testfile *ast.File
Expand Down
Loading

0 comments on commit 76110b9

Please sign in to comment.