Skip to content

Commit

Permalink
feat(modules): add import!() expression (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzlk authored Sep 8, 2023
1 parent d03ffe7 commit 6e8fba7
Show file tree
Hide file tree
Showing 18 changed files with 527 additions and 52 deletions.
48 changes: 48 additions & 0 deletions hydroflow/examples/modules/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::cell::RefCell;
use std::rc::Rc;

use hydroflow::hydroflow_syntax;
use hydroflow::scheduled::graph::Hydroflow;
use hydroflow::util::multiset::HashMultiSet;

pub fn main() {
let output = Rc::new(RefCell::new(
HashMultiSet::<(usize, usize, usize)>::default(),
));

let mut df: Hydroflow = {
let output = output.clone();
hydroflow_syntax! {
source_iter(0..2) -> [0]cj;
source_iter(0..2) -> [1]cj;
source_iter(0..2) -> [2]cj;

cj = import!("triple_cross_join.hf")
-> for_each(|x| output.borrow_mut().insert(x));
}
};

println!("{}", df.meta_graph().unwrap().to_mermaid());

df.run_available();

#[rustfmt::skip]
assert_eq!(
*output.borrow(),
HashMultiSet::from_iter([
(0, 0, 0),
(0, 0, 1),
(0, 1, 0),
(0, 1, 1),
(1, 0, 0),
(1, 0, 1),
(1, 1, 0),
(1, 1, 1),
])
);
}

#[test]
fn test() {
main();
}
15 changes: 15 additions & 0 deletions hydroflow/examples/modules/triple_cross_join.hf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mod[0]
-> [0]cj1;

mod[1]
-> [1]cj1;

cj1 = cross_join()
-> [0]cj2;

mod[2]
-> [1]cj2;

cj2 = cross_join()
-> map(|((a, b), c)| (a, b, c))
-> mod;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: unexpected end of input, expected one of: square brackets, identifier, parentheses
error: unexpected end of input, expected one of: square brackets, `mod`, identifier, parentheses
--> tests/compile-fail/surface_syntax_eol_arrow.rs:4:18
|
4 | let mut df = hydroflow_syntax! {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: unexpected end of input, expected parentheses or identifier
error: unexpected end of input, expected one of: parentheses, identifier, `mod`
--> tests/compile-fail/surface_syntax_eol_indexing.rs:4:18
|
4 | let mut df = hydroflow_syntax! {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expected one of: square brackets, identifier, parentheses
error: expected one of: square brackets, `mod`, identifier, parentheses
--> tests/compile-fail/surface_syntax_eol_missingop.rs:5:31
|
5 | source_iter(0..10) -> ;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: unexpected end of input, expected one of: square brackets, identifier, parentheses
error: unexpected end of input, expected one of: square brackets, `mod`, identifier, parentheses
--> tests/compile-fail/surface_syntax_indexing_empty.rs:5:35
|
5 | source_iter(0..10) -> [0]();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: unexpected end of input, expected one of: square brackets, identifier, parentheses
error: unexpected end of input, expected one of: square brackets, `mod`, identifier, parentheses
--> tests/compile-fail/surface_syntax_paren_arrow.rs:5:31
|
5 | (source_iter(0..10) ->);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: unexpected end of input, expected parentheses or identifier
error: unexpected end of input, expected one of: parentheses, identifier, `mod`
--> tests/compile-fail/surface_syntax_paren_indexing.rs:5:35
|
5 | (source_iter(0..10) -> [0]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: unexpected end of input, expected one of: square brackets, identifier, parentheses
error: unexpected end of input, expected one of: square brackets, `mod`, identifier, parentheses
--> tests/compile-fail/surface_syntax_paren_missingop.rs:5:32
|
5 | (source_iter(0..10) -> );
Expand Down
25 changes: 15 additions & 10 deletions hydroflow_datalog_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,22 @@ pub fn gen_hydroflow_graph(
}

if !diagnostics.is_empty() {
Err(diagnostics)
} else {
let (mut flat_graph, _uses, mut diagnostics) = flat_graph_builder.build();
diagnostics.retain(Diagnostic::is_error);
if !diagnostics.is_empty() {
Err(diagnostics)
} else {
eliminate_extra_unions_tees(&mut flat_graph);
Ok(flat_graph)
}
return Err(diagnostics);
}

let (mut flat_graph, _uses, mut diagnostics) = flat_graph_builder.build();
diagnostics.retain(Diagnostic::is_error);
if !diagnostics.is_empty() {
return Err(diagnostics);
}

if let Err(err) = flat_graph.merge_modules() {
diagnostics.push(err);
return Err(diagnostics);
}

eliminate_extra_unions_tees(&mut flat_graph);
Ok(flat_graph)
}

fn handle_errors(
Expand Down
21 changes: 21 additions & 0 deletions hydroflow_lang/src/graph/di_mul_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,27 @@ where
Some((new_edge, (pred_edge, succ_edge)))
}

/// Remove an edge from the graph. If the edgeId is found then the edge is removed from the graph and returned.
/// If the edgeId was not found in the graph then nothing is returned and nothing is done.
pub fn remove_edge(&mut self, e: E) -> Option<(V, V)> {
let Some((src, dst)) = self.edges.remove(e) else {
return None;
};

self.succs[src].retain(|x| *x != e);
self.preds[dst].retain(|x| *x != e);

Some((src, dst))
}

/// Remove a vertex from the graph, it must have no edges to or from it when doing this.
pub fn remove_vertex(&mut self, v: V) {
assert!(self.preds[v].is_empty() && self.succs[v].is_empty());

self.preds.remove(v);
self.succs.remove(v);
}

/// Get the source and destination vertex IDs for the given edge ID.
pub fn edge(&self, e: E) -> Option<(V, V)> {
self.edges.get(e).copied()
Expand Down
Loading

0 comments on commit 6e8fba7

Please sign in to comment.