-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a0f6922
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/dranidis/sdlspec" | ||
) | ||
|
||
type UP struct { | ||
n int | ||
} | ||
type DN struct{} | ||
type OVER struct{} | ||
|
||
var out chan sdlspec.Signal | ||
|
||
func Counter(p *sdlspec.Process) { | ||
var goingDn func() // for mutual definition | ||
|
||
counter := 0 | ||
goingUp := sdlspec.State(p, "goingUp", func(s sdlspec.Signal) { | ||
switch v := s.(type) { | ||
case UP: | ||
counter += v.n | ||
if counter > 4 { | ||
out <- OVER{} | ||
defer goingDn() | ||
return | ||
} | ||
default: | ||
sdlspec.DefaultMessage(p, s) | ||
} | ||
}) | ||
goingDn = sdlspec.State(p, "goingDn", func(s sdlspec.Signal) { | ||
switch s.(type) { | ||
case DN: | ||
counter -= 1 | ||
if counter == 0 { | ||
defer goingUp() | ||
return | ||
} | ||
default: | ||
sdlspec.DefaultMessage(p, s) | ||
} | ||
}) | ||
|
||
go goingUp() | ||
} | ||
|
||
func main() { | ||
//sdlspec.DisableLogging() | ||
die := make(chan sdlspec.Signal) | ||
|
||
out = sdlspec.MakeBuffer() | ||
counterChan := sdlspec.MakeProcess(Counter, "Counter", die) | ||
|
||
go sdlspec.ChannelConsumer(die, "ENV", out) | ||
|
||
sdlspec.Execute( | ||
sdlspec.Transmission{MsDelay: 10, Receiver: counterChan, Signal: UP{}}, | ||
sdlspec.Transmission{MsDelay: 10, Receiver: counterChan, Signal: DN{}}, | ||
sdlspec.Transmission{MsDelay: 10, Receiver: counterChan, Signal: UP{4}}, | ||
sdlspec.Transmission{MsDelay: 10, Receiver: counterChan, Signal: DN{}}, | ||
sdlspec.Transmission{MsDelay: 10, Receiver: counterChan, Signal: DN{}}, | ||
sdlspec.Transmission{MsDelay: 10, Receiver: counterChan, Signal: DN{}}, | ||
sdlspec.Transmission{MsDelay: 10, Receiver: counterChan, Signal: DN{}}, | ||
sdlspec.Transmission{MsDelay: 10, Receiver: counterChan, Signal: DN{}}, | ||
sdlspec.Transmission{MsDelay: 10, Receiver: counterChan, Signal: UP{}}, | ||
) | ||
|
||
time.Sleep(2000 * time.Millisecond) | ||
close(die) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/dranidis/sdlspec-examples | ||
|
||
go 1.16 | ||
|
||
require github.com/dranidis/sdlspec v0.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
github.com/dranidis/sdlspec v0.1.1 h1:4F9bsoXSKdVRkll8vdSo31IyHdQKxOzODhHGRzhe21g= | ||
github.com/dranidis/sdlspec v0.1.1/go.mod h1:pxBWQ3ZRu/ZO2bmrHe3abVm3b75SOmkwGJnN3IUvfl8= | ||
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= | ||
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= | ||
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= | ||
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= | ||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= | ||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= | ||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= | ||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/dranidis/sdlspec" | ||
) | ||
|
||
type HI struct{} | ||
|
||
func helloStates(p *sdlspec.Process) { | ||
start := sdlspec.State(p, "start", func(s sdlspec.Signal) { | ||
switch s.(type) { | ||
case HI: | ||
fmt.Println("Hello SDL") | ||
default: | ||
} | ||
}) | ||
go start() | ||
} | ||
|
||
func main() { | ||
die := make(chan sdlspec.Signal) | ||
helloProcess := sdlspec.MakeProcess(helloStates, "hello", die) | ||
helloProcess <- HI{} | ||
|
||
time.Sleep(1000 * time.Millisecond) | ||
close(die) | ||
} |