diff options
Diffstat (limited to 'autonibble/src/main.rs')
-rw-r--r-- | autonibble/src/main.rs | 61 |
1 files changed, 61 insertions, 0 deletions
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) + } +} |