Skip to content

Commit

Permalink
if is now native because of speed
Browse files Browse the repository at this point in the history
  • Loading branch information
sty00A4 committed Jan 2, 2023
1 parent f8cc679 commit e00e878
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 27 deletions.
20 changes: 7 additions & 13 deletions nody_std/std.nd
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
; control flow
(def-global-inline @if $(:cond bool :case closure)
#(do (? :cond :case #())))
(def-global-inline @if $(:cond bool :case closure :else closure)
#(do (? :cond :case :else)))
; vars
(def-global @add? $(t type) #(contains [int float str] t))
(def-global @sub? $(t type) #(contains [int float] t))
(def-global @number? $(t type) #(contains [int float] t))
(def-global-inline @inc! $(:var key) #(if (exist? :var)
#(if (add? (type (get :var)))
#(if (number? (type (get :var)))
#(set :var (+ (get :var) ((type (get :var)) 1))))
))
(def-global-inline @dec! $(:var key) #(if (exist? :var)
#(if (sub? (type (get :var)))
#(if (number? (type (get :var)))
#(set :var (- (get :var) ((type (get :var)) 1))))
))
(def-global-inline @inc! $(:var index) #(if (exist? :var)
#(if (add? (type (get :var)))
#(if (number? (type (get :var)))
#(set :var (+ (get :var) ((type (get :var)) 1))))
))
(def-global-inline @dec! $(:var index) #(if (exist? :var)
#(if (sub? (type (get :var)))
#(if (number? (type (get :var)))
#(set :var (- (get :var) ((type (get :var)) 1))))
))
(def-global-inline @inc! $(:var path) #(if (exist? :var)
#(if (add? (type (get :var)))
#(if (number? (type (get :var)))
#(set :var (+ (get :var) ((type (get :var)) 1))))
))
(def-global-inline @dec! $(:var path) #(if (exist? :var)
#(if (sub? (type (get :var)))
#(if (number? (type (get :var)))
#(set :var (- (get :var) ((type (get :var)) 1))))
))
; logic
Expand Down
30 changes: 16 additions & 14 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,92 +275,92 @@ impl Context {
pub fn trace_pop(&mut self) -> Option<Position> { self.trace.pop() }
// scope of var
pub fn get_scope_var(&self, id: &String) -> Option<&Scope> {
if self.global.get_var(id).is_some() { return Some(&self.global) }
for scope in self.scopes.iter().rev() {
if scope.get_var(id).is_some() { return Some(scope) }
}
if self.global.get_var(id).is_some() { return Some(&self.global) }
None
}
pub fn get_scope_var_mut(&mut self, id: &String) -> Option<&mut Scope> {
if self.global.get_var(id).is_some() { return Some(&mut self.global) }
for scope in self.scopes.iter_mut().rev() {
if scope.get_var(id).is_some() { return Some(scope) }
}
if self.global.get_var(id).is_some() { return Some(&mut self.global) }
None
}
// scope of fn
pub fn get_scope_fn(&self, id: &String, pattern: &Vec<Type>) -> Option<&Scope> {
if self.global.get_fn(id, pattern).is_some() { return Some(&self.global) }
for scope in self.scopes.iter().rev() {
if scope.get_fn(id, pattern).is_some() { return Some(scope) }
}
if self.global.get_fn(id, pattern).is_some() { return Some(&self.global) }
None
}
pub fn get_scope_fn_mut(&mut self, id: &String, pattern: &Vec<Type>) -> Option<&mut Scope> {
if self.global.get_fn_mut(id, pattern).is_some() { return Some(&mut self.global) }
for scope in self.scopes.iter_mut().rev() {
if scope.get_fn_mut(id, pattern).is_some() { return Some(scope) }
}
if self.global.get_fn_mut(id, pattern).is_some() { return Some(&mut self.global) }
None
}
// scope of fn
pub fn get_scope_fn_any(&self, id: &String) -> Option<&Scope> {
if self.global.get_fn_any(id).is_some() { return Some(&self.global) }
for scope in self.scopes.iter().rev() {
if scope.get_fn_any(id).is_some() { return Some(scope) }
}
if self.global.get_fn_any(id).is_some() { return Some(&self.global) }
None
}
pub fn get_scope_fn_any_mut(&mut self, id: &String) -> Option<&mut Scope> {
if self.global.get_fn_any_mut(id).is_some() { return Some(&mut self.global) }
for scope in self.scopes.iter_mut().rev() {
if scope.get_fn_any_mut(id).is_some() { return Some(scope) }
}
if self.global.get_fn_any_mut(id).is_some() { return Some(&mut self.global) }
None
}
// scope of fn params
pub fn get_scope_fn_params(&self, id: &String, params: &Params) -> Option<&Scope> {
if self.global.get_fn_params(id, params).is_some() { return Some(&self.global) }
for scope in self.scopes.iter().rev() {
if scope.get_fn_params(id, params).is_some() { return Some(scope) }
}
if self.global.get_fn_params(id, params).is_some() { return Some(&self.global) }
None
}
pub fn get_scope_fn_params_mut(&mut self, id: &String, params: &Params) -> Option<&mut Scope> {
if self.global.get_fn_params_mut(id, params).is_some() { return Some(&mut self.global) }
for scope in self.scopes.iter_mut().rev() {
if scope.get_fn_params_mut(id, params).is_some() { return Some(scope) }
}
if self.global.get_fn_params_mut(id, params).is_some() { return Some(&mut self.global) }
None
}
// scope of native fn
pub fn get_scope_native_fn(&self, id: &String, pattern: &Vec<Type>) -> Option<&Scope> {
if self.global.get_native_fn(id, pattern).is_some() { return Some(&self.global) }
for scope in self.scopes.iter().rev() {
if scope.get_native_fn(id, pattern).is_some() { return Some(scope) }
}
if self.global.get_native_fn(id, pattern).is_some() { return Some(&self.global) }
None
}
pub fn get_scope_native_fn_mut(&mut self, id: &String, pattern: &Vec<Type>) -> Option<&mut Scope> {
if self.global.get_native_fn_mut(id, pattern).is_some() { return Some(&mut self.global) }
for scope in self.scopes.iter_mut().rev() {
if scope.get_native_fn_mut(id, pattern).is_some() { return Some(scope) }
}
if self.global.get_native_fn_mut(id, pattern).is_some() { return Some(&mut self.global) }
None
}
// scope of fn
pub fn get_scope_native_fn_any(&self, id: &String) -> Option<&Scope> {
if self.global.get_native_fn_any(id).is_some() { return Some(&self.global) }
for scope in self.scopes.iter().rev() {
if scope.get_native_fn_any(id).is_some() { return Some(scope) }
}
if self.global.get_native_fn_any(id).is_some() { return Some(&self.global) }
None
}
pub fn get_scope_native_fn_any_mut(&mut self, id: &String) -> Option<&mut Scope> {
if self.global.get_native_fn_any_mut(id).is_some() { return Some(&mut self.global) }
for scope in self.scopes.iter_mut().rev() {
if scope.get_native_fn_any_mut(id).is_some() { return Some(scope) }
}
if self.global.get_native_fn_any_mut(id).is_some() { return Some(&mut self.global) }
None
}

Expand Down Expand Up @@ -472,10 +472,11 @@ impl Context {
self.get_scope_fn_mut(id, pattern)?.get_fn_mut(id, pattern)
}
pub fn fn_exists(&self, id: &String) -> bool {
if self.global.fn_exists(id) { return true }
for scope in self.scopes.iter().rev() {
if scope.fn_exists(id) { return true }
}
self.global.fn_exists(id)
false
}
// get native fn
pub fn get_native_fn(&self, id: &String, pattern: &Vec<Type>) -> Option<&NativFunction> {
Expand All @@ -485,10 +486,11 @@ impl Context {
self.get_scope_native_fn_mut(id, pattern)?.get_native_fn_mut(id, pattern)
}
pub fn native_fn_exists(&self, id: &String) -> bool {
if self.global.native_fn_exists(id) { return true }
for scope in self.scopes.iter().rev() {
if scope.native_fn_exists(id) { return true }
}
self.global.native_fn_exists(id)
false
}
// get patterns
pub fn get_patterns(&self, id: &String) -> Option<Vec<Vec<(Type, bool)>>> {
Expand Down
43 changes: 43 additions & 0 deletions src/nody_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,30 @@ fn _if(context: &mut Context) -> Result<(Option<Value>, Return), Error> {
Ok((Some(else_.clone()), Return::None))
}
}
fn _if_do(context: &mut Context) -> Result<(Option<Value>, Return), Error> {
let cond = context.get_var(&":cond".to_string()).unwrap();
let case = context.get_var(&":case".to_string()).unwrap().clone();
if let Value::Closure(case) = case {
if cond == &Value::Bool(true) {
return interpret(&case, context)
}
Ok((None, Return::None))
} else { panic!("type checking doesn't work") }
}
fn _if_do_else(context: &mut Context) -> Result<(Option<Value>, Return), Error> {
let cond = context.get_var(&":cond".to_string()).unwrap();
let case = context.get_var(&":case".to_string()).unwrap().clone();
let else_ = context.get_var(&":else".to_string()).unwrap().clone();
if let Value::Closure(case) = case {
if let Value::Closure(else_) = else_ {
if cond == &Value::Bool(true) {
interpret(&case, context)
} else {
interpret(&else_, context)
}
} else { panic!("type checking doesn't work") }
} else { panic!("type checking doesn't work") }
}
fn _for(context: &mut Context) -> Result<(Option<Value>, Return), Error> {
let id = context.get_var(&":id".to_string()).unwrap().clone();
let pos = context.get_var_pos(&":id".to_string()).unwrap().clone();
Expand Down Expand Up @@ -1284,6 +1308,25 @@ pub fn std_context() -> Result<Context, Error> {
body: _if,
inline: false
}, pos.clone())?;
context.create_native_fn(String::from("if"), NativFunction {
params: vec![
(":cond".to_string(), Type::Bool, false),
(":case".to_string(), Type::Any, false)
],
return_type: Some(Type::Any),
body: _if_do,
inline: true
}, pos.clone())?;
context.create_native_fn(String::from("if"), NativFunction {
params: vec![
(":cond".to_string(), Type::Bool, false),
(":case".to_string(), Type::Any, false),
(":else".to_string(), Type::Any, false)
],
return_type: Some(Type::Any),
body: _if_do_else,
inline: true
}, pos.clone())?;
context.create_native_fn(String::from("for"), NativFunction {
params: vec![
(":id".to_string(), Type::Key, false),
Expand Down

0 comments on commit e00e878

Please sign in to comment.