-
Notifications
You must be signed in to change notification settings - Fork 15
/
expr_arrow.go
40 lines (34 loc) · 918 Bytes
/
expr_arrow.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
package rel
import (
"fmt"
"github.com/arr-ai/wbnf/parser"
)
// ArrowExpr returns the tuple applied to a function.
type ArrowExpr struct {
ExprScanner
lhs Expr
fn *Function
}
// NewArrowExpr returns a new ArrowExpr.
func NewArrowExpr(scanner parser.Scanner, lhs, fn Expr) Expr {
return &ArrowExpr{ExprScanner{scanner}, lhs, ExprAsFunction(fn)}
}
// String returns a string representation of the expression.
func (e *ArrowExpr) String() string {
if e.fn.Arg() == "." {
return fmt.Sprintf("(%s -> %s)", e.lhs, e.fn.Body())
}
return fmt.Sprintf("(%s -> %s)", e.lhs, e.fn)
}
// Eval returns the lhs
func (e *ArrowExpr) Eval(local Scope) (_ Value, err error) {
value, err := e.lhs.Eval(local)
if err != nil {
return nil, wrapContext(err, e, local)
}
scope, err := e.fn.arg.Bind(local, value)
if err != nil {
return nil, wrapContext(err, e, local)
}
return e.fn.body.Eval(local.Update(scope))
}