-
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.
Showing
8 changed files
with
198 additions
and
15 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,52 @@ | ||
use crate::callable::*; | ||
use crate::enviroment::Environment; | ||
use crate::errors::*; | ||
use crate::stmt::*; | ||
use crate::tokens::*; | ||
use std::rc::Rc; | ||
|
||
use crate::interpreter::Interpreter; | ||
pub struct Function { | ||
declaration: Rc<Stmt>, | ||
} | ||
|
||
impl Function { | ||
pub fn new(declaration: &Rc<Stmt>) -> Self { | ||
Self { | ||
declaration: Rc::clone(declaration), | ||
} | ||
} | ||
} | ||
|
||
impl CallableTrait for Function { | ||
fn call(&self, interpreter: &Interpreter, arguments: &Vec<Object>) -> Result<Object, Error> { | ||
if let Stmt::Function(FunctionStmt { name, params, body }) = &*self.declaration { | ||
let mut e = Environment::new_with_enclosing(Rc::clone(&interpreter.globals)); | ||
for (param, arg) in params.iter().zip(arguments.iter()) { | ||
e.define(param.lexeme.clone(), arg.clone()); | ||
} | ||
|
||
interpreter.execute_block(body, e)?; | ||
Ok(Object::Nil) | ||
}else{ | ||
Err(Error::new(0, "Function declaration is not a function".to_string())) | ||
} | ||
|
||
} | ||
|
||
fn arity(&self) -> usize { | ||
if let Stmt::Function(FunctionStmt { name, params, body }) = &*self.declaration { | ||
params.len() | ||
}else{ | ||
panic!("Function declaration is not a function") | ||
} | ||
} | ||
|
||
fn stringify(&self) -> String { | ||
if let Stmt::Function(FunctionStmt { name, params, body }) = &*self.declaration { | ||
format!("<fn {}>", name.lexeme) | ||
}else{ | ||
panic!("Function declaration is not a function") | ||
} | ||
} | ||
} |
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,25 @@ | ||
use crate::callable::*; | ||
use crate::errors::*; | ||
use crate::interpreter::*; | ||
use crate::tokens::*; | ||
|
||
use std::time::SystemTime; | ||
|
||
pub struct NativeClock {} | ||
|
||
impl CallableTrait for NativeClock { | ||
fn call(&self, _terp: &Interpreter, _args: &Vec<Object>) -> Result<Object, Error> { | ||
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) { | ||
Ok(n) => Ok(Object::Num(n.as_millis() as f64)), | ||
Err(_) => Err(Error::new(0, "Could not get time".to_string())), | ||
} | ||
} | ||
|
||
fn arity(&self) -> usize { | ||
0 | ||
} | ||
|
||
fn stringify(&self) -> String { | ||
"Native::Clock".to_string() | ||
} | ||
} |
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