From c472f46cda2c54b28c91fcec14fe5b77fc47a20f Mon Sep 17 00:00:00 2001 From: Stephen D <webmaster@scd31.com> Date: Sun, 9 Mar 2025 12:16:21 -0400 Subject: [PATCH] blc output --- Cargo.lock | 42 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/expr.rs | 29 +++++++++++++++++++++++++++++ src/main.rs | 19 ++++++++++++++----- src/parser.rs | 3 --- 5 files changed, 86 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d8338a3..3a7cae6 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 07b4cfa..10f50da 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 c1652e2..42eef34 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 5c20282..45cec78 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 076388a..576b45f 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; -- GitLab