Skip to content

Commit

Permalink
Merge pull request #395 from xushiwei/q
Browse files Browse the repository at this point in the history
cb.BinaryOp: -> <>
  • Loading branch information
xushiwei authored Feb 21, 2024
2 parents a29adc8 + 0a6252d commit e20cebf
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
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()
}

0 comments on commit e20cebf

Please sign in to comment.