From f9b6a64b845dabdf1773da49f248def479659934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 1 Feb 2021 21:49:12 +0100 Subject: tihdy -> sylt --- tihdy_derive/Cargo.toml | 14 -------- tihdy_derive/src/lib.rs | 87 ------------------------------------------------- 2 files changed, 101 deletions(-) delete mode 100644 tihdy_derive/Cargo.toml delete mode 100644 tihdy_derive/src/lib.rs (limited to 'tihdy_derive') diff --git a/tihdy_derive/Cargo.toml b/tihdy_derive/Cargo.toml deleted file mode 100644 index 411f1ab..0000000 --- a/tihdy_derive/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "tihdy_derive" -version = "0.1.0" -authors = ["Gustav Sörnäs "] -edition = "2018" - -[lib] -proc-macro = true - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -syn = { version = "1.0", features=["full"] } -quote = "1.0" diff --git a/tihdy_derive/src/lib.rs b/tihdy_derive/src/lib.rs deleted file mode 100644 index c0806ab..0000000 --- a/tihdy_derive/src/lib.rs +++ /dev/null @@ -1,87 +0,0 @@ -use proc_macro::TokenStream; -use quote::quote; -use syn::{Expr, Pat, Token, parse::{Parse, ParseStream, Result}, parse_macro_input}; - -struct ExternBlock { - pattern: Pat, - _arrow: Token![->], - return_ty: Expr, - _fat_arrow: Token![=>], - block: Expr, - _comma: Token![,], -} - -struct ExternFunction { - function: syn::Ident, - blocks: Vec -} - -impl Parse for ExternBlock { - fn parse(input: ParseStream) -> Result { - Ok(Self { - pattern: input.parse()?, - _arrow: input.parse()?, - return_ty: input.parse()?, - _fat_arrow: input.parse()?, - block: input.parse()?, - _comma: input.parse()?, - }) - } -} - -impl Parse for ExternFunction { - fn parse(input: ParseStream) -> Result { - let mut res = Self { - function: input.parse()?, - blocks: Vec::new(), - }; - while !input.is_empty() { - res.blocks.push(input.parse()?); - } - Ok(res) - } -} - -#[proc_macro] -pub fn extern_function(tokens: TokenStream) -> TokenStream { - let parsed: ExternFunction = parse_macro_input!(tokens); - let function = parsed.function; - - let typecheck_blocks: Vec<_> = parsed.blocks.iter().map(|block| { - let pat = block.pattern.clone(); - let ty = block.return_ty.clone(); - quote! { - #pat => { Ok(#ty.as_value()) } - } - }).collect(); - - let eval_blocks: Vec<_> = parsed.blocks.iter().map(|block| { - let pat = block.pattern.clone(); - let expr = block.block.clone(); - quote! { - #pat => #expr - } - }).collect(); - - let tokens = quote! { - pub fn #function ( - __values: &[tihdy::Value], - __typecheck: bool - ) -> ::std::result::Result - { - if __typecheck { - #[allow(unused_variables)] - match __values { - #(#typecheck_blocks),* - _ => Err(tihdy::error::ErrorKind::ExternTypeMismatch(stringify!(#function).to_string(), __values.iter().map(|v| tihdy::Type::from(v)).collect())) - } - } else { - match __values { - #(#eval_blocks),* - _ => Err(tihdy::error::ErrorKind::ExternTypeMismatch(stringify!(#function).to_string(), __values.iter().map(|v| tihdy::Type::from(v)).collect())) - } - } - } - }; - TokenStream::from(tokens) -} -- cgit v1.2.1