-
Notifications
You must be signed in to change notification settings - Fork 2
/
tlps_function.go
60 lines (51 loc) · 1.48 KB
/
tlps_function.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
package tlps
// TLPSFunction is struct of tlps function
type TLPSFunction struct {
declaration *Function
closure *Environment
IsInitializer bool
}
// NewTLPSFunction is constructor of TLPSFunction
func NewTLPSFunction(declaration *Function, closure *Environment, IsInitializer bool) *TLPSFunction {
return &TLPSFunction{
declaration: declaration,
closure: closure,
IsInitializer: IsInitializer,
}
}
// Call calls the function
func (lf *TLPSFunction) Call(interpreter *Interpreter, arguments []interface{}) (interface{}, error) {
environment := NewEnvironment(lf.closure)
for i, param := range lf.declaration.Params {
environment.Define(param.Lexeme, arguments[i])
}
_, err := interpreter.executeBlock(lf.declaration.Body, environment)
if err != nil {
var v interface{} = err
switch v.(type) {
case *ReturnValue:
if lf.IsInitializer {
return lf.closure.GetAt(0, "this")
}
return v.(*ReturnValue).Value, nil
default:
return nil, err
}
}
if lf.IsInitializer {
return lf.closure.GetAt(0, "this")
}
return nil, nil
}
// Arity returns arity of function
func (lf *TLPSFunction) Arity() int {
return len(lf.declaration.Params)
}
func (lc *TLPSFunction) Bind(instance *TLPSInstance) *TLPSFunction {
environment := NewEnvironment(lc.closure)
environment.Define("this", instance)
return NewTLPSFunction(lc.declaration, environment, lc.IsInitializer)
}
func (lf *TLPSFunction) String() string {
return "<fn " + lf.declaration.Name.Lexeme + ">"
}