diff options
author | Greg Brown <gmb60@cam.ac.uk> | 2021-05-11 13:53:56 +0100 |
---|---|---|
committer | Greg Brown <gmb60@cam.ac.uk> | 2021-05-11 13:53:56 +0100 |
commit | 387959675cd53b3c75ad9b6215b07843f8c8f1d8 (patch) | |
tree | bc972c31a0b48f8fd8c9240465c8bd83e16f5258 /autonibble/src/main.rs | |
parent | dfc08ff2c6580bbeb3951b223e0332546ba3b0d9 (diff) |
Rename autochomp to autonibble.
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) + } +} |