-
Notifications
You must be signed in to change notification settings - Fork 15
/
expr_ident.go
49 lines (40 loc) · 1.18 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
package rel
import (
"fmt"
"github.com/arr-ai/wbnf/parser"
)
// 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, wrapContext(fmt.Errorf("name %q not found in %v", e.ident, local.m.Keys()), e, local)
}
func (e IdentExpr) Bind(scope Scope, value Value) (Scope, error) {
return EmptyScope.With(e.ident, value), nil
}