-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendrecv_reduce2.go
89 lines (79 loc) · 1.72 KB
/
sendrecv_reduce2.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
87
88
89
// +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 chainReaction = `
(new ch2)(ch2<1, 2> | ch2(x, y).((new ch3) (ch3<x> | ch3(z).ch3(z).0 | ch3<y>)))`
proc := mustParse(chainReaction)
fmt.Println(proc.Calculi())
// Keep reducing until not possible
for { // HL
changed, err := asyncpi.Reduce1(proc) // HL
if err != nil {
log.Fatal("reduction error", err) // handle errors
break
}
if !changed { // HL
break // HL
} // HL
proc, _ = asyncpi.SimplifyBySC(proc)
fmt.Println("→ Reduces to:", proc.Calculi())
} // HL
}
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")
}