Skip to content

Commit a7f173e

Browse files
committed
Refactor π-calculi generation.
π-calculi generation moved to separate file with tests. Fixes the following so input matches output: - Parenthesis on !(P) in parser accepted - Output: !((P|Q)) → !(P|Q)
1 parent 80a0a4b commit a7f173e

File tree

5 files changed

+237
-137
lines changed

5 files changed

+237
-137
lines changed

asyncpi.go

+1-90
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ package asyncpi
55
import (
66
"bytes"
77
"fmt"
8-
"log"
98
"sort"
10-
11-
"text/template"
129
)
1310

1411
// Name is channel or value.
@@ -39,6 +36,7 @@ type Process interface {
3936
FreeNames() []Name
4037
FreeVars() []Name
4138

39+
// Calculi returns the calculi representation.
4240
Calculi() string
4341
String() string
4442
}
@@ -61,11 +59,6 @@ func (n *NilProcess) FreeVars() []Name {
6159
return []Name{}
6260
}
6361

64-
// Calculi returns the calculi representation.
65-
func (n *NilProcess) Calculi() string {
66-
return "0"
67-
}
68-
6962
func (n *NilProcess) String() string {
7063
return "Inaction\n"
7164
}
@@ -98,21 +91,6 @@ func (p *Par) FreeVars() []Name {
9891
return remDup(fv)
9992
}
10093

