Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cb.BinaryOp: -> <> #395

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"syscall"

"github.com/goplus/gox/internal"
xtoken "github.com/goplus/gox/token"
"golang.org/x/tools/go/types/typeutil"
)

Expand Down Expand Up @@ -1351,7 +1352,7 @@ func (p *CodeBuilder) VarVal(name string, src ...ast.Node) *CodeBuilder {
if o == nil {
log.Panicf("VarVal: variable `%v` not found\n", name)
}
return p.Val(o)
return p.Val(o, src...)
}

// Val func
Expand Down Expand Up @@ -2196,15 +2197,18 @@ func (p *CodeBuilder) UnaryOp(op token.Token, params ...interface{}) *CodeBuilde

// BinaryOp func
func (p *CodeBuilder) BinaryOp(op token.Token, src ...ast.Node) *CodeBuilder {
const (
errNotFound = syscall.ENOENT
)
if debugInstr {
log.Println("BinaryOp", op)
log.Println("BinaryOp", xtoken.String(op))
}
pkg := p.pkg
name := goxPrefix + binaryOps[op]
args := p.stk.GetArgs(2)

var ret *internal.Elem
var err error = syscall.ENOENT
var err error = errNotFound
isUserDef := false
arg0 := args[0].Type
named0, ok0 := checkNamed(arg0)
Expand Down Expand Up @@ -2240,20 +2244,28 @@ func (p *CodeBuilder) BinaryOp(op token.Token, src ...ast.Node) *CodeBuilder {
CVal: binaryOp(p, op, args),
}, nil
}
} else {
lm := pkg.builtin.Ref(name)
} else if lm := pkg.builtin.TryRef(name); lm != nil {
ret, err = matchFuncCall(pkg, toObject(pkg, lm, nil), args, 0)
} else {
err = errNotFound
}
}

expr := getSrc(src)
if err != nil {
opstr := xtoken.String(op)
src, pos := p.loadExpr(expr)
if src == "" {
src = op.String()
src = opstr
}
if err != errNotFound {
p.panicCodeErrorf(
pos, "invalid operation: %s (mismatched types %v and %v)", src, arg0, args[1].Type)
} else {
arg0Src, _ := p.loadExpr(args[0].Src)
p.panicCodeErrorf(
pos, "invalid operation: operator %s not defined on %s (%v)", opstr, arg0Src, arg0)
}
p.panicCodeErrorf(
pos, "invalid operation: %s (mismatched types %v and %v)", src, arg0, args[1].Type)
}
ret.Src = expr
p.stk.Ret(2, ret)
Expand Down Expand Up @@ -2300,6 +2312,9 @@ var (
token.GEQ: "GE",
token.EQL: "EQ",
token.NEQ: "NE",

xtoken.SRARROW: "PointTo", // ->
xtoken.BIDIARROW: "PointBi", // <>
}
)

Expand Down
12 changes: 12 additions & 0 deletions error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"testing"

"github.com/goplus/gox"
xtoken "github.com/goplus/gox/token"
)

type txtNode struct {
Expand Down Expand Up @@ -278,6 +279,17 @@ func TestErrBinaryOp(t *testing.T) {
})
}

func TestErrBinaryOp2(t *testing.T) {
codeErrorTest(t, `-: invalid operation: operator <> not defined on a (int)`,
func(pkg *gox.Package) {
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
NewVar(types.Typ[types.Int], "a").
NewVar(types.Typ[types.Int], "b").
VarVal("a", source("a")).VarVal("b").BinaryOp(xtoken.BIDIARROW).EndStmt().
End()
})
}

func TestErrTypeAssert(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:9: impossible type assertion:\n\tstring does not implement bar (missing Bar method)",
func(pkg *gox.Package) {
Expand Down
41 changes: 41 additions & 0 deletions token/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2024 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package token

import (
"go/token"
)

// Token is the set of lexical tokens of the Go/Go+ programming language.
type Token = token.Token

const (
additional_beg = token.TILDE - 1
additional_end = token.TILDE + 1

SRARROW = additional_beg // -> (single right arrow)
BIDIARROW = additional_end // <> (bidirectional arrow)
)

func String(tok Token) string {
if tok >= additional_beg {
switch tok {
case SRARROW:
return "->"
case BIDIARROW:
return "<>"
}
}
return tok.String()
}