-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch implements the [chapter on functions](http://craftinginterpreters.com/functions.html). Instead of using an interface `LoxCallable` and and anonymous instance for native functions I opted for an enum with the variants `Native` and `User`. Native functions are defined with a function pointer and user functions are implemented the same way `LoxFunction`s are implemented in the book. The biggest difference is that we use `Err` and `Result` to jump back out of a call stack as we don't have `Exception`s.
- Loading branch information
Showing
11 changed files
with
342 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
fun makeCounter() { | ||
var i = 0; | ||
fun count() { | ||
i = i + 1; | ||
print i; | ||
} | ||
|
||
return count; | ||
} | ||
|
||
var counter = makeCounter(); | ||
counter(); // "1". | ||
counter(); // "2". |
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,8 @@ | ||
fun fibonacci(n) { | ||
if (n <= 1) return n; | ||
return fibonacci(n - 2) + fibonacci(n - 1); | ||
} | ||
|
||
for (var i = 0; i < 20; i = i + 1) { | ||
print fibonacci(i); | ||
} |
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,5 @@ | ||
fun sayHi(first, last) { | ||
print "Hi, " + first + " " + last + "!"; | ||
} | ||
|
||
sayHi("Dear", "Reader"); |
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,79 @@ | ||
use crate::env::Environment; | ||
use crate::error::Error; | ||
use crate::interpreter::Interpreter; | ||
use crate::object::Object; | ||
use crate::syntax::Stmt; | ||
use crate::token::Token; | ||
|
||
use std::cell::RefCell; | ||
use std::fmt; | ||
use std::rc::Rc; | ||
|
||
#[derive(Clone)] | ||
pub enum Function { | ||
// An anonymous implementation of LoxCallable in the book. | ||
Native { | ||
arity: usize, | ||
body: Box<fn(&Vec<Object>) -> Object>, | ||
}, | ||
|
||
// A LoxFunction in the book. | ||
User { | ||
name: Token, | ||
params: Vec<Token>, | ||
body: Vec<Stmt>, | ||
closure: Rc<RefCell<Environment>>, | ||
}, | ||
} | ||
|
||
impl Function { | ||
pub fn call( | ||
&self, | ||
interpreter: &mut Interpreter, | ||
arguments: &Vec<Object>, | ||
) -> Result<Object, Error> { | ||
match self { | ||
Function::Native { body, .. } => Ok(body(arguments)), | ||
Function::User { params, body, closure, .. } => { | ||
let mut environment = | ||
Rc::new(RefCell::new(Environment::from(closure))); | ||
for (param, argument) in params.iter().zip(arguments.iter()) { | ||
environment | ||
.borrow_mut() | ||
.define(param.lexeme.clone(), argument.clone()); | ||
} | ||
match interpreter.execute_block(body, environment) { | ||
Err(Error::Return { value }) => Ok(value), | ||
Err(other) => Err(other), | ||
Ok(..) => Ok(Object::Null), // We don't have a return statement. | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn arity(&self) -> usize { | ||
match self { | ||
Function::Native { arity, .. } => *arity, | ||
Function::User { params, .. } => params.len(), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Debug for Function { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Function::Native { .. } => write!(f, "<native func>"), | ||
Function::User { name, .. } => write!(f, "<fn {}>", name.lexeme), | ||
} | ||
} | ||
} | ||
|
||
/// This implements the `to_string` aka `toString` from the book. | ||
impl fmt::Display for Function { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Function::Native { .. } => write!(f, "<native func>"), | ||
Function::User { name, .. } => write!(f, "<fn {}>", 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
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
Oops, something went wrong.