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

ArrowOp: -> <> #1766

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
3 changes: 3 additions & 0 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,9 @@ var binaryGopNames = map[string]string{
">=": "Gop_GE",
">": "Gop_GT",

"->": "Gop_PointTo",
"<>": "Gop_PointBi",

"&&": "Gop_LAnd",
"||": "Gop_LOr",

Expand Down
28 changes: 28 additions & 0 deletions cl/compile_gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@ import (
"testing"
)

func TestArrowOp(t *testing.T) {
gopClTest(t, `
type foo struct {
}

func (a foo) -> (b foo) {
println "a -> b"
}

func (a foo) <> (b foo) {
println "a <> b"
}
`, `package main

import "fmt"

type foo struct {
}

func (a foo) Gop_PointTo(b foo) {
fmt.Println("a -> b")
}
func (a foo) Gop_PointBi(b foo) {
fmt.Println("a <> b")
}
`)
}

func TestMapLit(t *testing.T) {
gopClTest(t, `
func foo(map[string]string) {}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/goplus/c2go v0.7.24-0.20240221044754-e542e30f9dbc
github.com/goplus/gox v1.14.13-0.20240221041432-a29adc8f26fd
github.com/goplus/gox v1.14.13-0.20240221143457-e20cebf7f229
github.com/goplus/mod v0.13.8-0.20240218230953-c1aeebf6e4f7
github.com/qiniu/x v1.13.9-0.20240218231431-55e88daed284
golang.org/x/tools v0.18.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyT
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/goplus/c2go v0.7.24-0.20240221044754-e542e30f9dbc h1:HGH6KoedGQ+DikZ1n9cDkCpMjODHftxbPqEhb7zDz4I=
github.com/goplus/c2go v0.7.24-0.20240221044754-e542e30f9dbc/go.mod h1:m+2bOIErSOA4sxyrg0deb7RS6cnC3czzo7AaL9IZ+YE=
github.com/goplus/gox v1.14.13-0.20240221041432-a29adc8f26fd h1:DZsCu/zgtGKDwp6MBjRRrmSvZRktI/Fs6TYS8O8pKVs=
github.com/goplus/gox v1.14.13-0.20240221041432-a29adc8f26fd/go.mod h1:6b6XYHmyiCevhwuEHcV/jzm7Z2FXLDBhuxgvkjceA+o=
github.com/goplus/gox v1.14.13-0.20240221143457-e20cebf7f229 h1:zKgGmIvM7dvMEMF4Q5K51vo3aPqRf5GjPzlYMZhp7bk=
github.com/goplus/gox v1.14.13-0.20240221143457-e20cebf7f229/go.mod h1:6b6XYHmyiCevhwuEHcV/jzm7Z2FXLDBhuxgvkjceA+o=
github.com/goplus/mod v0.13.8-0.20240218230953-c1aeebf6e4f7 h1:3r+SsB6gkVApZgxG6WaFk41nIKHWaSDx7T/C4aTo2S8=
github.com/goplus/mod v0.13.8-0.20240218230953-c1aeebf6e4f7/go.mod h1:edZE5Qs+9mRnIjvWhyMNUkm5ayJ+dMlJ3J3YeXODNwA=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down
10 changes: 10 additions & 0 deletions testdata/overloadop1/overloadop.gop
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ func (a foo) - (b foo) (ret foo) {
return
}

func (a foo) -> (b foo) {
println "a -> b"
}

func (a foo) <> (b foo) {
println "a <> b"
}

func -(a foo) (ret foo) {
println "-a"
return
Expand All @@ -32,3 +40,5 @@ var e = -a
var f = a != b
println f
a++
a -> b
a <> b
15 changes: 9 additions & 6 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package token
import (
"go/token"
"strconv"

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

// Token is the set of lexical tokens of the Go+ programming language.
Expand Down Expand Up @@ -140,12 +142,13 @@ const (
TILDE // additional tokens, handled in an ad-hoc manner
additional_end

CSTRING = literal_beg // C"Hello"
RAT = literal_end // 123.5r
DRARROW = operator_beg // => (double right arrow)
QUESTION = operator_end // ?
SRARROW = additional_beg // -> (single right arrow)
BIDIARROW = additional_end // <> (bidirectional arrow)
CSTRING = literal_beg // C"Hello"
RAT = literal_end // 123.5r
DRARROW = operator_beg // => (double right arrow)
QUESTION = operator_end // ?

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

// Deprecated: use DRARROW instead of RARROW
RARROW = DRARROW
Expand Down
92 changes: 92 additions & 0 deletions token/token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
*
* 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 (
"reflect"
"testing"
)

func TestArrowOp(t *testing.T) {
if v := BIDIARROW.IsOperator(); !v {
t.Fatal("BIDIARROW not op?")
}
if v := SRARROW.IsOperator(); !v {
t.Fatal("SRARROW not op?")
}
if BIDIARROW.Precedence() != NEQ.Precedence() {
t.Fatal("BIDIARROW.Precedence")
}
if v := BIDIARROW.String(); v != "<>" {
t.Fatal("BIDIARROW.String:", v)
}
if v := (additional_end + 100).String(); v != "token(189)" {
t.Fatal("token.String:", v)
}
}

func TestPrecedence(t *testing.T) {
cases := map[Token]int{
LOR: 1,
LAND: 2,
EQL: 3,
SUB: 4,
MUL: 5,
ARROW: LowestPrec,
}
for op, prec := range cases {
if v := op.Precedence(); v != prec {
t.Fatal("Precedence:", op, v)
}
}
}

func TestLookup(t *testing.T) {
if v := Lookup("type"); v != TYPE {
t.Fatal("TestLookup type:", v)
} else if !v.IsKeyword() {
t.Fatal("v.IsKeyword:", v)
}
if v := Lookup("new"); v != IDENT {
t.Fatal("TestLookup new:", v)
} else if !v.IsLiteral() {
t.Fatal("v.IsLiteral:", v)
}
}

func TestBasic(t *testing.T) {
if !IsExported("Name") {
t.Fatal("IsExported")
}
if !IsKeyword("func") {
t.Fatal("IsKeyword")
}
if !IsIdentifier("new") {
t.Fatal("IsIdentifier")
}
}

func TestLines(t *testing.T) {
fset := NewFileSet()
f := fset.AddFile("foo.go", 100, 100)
lines := []int{0, 10, 50}
f.SetLines(lines)
ret := Lines(f)
if !reflect.DeepEqual(ret, lines) {
t.Fatal("TestLines failed:", ret)
}
}