-
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
20 changed files
with
263 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,42 @@ | ||
use crate::{function::Function, value::value::Val}; | ||
use std::{collections::HashMap, rc::Rc}; | ||
use crate::value::value::{Val}; | ||
|
||
#[derive(Default)] | ||
pub struct Context { | ||
par: Option<Rc<Context>>, | ||
vars: HashMap<&'static str, Val> | ||
variables: HashMap<&'static str, Val>, | ||
funtions: HashMap<&'static str, Function>, | ||
} | ||
|
||
impl Default for Context { | ||
fn default() -> Self { | ||
Self { | ||
par: Default::default(), | ||
variables: Default::default(), | ||
funtions: HashMap::from([ | ||
("dyn", crate::std::new_dyn()) | ||
]), | ||
} | ||
} | ||
} | ||
|
||
impl Context { | ||
pub fn add_variable(mut self, name: &'static str, val: Val) -> Self { | ||
self.vars.insert(name, val); | ||
pub fn add_variable(&mut self, name: &'static str, val: Val) -> &mut Self { | ||
self.variables.insert(name, val); | ||
self | ||
} | ||
pub fn resolve(&self, name: &String) -> Option<&Val> { | ||
self.vars.get(name.as_str()) | ||
pub fn resolve_variable(&self, name: &String) -> Option<&Val> { | ||
self.variables.get(name.as_str()) | ||
} | ||
|
||
pub fn add_function(&mut self, name: &'static str, func: Function) -> &mut Self { | ||
self.funtions.insert(name, func); | ||
self | ||
} | ||
pub fn resolve_function(&self, name: &String) -> Option<&Function> { | ||
self.funtions.get(name.as_str()) | ||
} | ||
|
||
pub fn parent(&self) -> Option<Rc<Context>> { | ||
self.par.clone() | ||
self.par.clone() | ||
} | ||
} | ||
} |
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 @@ | ||
use crate::Val; | ||
|
||
#[derive(PartialEq, Eq, Debug, Clone)] | ||
pub struct Function { | ||
pub name: &'static str, | ||
pub overloads: &'static [Overload], | ||
} | ||
|
||
type Func = fn(args: Vec<Val>) -> Val; | ||
|
||
#[derive(PartialEq, Eq, Debug, Clone)] | ||
pub struct Overload { | ||
pub key: &'static str, | ||
pub func: Func | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::{function::{Function, Overload}, Val}; | ||
|
||
|
||
fn invoke_dyn(args: Vec<Val>) -> Val { | ||
args.first().unwrap().clone() | ||
} | ||
|
||
pub fn new_dyn() -> Function { | ||
Function { | ||
name: "dyn", | ||
overloads: &[Overload { | ||
key: "dyn", | ||
func: invoke_dyn, | ||
}], | ||
} | ||
} | ||
|
||
|
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
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.