Skip to content

Commit

Permalink
NilTrees can now store a number representation for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkos-net committed May 9, 2024
1 parent 64ea07d commit 5300096
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/extended_to_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn num_to_core(n: usize) -> Expression {
res
}

pub fn num_to_niltree(n: usize) -> NilTree {
pub fn num_to_nils(n: usize) -> NilTree {
if n == 0 {
NilTree::Nil
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use indexmap::IndexMap;
use regex::Regex;

use crate::{
extended_to_core::{list_to_cons, num_to_niltree, prog_to_core, switch_to_ifs},
extended_to_core::{list_to_cons, prog_to_core, switch_to_ifs},
lang::{Block, Expression, Prog, ProgName, Statement},
niltree::{cons, NilTree},
parser::expression,
Expand Down Expand Up @@ -107,7 +107,7 @@ fn eval(expr: &Expression, store: &ExecState) -> NilTree {
E::Tl(e) => eval(e, store).tl(),
E::Nil => NilTree::Nil,
E::Var(var) => store.get(var).clone(),
E::Num(n) => num_to_niltree(*n),
E::Num(n) => NilTree::Num(*n),
E::Bool(b) => match b {
true => cons(NilTree::Nil, NilTree::Nil),
false => NilTree::Nil,
Expand Down
9 changes: 9 additions & 0 deletions src/niltree.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::fmt::Display;

use crate::extended_to_core::num_to_nils;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum NilTree {
Nil,
List(Vec<NilTree>),
Num(usize),
}

impl NilTree {
Expand All @@ -26,6 +29,10 @@ impl NilTree {

(h, t)
}
NilTree::Num(n) => match n {
0 => (NilTree::Nil, NilTree::Nil),
n => (NilTree::Nil, NilTree::Num(n - 1)),
},
}
}
}
Expand All @@ -37,6 +44,7 @@ pub fn cons(a: NilTree, b: NilTree) -> NilTree {
v.push(a);
NilTree::List(v)
}
NilTree::Num(n) => cons(a, num_to_nils(n)),
}
}

Expand All @@ -60,6 +68,7 @@ impl Display for NilTree {

s.fmt(f)
}
NilTree::Num(n) => num_to_nils(*n).fmt(f),
}
}
}
1 change: 1 addition & 0 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub fn parse_num(tree: &NilTree) -> anyhow::Result<usize> {
bail!("NaN")
}
}
NilTree::Num(n) => *n,
})
}

Expand Down
7 changes: 3 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use regex::Regex;

use crate::{
atoms::Atom,
extended_to_core::num_to_core,
lang::{Block, Expression, Prog, ProgName, Statement},
variables::VarName,
};
Expand Down Expand Up @@ -325,7 +324,7 @@ fn brackets_expr(s: &str) -> IResult<&str, Expression, VerboseError<&str>> {

fn atom_expr(s: &str) -> IResult<&str, Expression, VerboseError<&str>> {
map_res(preceded(tag("@"), alt((alpha1, tag(":=")))), |atom| {
Atom::from_str(format!("@{atom}").as_str()).map(|atom| num_to_core(atom as u8 as usize))
Atom::from_str(format!("@{atom}").as_str()).map(|atom| Expression::Num(atom as u8 as usize))
})(s)
}

Expand All @@ -347,7 +346,7 @@ fn tree_literal_expr(s: &str) -> IResult<&str, Expression, VerboseError<&str>> {

#[cfg(test)]
mod tests {
use crate::extended_to_core::num_to_niltree;
use crate::extended_to_core::num_to_nils;
use crate::parser::Expression;
use crate::parser::{parse, Block, Prog, ProgName, VarName};

Expand Down Expand Up @@ -497,7 +496,7 @@ mod tests {
#[test]
fn test_stack_overflow() {
let n = 1_000_000;
let n = num_to_niltree(n);
let n = num_to_nils(n);
let m = n.clone();
let _ = m == n;
}
Expand Down

0 comments on commit 5300096

Please sign in to comment.