-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from hutcho66/classes
Classes
- Loading branch information
Showing
15 changed files
with
756 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,6 @@ | ||
package interpreter | ||
|
||
import ( | ||
"github.com/hutcho66/glox/src/pkg/ast" | ||
) | ||
|
||
type LoxCallable interface { | ||
Arity() int | ||
Call(interpreter *Interpreter, arguments []any) (any, error) | ||
} | ||
|
||
type LoxFunction struct { | ||
declaration *ast.FunctionStatement | ||
closure *Environment | ||
} | ||
|
||
func (f *LoxFunction) Call(interpreter *Interpreter, arguments []any) (returnValue any, err error) { | ||
enclosingEnvironment := interpreter.environment | ||
environment := NewEnclosingEnvironment(f.closure) | ||
|
||
defer func() { | ||
if val := recover(); val != nil { | ||
rv, ok := val.(*LoxControl) | ||
if !ok || rv.controlType != RETURN { | ||
// repanic | ||
panic(val) | ||
} | ||
|
||
returnValue = rv.value | ||
interpreter.environment = enclosingEnvironment | ||
return | ||
} | ||
}() | ||
|
||
for i, param := range f.declaration.Params { | ||
environment.define(param.Lexeme, arguments[i]) | ||
} | ||
|
||
interpreter.executeBlock(f.declaration.Body, environment) | ||
|
||
// if we've reached here, there was no return statement, so implicitly return nil | ||
return nil, nil | ||
} | ||
|
||
func (f LoxFunction) Arity() int { | ||
return len(f.declaration.Params) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package interpreter | ||
|
||
import ( | ||
"github.com/hutcho66/glox/src/pkg/ast" | ||
"github.com/hutcho66/glox/src/pkg/lox_error" | ||
"github.com/hutcho66/glox/src/pkg/token" | ||
) | ||
|
||
type LoxClass struct { | ||
Name string | ||
Methods map[string]*LoxFunction | ||
Super *LoxClass | ||
} | ||
|
||
func (c LoxClass) Arity() int { | ||
initializer := c.findMethod("init") | ||
if initializer == nil { | ||
return 0 | ||
} | ||
|
||
return initializer.Arity() | ||
} | ||
|
||
func (c *LoxClass) Call(interpreter *Interpreter, arguments []any) (any, error) { | ||
instance := NewLoxInstance(c) | ||
|
||
initializer := c.findMethod("init") | ||
if initializer != nil { | ||
initializer.bind(instance).Call(interpreter, arguments) | ||
} | ||
|
||
return instance, nil | ||
} | ||
|
||
func (c *LoxClass) findMethod(name string) *LoxFunction { | ||
if method, ok := c.Methods[name]; ok { | ||
return method | ||
} | ||
|
||
if c.Super != nil { | ||
return c.Super.findMethod(name) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *LoxClass) get(name *token.Token) any { | ||
|
||
method := c.findMethod(name.Lexeme) | ||
|
||
if method == nil { | ||
panic(lox_error.RuntimeError(name, "Undefined property '"+name.Lexeme+"'.")) | ||
} | ||
|
||
if method.declaration.Kind != ast.STATIC_METHOD { | ||
panic(lox_error.RuntimeError(name, "Cannot call non-static method '"+name.Lexeme+"' directly on class.")) | ||
} | ||
|
||
return method.bind(c) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package interpreter | ||
|
||
import "github.com/hutcho66/glox/src/pkg/ast" | ||
|
||
type LoxFunction struct { | ||
declaration *ast.FunctionStatement | ||
closure *Environment | ||
isInitializer bool | ||
} | ||
|
||
func (f *LoxFunction) Call(interpreter *Interpreter, arguments []any) (returnValue any, err error) { | ||
enclosingEnvironment := interpreter.environment | ||
environment := NewEnclosingEnvironment(f.closure) | ||
|
||
defer func() { | ||
if val := recover(); val != nil { | ||
rv, ok := val.(*LoxControl) | ||
if !ok || rv.controlType != RETURN { | ||
// repanic | ||
panic(val) | ||
} | ||
|
||
if f.isInitializer { | ||
returnValue = f.closure.getAt(0, "this") | ||
} else { | ||
returnValue = rv.value | ||
} | ||
|
||
interpreter.environment = enclosingEnvironment | ||
return | ||
} | ||
}() | ||
|
||
for i, param := range f.declaration.Params { | ||
environment.define(param.Lexeme, arguments[i]) | ||
} | ||
|
||
interpreter.executeBlock(f.declaration.Body, environment) | ||
|
||
if f.isInitializer { | ||
return f.closure.getAt(0, "this"), nil | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func (f LoxFunction) Arity() int { | ||
return len(f.declaration.Params) | ||
} | ||
|
||
func (f *LoxFunction) bind(instance LoxObject) *LoxFunction { | ||
environment := NewEnclosingEnvironment(f.closure) | ||
environment.define("this", instance) | ||
return &LoxFunction{f.declaration, environment, f.isInitializer} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package interpreter | ||
|
||
import ( | ||
"github.com/hutcho66/glox/src/pkg/lox_error" | ||
"github.com/hutcho66/glox/src/pkg/token" | ||
) | ||
|
||
type LoxInstance struct { | ||
Class *LoxClass | ||
Fields map[string]any | ||
} | ||
|
||
func NewLoxInstance(class *LoxClass) *LoxInstance { | ||
return &LoxInstance{ | ||
Class: class, | ||
Fields: make(map[string]any), | ||
} | ||
} | ||
|
||
func (i *LoxInstance) get(name *token.Token) any { | ||
if field, ok := i.Fields[name.Lexeme]; ok { | ||
return field | ||
} | ||
|
||
method := i.Class.findMethod(name.Lexeme) | ||
if method != nil { | ||
return method.bind(i) | ||
} | ||
|
||
panic(lox_error.RuntimeError(name, "Undefined property '"+name.Lexeme+"'.")) | ||
} | ||
|
||
func (i *LoxInstance) set(name *token.Token, value any) { | ||
i.Fields[name.Lexeme] = value | ||
} |
Oops, something went wrong.