-
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.
- Loading branch information
Showing
10 changed files
with
258 additions
and
30 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 |
---|---|---|
@@ -1,20 +1,7 @@ | ||
|
||
var a = "global a"; | ||
var b = "global b"; | ||
var c = "global c"; | ||
{ | ||
var a = "outer a"; | ||
var b = "outer b"; | ||
{ | ||
var a = "inner a"; | ||
print a; | ||
print b; | ||
print c; | ||
class Bacon { | ||
eat() { | ||
print "Crunch crunch crunch!"; | ||
} | ||
print a; | ||
print b; | ||
print c; | ||
} | ||
print a; | ||
print b; | ||
print c; | ||
|
||
Bacon().eat(); |
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,38 @@ | ||
use crate::errors::*; | ||
use crate::tokens::*; | ||
use std::cell::RefCell; | ||
use std::collections::hash_map::Entry; | ||
use std::collections::HashMap; | ||
use std::rc::Rc; | ||
|
||
#[derive(Debug, PartialEq, Clone)] | ||
pub struct InstanceStruct { | ||
pub class: Rc<ClassStruct>, | ||
fields: RefCell<HashMap<String, Object>>, | ||
} | ||
|
||
impl InstanceStruct { | ||
pub fn new(class: Rc<ClassStruct>) -> Self { | ||
InstanceStruct { | ||
class: Rc::clone(&class), | ||
fields: RefCell::new(HashMap::new()), | ||
} | ||
} | ||
|
||
pub fn get(&self, name: &Token) -> Result<Object, Error> { | ||
if let Entry::Occupied(entry) = self.fields.borrow_mut().entry(name.lexeme.clone()) { | ||
Ok(entry.get().clone()) | ||
} else if let Some(method) = self.class.find_method(name.lexeme.clone()) { | ||
Ok(method) | ||
} else { | ||
Err(Error::runtime_error( | ||
name, | ||
&format!("Undefined property '{}'.", name.lexeme), | ||
)) | ||
} | ||
} | ||
|
||
pub fn set(&self, name: &Token, value: Object) { | ||
self.fields.borrow_mut().insert(name.lexeme.clone(), value); | ||
} | ||
} |
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.