Skip to content

Commit

Permalink
fix: calling function throws exception
Browse files Browse the repository at this point in the history
  • Loading branch information
silverhairs committed Aug 29, 2023
1 parent ccd19de commit 688dea6
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
7 changes: 7 additions & 0 deletions glox/interpreter/callable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package interpreter

type Callable interface {
Call(i *Interpreter, arguments []any) any
Arity() int
String() string
}
5 changes: 1 addition & 4 deletions glox/interpreter/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import (
"fmt"
"glox/ast"
"glox/env"
"glox/object"
"glox/token"
)

type LoxFunction struct {
object.Callable[*Interpreter]
declaration *ast.Function
}

func NewFunction(declaration *ast.Function) *LoxFunction {
return &LoxFunction{declaration: declaration}
}

func (fn *LoxFunction) Call(i *Interpreter, args []token.Token) any {
func (fn *LoxFunction) Call(i *Interpreter, args []any) any {
env := env.New(i.Env)
for i, param := range fn.declaration.Params {
arg := args[i]
Expand Down
3 changes: 1 addition & 2 deletions glox/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"glox/env"
"glox/exception"
"glox/native"
"glox/object"
"glox/token"
"io"
"math"
Expand Down Expand Up @@ -338,7 +337,7 @@ func (i *Interpreter) VisitCall(expr *ast.Call) any {
if err != nil {
return err
}
function, isOk := calle.(object.Callable[*Interpreter])
function, isOk := calle.(Callable)
if !isOk {
return exception.Runtime(expr.Paren, fmt.Sprintf("'%v' cannot be called.", expr.Callee.String()))
} else if function.Arity() != len(args) {
Expand Down
7 changes: 0 additions & 7 deletions glox/object/object.go

This file was deleted.

0 comments on commit 688dea6

Please sign in to comment.