Skip to content

Commit

Permalink
fix problem with builtin functions
Browse files Browse the repository at this point in the history
make and new mostly, since the result is from type of parameter
not the result in definition. I should handle more builtin func
  • Loading branch information
fzerorubigd committed Mar 9, 2018
1 parent 2bcfec6 commit f9186ad
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
42 changes: 27 additions & 15 deletions var.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,38 @@ func (v *Variable) lateBind() error {
switch c := v.caller.Fun.(type) {
case *ast.Ident:
name := nameFromIdent(c)
bl, err := getBuiltin().FindFunction(name)
fn, err := getBuiltin().FindFunction(name)
if err == nil {
v.Type = bl.Type
} else {
var t Type
fn, err := v.pkg.FindFunction(name)
if err == nil {
if len(fn.Type.Results) <= v.index {
return fmt.Errorf("%d result is available but want the %d", len(fn.Type.Results), v.index)
}
t = fn.Type.Results[v.index].Type
} else {
t, err = checkTypeCast(v.pkg, getBuiltin(), v.caller.Args, name)
if err != nil {
return err
// make and new are exceptions,
if fn.Name == "make" {
v.Type = newType(v.pkg, v.file, v.caller.Args[0])
}

if fn.Name == "new" {
tt := newType(v.pkg, v.file, v.caller.Args[0])
v.Type = &StarType{
Target: tt,
}
}
break
}

v.Type = t
fn, err = v.pkg.FindFunction(name)
var t Type
if err == nil {
if len(fn.Type.Results) <= v.index {
return fmt.Errorf("%d result is available but want the %d", len(fn.Type.Results), v.index)
}
t = fn.Type.Results[v.index].Type
} else {
t, err = checkTypeCast(v.pkg, getBuiltin(), v.caller.Args, name)
if err != nil {
return err
}
}

v.Type = t

case *ast.SelectorExpr:
var pkg string
switch c.X.(type) {
Expand Down
26 changes: 20 additions & 6 deletions var_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var i5 = []int{1,2,3,4,5}
var i6 = 10i
var i7 = make([]int, 10)
var i8 = new(int)
`

func TestVariable(t *testing.T) {
Expand All @@ -43,48 +46,59 @@ func TestVariable(t *testing.T) {
i, err := p.FindVariable("i1")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i1")
So(i.Name, ShouldEqual, "i1")
So(i.Type.(*IdentType).Ident, ShouldEqual, "int")
})

Convey("by value i2", func() {
i, err := p.FindVariable("i2")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i2")
So(i.Name, ShouldEqual, "i2")
So(i.Type.(*IdentType).Ident, ShouldEqual, "float64")
})

Convey("by value i3", func() {
i, err := p.FindVariable("i3")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i3")
So(i.Name, ShouldEqual, "i3")
So(i.Type.(*IdentType).Ident, ShouldEqual, "string")
})

Convey("by value i4", func() {
i, err := p.FindVariable("i4")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i4")
So(i.Name, ShouldEqual, "i4")
So(i.Type.(*IdentType).Ident, ShouldEqual, "char")
})

Convey("by value i5", func() {
i, err := p.FindVariable("i5")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i5")
So(i.Name, ShouldEqual, "i5")
So(i.Type.(*ArrayType).Type.(*IdentType).Ident, ShouldEqual, "int")
})

Convey("by value i6", func() {
i, err := p.FindVariable("i6")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i6")
So(i.Name, ShouldEqual, "i6")
So(i.Type.(*IdentType).Ident, ShouldEqual, "complex64")
})

Convey("call builtin func", func() {
So(p.Bind(), ShouldBeNil)
Convey("make func i7", func() {
i, err := p.FindVariable("i7")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i7")
So(i.Type.(*ArrayType).Type.(*IdentType).Ident, ShouldEqual, "int")
})

Convey("new func i8", func() {
i, err := p.FindVariable("i8")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i8")
So(i.Type.(*StarType).Target.(*IdentType).Ident, ShouldEqual, "int")
})
})
})
}

0 comments on commit f9186ad

Please sign in to comment.