/
enode.go
61 lines (47 loc) · 1.05 KB
/
enode.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
/*
* Copyright (C) 2017 Lawrence Woodman <lwoodman@vlifesystems.com>
*
* Licensed under an MIT licence. Please see LICENCE.md for details.
*/
package dexpr
import (
"github.com/lawrencewoodman/dlit"
)
type enode interface {
Eval(map[string]*dlit.Literal) *dlit.Literal
}
type enErr struct {
err error
}
type enFunc struct {
fn func(map[string]*dlit.Literal) *dlit.Literal
}
type enLit struct {
val *dlit.Literal
}
type enVar string
func (ee enErr) Err() error {
return ee.err
}
func (ee enErr) Eval(vars map[string]*dlit.Literal) *dlit.Literal {
return dlit.MustNew(ee)
}
func (ef enFunc) Eval(vars map[string]*dlit.Literal) *dlit.Literal {
return ef.fn(vars)
}
func (el enLit) Eval(vars map[string]*dlit.Literal) *dlit.Literal {
return el.val
}
func (el enLit) Int() (int64, bool) {
i, isInt := el.val.Int()
return i, isInt
}
func (el enLit) String() string {
return el.val.String()
}
func (ev enVar) Eval(vars map[string]*dlit.Literal) *dlit.Literal {
if l, ok := vars[string(ev)]; ok {
return l
}
return dlit.MustNew(VarNotExistError(ev))
}