101-
// Calculi returns the calculi representation.
102-
func (p *Par) Calculi() string {
103-
buf := new(bytes.Buffer)
104-
t := template.Must(template.New("par").Parse(`(
105-
{{- range $i, $p := .Procs -}}
106-
{{- if $i }} | {{ end -}}{{- $p.Calculi -}}
107-
{{- end -}})`))
108-
err := t.Execute(buf, p)
109-
if err != nil {
110-
log.Println("Executing template:", err)
111-
return ""
112-
}
113-
return buf.String()
114-
}
115-
11694
func (p *Par) String() string {
11795
var buf bytes.Buffer
11896
for i, proc := range p.Procs {
@@ -172,22 +150,6 @@ func (r *Recv) FreeVars() []Name {
172150
return remDup(ffv)
173151
}
174152

175-
// Calculi returns the calculi representation.
176-
func (r *Recv) Calculi() string {
177-
buf := new(bytes.Buffer)
178-
t := template.Must(template.New("send").Parse(`
179-
{{- .Chan.Name -}}(
180-
{{- range $i, $v := .Vars -}}
181-
{{- if $i -}},{{- end -}}{{- $v.Name -}}
182-
{{- end -}}).{{ .Cont.Calculi }}`))
183-
err := t.Execute(buf, r)
184-
if err != nil {
185-
log.Println("Executing template:", err)
186-
return ""
187-
}
188-
return buf.String()
189-
}
190-
191153
func (r *Recv) String() string {
192154
return fmt.Sprintf("Recv(%s, %s)\n%s", r.Chan.Name(), r.Vars, r.Cont)
193155
}
@@ -212,18 +174,6 @@ func (r *Repeat) FreeVars() []Name {
212174
return r.Proc.FreeVars()
213175
}
214176

215-
// Calculi returns the calculi representation.
216-
func (r *Repeat) Calculi() string {
217-
buf := new(bytes.Buffer)
218-
t := template.Must(template.New("rep").Parse(`!{{- .Proc.Calculi -}}`))
219-
err := t.Execute(buf, r)
220-
if err != nil {
221-
log.Println("Executing template:", err)
222-
return ""
223-
}
224-
return buf.String()
225-
}
226-
227177
func (r *Repeat) String() string {
228178
return fmt.Sprintf("repeat {\n%s}\n", r.Proc)
229179
}
@@ -269,29 +219,6 @@ func (r *Restrict) FreeVars() []Name {
269219
return r.Proc.FreeVars()
270220
}
271221

272-
// Calculi returns the calculi representation.
273-
func (r *Restrict) Calculi() string {
274-
buf := new(bytes.Buffer)
275-
if _, ok := r.Proc.(*Par); ok {
276-
t := template.Must(template.New("res").Parse(`(new {{ .Name.Name -}})(
277-
{{- .Proc.Calculi -}})`))
278-
err := t.Execute(buf, r)
279-
if err != nil {
280-
log.Println("Executing template:", err)
281-
return ""
282-
}
283-
} else {
284-
t := template.Must(template.New("res").Parse(`(new {{ .Name.Name -}})
285-
{{- .Proc.Calculi -}}`))
286-
err := t.Execute(buf, r)
287-
if err != nil {
288-
log.Println("Executing template:", err)
289-
return ""
290-
}
291-
}
292-
return buf.String()
293-
}
294-
295222
func (r *Restrict) String() string {
296223
return fmt.Sprintf("scope %s {\n%s}\n", r.Name, r.Proc)
297224
}
@@ -334,22 +261,6 @@ func (s *Send) FreeVars() []Name {
334261
return remDup(fv)
335262
}
336263

337-
// Calculi returns the calculi representation.
338-
func (s *Send) Calculi() string {
339-
buf := new(bytes.Buffer)
340-
t := template.Must(template.New("send").Parse(`
341-
{{- .Chan.Name -}}<
342-
{{- range $i, $v := .Vals -}}
343-
{{- if $i -}},{{- end -}}{{- $v.Name -}}
344-
{{- end -}}>`))
345-
err := t.Execute(buf, s)
346-
if err != nil {
347-
log.Println("Executing template:", err)
348-
return ""
349-
}
350-
return buf.String()
351-
}
352-
353264
func (s *Send) String() string {
354265
return fmt.Sprintf("Send(%s, %s)\n", s.Chan.Name(), s.Vals)
355266
}

asyncpi.y

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ simpleproc : kNIL { $$ = NewNilProcess() }
4343
| kNAME kLPAREN names kRPAREN kPREFIX kLPAREN proc kRPAREN { $$ = NewRecv(newPiName($1), $7); $$.(*Recv).SetVars($3) }
4444
| kLPAREN kNEW scopename kRPAREN scope { $$ = NewRestrict($3, $5) }
4545
| kLPAREN kNEW scopename kCOMMA names kRPAREN scope { $$ = NewRestricts(append([]Name{$3}, $5...), $7) }
46-
| kREPEAT proc { $$ = NewRepeat($2) }
46+
| kREPEAT proc { $$ = NewRepeat($2) }
47+
| kREPEAT kLPAREN proc kRPAREN { $$ = NewRepeat($3) }
4748
;
4849

4950
scopename : kNAME { $$ = newPiName($1) }

calculi.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 asyncpi
16+
17+
import (
18+
"bytes"
19+
"log"
20+
"text/template"
21+
)
22+
23+
const parTmpl = `(
24+
{{- range $i, $p := .Procs -}}
25+
{{- if $i }} | {{ end -}}{{- $p.Calculi -}}
26+
{{- end -}})`
27+
28+
const recvTmpl = `{{- .Chan.Name -}}(
29+
{{- range $i, $v := .Vars -}}
30+
{{- if $i -}},{{- end -}}{{- $v.Name -}}
31+
{{- end -}}).{{ .Cont.Calculi }}`
32+
33+
const sendTmpl = `{{- .Chan.Name -}}<
34+
{{- range $i, $v := .Vals -}}
35+
{{- if $i -}},{{- end -}}{{- $v.Name -}}
36+
{{- end -}}>`
37+
38+
const repTmpl = `!{{- .Proc.Calculi -}}`
39+
40+
const resTmpl = `(new {{ .Name.Name -}}){{- .Proc.Calculi -}}`
41+
42+
var (
43+
parT = template.Must(template.New("").Parse(parTmpl))
44+
recvT = template.Must(template.New("").Parse(recvTmpl))
45+
repT = template.Must(template.New("").Parse(repTmpl))
46+
resT = template.Must(template.New("").Parse(resTmpl))
47+
sendT = template.Must(template.New("").Parse(sendTmpl))
48+
)
49+
50+
func (p *NilProcess) Calculi() string {
51+
return "0"
52+
}
53+
54+
func (p *Par) Calculi() string {
55+
var buf bytes.Buffer
56+
if err := parT.Execute(&buf, p); err != nil {
57+
log.Print(err)
58+
}
59+
return buf.String()
60+
}
61+
62+
func (p *Recv) Calculi() string {
63+
var buf bytes.Buffer
64+
if err := recvT.Execute(&buf, p); err != nil {
65+
log.Print(err)
66+
}
67+
return buf.String()
68+
}
69+
70+
func (p *Repeat) Calculi() string {
71+
var buf bytes.Buffer
72+
if err := repT.Execute(&buf, p); err != nil {
73+
log.Print(err)
74+
}
75+
return buf.String()
76+
}
77+
78+
func (p *Restrict) Calculi() string {
79+
var buf bytes.Buffer
80+
if err := resT.Execute(&buf, p); err != nil {
81+
log.Print(err)
82+
}
83+
return buf.String()
84+
}
85+
86+
func (p *Send) Calculi() string {
87+
var buf bytes.Buffer
88+
if err := sendT.Execute(&buf, p); err != nil {
89+
log.Print(err)
90+
}
91+
return buf.String()
92+
}

calculi_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package asyncpi
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestNilProcessCalculi(t *testing.T) {
11+
const procStr = `0`
12+
p, err := Parse(strings.NewReader(procStr))
13+
if err != nil {
14+
t.Error(err)
15+
}
16+
if want, got := procStr, p.Calculi(); want != got {
17+
t.Errorf("expecting calculi to be %s but got %s", want, got)
18+
}
19+
}
20+
21+
func TestRecvCalculi(t *testing.T) {
22+
const procStr = `a(b,c).0`
23+
p, err := Parse(strings.NewReader(procStr))
24+
if err != nil {
25+
t.Error(err)
26+
}
27+
if want, got := procStr, p.Calculi(); want != got {
28+
t.Errorf("expecting calculi to be %s but got %s", want, got)
29+
}
30+
}
31+
32+
func TestRepeatCalculi(t *testing.T) {
33+
const procStr = `!a(b,c).0`
34+
p, err := Parse(strings.NewReader(procStr))
35+
if err != nil {
36+
t.Error(err)
37+
}
38+
if want, got := procStr, p.Calculi(); want != got {
39+
t.Errorf("expecting calculi to be %s but got %s", want, got)
40+
}
41+
}
42+
43+
func TestRepeatParCalculi(t *testing.T) {
44+
const procStr = `!(a(u,v).0 | a<b,c>)`
45+
var buf bytes.Buffer
46+
p, err := Parse(io.TeeReader(strings.NewReader(procStr), &buf))
47+
if err != nil {
48+
if pe, ok := err.(*ParseError); ok {
49+
t.Logf("\n%s", string(pe.Pos.CaretDiag(buf.Bytes())))
50+
}
51+
t.Error(err)
52+
}
53+
if want, got := procStr, p.Calculi(); want != got {
54+
t.Errorf("expecting calculi to be %s but got %s", want, got)
55+
}
56+
}
57+
58+
func TestRestrictCalculi(t *testing.T) {
59+
const procStr = `(new a)a(x,y).0`
60+
p, err := Parse(strings.NewReader(procStr))
61+
if err != nil {
62+
t.Error(err)
63+
}
64+
if want, got := procStr, p.Calculi(); want != got {
65+
t.Errorf("expecting calculi to be %s but got %s", want, got)
66+
}
67+
}
68+
69+
func TestRestricParCalculi(t *testing.T) {
70+
const procStr = `(new a)(a(x,y).0 | a<b,c>)`
71+
p, err := Parse(strings.NewReader(procStr))
72+
if err != nil {
73+
t.Error(err)
74+
}
75+
if want, got := procStr, p.Calculi(); want != got {
76+
t.Errorf("expecting calculi to be %s but got %s", want, got)
77+
}
78+
}
79+
80+
func TestSendCalculi(t *testing.T) {
81+
const procStr = `a<b,c>`
82+
p, err := Parse(strings.NewReader(procStr))
83+
if err != nil {
84+
t.Error(err)
85+
}
86+
if want, got := procStr, p.Calculi(); want != got {
87+
t.Errorf("expecting calculi to be %s but got %s", want, got)
88+
}
89+
}

0 commit comments

Comments
 (0)