diff options
Diffstat (limited to 'autonibble')
-rw-r--r-- | autonibble/Cargo.toml | 19 | ||||
-rw-r--r-- | autonibble/benches/parse/data/12.nb | 1 | ||||
-rw-r--r-- | autonibble/benches/parse/data/1548.nb | 40 | ||||
-rw-r--r-- | autonibble/benches/parse/data/194.nb | 7 | ||||
-rw-r--r-- | autonibble/benches/parse/data/24.nb | 1 | ||||
-rw-r--r-- | autonibble/benches/parse/data/3096.nb | 82 | ||||
-rw-r--r-- | autonibble/benches/parse/data/387.nb | 13 | ||||
-rw-r--r-- | autonibble/benches/parse/data/48.nb | 2 | ||||
-rw-r--r-- | autonibble/benches/parse/data/774.nb | 21 | ||||
-rw-r--r-- | autonibble/benches/parse/data/97.nb | 3 | ||||
-rw-r--r-- | autonibble/benches/parse/main.rs | 64 | ||||
-rw-r--r-- | autonibble/src/lib.rs | 484 | ||||
-rw-r--r-- | autonibble/src/main.rs | 61 | ||||
-rw-r--r-- | autonibble/tests/compare/main.rs | 39 | ||||
l--------- | autonibble/tests/compare/nibble | 1 |
15 files changed, 838 insertions, 0 deletions
diff --git a/autonibble/Cargo.toml b/autonibble/Cargo.toml new file mode 100644 index 0000000..46fc075 --- /dev/null +++ b/autonibble/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "autonibble" +version = "0.1.0" +authors = ["Greg Brown <gmb60@cam.ac.uk>"] +edition = "2018" + +[dev-dependencies] +criterion = "0.3.3" +syn = "1.0.58" + +[dependencies] +chewed = {path = "../chewed"} +chomp = {path = "../"} +chomp-macro = {path = "../chomp-macro"} +proc-macro2 = "1.0.24" + +[[bench]] +name = "parse" +harness = false diff --git a/autonibble/benches/parse/data/12.nb b/autonibble/benches/parse/data/12.nb new file mode 100644 index 0000000..eaa4670 --- /dev/null +++ b/autonibble/benches/parse/data/12.nb @@ -0,0 +1 @@ +match "ab"; diff --git a/autonibble/benches/parse/data/1548.nb b/autonibble/benches/parse/data/1548.nb new file mode 100644 index 0000000..cabdfe9 --- /dev/null +++ b/autonibble/benches/parse/data/1548.nb @@ -0,0 +1,40 @@ +let bot = !(/rec/ "a" . rec); +let zero = /zero suc/ zero; +let suc n = /zero suc/ suc (n zero suc); + +let opt some = _ : None | some; +let plus iter = !(/plus/ iter . (opt plus)); +let star iter = opt (plus iter); + +let up_to x n = n bot (/rec/ x . opt rec); + +let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}"; + +let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" ; +let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"; +let hex_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | + "a" | "b" | "c" | "d" | "e" | "f" | + "A" | "B" | "C" | "D" | "E" | "F" ; + +let XID_Start = + "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | + "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | + "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | + "y" | "z" | + "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | + "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | + "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | + "Y" | "Z" ; +let XID_Continue = + XID_Start | "_" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; + +match + (" " | "!" | "#" | "$" | "%" | "&" | "'" | + "(" | ")" | "*" | "+" | "," | "-" | "." | "/" | + "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | + "8" | "9" | ":" | ";" | "<" | "=" | ">" | "?" | + "@" | "A" | "B" | "C" | "D" | "E" | "F" | "G" | + "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | + "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | + "X"|"Y" + ); diff --git a/autonibble/benches/parse/data/194.nb b/autonibble/benches/parse/data/194.nb new file mode 100644 index 0000000..01730c6 --- /dev/null +++ b/autonibble/benches/parse/data/194.nb @@ -0,0 +1,7 @@ +let bot = !(/rec/ "a" . rec); +let zero = /zero suc/ zero; +let suc n = /zero suc/ suc (n zero suc); + +let opt some = _ : None | some; +let plus iter = !(/plus/ iter . (opt plus)); +match opt "ab"; diff --git a/autonibble/benches/parse/data/24.nb b/autonibble/benches/parse/data/24.nb new file mode 100644 index 0000000..f960c3b --- /dev/null +++ b/autonibble/benches/parse/data/24.nb @@ -0,0 +1 @@ +match !(/rec/ "a".rec); diff --git a/autonibble/benches/parse/data/3096.nb b/autonibble/benches/parse/data/3096.nb new file mode 100644 index 0000000..f90100c --- /dev/null +++ b/autonibble/benches/parse/data/3096.nb @@ -0,0 +1,82 @@ +let bot = !(/rec/ "a" . rec); +let zero = /zero suc/ zero; +let suc n = /zero suc/ suc (n zero suc); + +let opt some = _ : None | some; +let plus iter = !(/plus/ iter . (opt plus)); +let star iter = opt (plus iter); + +let up_to x n = n bot (/rec/ x . opt rec); + +let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}"; + +let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" ; +let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"; +let hex_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | + "a" | "b" | "c" | "d" | "e" | "f" | + "A" | "B" | "C" | "D" | "E" | "F" ; + +let XID_Start = + "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | + "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | + "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | + "y" | "z" | + "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | + "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | + "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | + "Y" | "Z" ; +let XID_Continue = + XID_Start | "_" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; + +let literal_char = + (" " | "!" | "#" | "$" | "%" | "&" | "'" | + "(" | ")" | "*" | "+" | "," | "-" | "." | "/" | + "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | + "8" | "9" | ":" | ";" | "<" | "=" | ">" | "?" | + "@" | "A" | "B" | "C" | "D" | "E" | "F" | "G" | + "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | + "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | + "X" | "Y" | "Z" | "[" | "]" | "^" | "_" | + "`" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | + "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | + "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | + "x" | "y" | "z" | "{" | "|" | "}" | "~") : Literal | + "\\" . ( + ("\"" | "'" | "n" | "r" | "t" | "\\" | "0") : Ascii | + "x" . oct_digit . hex_digit : Oct | + "u{" . up_to hex_digit (suc (suc (suc (suc (suc (suc zero)))))) . "}" : Unicode + ) : Escape ; + +let ws = plus Pattern_Whitespace; +let ows = opt ws; + +let list inner = !(/list/ inner . opt (ws . opt list)); +let separated inner sep = !(/separated/ inner . opt (sep . ows . separated)); + +let epsilon = "_"; +let ident = XID_Start . star XID_Continue; +let literal = "\"" . (plus literal_char : Contents) . "\""; +let fix term = "!" . ows . term; +let parens expr = "(" . ows . expr . ")"; + +let names = list ident; + +let term expr = !(/term/ + epsilon : Epsilon + | literal : Literal + | parens expr : Parens + | fix term : Fix + | ident : Variable + ); + +let label = ":" . ows . ident . ows; + +let call expr = list (term expr); +let cat expr = separated (call expr) "."; +let alt expr = separated (cat expr . opt label : Labelled) "|"; +let lambda expr = "/" . ows . names . "/" . ows . alt expr; +let expr = !(/expr/ alt expr | lambda expr); +let goal = "match" . ws . expr . ";" . ows; +let let stmt = "let" . ws . names . "=" . ows . expr . ";" . ows . stmt; +let stmt = !(/stmt/ let stmt | goal); +match !(/skip/ Pattern_Whitespace . skip | stmt); diff --git a/autonibble/benches/parse/data/387.nb b/autonibble/benches/parse/data/387.nb new file mode 100644 index 0000000..0f60b89 --- /dev/null +++ b/autonibble/benches/parse/data/387.nb @@ -0,0 +1,13 @@ +let bot = !(/rec/ "a" . rec); +let zero = /zero suc/ zero; +let suc n = /zero suc/ suc (n zero suc); + +let opt some = _ : None | some; +let plus iter = !(/plus/ iter . (opt plus)); +let star iter = opt (plus iter); + +let up_to x n = n bot (/rec/ x . opt rec); + +let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}"; + +match = "0" |"1"; diff --git a/autonibble/benches/parse/data/48.nb b/autonibble/benches/parse/data/48.nb new file mode 100644 index 0000000..7059bb8 --- /dev/null +++ b/autonibble/benches/parse/data/48.nb @@ -0,0 +1,2 @@ +let bot = !(/rec/ "a" . rec); +match /zero/zero; diff --git a/autonibble/benches/parse/data/774.nb b/autonibble/benches/parse/data/774.nb new file mode 100644 index 0000000..89c22ca --- /dev/null +++ b/autonibble/benches/parse/data/774.nb @@ -0,0 +1,21 @@ +let bot = !(/rec/ "a" . rec); +let zero = /zero suc/ zero; +let suc n = /zero suc/ suc (n zero suc); + +let opt some = _ : None | some; +let plus iter = !(/plus/ iter . (opt plus)); +let star iter = opt (plus iter); + +let up_to x n = n bot (/rec/ x . opt rec); + +let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}"; + +let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" ; +let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"; +let hex_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | + "a" | "b" | "c" | "d" | "e" | "f" | + "A" | "B" | "C" | "D" | "E" | "F" ; + +match + "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | + "i" | "j" | "k" | "l"; diff --git a/autonibble/benches/parse/data/97.nb b/autonibble/benches/parse/data/97.nb new file mode 100644 index 0000000..d0d00f9 --- /dev/null +++ b/autonibble/benches/parse/data/97.nb @@ -0,0 +1,3 @@ +let bot = !(/rec/ ("a" . rec)); +let zero = /zero suc/ zero; +match (/zero suc/ suc (_ zero suc)); diff --git a/autonibble/benches/parse/main.rs b/autonibble/benches/parse/main.rs new file mode 100644 index 0000000..98ad0f3 --- /dev/null +++ b/autonibble/benches/parse/main.rs @@ -0,0 +1,64 @@ +use std::error::Error; + +use chewed::{IterWrapper, Parser}; +use chomp::{ + chomp::ast::NamedExpression, + nibble::{ + self, + convert::{Context, Convert}, + }, +}; +use criterion::{ + criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration, + Throughput, +}; + +const INPUTS: &[&str] = &[ + include_str!("data/12.nb"), + include_str!("data/24.nb"), + include_str!("data/48.nb"), + include_str!("data/97.nb"), + include_str!("data/194.nb"), + include_str!("data/387.nb"), + include_str!("data/774.nb"), + include_str!("data/1548.nb"), + include_str!("data/3096.nb"), + +]; + +fn parse_autonibble(input: &str) -> Result<NamedExpression, Box<dyn Error>> { + IterWrapper::new(input.chars()) + .parse::<autonibble::Ast>() + .map_err(|e| Box::new(e) as Box<dyn Error>) + .and_then(|ast| { + ast.convert(&mut Context::default()) + .map_err(|e| Box::new(e) as Box<dyn Error>) + }) +} + +fn parse_chomp(input: &str) -> Result<NamedExpression, Box<dyn Error>> { + syn::parse_str::<nibble::Statement>(input) + .map_err(|e| Box::new(e) as Box<dyn Error>) + .and_then(|stmt| { + stmt.convert(&mut Context::default()) + .map_err(|e| Box::new(e) as Box<dyn Error>) + }) +} + +fn bench_parse(c: &mut Criterion) { + let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic); + let mut group = c.benchmark_group("Parse"); + group.plot_config(plot_config); + for (i, input) in INPUTS.iter().enumerate() { + group.throughput(Throughput::Bytes(input.len() as u64)); + group.bench_with_input(BenchmarkId::new("Chomp", i), *input, |b, i| { + b.iter(|| parse_chomp(i)) + }); + group.bench_with_input(BenchmarkId::new("AutoChomp", i), *input, |b, i| { + b.iter(|| parse_autonibble(i)) + }); + } +} + +criterion_group!(benches, bench_parse); +criterion_main!(benches); diff --git a/autonibble/src/lib.rs b/autonibble/src/lib.rs new file mode 100644 index 0000000..2fed0f0 --- /dev/null +++ b/autonibble/src/lib.rs @@ -0,0 +1,484 @@ +use std::{convert::TryInto, mem}; + +use chomp::{ + chomp::{ + ast::{self, Alt, Call, Cat, Fix, Lambda, Let, Literal, NamedExpression, Variable}, + name::{Content, Name}, + }, + nibble::convert::{Context, Convert, ConvertError}, +}; +use chomp_macro::nibble; +use proc_macro2::Span; + +nibble! { + let bot = !(/rec/ "a" . rec); + let zero = /zero suc/ zero; + let suc n = /zero suc/ suc (n zero suc); + + let opt some = _ : None | some; + let plus iter = !(/plus/ iter . (opt plus)); + let star iter = opt (plus iter); + + let up_to x n = n bot (/rec/ x . opt rec); + + let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}"; + + let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" ; + let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"; + let hex_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | + "a" | "b" | "c" | "d" | "e" | "f" | + "A" | "B" | "C" | "D" | "E" | "F" ; + + let XID_Start = + "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | + "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | + "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | + "y" | "z" | + "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | + "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | + "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | + "Y" | "Z" ; + let XID_Continue = + XID_Start | "_" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; + + let literal_char = + (" " | "!" | "#" | "$" | "%" | "&" | "'" | + "(" | ")" | "*" | "+" | "," | "-" | "." | "/" | + "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | + "8" | "9" | ":" | ";" | "<" | "=" | ">" | "?" | + "@" | "A" | "B" | "C" | "D" | "E" | "F" | "G" | + "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | + "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | + "X" | "Y" | "Z" | "[" | "]" | "^" | "_" | + "`" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | + "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | + "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | + "x" | "y" | "z" | "{" | "|" | "}" | "~") : Literal | + "\\" . ( + ("\"" | "'" | "n" | "r" | "t" | "\\" | "0") : Ascii | + "x" . oct_digit . hex_digit : Oct | + "u{" . up_to hex_digit (suc (suc (suc (suc (suc (suc zero)))))) . "}" : Unicode + ) : Escape ; + + let ws = plus Pattern_Whitespace; + let ows = opt ws; + + let list inner = !(/list/ inner . opt (ws . opt list)); + let separated inner sep = !(/separated/ inner . opt (sep . ows . separated)); + + let epsilon = "_"; + let ident = XID_Start . star XID_Continue; + let literal = "\"" . (plus literal_char : Contents) . "\""; + let fix term = "!" . ows . term; + let parens expr = "(" . ows . expr . ")"; + + let names = list ident; + + let term expr = !(/term/ + epsilon : Epsilon + | literal : Literal + | parens expr : Parens + | fix term : Fix + | ident : Variable + ); + + let label = ":" . ows . ident . ows; + + let call expr = list (term expr); + let cat expr = separated (call expr) "."; + let alt expr = separated (cat expr . opt label : Labelled) "|"; + let lambda expr = "/" . ows . names . "/" . ows . alt expr; + let expr = !(/expr/ alt expr | lambda expr); + let goal = "match" . ws . expr . ";" . ows; + let let stmt = "let" . ws . names . "=" . ows . expr . ";" . ows . stmt; + let stmt = !(/stmt/ let stmt | goal); + match !(/skip/ Pattern_Whitespace . skip | stmt); +} + +impl Convert for Ast { + fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> { + let mut inner = self; + + while let Ast::Branch1(cat) = inner { + inner = *cat.skip1; + } + + match inner { + Ast::Branch1(_) => unreachable!(), + Ast::Stmt1(stmt) => stmt.convert(context), + } + } +} + +impl Convert for Stmt1 { + fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> { + match self { + Stmt1::Goal1(goal) => goal.expr1.convert(context), + Stmt1::Let1(stmt) => { + let mut names = stmt.names1.into_iter().peekable(); + let name = Name::new_let(names.next().unwrap()); + let bound = if names.peek().is_none() { + stmt.expr1.convert(context)? + } else { + let args: Vec<Name> = names.map(Name::new_variable).collect(); + let expr = stmt.expr1; + let inner = context.with_variables(args.clone(), |ctx| expr.convert(ctx))?; + NamedExpression { + name: None, + expr: Lambda { + args, + inner: Box::new(inner), + } + .into(), + span: Span::call_site(), + } + }; + context.push_variable(name.clone()); + let body = stmt.stmt1.convert(context)?; + Ok(NamedExpression { + name: None, + expr: Let { + name: name.clone(), + bound: Box::new(NamedExpression { + name: Some(name), + ..bound + }), + body: Box::new(body), + } + .into(), + span: Span::call_site(), + }) + } + } + } +} + +impl Convert for Expr1 { + fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> { + match self { + Expr1::Alt1(alt) => alt.convert(context), + Expr1::Lambda1(lambda) => lambda.convert(context), + } + } +} + +impl Convert for Lambda1 { + fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> { + let args: Vec<Name> = self.names1.into_iter().map(Name::new_variable).collect(); + let alt = self.alt1; + let inner = context.with_variables(args.clone(), |ctx| alt.convert(ctx))?; + Ok(NamedExpression { + name: None, + expr: Lambda { + args, + inner: Box::new(inner), + } + .into(), + span: Span::call_site(), + }) + } +} + +impl Convert for Alt1 { + fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> { + let first = self.labelled1.convert(context)?; + let mut rest = self + .opt1 + .into_iter() + .map(|inner| inner.convert(context).map(|e| (Span::call_site(), e))) + .peekable(); + if rest.peek().is_some() { + Ok(NamedExpression { + name: None, + expr: Alt { + first: Box::new(first), + rest: rest.collect::<Result<_, _>>()?, + } + .into(), + span: Span::call_site(), + }) + } else { + Ok(first) + } + } +} + +impl Convert for Labelled1 { + fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> { + let named = self.cat1.convert(context)?; + let label = match self.opt1 { + Opt15::None1(_) => None, + Opt15::Label1(l) => Some(Name::new_label(l.ident1)), + }; + let name = Name::merge(label, named.name); + Ok(NamedExpression { + name, + expr: named.expr, + span: named.span, + }) + } +} + +impl Convert for Cat1 { + fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> { + let first = self.call1.convert(context)?; + let mut rest = self + .opt1 + .into_iter() + .map(|inner| inner.convert(context).map(|e| (Span::call_site(), e))) + .peekable(); + if rest.peek().is_some() { + Ok(NamedExpression { + name: None, + expr: Cat { + first: Box::new(first), + rest: rest.collect::<Result<_, _>>()?, + } + .into(), + span: Span::call_site(), + }) + } else { + Ok(first) + } + } +} + +impl Convert for Call1 { + fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> { + let first = self.term1.convert(context)?; + let mut rest = self + .opt1 + .into_iter() + .map(|inner| inner.convert(context)) + .peekable(); + if rest.peek().is_some() { + Ok(NamedExpression { + name: None, + expr: Call { + on: Box::new(first), + args: rest.collect::<Result<_, _>>()?, + } + .into(), + span: Span::call_site(), + }) + } else { + Ok(first) + } + } +} + +impl Convert for Term1 { + fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> { + match self { + Term1::Epsilon1(_) => Ok(NamedExpression { + name: None, + expr: ast::Epsilon.into(), + span: Span::call_site(), + }), + Term1::Literal1(literal) => Ok(NamedExpression { + name: None, + expr: literal.contents1.into_iter().collect::<Literal>().into(), + span: Span::call_site(), + }), + Term1::Parens1(parens) => parens.expr1.convert(context), + Term1::Fix1(fix) => fix.convert(context), + Term1::Variable1(var) => var.convert(context), + } + } +} + +impl Convert for Fix1 { + fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> { + let inner = self.term1.convert(context)?; + Ok(NamedExpression { + name: None, + expr: Fix { + inner: Box::new(inner), + } + .into(), + span: Span::call_site(), + }) + } +} + +impl Convert for Variable1 { + fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> { + let name = Name::new_variable(self); + let index = context + .lookup(&name) + .ok_or_else(|| ConvertError::UndeclaredName(Box::new(name.clone())))?; + + Ok(NamedExpression { + name: Some(name), + expr: Variable { index }.into(), + span: Span::call_site(), + }) + } +} + +impl IntoIterator for Names1 { + type Item = Ident1; + + type IntoIter = Opt3; + + fn into_iter(self) -> Self::IntoIter { + Opt3::List1(Box::new(self)) + } +} + +impl Iterator for Opt3 { + type Item = Ident1; + + fn next(&mut self) -> Option<Self::Item> { + let orig = mem::replace(self, Opt3::None1(Epsilon)); + match orig { + Opt3::None1(_) => None, + Opt3::List1(names) => { + if let Opt4::Some1(some) = names.opt1 { + *self = some.opt1; + } + Some(names.ident1) + } + } + } +} + +impl Iterator for Opt16 { + type Item = Labelled1; + + fn next(&mut self) -> Option<Self::Item> { + let orig = mem::replace(self, Opt16::None1(Epsilon)); + match orig { + Opt16::None1(_) => None, + Opt16::Some1(some) => { + *self = some.separated1.opt1; + Some(some.separated1.labelled1) + } + } + } +} + +impl Iterator for Opt14 { + type Item = Call1; + + fn next(&mut self) -> Option<Self::Item> { + let orig = mem::replace(self, Opt14::None1(Epsilon)); + match orig { + Opt14::None1(_) => None, + Opt14::Some1(some) => { + *self = some.separated1.opt1; + Some(some.separated1.call1) + } + } + } +} + +impl Iterator for Opt13 { + type Item = Term1; + + fn next(&mut self) -> Option<Self::Item> { + let orig = mem::replace(self, Opt13::None1(Epsilon)); + match orig { + Opt13::None1(_) => None, + Opt13::Some1(some) => match some.opt1 { + Opt12::None1(_) => None, + Opt12::List1(call) => { + *self = call.opt1; + Some(call.term1) + } + }, + } + } +} + +impl IntoIterator for Contents1 { + type Item = char; + + type IntoIter = Opt11; + + fn into_iter(self) -> Self::IntoIter { + Opt11::Plus1(Box::new(self)) + } +} + +impl Iterator for Opt11 { + type Item = char; + + fn next(&mut self) -> Option<Self::Item> { + let orig = mem::replace(self, Opt11::None1(Epsilon)); + match orig { + Opt11::None1(_) => None, + Opt11::Plus1(contents) => { + *self = contents.opt1; + Some(contents.literal_char1.into()) + } + } + } +} + +impl From<LiteralChar1> for char { + fn from(c: LiteralChar1) -> Self { + match c { + LiteralChar1::Literal1(c) => c.into(), + LiteralChar1::Escape1(e) => e.into(), + } + } +} + +impl From<Escape1> for char { + fn from(e: Escape1) -> Self { + match e.1 { + Alt171::Ascii1(a) => a.escape(), + Alt171::Oct1(o) => o.into(), + Alt171::Unicode1(u) => u.into(), + } + } +} + +impl Ascii1 { + fn escape(self) -> char { + match self { + Ascii1::Branch1(_) => '\"', + Ascii1::Branch2(_) => '\'', + Ascii1::Branch3(_) => '\n', + Ascii1::Branch4(_) => '\r', + Ascii1::Branch5(_) => '\t', + Ascii1::Branch6(_) => '\\', + Ascii1::Branch7(_) => '\0', + } + } +} + +impl From<Oct1> for char { + fn from(o: Oct1) -> Self { + let s: String = [char::from(o.oct_digit1), char::from(o.hex_digit1)] + .iter() + .collect(); + u32::from_str_radix(&s, 16).unwrap().try_into().unwrap() + } +} + +impl From<Unicode1> for char { + fn from(u: Unicode1) -> Self { + let s = u.up_to1.to_string(); + u32::from_str_radix(&s, 16).unwrap().try_into().unwrap() + } +} + +impl From<Variable1> for Content { + fn from(i: Variable1) -> Self { + i.to_string().into() + } +} + +impl From<Ident2> for Content { + fn from(i: Ident2) -> Self { + i.to_string().into() + } +} + +impl From<Ident1> for Content { + fn from(i: Ident1) -> Self { + i.to_string().into() + } +} diff --git a/autonibble/src/main.rs b/autonibble/src/main.rs new file mode 100644 index 0000000..56c757d --- /dev/null +++ b/autonibble/src/main.rs @@ -0,0 +1,61 @@ +use std::{ + error::Error, + io::{self, Read, Write}, + process::exit, +}; + +use chewed::{IterWrapper, Parser}; +use chomp::{ + chomp::{ + ast::substitute::Reduce, + typed::{ + context::Context, + lower::{Backend, GenerateCode}, + TypeInfer, + }, + visit::Visitable, + }, + lower::RustBackend, + nibble::convert::{self, Convert}, +}; +use proc_macro2::Span; + +fn main() { + let mut input = String::new(); + let res = io::stdin() + .read_to_string(&mut input) + .map_err(|e| Box::new(e) as Box<dyn Error>) + .and_then(|_| { + IterWrapper::new(input.chars()) + .parse::<autonibble::Ast>() + .map_err(|e| Box::new(e) as Box<dyn Error>) + }) + .and_then(|ast| { + ast.convert(&mut convert::Context::default()) + .map_err(|e| Box::new(e) as Box<dyn Error>) + }) + .and_then(|expr| { + expr.fold(&mut Reduce) + .map_err(|e| Box::new(e) as Box<dyn Error>) + }) + .and_then(|term| { + let mut context = Context::default(); + term.fold(&mut TypeInfer { + context: &mut context, + }) + .map_err(|e| Box::new(e) as Box<dyn Error>) + }) + .map(|typed| { + let mut backend = RustBackend::default(); + let id = typed.gen(&mut backend); + backend.emit_code(None, Span::call_site(), id) + }) + .and_then(|code| { + write!(io::stdout(), "{:#}", code).map_err(|e| Box::new(e) as Box<dyn Error>) + }); + + if let Err(e) = res { + eprintln!("{}", e); + exit(1) + } +} diff --git a/autonibble/tests/compare/main.rs b/autonibble/tests/compare/main.rs new file mode 100644 index 0000000..014cfec --- /dev/null +++ b/autonibble/tests/compare/main.rs @@ -0,0 +1,39 @@ +use chewed::{IterWrapper, Parser}; +use chomp::{ + chomp::ast::NamedExpression, + nibble::{ + self, + convert::{Context, Convert}, + }, +}; + +fn chomp(input: &str) -> NamedExpression { + syn::parse_str::<nibble::Statement>(&input) + .unwrap() + .convert(&mut Context::default()) + .unwrap() +} + +fn autonibble(input: &str) -> NamedExpression { + IterWrapper::new(input.chars()) + .parse::<autonibble::Ast>() + .unwrap() + .convert(&mut Context::default()) + .unwrap() +} + +macro_rules! compare { + ($name:ident, $file:literal) => { + #[test] + fn $name() { + let input = include_str!($file); + assert_eq!(chomp(input), autonibble(input)) + } + }; +} + +compare!(compare_sheep, "nibble/sheep.nb"); +compare!(compare_ratata, "nibble/ratata.nb"); +compare!(compare_regex, "nibble/regex.nb"); +compare!(compare_regex_fix, "nibble/regex_fix.nb"); +compare!(compare_nibble, "nibble/nibble_exp.nb"); diff --git a/autonibble/tests/compare/nibble b/autonibble/tests/compare/nibble new file mode 120000 index 0000000..bfe7e8e --- /dev/null +++ b/autonibble/tests/compare/nibble @@ -0,0 +1 @@ +../../../tests/full/nibble
\ No newline at end of file |