Skip to content

Commit de7a2aa

Browse files
committed
Migrate from piNames to internal name package.
This migration allows other Name wrapper to extend default Name implementation.
1 parent b90aabf commit de7a2aa

File tree

7 files changed

+179
-139
lines changed

7 files changed

+179
-139
lines changed

asyncpi.y

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package asyncpi
33

44
import (
55
"io"
6+
7+
"go.nickng.io/asyncpi/internal/name"
68
)
79

810
var proc Process
@@ -38,31 +40,31 @@ proc : simpleproc { $$ = $1 }
3840
;
3941

4042
simpleproc : kNIL { $$ = NewNilProcess() }
41-
| kNAME kLANGLE values kRANGLE { $$ = NewSend(newPiName($1)); $$.(*Send).SetVals($3) }
42-
| kNAME kLPAREN names kRPAREN kPREFIX proc { $$ = NewRecv(newPiName($1), $6); $$.(*Recv).SetVars($3) }
43-
| kNAME kLPAREN names kRPAREN kPREFIX kLPAREN proc kRPAREN { $$ = NewRecv(newPiName($1), $7); $$.(*Recv).SetVars($3) }
43+
| kNAME kLANGLE values kRANGLE { $$ = NewSend(name.New($1)); $$.(*Send).SetVals($3) }
44+
| kNAME kLPAREN names kRPAREN kPREFIX proc { $$ = NewRecv(name.New($1), $6); $$.(*Recv).SetVars($3) }
45+
| kNAME kLPAREN names kRPAREN kPREFIX kLPAREN proc kRPAREN { $$ = NewRecv(name.New($1), $7); $$.(*Recv).SetVars($3) }
4446
| kLPAREN kNEW scopename kRPAREN scope { $$ = NewRestrict($3, $5) }
4547
| kLPAREN kNEW scopename kCOMMA names kRPAREN scope { $$ = NewRestricts(append([]Name{$3}, $5...), $7) }
4648
| kREPEAT proc { $$ = NewRepeat($2) }
4749
| kREPEAT kLPAREN proc kRPAREN { $$ = NewRepeat($3) }
4850
;
4951

50-
scopename : kNAME { $$ = newPiName($1) }
51-
| kNAME kCOLON kNAME { $$ = newHintedName(newPiName($1), $3) }
52+
scopename : kNAME { $$ = name.New($1) }
53+
| kNAME kCOLON kNAME { $$ = name.NewHinted($1, $3) }
5254
;
5355

5456
scope : simpleproc { $$ = $1 }
5557
| kLPAREN proc kRPAREN { $$ = $2 }
5658
;
5759

58-
names : /* empty */ { $$ = []Name{} }
60+
names : /* empty */ { $$ = nil }
5961
| scopename { $$ = []Name{$1} }
6062
| names kCOMMA scopename { $$ = append($1, $3) }
6163
;
6264

63-
values : /* empty */ { $$ = []Name{} }
64-
| kNAME { $$ = []Name{newPiName($1)} }
65-
| values kCOMMA kNAME { $$ = append($1, newPiName($3)) }
65+
values : /* empty */ { $$ = nil }
66+
| kNAME { $$ = []Name{name.New($1)} }
67+
| values kCOMMA kNAME { $$ = append($1, name.New($3)) }
6668
;
6769

6870
%%

asyncpi_test.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"sort"
66
"strings"
77
"testing"
8+
9+
"go.nickng.io/asyncpi/internal/name"
810
)
911

