diff --git a/Cargo.lock b/Cargo.lock index d8338a3df6095f05643e8d82574e9f7f3dc44d7f..3a7cae61da2977ab8ef52d13b116ad8d243b6331 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,48 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "sophiescript" version = "0.1.0" +dependencies = [ + "bitvec", +] + +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] diff --git a/Cargo.toml b/Cargo.toml index 07b4cfa2a2b9d9f880de2ee0958019cce4594955..10f50da99380834144df0e284d460302ffb8673e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +bitvec = "1.0.1" diff --git a/src/expr.rs b/src/expr.rs index c1652e2e54cbab0d3d5c4599f31ee65d1c3efcaa..42eef3419131b5eacb5c99f9a44463a16bf64aea 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1,3 +1,5 @@ +use bitvec::{order::Msb0, vec::BitVec}; + #[derive(Debug)] pub enum Expr { Abs(Box<Expr>), @@ -12,3 +14,30 @@ pub enum FlattenedExpr { App(Box<FlattenedExpr>, Box<FlattenedExpr>), Var(usize), } + +impl FlattenedExpr { + pub fn to_blc(self, bv: &mut BitVec<u8, Msb0>) { + match self { + FlattenedExpr::Abs(expr) => { + bv.push(false); + bv.push(false); + + expr.to_blc(bv); + } + FlattenedExpr::App(left, right) => { + bv.push(false); + bv.push(true); + + left.to_blc(bv); + right.to_blc(bv); + } + FlattenedExpr::Var(x) => { + for _ in 0..=x { + bv.push(true); + } + + bv.push(false); + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 5c2028210b9c56d6f34ce09e50690eb930663218..45cec789815eb4e72331d21083b23943caa6fc2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use bitvec::vec::BitVec; use flatten::Flattener; use parser::Parser; use scanner::Scanner; @@ -11,10 +12,11 @@ mod token; fn main() { let scanner = Scanner::new( " -$false = λtf.f -$true = λtf.t +$false=λtf.f +$omega=λa. (a a) -λa.(a $true) + +λa.a($omega(λbcde.d(bb)(λf.fce)))$false ", ); @@ -24,7 +26,14 @@ $true = λtf.t let (funcs, main) = parser.parse().unwrap(); let mut flattener = Flattener::new(funcs); - let flat = flattener.flatten(&main); + let flat = flattener.flatten(&main).unwrap(); + + dbg!(&flat); + + let mut bv = BitVec::new(); + flat.to_blc(&mut bv); - dbg!(flat); + for x in bv.as_raw_slice() { + print!("{}", *x as char); + } } diff --git a/src/parser.rs b/src/parser.rs index 076388a2c7ae9ce63e75bcea0755e1d5a7e66512..576b45ff9196ccbd7191c2539bb1e9dd567b041a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -134,9 +134,6 @@ impl Parser { let left = self.expr()?; let right = self.expr()?; - dbg!(&left, &right); - dbg!(&self.peek().token_type); - if self.peek().token_type != TokenType::RightParen { self.print_error("Expected ')' after application"); return None;