-
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.
Implement methods on class instances
- Loading branch information
Showing
13 changed files
with
217 additions
and
71 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,58 @@ | ||
use std::{cell::RefCell, fmt::Display, rc::Rc}; | ||
|
||
use crate::ast::{name::Name, stmt::Stmt}; | ||
|
||
use super::{ | ||
environment::Environment, runtime_error::RuntimeErrorOrReturn, value::Value, Interpreter, | ||
Result, | ||
}; | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct Function { | ||
name: Name, | ||
parameters: Vec<Name>, | ||
body: Vec<Stmt>, | ||
closure: Rc<RefCell<Environment>>, | ||
} | ||
|
||
impl Function { | ||
pub fn new( | ||
name: Name, | ||
parameters: Vec<Name>, | ||
body: Vec<Stmt>, | ||
closure: Rc<RefCell<Environment>>, | ||
) -> Self { | ||
Self { | ||
name, | ||
parameters, | ||
body, | ||
closure, | ||
} | ||
} | ||
|
||
pub fn call(&self, interpreter: &mut Interpreter, arguments: Vec<Value>) -> Result<Value> { | ||
let mut env = Environment::from_parent(self.closure.clone()); | ||
self.parameters | ||
.iter() | ||
.zip(arguments.iter()) | ||
.for_each(|(p, a)| env.define(p, a.clone())); | ||
let result = interpreter.execute_block(&self.body, env); | ||
match result { | ||
Ok(_) => Ok(Value::Nil), | ||
Err(RuntimeErrorOrReturn::Return(value)) => Ok(value), | ||
Err(RuntimeErrorOrReturn::RuntimeError(err)) => Err(err), | ||
} | ||
} | ||
|
||
pub fn arity(&self) -> usize { | ||
self.parameters.len() | ||
} | ||
} | ||
|
||
impl Display for Function { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let arity = self.parameters.len(); | ||
let name = &self.name; | ||
write!(f, "<fun {name} ({arity} parameters)>") | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -13,4 +13,4 @@ print leo2.name; | |
<class Dog> | ||
<Dog instance> | ||
Leo | ||
Leo2 | ||
Leo2 |
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,10 @@ | ||
interpret | ||
class Bacon { | ||
eat() { | ||
print "Crunch crunch crunch!"; | ||
} | ||
} | ||
|
||
Bacon().eat(); | ||
---- | ||
Crunch crunch crunch! |
Oops, something went wrong.