Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose AST in Rust library #282

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ members = [
"cozo-lib-wasm",
"cozo-lib-swift",
"cozo-lib-python",
"cozo-lib-nodejs"
"cozo-lib-nodejs",
"cozo-core-examples",
]

[profile.bench]
Expand Down
9 changes: 9 additions & 0 deletions cozo-core-examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "cozo-core-examples"
version = "0.1.0"
edition = "2021"

[dependencies]
cozo = { version = "0.7.6", path = "../cozo-core", default-features = false, features = [
"rayon",
] }
10 changes: 10 additions & 0 deletions cozo-core-examples/src/bin/run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use cozo::{DbInstance, ScriptMutability};

fn main() {
let db = DbInstance::new("mem", "", Default::default()).unwrap();
let script = "?[a] := a in [1, 2, 3]";
let result = db
.run_script(script, Default::default(), ScriptMutability::Immutable)
.unwrap();
println!("{:?}", result);
}
58 changes: 58 additions & 0 deletions cozo-core-examples/src/bin/run_ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::collections::BTreeMap;

use cozo::{
data::{
functions::current_validity,
program::{InputAtom, InputInlineRule, InputInlineRulesOrFixed, InputProgram, Unification},
symb::PROG_ENTRY,
},
parse::{CozoScript, ImperativeStmt, ImperativeStmtClause},
DataValue, DbInstance, Num, ScriptMutability, Symbol,
};

fn main() {
let db = DbInstance::new("mem", "", Default::default()).unwrap();
let sym_a = Symbol::new("a", Default::default());
let script = CozoScript::Imperative(vec![ImperativeStmt::Program {
prog: ImperativeStmtClause {
prog: InputProgram {
prog: {
let mut p = BTreeMap::new();
p.insert(
Symbol::new(PROG_ENTRY, Default::default()),
InputInlineRulesOrFixed::Rules {
rules: vec![InputInlineRule {
head: vec![sym_a.clone()],
aggr: vec![None],
body: vec![InputAtom::Unification {
inner: Unification {
binding: sym_a,
expr: cozo::Expr::Const {
val: DataValue::List(vec![
DataValue::Num(Num::Int(1)),
DataValue::Num(Num::Int(2)),
DataValue::Num(Num::Int(3)),
]),
span: Default::default(),
},
one_many_unif: true,
span: Default::default(),
},
}],
span: Default::default(),
}],
},
);
p
},
out_opts: Default::default(),
disable_magic_rewrite: false,
},
store_as: None,
},
}]);
let result = db
.run_script_ast(script, current_validity(), ScriptMutability::Immutable)
.unwrap();
println!("{:?}", result);
}
14 changes: 14 additions & 0 deletions cozo-core-examples/src/bin/run_parse_ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use cozo::{data::functions::current_validity, parse::parse_script, DbInstance, ScriptMutability};

fn main() {
let db = DbInstance::new("mem", "", Default::default()).unwrap();
let script = "?[a] := a in [1, 2, 3]";
let cur_vld = current_validity();
let script_ast =
parse_script(script, &Default::default(), &db.get_fixed_rules(), cur_vld).unwrap();
println!("AST: {:?}", script_ast);
let result = db
.run_script_ast(script_ast, cur_vld, ScriptMutability::Immutable)
.unwrap();
println!("Result: {:?}", result);
}
14 changes: 7 additions & 7 deletions cozo-core/src/data/aggr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use rand::prelude::*;

use crate::data::value::DataValue;

pub(crate) struct Aggregation {
pub(crate) name: &'static str,
pub(crate) is_meet: bool,
pub(crate) meet_op: Option<Box<dyn MeetAggrObj>>,
pub(crate) normal_op: Option<Box<dyn NormalAggrObj>>,
pub struct Aggregation {
pub name: &'static str,
pub is_meet: bool,
pub meet_op: Option<Box<dyn MeetAggrObj>>,
pub normal_op: Option<Box<dyn NormalAggrObj>>,
}

impl Clone for Aggregation {
Expand All @@ -32,12 +32,12 @@ impl Clone for Aggregation {
}
}

pub(crate) trait NormalAggrObj: Send + Sync {
pub trait NormalAggrObj: Send + Sync {
fn set(&mut self, value: &DataValue) -> Result<()>;
fn get(&self) -> Result<DataValue>;
}

pub(crate) trait MeetAggrObj: Send + Sync {
pub trait MeetAggrObj: Send + Sync {
fn init_val(&self) -> DataValue;
fn update(&self, left: &mut DataValue, right: &DataValue) -> Result<bool>;
}
Expand Down
2 changes: 1 addition & 1 deletion cozo-core/src/data/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2453,7 +2453,7 @@ pub(crate) fn op_now(_args: &[DataValue]) -> Result<DataValue> {
))
}

pub(crate) fn current_validity() -> ValidityTs {
pub fn current_validity() -> ValidityTs {
#[cfg(not(target_arch = "wasm32"))]
let ts_micros = {
let now = SystemTime::now();
Expand Down
6 changes: 3 additions & 3 deletions cozo-core/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

pub(crate) mod aggr;
pub(crate) mod expr;
pub(crate) mod functions;
pub mod functions;
pub(crate) mod json;
pub(crate) mod memcmp;
pub(crate) mod program;
pub mod program;
pub(crate) mod relation;
pub(crate) mod symb;
pub mod symb;
pub(crate) mod tuple;
pub(crate) mod value;

Expand Down
Loading
Loading