-
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.
Merge branch 'main' of https://github.com/lykia-rs/lykiadb into featu…
…re/planner
- Loading branch information
Showing
8 changed files
with
215 additions
and
117 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,86 @@ | ||
use std::sync::Arc; | ||
use std::fmt::{Debug, Display, Formatter}; | ||
use lykiadb_lang::ast::stmt::Stmt; | ||
use crate::{engine::interpreter::{HaltReason, Interpreter}, util::Shared}; | ||
use super::environment::EnvId; | ||
use super::types::RV; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum CallableKind { | ||
Generic, | ||
Aggregator, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Callable { | ||
pub arity: Option<usize>, | ||
pub kind: CallableKind, | ||
pub function: Arc<Function>, | ||
} | ||
|
||
impl Callable { | ||
pub fn new(arity: Option<usize>, call_type: CallableKind, function: Function) -> Self { | ||
Callable { | ||
arity, | ||
kind: call_type, | ||
function: Arc::new(function), | ||
} | ||
} | ||
|
||
pub fn call(&self, interpreter: &mut Interpreter, arguments: &[RV]) -> Result<RV, HaltReason> { | ||
match self.function.as_ref() { | ||
Function::Stateful(stateful) => stateful.write().unwrap().call(interpreter, arguments), | ||
Function::Lambda { function } => function(interpreter, arguments), | ||
Function::UserDefined { | ||
name: _, | ||
parameters, | ||
closure, | ||
body, | ||
} => interpreter.user_fn_call(body, *closure, parameters, arguments), | ||
} | ||
} | ||
} | ||
|
||
pub trait Stateful { | ||
fn call(&mut self, interpreter: &mut Interpreter, rv: &[RV]) -> Result<RV, HaltReason>; | ||
} | ||
|
||
#[derive(Clone)] | ||
pub enum Function { | ||
Lambda { | ||
function: fn(&mut Interpreter, &[RV]) -> Result<RV, HaltReason>, | ||
}, | ||
Stateful(Shared<dyn Stateful + Send + Sync>), | ||
UserDefined { | ||
name: String, | ||
parameters: Vec<String>, | ||
closure: EnvId, | ||
body: Arc<Vec<Stmt>>, | ||
}, | ||
} | ||
|
||
impl Function { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Function::Stateful(_) | Function::Lambda { function: _ } => write!(f, "<native_fn>"), | ||
Function::UserDefined { | ||
name, | ||
parameters: _, | ||
closure: _, | ||
body: _, | ||
} => write!(f, "{}", name), | ||
} | ||
} | ||
} | ||
|
||
impl Debug for Function { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
self.fmt(f) | ||
} | ||
} | ||
|
||
impl Display for Function { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
self.fmt(f) | ||
} | ||
} |
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,2 +1,3 @@ | ||
pub mod environment; | ||
pub mod types; | ||
pub mod callable; |
Oops, something went wrong.