-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ee64ea
commit 22648e4
Showing
7 changed files
with
266 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package glox | ||
|
||
// LoxCallable interface should be implemented by any lox object that can be called like | ||
// a function. | ||
type LoxCallable interface { | ||
// Call is the method that is called to evaluate the function. We pass in the | ||
// interpreter in case the implementing object needs it and the list of arguments. | ||
// The implementing object should return the evaluated value as return parameter. | ||
Call(interpreter *Interpreter, arguments []interface{}) (interface{}, error) | ||
|
||
// Arity is the number of arguments a function expects. It's used to check if the | ||
// number of arguments passed to the function matches the number of arguments the | ||
// function expects. | ||
Arity() int | ||
} |
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,36 @@ | ||
package glox | ||
|
||
// LoxFunction is the representation of the lox function in terms of the interpreter. | ||
// This struct also implements the LoxCallable interface so the runtime can call this | ||
// function. | ||
type LoxFunction struct { | ||
declaration *FunctionStmt | ||
} | ||
|
||
func NewLoxFunction(declaration *FunctionStmt) LoxCallable { | ||
return LoxFunction{declaration: declaration} | ||
} | ||
|
||
// Call will execute the function body with the arguments passed to it. The parameters are | ||
// core to a function, a function encapsulates its parameters. No other code outside the | ||
// function should see them. This means each function gets its own environment. And this | ||
// environment is generated at runtime during the function call. Then it walks the parameters | ||
// and argument lists and for each pair it creates a new variable with the parameter's name | ||
// and binds it to the argument's value. | ||
func (lf LoxFunction) Call(interpreter *Interpreter, arguments []interface{}) (interface{}, error) { | ||
env := NewEnvironment(interpreter.globals) | ||
for i, param := range lf.declaration.Params { | ||
env.Define(param.Lexeme, arguments[i]) | ||
} | ||
|
||
return nil, interpreter.executeBlock(lf.declaration.Body, env) | ||
} | ||
|
||
|
||
func (lf LoxFunction) Arity() int { | ||
return len(lf.declaration.Params) | ||
} | ||
|
||
func (lf LoxFunction) String() string { | ||
return "<fn " + lf.declaration.Name.Lexeme + ">" | ||
} |
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,17 @@ | ||
package glox | ||
|
||
import "time" | ||
|
||
type Clock struct{} | ||
|
||
func (c Clock) Call(interpreter *Interpreter, arguments []interface{}) (interface{}, error) { | ||
return float64(time.Now().Unix()), nil | ||
} | ||
|
||
func (c Clock) Arity() int { | ||
return 0 | ||
} | ||
|
||
func (c Clock) String() string { | ||
return "<native fn>" | ||
} |
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