Skip to content

Commit

Permalink
Fixing no_std builds
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbor committed Oct 20, 2024
1 parent 572d13a commit 30e195e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/debruijn.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
use std::{collections::HashMap, fmt::Display};
extern crate alloc;

use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::format;
use alloc::string::String;
use alloc::string::ToString;
use core::fmt::Display;

use crate::lambda::{LambdaNode, LambdaTree};

Expand All @@ -11,7 +19,7 @@ pub enum DeBruijnNode {
Application(Box<DeBruijnNode>, Box<DeBruijnNode>),
}

fn to_debrujin_helper(l: LambdaTree, map: &mut HashMap<String, usize>, depth: usize) -> DeBruijnNode {
fn to_debrujin_helper(l: LambdaTree, map: &mut BTreeMap<String, usize>, depth: usize) -> DeBruijnNode {
match l.node() {
LambdaNode::Variable(name) => {
if map.contains_key(name.as_str()) {
Expand Down Expand Up @@ -42,12 +50,12 @@ fn to_debrujin_helper(l: LambdaTree, map: &mut HashMap<String, usize>, depth: us

impl From<LambdaTree> for DeBruijnNode {
fn from(value: LambdaTree) -> Self {
to_debrujin_helper(value, &mut HashMap::new(), 0)
to_debrujin_helper(value, &mut BTreeMap::new(), 0)
}
}

impl Display for DeBruijnNode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use DeBruijnNode::*;
match self {
FreeVariable(name) => write!(f, "{}", name),
Expand Down
1 change: 1 addition & 0 deletions src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Default for StdEnvironment {
}
}

#[cfg(feature = "std")]
impl StdEnvironment {
pub fn new() -> Self {
StdEnvironment {
Expand Down
7 changes: 4 additions & 3 deletions src/macro.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate alloc;

use alloc::borrow::ToOwned;
use alloc::string::ToString;
use alloc::vec::Vec;
use core::fmt::{Display, Write};
Expand Down Expand Up @@ -77,14 +78,14 @@ impl Macro {
let mut stdout = interpreter.env().stdout();
let term = match self {
AlphaEq => if terms[0].alpha_eq(&terms[1]) {
println!("Terms are alpha equivalent");
writeln!(stdout, "Terms are alpha equivalent")?;
LambdaTree::new_abstraction("x".to_owned(),
LambdaTree::new_abstraction("y".to_owned(),
LambdaTree::new_variable("x".to_owned())
)
)
} else {
println!("Terms are NOT alpha equivalent");
writeln!(stdout, "Terms are NOT alpha equivalent")?;
LambdaTree::new_abstraction("x".to_owned(),
LambdaTree::new_abstraction("y".to_owned(),
LambdaTree::new_variable("y".to_owned())
Expand All @@ -97,7 +98,7 @@ impl Macro {
term
},
DeBruijn => {
println!("{}", DeBruijnNode::from(terms[0].clone()));
writeln!(stdout, "{}", DeBruijnNode::from(terms[0].clone()))?;
terms[0].clone()
},
Debug => {
Expand Down

0 comments on commit 30e195e

Please sign in to comment.