This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
std.go
102 lines (88 loc) · 2.35 KB
/
std.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
// Package std provides built-in expression resolvers.
package std
import (
"github.com/mb0/xelf/exp"
"github.com/mb0/xelf/typ"
)
// Std is an environment that includes both the Core and Decl lookup function.
var Std = exp.Builtin{Core, Decl}
var core = make(formMap, 32)
var decl = make(formMap, 8)
// Core is a resolver lookup for all standard forms, not involving declarations or functions.
//
// Logic forms:
// and, or, bool, not, if
// Arithmetic forms:
// add, mul, sub, div, rem, abs, neg, min, max
// Comparison forms:
// eq, ne, equal, in, ni, lt, le, gt, ge
// Other forms:
// len, with, dyn, con, cat, apd, set
func Core(sym string) *exp.Spec {
if f, ok := core[sym]; ok {
return f
}
return nil
}
// Decl is a resolver lookup for the declaration and container forms.
//
// Declaration forms:
// let and fn
// Container forms:
// fst, lst, nth, filter, map, fold, foldr
func Decl(sym string) *exp.Spec {
if f, ok := decl[sym]; ok {
return f
}
return nil
}
type formMap map[string]*exp.Spec
func (m formMap) add(s *exp.Spec) *exp.Spec {
m[s.Ref] = s
return s
}
type CallCtx struct {
*exp.Prog
Env exp.Env
*exp.Call
Hint typ.Type
}
type Evaler func(CallCtx) (exp.El, error)
func DefaultResl(x CallCtx) (exp.El, error) {
err := x.Layout.Resl(x.Prog, x.Env, x.Hint)
return x.Call, err
}
type SpecImpl struct {
resl Evaler
eval Evaler
part bool
}
func (r *SpecImpl) Resl(p *exp.Prog, env exp.Env, c *exp.Call, h typ.Type) (exp.El, error) {
req := CallCtx{p, env, c, h}
if r.resl == nil {
return r.eval(req)
}
return r.resl(req)
}
func (r *SpecImpl) Eval(p *exp.Prog, env exp.Env, c *exp.Call, h typ.Type) (exp.El, error) {
req := CallCtx{p, env, c, h}
if r.resl != nil {
v, err := r.resl(req)
if err != nil {
if r.part && err == exp.ErrUnres {
req.Call = v.(*exp.Call)
return r.eval(req)
}
return v, err
}
}
return r.eval(req)
}
func SpecXX(sig string, x Evaler) *exp.Spec { return newSpec(sig, nil, x, false) }
func SpecRX(sig string, r, x Evaler) *exp.Spec { return newSpec(sig, r, x, false) }
func SpecDX(sig string, x Evaler) *exp.Spec { return newSpec(sig, DefaultResl, x, false) }
func SpecDXX(sig string, x Evaler) *exp.Spec { return newSpec(sig, DefaultResl, x, true) }
func newSpec(sig string, r, x Evaler, part bool) *exp.Spec {
s := exp.MustSig(sig)
return &exp.Spec{s, &SpecImpl{r, x, part}}
}