-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
math: Add {int,float}_div and int_mod ops
Old int_mod was renamed CaseMod (int_casemod) to reflect its usage. Examples using it were updated.
- Loading branch information
Showing
7 changed files
with
249 additions
and
45 deletions.
There are no files selected for viewing
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
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,64 @@ | ||
package funcs | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/nevalang/neva/internal/runtime" | ||
) | ||
|
||
type floatDiv struct{} | ||
|
||
func (floatDiv) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) { | ||
xIn, err := io.In.Port("x") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
yIn, err := io.In.Port("y") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resOut, err := io.Out.Port("res") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
errOut, err := io.Out.Port("err") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return func(ctx context.Context) { | ||
for { | ||
var x, y float64 | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case msg := <-xIn: | ||
x = msg.Float() | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case msg := <-yIn: | ||
y = msg.Float() | ||
} | ||
if y == 0 { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case errOut <- runtime.NewMapMsg(map[string]runtime.Msg{ | ||
"text": runtime.NewStrMsg(errIntegerDivideByZero.Error()), | ||
}): | ||
continue | ||
} | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case resOut <- runtime.NewFloatMsg(x / y): | ||
} | ||
} | ||
}, nil | ||
} |
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,82 @@ | ||
package funcs | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/nevalang/neva/internal/runtime" | ||
) | ||
|
||
type intCaseMod struct{} | ||
|
||
func (intCaseMod) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) { | ||
dataIn, err := io.In.Port("data") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
caseIn, ok := io.In["case"] | ||
if !ok { | ||
return nil, errors.New("port 'case' is required") | ||
} | ||
|
||
caseOut, ok := io.Out["case"] | ||
if !ok { | ||
return nil, errors.New("port 'then' is required") | ||
} | ||
|
||
elseOut, err := io.Out.Port("else") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(caseIn) != len(caseOut) { | ||
return nil, errors.New("number of 'case' inports must match number of 'then' outports") | ||
} | ||
|
||
return func(ctx context.Context) { | ||
var data runtime.Msg | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case data = <-dataIn: | ||
} | ||
|
||
cases := make([]runtime.Msg, len(caseIn)) | ||
for i, slot := range caseIn { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case caseMsg := <-slot: | ||
cases[i] = caseMsg | ||
} | ||
} | ||
|
||
matchIdx := -1 | ||
dataInt := data.Int() | ||
for i, caseMsg := range cases { | ||
if dataInt%caseMsg.Int() == 0 { | ||
matchIdx = i | ||
break | ||
} | ||
} | ||
|
||
if matchIdx != -1 { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case caseOut[matchIdx] <- data: | ||
continue | ||
} | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return | ||
case elseOut <- data: | ||
} | ||
} | ||
}, nil | ||
} |
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,64 @@ | ||
package funcs | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/nevalang/neva/internal/runtime" | ||
) | ||
|
||
type intDiv struct{} | ||
|
||
func (intDiv) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) { | ||
xIn, err := io.In.Port("x") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
yIn, err := io.In.Port("y") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resOut, err := io.Out.Port("res") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
errOut, err := io.Out.Port("err") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return func(ctx context.Context) { | ||
for { | ||
var x, y int64 | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case msg := <-xIn: | ||
x = msg.Int() | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case msg := <-yIn: | ||
y = msg.Int() | ||
} | ||
if y == 0 { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case errOut <- runtime.NewMapMsg(map[string]runtime.Msg{ | ||
"text": runtime.NewStrMsg(errIntegerDivideByZero.Error()), | ||
}): | ||
continue | ||
} | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case resOut <- runtime.NewIntMsg(x / y): | ||
} | ||
} | ||
}, nil | ||
} |
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
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
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