Skip to content

Commit

Permalink
1st commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dranidis committed Apr 14, 2021
0 parents commit a0f6922
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
Empty file added README.md
Empty file.
73 changes: 73 additions & 0 deletions counter/counter.go
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)
}
5 changes: 5 additions & 0 deletions go.mod
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
11 changes: 11 additions & 0 deletions go.sum
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=
30 changes: 30 additions & 0 deletions hello/hello.go
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)
}

0 comments on commit a0f6922

Please sign in to comment.