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 --- Cargo.lock | 36 +++++++++---------- Cargo.toml | 11 +++--- benches/sylt_benchmark.rs | 42 ++++++++++++++++++++++ benches/tihdy_benchmark.rs | 42 ---------------------- src/main.rs | 12 +++---- sylt_macro/Cargo.toml | 14 ++++++++ sylt_macro/src/lib.rs | 87 ++++++++++++++++++++++++++++++++++++++++++++++ tihdy_derive/Cargo.toml | 14 -------- tihdy_derive/src/lib.rs | 87 ---------------------------------------------- 9 files changed, 172 insertions(+), 173 deletions(-) create mode 100644 benches/sylt_benchmark.rs delete mode 100644 benches/tihdy_benchmark.rs create mode 100644 sylt_macro/Cargo.toml create mode 100644 sylt_macro/src/lib.rs delete mode 100644 tihdy_derive/Cargo.toml delete mode 100644 tihdy_derive/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 3e3759e..fde14d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -498,6 +498,24 @@ dependencies = [ "serde", ] +[[package]] +name = "sylt" +version = "0.1.0" +dependencies = [ + "criterion", + "logos", + "owo-colors", + "sylt_macro", +] + +[[package]] +name = "sylt_macro" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "syn" version = "1.0.58" @@ -518,24 +536,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "tihdy" -version = "0.1.0" -dependencies = [ - "criterion", - "logos", - "owo-colors", - "tihdy_derive", -] - -[[package]] -name = "tihdy_derive" -version = "0.1.0" -dependencies = [ - "quote", - "syn", -] - [[package]] name = "tinytemplate" version = "1.2.0" diff --git a/Cargo.toml b/Cargo.toml index e78987e..f6b763f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,25 +1,24 @@ [package] -name = "tihdy" +name = "sylt" version = "0.1.0" -authors = ["Edvard Thörnros "] +authors = ["Edvard Thörnros ", "Gustav Sörnäs "] edition = "2018" [lib] -name = "tihdy" +name = "sylt" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] logos = "0.11.4" owo-colors = { git="https://github.com/FredTheDino/owo-colors.git" } -tihdy_derive = { path = "tihdy_derive" } +sylt_macro = { path = "sylt_macro" } criterion = { version = "0.3", optional = true } [profile.release] -debug = true lto = "thin" [[bench]] -name = "tihdy_benchmark" +name = "sylt_benchmark" harness = false diff --git a/benches/sylt_benchmark.rs b/benches/sylt_benchmark.rs new file mode 100644 index 0000000..a68a762 --- /dev/null +++ b/benches/sylt_benchmark.rs @@ -0,0 +1,42 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use std::path::Path; + +pub fn fib_50(c: &mut Criterion) { + let prog = +" +j := 0 +for , j < 1000, j = j + 1 { + a := 0 + b := 1 + + for i := 0, i < 50, i = i + 1 { + c := a + a = b + b = c + b + } + a <=> 12586269025 +} +"; + let compiled = sylt::compiler::compile("main", Path::new("prog"), sylt::tokenizer::string_to_tokens(prog)).unwrap(); + c.bench_function("fib 50", |b| b.iter(|| sylt::vm::run_block(&compiled).unwrap())); +} + +pub fn fib_90(c: &mut Criterion) { + let prog = +" +a := 0 +b := 1 + +for i := 0, i < 90, i = i + 1 { + c := a + a = b + b = c + b +} +a <=> 2880067194370816120 +"; + let compiled = sylt::compiler::compile("main", Path::new("prog"), sylt::tokenizer::string_to_tokens(prog)).unwrap(); + c.bench_function("fib 90", |b| b.iter(|| sylt::vm::run_block(&compiled).unwrap())); +} + +criterion_group!(benches, fib_50, fib_90); +criterion_main!(benches); diff --git a/benches/tihdy_benchmark.rs b/benches/tihdy_benchmark.rs deleted file mode 100644 index f639fb0..0000000 --- a/benches/tihdy_benchmark.rs +++ /dev/null @@ -1,42 +0,0 @@ -use criterion::{criterion_group, criterion_main, Criterion}; -use std::path::Path; - -pub fn fib_50(c: &mut Criterion) { - let prog = -" -j := 0 -for , j < 1000, j = j + 1 { - a := 0 - b := 1 - - for i := 0, i < 50, i = i + 1 { - c := a - a = b - b = c + b - } - a <=> 12586269025 -} -"; - let compiled = tihdy::compiler::compile("main", Path::new("prog"), tihdy::tokenizer::string_to_tokens(prog)).unwrap(); - c.bench_function("fib 50", |b| b.iter(|| tihdy::vm::run_block(&compiled).unwrap())); -} - -pub fn fib_90(c: &mut Criterion) { - let prog = -" -a := 0 -b := 1 - -for i := 0, i < 90, i = i + 1 { - c := a - a = b - b = c + b -} -a <=> 2880067194370816120 -"; - let compiled = tihdy::compiler::compile("main", Path::new("prog"), tihdy::tokenizer::string_to_tokens(prog)).unwrap(); - c.bench_function("fib 90", |b| b.iter(|| tihdy::vm::run_block(&compiled).unwrap())); -} - -criterion_group!(benches, fib_50, fib_90); -criterion_main!(benches); diff --git a/src/main.rs b/src/main.rs index c52acc8..37ae256 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::path::{Path, PathBuf}; -use tihdy::run_file; +use sylt::run_file; struct Args { file: Option, @@ -39,12 +39,12 @@ fn parse_args() -> Args { args } -tihdy_derive::extern_function!( +sylt_macro::extern_function!( extern_test - [tihdy::Value::Float(x), tihdy::Value::Float(y)] -> tihdy::Type::Float => { - Ok(tihdy::Value::Float(x + y)) + [sylt::Value::Float(x), sylt::Value::Float(y)] -> sylt::Type::Float => { + Ok(sylt::Value::Float(x + y)) }, - [tihdy::Value::Float(x)] -> tihdy::Type::Float => { - Ok(tihdy::Value::Float(*x)) + [sylt::Value::Float(x)] -> sylt::Type::Float => { + Ok(sylt::Value::Float(*x)) }, ); diff --git a/sylt_macro/Cargo.toml b/sylt_macro/Cargo.toml new file mode 100644 index 0000000..9ac045e --- /dev/null +++ b/sylt_macro/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "sylt_macro" +version = "0.1.0" +authors = ["Edvard Thörnros "] +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/sylt_macro/src/lib.rs b/sylt_macro/src/lib.rs new file mode 100644 index 0000000..241ef7d --- /dev/null +++ b/sylt_macro/src/lib.rs @@ -0,0 +1,87 @@ +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: &[sylt::Value], + __typecheck: bool + ) -> ::std::result::Result + { + if __typecheck { + #[allow(unused_variables)] + match __values { + #(#typecheck_blocks),* + _ => Err(sylt::error::ErrorKind::ExternTypeMismatch(stringify!(#function).to_string(), __values.iter().map(|v| sylt::Type::from(v)).collect())) + } + } else { + match __values { + #(#eval_blocks),* + _ => Err(sylt::error::ErrorKind::ExternTypeMismatch(stringify!(#function).to_string(), __values.iter().map(|v| sylt::Type::from(v)).collect())) + } + } + } + }; + TokenStream::from(tokens) +} 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