-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendrecv_reduce.go
86 lines (75 loc) · 1.65 KB
/
sendrecv_reduce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// +build ignore
package main
import (
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"strings"
"go.nickng.io/asyncpi"
"golang.org/x/tools/imports"
)
func main() {
const sendrecv = `
(new ch)( # Create a channel "ch"
ch<a> # Send to "ch"
| ch(x).0 # Concurrently, Receive from "ch"
)`
proc, err := asyncpi.Parse(strings.NewReader(sendrecv))
if err != nil {
// handle error
}
fmt.Printf("Before reduction:\n\t%s\n", proc.Calculi())
// Reduce system for a single step // HL
asyncpi.Reduce1(proc) // HL
fmt.Printf("After reduction:\n\t%s\n", proc.Calculi())
}
func mustParse(s string) asyncpi.Process {
proc, err := asyncpi.Parse(strings.NewReader(s))
if err != nil {
log.Fatal("parse failed:", err)
}
return proc
}
func reduceAll(proc asyncpi.Process) asyncpi.Process {
for {
changed, err := asyncpi.Reduce1(proc)
if err != nil {
log.Fatal("reduction error", err) // handle errors
break
}
if !changed {
break
}
proc, _ = asyncpi.SimplifyBySC(proc)
fmt.Println("→ Reduces to:", proc.Calculi())
}
return proc
}
func fixImports(src []byte) []byte {
opts := &imports.Options{TabIndent: true, Fragment: false}
imported, err := imports.Process("/tmp/main.go", src, opts)
if err != nil {
log.Fatal(err)
}
return imported
}
func goFmt(src []byte) []byte {
fmtd, err := format.Source(src)
if err != nil {
log.Fatal(err)
}
return fmtd
}
func writeTemp(content []byte) {
f, err := ioutil.TempFile("", "generated")
os.Rename(f.Name(), f.Name()+".go")
if err != nil {
log.Fatal(err)
}
if _, err := f.Write(content); err != nil {
log.Fatal(err)
}
fmt.Println("written to temp file:", f.Name()+".go")
}