Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
LivingInSyn committed Feb 11, 2018
0 parents commit 0d2ddb1
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "translator"
version = "0.1.0"
authors = ["Jeremy Mill <jeremymill@gmail.com>"]

[dependencies]
syn = "0.12.12"
quote = "0.3.15"
proc-macro2 = "0.2.2"

[lib]
proc-macro = true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Jeremy Mill

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions TranslatorWorkspace.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"folders": [
{
"path": "."
},
{
"path": "C:\\Users\\bobo\\Documents\\TranslatorTest"
}
]
}
80 changes: 80 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
extern crate proc_macro;
extern crate proc_macro2;
extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;
use proc_macro2::TokenNode;
use syn::Data;

#[proc_macro_derive(Translate)]
pub fn translate(input: TokenStream) -> TokenStream {
// Construct a string representation of the type definition
println!("inside of translate");
//let s = input.to_string();

// Parse the string representation
let ast = syn::parse(input).unwrap();

// Build the impl
let gen = impl_translate(ast);

// Return the generated impl
gen.parse().unwrap()
}

fn impl_translate(ast: syn::DeriveInput) -> quote::Tokens {
// let name = ast.ident;

println!("name is: {}", ast.ident);
let mut is_reprc: bool = false;
//determine if this struct is repr c
for attr in ast.attrs {
for token in attr.tts.into_iter() {
match token.kind {
TokenNode::Group(_, ts) => {
for t in ts.into_iter() {
match t.kind {
TokenNode::Term(t) => {
is_reprc = t.as_str() == "C";
},
_ => {}
}
}
}
_ => {}
};
//println!("token: {}", token.span);

}
//println!("attr: {}", attr.tts);
}
if is_reprc {
match ast.data {
Data::Struct(ds) => {
//i'm here (https://dtolnay.github.io/syn/syn/struct.DataStruct.html)
//figuring out how to iterate through struct fields
//so far everything is named?
match ds.fields {
syn::Fields::Named(_) => {println!("named")},
syn::Fields::Unnamed(_) => {println!("unnamed")},
syn::Fields::Unit => {println!("unit")}
}
println!("it's a struct!");
},
_ => {}
}
}

quote! {
}
}

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

0 comments on commit 0d2ddb1

Please sign in to comment.