-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement an initial version of datapond macro.
- Loading branch information
Showing
14 changed files
with
268 additions
and
58 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,3 +1,3 @@ | ||
/target | ||
target | ||
**/*.rs.bk | ||
Cargo.lock |
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,13 @@ | ||
[package] | ||
name = "datapond-derive" | ||
version = "0.1.0" | ||
authors = ["Vytautas Astrauskas <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
proc-macro-hack = "0.5" | ||
datapond-macro = { path = "../datapond-macro" } | ||
|
||
[dev-dependencies] | ||
trybuild = "1.0" | ||
datafrog = "2" |
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,4 @@ | ||
use proc_macro_hack::proc_macro_hack; | ||
|
||
#[proc_macro_hack] | ||
pub use datapond_macro::datapond; |
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,14 @@ | ||
use datapond_derive::datapond; | ||
|
||
fn main() { | ||
let inp = vec![(1, 2), (2, 3)]; | ||
let out; | ||
datapond! { | ||
input inp(x: u32, y: u32) | ||
output out(x: u32, y: u32) | ||
out(x, y) :- inp(y, x). | ||
}; | ||
assert!(out.len() == 2); | ||
assert!(out[0] == (2, 1)); | ||
assert!(out[1] == (3, 2)); | ||
} |
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,5 @@ | ||
#[test] | ||
fn tests() { | ||
let runner = trybuild::TestCases::new(); | ||
runner.pass("tests/pass/*.rs"); | ||
} |
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,12 @@ | ||
[package] | ||
name = "datapond-macro" | ||
version = "0.1.0" | ||
authors = ["Vytautas Astrauskas <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
datapond = { path = ".." } | ||
proc-macro-hack = "0.5" |
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,7 @@ | ||
use proc_macro::TokenStream; | ||
use proc_macro_hack::proc_macro_hack; | ||
|
||
#[proc_macro_hack] | ||
pub fn datapond(input: TokenStream) -> TokenStream { | ||
datapond::generate_datafrog(input.into()).into() | ||
} |
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,48 @@ | ||
use std::collections::HashMap; | ||
|
||
/// A map that tracks insertion order. | ||
#[derive(Debug)] | ||
pub struct OrderedMap<K, V> | ||
where | ||
K: Eq + std::hash::Hash, | ||
{ | ||
key_order: Vec<K>, | ||
map: HashMap<K, V>, | ||
} | ||
|
||
impl<K: Eq + std::hash::Hash + Clone, V> OrderedMap<K, V> { | ||
pub fn len(&self) -> usize { | ||
self.map.len() | ||
} | ||
pub fn insert(&mut self, k: K, v: V) { | ||
assert!(self.map.insert(k.clone(), v).is_none()); | ||
self.key_order.push(k); | ||
} | ||
pub fn get(&self, k: &K) -> Option<&V> { | ||
self.map.get(k) | ||
} | ||
pub fn values<'a>(&'a self) -> Vec<&'a V> { | ||
self.key_order.iter().map(|k| &self.map[k]).collect() | ||
} | ||
} | ||
|
||
impl<K: Eq + std::hash::Hash + Clone, V> std::iter::FromIterator<(K, V)> for OrderedMap<K, V> { | ||
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self { | ||
let mut s = Self { | ||
key_order: Vec::new(), | ||
map: HashMap::new(), | ||
}; | ||
for (k, v) in iter { | ||
s.insert(k, v); | ||
} | ||
s | ||
} | ||
} | ||
|
||
impl<K: Eq + std::hash::Hash, V> std::ops::Index<&K> for OrderedMap<K, V> { | ||
type Output = V; | ||
|
||
fn index(&self, key: &K) -> &Self::Output { | ||
&self.map[key] | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,16 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::ToTokens; | ||
|
||
mod ast; | ||
mod encode; | ||
mod to_tokens; | ||
|
||
pub fn generate_datafrog(input: TokenStream) -> TokenStream { | ||
let parsed_program = match syn::parse2(input) { | ||
Ok(program) => program, | ||
Err(err) => return TokenStream::from(err.to_compile_error()), | ||
}; | ||
let typechecked_program = crate::typechecker::typecheck(parsed_program).unwrap(); | ||
let encoded_program = encode::encode(typechecked_program); | ||
encoded_program.to_token_stream() | ||
} |
Oops, something went wrong.