-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
atom.go
58 lines (52 loc) · 1.72 KB
/
atom.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
package predicates
import (
"context"
"strings"
"github.com/ichiban/prolog/engine"
)
// DowncaseAtom (downcase_atom/2) converts atom into its lowercase equivalent.
//
// downcase_atom(+Atom, -LowerCase).
func DowncaseAtom(atom, lowercase engine.Term, k func(*engine.Env) *engine.Promise, env *engine.Env) *engine.Promise {
var a engine.Atom
switch atom := env.Resolve(atom).(type) {
case engine.Variable:
return engine.Error(engine.InstantiationError(env))
case engine.Atom:
a = atom
default:
return engine.Error(engine.TypeError(engine.ValidTypeAtom, atom, env))
}
switch low := env.Resolve(lowercase).(type) {
case engine.Atom, engine.Variable:
transformed := engine.Atom(strings.ToLower(string(a)))
return engine.Delay(func(context.Context) *engine.Promise {
return engine.Unify(low, transformed, k, env)
})
default:
return engine.Error(engine.TypeError(engine.ValidTypeAtom, low, env))
}
}
// UpcaseAtom (upcase_atom/2) converts atom into its uppercase equivalent.
//
// upcase_atom(+Atom, -UpperCase).
func UpcaseAtom(atom, uppercase engine.Term, k func(*engine.Env) *engine.Promise, env *engine.Env) *engine.Promise {
var a engine.Atom
switch atom := env.Resolve(atom).(type) {
case engine.Variable:
return engine.Error(engine.InstantiationError(env))
case engine.Atom:
a = atom
default:
return engine.Error(engine.TypeError(engine.ValidTypeAtom, atom, env))
}
switch low := env.Resolve(uppercase).(type) {
case engine.Atom, engine.Variable:
transformed := engine.Atom(strings.ToUpper(string(a)))
return engine.Delay(func(context.Context) *engine.Promise {
return engine.Unify(low, transformed, k, env)
})
default:
return engine.Error(engine.TypeError(engine.ValidTypeAtom, low, env))
}
}