diff options
author | Greg Brown <gmb60@cam.ac.uk> | 2021-01-07 12:44:06 +0000 |
---|---|---|
committer | Greg Brown <gmb60@cam.ac.uk> | 2021-01-07 12:44:06 +0000 |
commit | fe2eac31d9dbec772796c3ea75be32e9cd01b810 (patch) | |
tree | ad0c0ad42ce986d3dd915512de8c18968194c15a /autochomp | |
parent | eb280a903f8f20d0b0c0ef5acae955a20929d100 (diff) |
Add first steps of AutoChomp
Diffstat (limited to 'autochomp')
-rw-r--r-- | autochomp/Cargo.toml | 15 | ||||
-rw-r--r-- | autochomp/build.rs | 73 | ||||
-rw-r--r-- | autochomp/src/main.rs | 23 | ||||
-rw-r--r-- | autochomp/src/nibble.nb | 67 |
4 files changed, 178 insertions, 0 deletions
diff --git a/autochomp/Cargo.toml b/autochomp/Cargo.toml new file mode 100644 index 0000000..4e20c49 --- /dev/null +++ b/autochomp/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "autochomp" +version = "0.1.0" +authors = ["Greg Brown <gmb60@cam.ac.uk>"] +edition = "2018" + +[build-dependencies] +chomp = {path = ".."} + +[build-dependencies.syn] +version = "1" +features = ["extra-traits"] + +[dependencies] +chewed = {path = "../chewed"} diff --git a/autochomp/build.rs b/autochomp/build.rs new file mode 100644 index 0000000..952d49c --- /dev/null +++ b/autochomp/build.rs @@ -0,0 +1,73 @@ +use std::{ + env, + error::Error, + fmt::Display, + fs, + io::{Read, Write}, + path::Path, + process::exit, +}; + +use chomp::{ + chomp::{ + check::{InlineCall, TypeCheck}, + context::Context, + visit::Visitable, + }, + lower::{rust::RustBackend, Backend, GenerateCode}, + nibble::cst::File, +}; + +const PATH: &str = "src/nibble.nb"; + +#[derive(Debug)] +struct UndecVar; + +impl Display for UndecVar { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Undeclared variable somewhere.") + } +} +impl Error for UndecVar {} + +fn main() { + println!("cargo:rerun-if-changed={}", PATH); + + let out_dir = env::var("OUT_DIR").unwrap(); + + let mut input = String::new(); + let res = fs::File::open(PATH) + .map_err(|e| Box::new(e) as Box<dyn Error>) + .and_then(|mut file| file.read_to_string(&mut input).map_err(|e| Box::new(e) as Box<dyn Error>)) + .and_then(|_| syn::parse_str(&input).map_err(|e| Box::new(e) as Box<dyn Error>)) + .and_then(|nibble: File| nibble.convert().ok_or(Box::new(UndecVar) as Box<dyn Error>)) + .and_then(|(funs, goal)| { + funs.into_iter() + .try_rfold(goal, |goal, function| { + goal.fold(&mut InlineCall::new(function)) + }) + .map_err(|e| Box::new(e) as Box<dyn Error>) + }) + .and_then(|term| { + let mut context = Context::default(); + term.fold(&mut TypeCheck { + 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(id) + }) + .and_then(|code| { + fs::File::create(Path::new(&out_dir).join("nibble.rs")) + .and_then(|mut f| write!(f, "{}", code)) + .map_err(|e| Box::new(e) as Box<dyn Error>) + }); + + if let Err(e) = res { + eprintln!("{}", e); + exit(1) + } +} diff --git a/autochomp/src/main.rs b/autochomp/src/main.rs new file mode 100644 index 0000000..400cc2d --- /dev/null +++ b/autochomp/src/main.rs @@ -0,0 +1,23 @@ +use std::{error::Error, io::{self, Read, Write}, process::exit}; + +use chewed::Parse; + +mod nibble { + include!(concat!(env!("OUT_DIR"), "/nibble.rs")); +} + +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(|_| nibble::Ast::parse(input.chars().peekable()).map_err(|e| Box::new(e) as Box<dyn Error>)) + .and_then(|ast| { + write!(io::stdout(), "{:?}", ast).map_err(|e| Box::new(e) as Box<dyn Error>) + }); + + if let Err(e) = res { + eprintln!("{}", e); + exit(1) + } +} diff --git a/autochomp/src/nibble.nb b/autochomp/src/nibble.nb new file mode 100644 index 0000000..fed3d46 --- /dev/null +++ b/autochomp/src/nibble.nb @@ -0,0 +1,67 @@ +let opt(x) = _ | x; +let plus(x) = [plus](x . opt(plus)); +let star(x) = opt(plus(x)); +let star_(base, step) = [rec](base | step . rec); + +let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}"; + +let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"; +let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7"; +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" | "{" | "|" | "}" | "~" | + "\\" . ("\"" | "'" | "n" | "r" | "t" | "\\" | "0" | + "x" . oct_digit . hex_digit | + "u{" . hex_digit . opt(hex_digit . opt(hex_digit . opt(hex_digit . opt(hex_digit . opt(hex_digit))))) . "}"); + +let ws = plus(Pattern_Whitespace); + +let punctuated(x, p) = [rec](x . opt(p . opt(ws) . opt(rec))); +let list(x) = "(" . opt(ws) . punctuated(x, ",") . ")"; + +let epsilon = "_"; +let ident = XID_Start . star(XID_Continue); +let literal = "\"" . plus(literal_char) . "\""; +let parens(expr) = "(" . opt(ws) . expr . ")"; +let fix(expr) = "[" . opt(ws) . ident . opt(ws) . "]" . opt(ws) . parens(expr); + +let term(expr) = + epsilon . opt(ws) + | literal . opt(ws) + | parens(expr) . opt(ws) + | fix(expr) . opt(ws) + | ident . opt(ws) . opt(list(expr) . opt(ws)) + ; + +let seq(expr) = punctuated(term(expr), "."); +let alt(expr) = punctuated(seq(expr), "|"); +let expr = [expr](alt(expr)); +let let = "let" . ws . ident . opt(ws) . opt(list(ident . opt(ws)) . opt(ws)) . "=" . opt(ws) . expr . ";"; +let goal = "match" . ws . expr . ";"; +match star_(goal, let); |