1012
type TestCase struct {
@@ -26,17 +28,17 @@ func init() {
2628
Input: `b().a().0 | b<> | (new x)x(a,b,c).0`,
2729
Output: `((recv(b,[]).recv(a,[]).inact
2830
|send(b,[]))
29-
|restrict(x,recv(x,[a b c]).inact))`, FreeNames: newPiNames("a", "b"),
31+
|restrict(x,recv(x,[a b c]).inact))`, FreeNames: newNames("a", "b"),
3032
},
3133
"Recv": {
3234
Input: `a(b, c,d__). 0 `,
3335
Output: `recv(a,[b c d__]).inact`,
34-
FreeNames: newPiNames("a"),
36+
FreeNames: newNames("a"),
3537
},
3638
"Rep": {
3739
Input: `! a().0`,
3840
Output: `repeat(recv(a,[]).inact)`,
39-
FreeNames: []Name{newPiName("a")},
41+
FreeNames: []Name{name.New("a")},
4042
},
4143
"Res": {
4244
Input: `(new x) x().0 `,
@@ -46,23 +48,23 @@ func init() {
4648
"Send": {
4749
Input: `a<b, e_, b> `,
4850
Output: `send(a,[b e_ b])`,
49-
FreeNames: []Name{newPiName("a"), newPiName("b"), newPiName("e_")},
51+
FreeNames: []Name{name.New("a"), name.New("b"), name.New("e_")},
5052
},
5153
}
5254
}
5355

5456
// Tests fn(a) is a
5557
func TestFreeName(t *testing.T) {
56-
name := newPiName("a")
57-
freeNames := name.FreeNames()
58-
if len(freeNames) == 1 && freeNames[0].Ident() != name.Ident() {
59-
t.Errorf("FreeName: fn(a) does not match a: `%s` vs `%s`", freeNames, name)
58+
n := name.New("a")
59+
freeNames := FreeNames(n)
60+
if len(freeNames) == 1 && freeNames[0].Ident() != n.Ident() {
61+
t.Errorf("FreeName: fn(a) does not match a: `%s` vs `%s`", freeNames, n)
6062
}
6163
}
6264

6365
// Tests fn(a) U fn(b) is a U b
6466
func TestFreeNames(t *testing.T) {
65-
piNames := []Name{newPiName("a"), newPiName("c"), newPiName("b")}
67+
piNames := []Name{name.New("a"), name.New("c"), name.New("b")}
6668
freeNames := []Name{}
6769
for _, name := range piNames {
6870
freeNames = append(freeNames, FreeNames(name)...)

internal/name/name.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2018 Nicholas Ng <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package name provides internal default implementations of the asyncpi Names.
16+
package name
17+
18+
// base is a default Name implementation.
19+
type base struct {
20+
name string
21+
}
22+
23+
// New returns a new concrete name from a string.
24+
func New(name string) *base {
25+
return &base{name}
26+
}
27+
28+
// Ident returns the string identifier of the base name n.
29+
func (n *base) Ident() string {
30+
return n.name
31+
}
32+
33+
// SetName sets the internal name.
34+
func (n *base) SetName(name string) {
35+
n.name = name
36+
}
37+
38+
func (n *base) String() string {
39+
return n.name
40+
}
41+
42+
// hinted represents a name with type hint.
43+
type hinted struct {
44+
name string
45+
hint string
46+
}
47+
48+
// NewHinted returns a new hinted name from a string name and type hint.
49+
func NewHinted(name, hint string) *hinted {
50+
return &hinted{name, hint}
51+
}
52+
53+
// Ident returns the string identifier of the hinted name n.
54+
func (n *hinted) Ident() string {
55+
return n.name
56+
}
57+
58+
// SetName sets the internal name.
59+
func (n *hinted) SetName(name string) {
60+
n.name = name
61+
}
62+
63+
func (n *hinted) String() string {
64+
return n.name
65+
}
66+
67+
// TypeHint returns the type hint of hinted name n.
68+
func (n *hinted) TypeHint() string {
69+
return n.hint
70+
}

internal/name/name_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package name_test
2+
3+
import (
4+
"testing"
5+
6+
"go.nickng.io/asyncpi"
7+
"go.nickng.io/asyncpi/internal/name"
8+
)
9+
10+
func TestBaseName(t *testing.T) {
11+
var n asyncpi.Name
12+
n = name.New("abc")
13+
if _, ok := n.(name.Setter); !ok {
14+
t.Fatalf("%s should have SetName()", n.Ident())
15+
}
16+
}
17+
18+
func TestHuntedName(t *testing.T) {
19+
var n asyncpi.Name
20+
n = name.NewHinted("hinted", "int")
21+
if _, ok := n.(name.Setter); !ok {
22+
t.Fatalf("%v should have SetName()", n)
23+
}
24+
if _, ok := n.(name.TypeHinter); !ok {
25+
t.Fatalf("%v should have TypeHint()", n)
26+
}
27+
}

names.go

+4-71
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,21 @@
11
package asyncpi
22

33
import (
4-
"bytes"
54
"fmt"
5+
"go.nickng.io/asyncpi/internal/name"
66
"log"
77
)
88

9-
// name is a concrete Name.
10-
type piName struct {
11-
name string
12-
s sorts
13-
}
14-
15-
// newPiName creates a new concrete name from a string.
16-
func newPiName(n string) *piName {
17-
return &piName{name: n}
18-
}
19-
20-
// setSort sets the name sort.
21-
func (n *piName) setSort(s sorts) {
22-
n.s = s
23-
}
24-
25-
// SetName sets the internal name.
26-
func (n *piName) SetName(name string) {
27-
n.name = name
28-
}
29-
30-
// FreeNames of name is itself (if sort is name).
31-
func (n *piName) FreeNames() []Name {
32-
if n.s == nameSort {
33-
return []Name{n}
34-
}
35-
return []Name{}
36-
}
37-
38-
// FreeVars of name is itself (if sort is var).
39-
func (n *piName) FreeVars() []Name {
40-
if n.s == varSort {
41-
return []Name{n}
42-
}
43-
return []Name{}
44-
}
45-
46-
// Ident is the string identifier of a name.
47-
func (n *piName) Ident() string {
48-
return n.name
49-
}
50-
51-
func (n *piName) String() string {
52-
var buf bytes.Buffer
53-
if n.s == varSort {
54-
buf.WriteString("_")
55-
}
56-
buf.WriteString(n.name)
57-
return buf.String()
58-
}
59-
60-
// newPiNames is a convenient utility function
9+
// newNames is a convenient utility function
6110
// for creating a []Name from given strings.
62-
func newPiNames(names ...string) []Name {
11+
func newNames(names ...string) []Name {
6312
pn := make([]Name, len(names))
6413
for i, n := range names {
65-
pn[i] = newPiName(n)
14+
pn[i] = name.New(n)
6615
}
6716
return pn
6817
}
6918

70-
// hintedName represents a Name extended with type hint.
71-
type hintedName struct {
72-
Name
73-
hint string
74-
}
75-
76-
// newHintedName returns a new Name for the given n and t.
77-
func newHintedName(name Name, hint string) *hintedName {
78-
return &hintedName{name, hint}
79-
}
80-
81-
// TypeHint returns the type hint attached to given Name.
82-
func (n *hintedName) TypeHint() string {
83-
return n.hint
84-
}
85-
8619
type TypeHinter interface {
8720
TypeHint() string
8821
}

0 commit comments

Comments
 (0)