generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 120
/
option.go
85 lines (74 loc) · 2.15 KB
/
option.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
package prolog
import (
"github.com/ichiban/prolog/engine"
)
// GetOption returns the value of the first option with the given name in the given options.
// An option is a compound with the given name as functor and one argument which is
// a term, for instance `opt(v)`.
// The options are either a list of options or an option.
// If no option is found nil is returned.
func GetOption(name engine.Atom, options engine.Term, env *engine.Env) (engine.Term, error) {
extractOption := func(opt engine.Term) (engine.Term, error) {
switch v := env.Resolve(opt).(type) {
case engine.Compound:
if v.Functor() == name {
if v.Arity() != 1 {
return nil, engine.TypeError(AtomTypeOption, opt, env)
}
return v.Arg(0), nil
}
return nil, nil
case engine.Atom:
if v == AtomEmptyList {
return nil, nil
}
case nil:
return nil, nil
}
return nil, engine.TypeError(AtomTypeOption, opt, env)
}
if IsList(options, env) {
iter, err := ListIterator(options, env)
if err != nil {
return nil, err
}
for iter.Next() {
opt := iter.Current()
term, err := extractOption(opt)
if err != nil {
return nil, err
}
if term != nil {
return term, nil
}
}
}
return extractOption(options)
}
// GetOptionWithDefault returns the value of the first option with the given name in the given options, or the given
// default value if no option is found.
func GetOptionWithDefault(
name engine.Atom, options engine.Term, defaultValue engine.Term, env *engine.Env,
) (engine.Term, error) {
if term, err := GetOption(name, options, env); err != nil {
return nil, err
} else if term != nil {
return term, nil
}
return defaultValue, nil
}
// GetOptionAsAtomWithDefault is a helper function that returns the value of the first option with the given name in the
// given options.
func GetOptionAsAtomWithDefault(
name engine.Atom, options engine.Term, defaultValue engine.Term, env *engine.Env,
) (engine.Atom, error) {
term, err := GetOptionWithDefault(name, options, defaultValue, env)
if err != nil {
return AtomEmpty, err
}
atom, err := AssertAtom(term, env)
if err != nil {
return AtomEmpty, err
}
return atom, nil
}