-
Notifications
You must be signed in to change notification settings - Fork 15
/
expr_ident.go
62 lines (51 loc) · 1.43 KB
/
expr_ident.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
package rel
import (
"fmt"
"github.com/arr-ai/wbnf/parser"
)
// IdentLookupFailed represents failure to lookup a scope variable.
type IdentLookupFailed struct {
scope Scope
expr IdentExpr
}
func (e IdentLookupFailed) Error() string {
return fmt.Sprintf("Name %q not found in %v \n%s",
e.expr.ident,
e.scope.m.Keys(),
e.expr.Source().Context(parser.DefaultLimit))
}
// IdentExpr returns the variable referenced by ident.
type IdentExpr struct {
ExprScanner
ident string
}
// DotIdent represents the special identifier '.'.
var DotIdent = IdentExpr{ExprScanner: ExprScanner{Src: *parser.NewScanner(".")}, ident: "."}
// NewIdentExpr returns a new identifier.
func NewIdentExpr(scanner parser.Scanner, ident string) IdentExpr {
if ident == "." {
return DotIdent
}
return IdentExpr{ExprScanner{scanner}, ident}
}
// Ident returns the ident for the IdentExpr.
func (e IdentExpr) Ident() string {
return e.ident
}
// String returns a string representation of the expression.
func (e IdentExpr) String() string {
if e.ident == "." {
return "(" + e.ident + ")"
}
return e.ident
}
// Eval returns the value from scope corresponding to the ident.
func (e IdentExpr) Eval(local Scope) (Value, error) {
if a, found := local.Get(e.ident); found && a != nil {
return a.Eval(local)
}
return nil, IdentLookupFailed{local, e}
}
func (e IdentExpr) Bind(scope Scope, value Value) (Scope, error) {
return EmptyScope.With(e.ident, value), nil
}