/
either_val_props.go
141 lines (120 loc) · 3.72 KB
/
either_val_props.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package props
import (
"fmt"
"github.com/Syuparn/pangaea/object"
)
// EitherValProps provides built-in props for EitherVal.
// NOTE: Some Val props are defind by native code (not by this function).
func EitherValProps(propContainer map[string]object.PanObject) map[string]object.PanObject {
// NOTE: inject some built-in functions which relate to parser or evaluator
return map[string]object.PanObject{
"_name": object.NewPanStr("EitherVal"),
"A": f(
func(
env *object.Env, kwargs *object.PanObj, args ...object.PanObject,
) object.PanObject {
if len(args) < 1 {
return object.NewTypeErr("EitherVal#A requires at least 1 arg")
}
valObj, ok := object.TraceProtoOfObj(args[0])
if !ok {
return object.NewTypeErr(
fmt.Sprintf("%s cannot be treated as EitherVal", args[0].Repr()))
}
val, ok := (*valObj.Pairs)[object.GetSymHash("_value")]
if !ok {
return object.NewTypeErr(
fmt.Sprintf("%s cannot be treated as EitherVal", args[0].Repr()))
}
return object.NewPanArr(val.Value, object.BuiltInNil)
},
),
"err": f(
func(
env *object.Env, kwargs *object.PanObj, args ...object.PanObject,
) object.PanObject {
if len(args) < 1 {
return object.NewTypeErr("EitherVal#err requires at least 1 arg")
}
return object.BuiltInNil
},
),
"fmap": f(
func(
env *object.Env, kwargs *object.PanObj, args ...object.PanObject,
) object.PanObject {
if len(args) < 2 {
return object.NewTypeErr("EitherVal#fmap requires at least 2 args")
}
valObj, ok := object.TraceProtoOfObj(args[0])
if !ok {
return object.NewTypeErr(
fmt.Sprintf("%s cannot be treated as EitherVal", args[0].Repr()))
}
val, ok := (*valObj.Pairs)[object.GetSymHash("_value")]
if !ok {
return object.NewTypeErr(
fmt.Sprintf("%s cannot be treated as EitherVal", args[0].Repr()))
}
f := args[1]
// eval `f(val)` (== `f.call(val)`)
callSym := object.NewPanStr("call")
result := propContainer["Obj_callProp"].(*object.PanBuiltIn).Fn(
env, object.EmptyPanObjPtr(),
object.EmptyPanObjPtr(), f, callSym, val.Value,
)
if err, ok := result.(*object.PanErr); ok {
return toEitherErr(object.WrapErr(err))
}
return toEitherVal(result)
},
),
"or": f(
func(
env *object.Env, kwargs *object.PanObj, args ...object.PanObject,
) object.PanObject {
if len(args) < 2 {
return object.NewTypeErr("EitherVal#or requires at least 2 args")
}
valObj, ok := object.TraceProtoOfObj(args[0])
if !ok {
return object.NewTypeErr(
fmt.Sprintf("%s cannot be treated as EitherVal", args[0].Repr()))
}
val, ok := (*valObj.Pairs)[object.GetSymHash("_value")]
if !ok {
return object.NewTypeErr(
fmt.Sprintf("%s cannot be treated as EitherVal", args[0].Repr()))
}
return val.Value
},
),
"val": f(
func(
env *object.Env, kwargs *object.PanObj, args ...object.PanObject,
) object.PanObject {
if len(args) < 1 {
return object.NewTypeErr("EitherVal#val requires at least 1 arg")
}
valObj, ok := object.TraceProtoOfObj(args[0])
if !ok {
return object.NewTypeErr(
fmt.Sprintf("%s cannot be treated as EitherVal", args[0].Repr()))
}
val, ok := (*valObj.Pairs)[object.GetSymHash("_value")]
if !ok {
return object.NewTypeErr(
fmt.Sprintf("%s cannot be treated as EitherVal", args[0].Repr()))
}
return val.Value
},
),
}
}
func toEitherErr(e *object.PanErrWrapper) object.PanObject {
// wrap e by EitherErr
pairMap := map[object.SymHash]object.Pair{
object.GetSymHash("_error"): {Key: object.NewPanStr("_error"), Value: e},
}
return object.NewPanObj(&pairMap, object.BuiltInEitherErrObj)
}