-
Notifications
You must be signed in to change notification settings - Fork 22
/
when.go
63 lines (52 loc) · 1.74 KB
/
when.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
package macros
import (
. "github.com/kocircuit/kocircuit/lang/circuit/eval"
. "github.com/kocircuit/kocircuit/lang/circuit/model"
. "github.com/kocircuit/kocircuit/lang/go/eval"
. "github.com/kocircuit/kocircuit/lang/go/eval/symbol"
. "github.com/kocircuit/kocircuit/lang/go/kit/util"
)
func init() {
RegisterEvalMacro("When", new(EvalWhenMacro))
}
type EvalWhenMacro struct{}
func (m EvalWhenMacro) MacroID() string { return m.Help() }
func (m EvalWhenMacro) Label() string { return "when" }
func (m EvalWhenMacro) MacroSheathString() *string { return PtrString("When") }
func (m EvalWhenMacro) Help() string { return "When" }
func (m EvalWhenMacro) Doc() string {
return `
When expects three arguments: have, then and else.
* When have is not empty, When returns the result of invoking (the variety) then,
passing to its default argument the non-empty value of have.
* When have is empty, When returns the result of invoking (the variety) else.`
}
// When(have:█, then:█, else:█)
func (EvalWhenMacro) Invoke(span *Span, arg Arg) (returns Return, effect Effect, err error) {
a := arg.(*StructSymbol)
have := ExtractMonadicOrNamed(a, "have")
if IsEmptySymbol(have) {
els := a.Walk("else")
if IsEmptySymbol(els) {
return EmptySymbol{}, nil, nil
} else {
elsVty, ok := els.(*VarietySymbol)
if !ok {
return nil, nil, span.Errorf(nil, "when else is not a variety")
}
return elsVty.Invoke(span)
}
} else {
then := a.Walk("then")
if IsEmptySymbol(then) {
return EmptySymbol{}, nil, nil
} else {
thenVty, ok := then.(*VarietySymbol)
if !ok {
return nil, nil, span.Errorf(nil, "when then is not a variety")
}
thenFields := Fields{{Name: "", Shape: have}}
return thenVty.Evoke(span, thenFields)
}
}
}