glox/callable.go

47 lines
737 B
Go
Raw Permalink Normal View History

2024-10-09 19:57:52 +03:00
package main
2024-10-12 00:09:25 +03:00
type Callable interface {
arity() int
call(i *Interpreter, args ...any) (ret any)
2024-10-09 19:57:52 +03:00
}
2024-10-09 23:36:18 +03:00
2024-10-12 00:09:25 +03:00
type Function struct {
2024-10-12 14:48:27 +03:00
name Token
args []Token
2024-10-14 22:53:26 +03:00
body []Stmt
2024-10-12 14:48:27 +03:00
closure *Environment
2024-10-12 00:09:25 +03:00
}
2024-10-11 17:01:12 +03:00
2024-10-12 00:09:25 +03:00
func (f *Function) call(i *Interpreter, args ...any) (ret any) {
2024-10-11 17:01:12 +03:00
2024-10-12 00:09:25 +03:00
defer func() {
if err := recover(); err != nil {
re, ok := err.(Return)
2024-10-09 23:36:18 +03:00
2024-10-12 00:09:25 +03:00
if !ok {
panic(err)
2024-10-09 23:36:18 +03:00
}
2024-10-12 00:09:25 +03:00
ret = re.val
}
}()
2024-10-09 23:36:18 +03:00
2024-10-12 00:09:25 +03:00
env := newEnvironment(f.closure)
2024-10-12 14:48:27 +03:00
for idx, arg := range f.args {
2024-10-12 00:09:25 +03:00
env.define(arg.lexeme, args[idx])
2024-10-09 23:36:18 +03:00
}
2024-10-12 00:09:25 +03:00
2024-10-12 14:48:27 +03:00
i.executeBlock(f.body, env)
2024-10-12 00:09:25 +03:00
return nil
}
func (f *Function) arity() int {
2024-10-12 14:48:27 +03:00
return len(f.args)
2024-10-12 00:09:25 +03:00
}
2024-10-14 22:53:26 +03:00
func newFunction(name Token, args []Token, body []Stmt, env *Environment) Callable {
2024-10-12 14:48:27 +03:00
return &Function{name, args, body, env}
2024-10-09 23:36:18 +03:00
}