mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-18 21:11:03 +01:00
added
This commit is contained in:
70
vendor/github.com/glycerine/zygomys/zygo/channels.go
generated
vendored
Normal file
70
vendor/github.com/glycerine/zygomys/zygo/channels.go
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
package zygo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type SexpChannel struct {
|
||||
Val chan Sexp
|
||||
Typ *RegisteredType
|
||||
}
|
||||
|
||||
func (ch *SexpChannel) SexpString(ps *PrintState) string {
|
||||
return "[chan]"
|
||||
}
|
||||
|
||||
func (ch *SexpChannel) Type() *RegisteredType {
|
||||
return ch.Typ // TODO what should this be?
|
||||
}
|
||||
|
||||
func MakeChanFunction(env *Zlisp, name string,
|
||||
args []Sexp) (Sexp, error) {
|
||||
if len(args) > 1 {
|
||||
return SexpNull, WrongNargs
|
||||
}
|
||||
|
||||
size := 0
|
||||
if len(args) == 1 {
|
||||
switch t := args[0].(type) {
|
||||
case *SexpInt:
|
||||
size = int(t.Val)
|
||||
default:
|
||||
return SexpNull, errors.New(
|
||||
fmt.Sprintf("argument to %s must be int", name))
|
||||
}
|
||||
}
|
||||
|
||||
return &SexpChannel{Val: make(chan Sexp, size)}, nil
|
||||
}
|
||||
|
||||
func ChanTxFunction(env *Zlisp, name string,
|
||||
args []Sexp) (Sexp, error) {
|
||||
if len(args) < 1 {
|
||||
return SexpNull, WrongNargs
|
||||
}
|
||||
var channel chan Sexp
|
||||
switch t := args[0].(type) {
|
||||
case *SexpChannel:
|
||||
channel = chan Sexp(t.Val)
|
||||
default:
|
||||
return SexpNull, errors.New(
|
||||
fmt.Sprintf("argument 0 of %s must be channel", name))
|
||||
}
|
||||
|
||||
if name == "send" {
|
||||
if len(args) != 2 {
|
||||
return SexpNull, WrongNargs
|
||||
}
|
||||
channel <- args[1]
|
||||
return SexpNull, nil
|
||||
}
|
||||
|
||||
return <-channel, nil
|
||||
}
|
||||
|
||||
func (env *Zlisp) ImportChannels() {
|
||||
env.AddFunction("makeChan", MakeChanFunction)
|
||||
env.AddFunction("send", ChanTxFunction)
|
||||
env.AddFunction("<!", ChanTxFunction)
|
||||
}
|
||||
Reference in New Issue
Block a user