wip: compiles and runs (not enabled yet)

This commit is contained in:
🪞👃🪞 2025-01-04 11:19:37 +01:00
parent ac3827b8f3
commit 98d2107e4e
15 changed files with 440 additions and 357 deletions

60
edn/src/edn_item.rs Normal file
View file

@ -0,0 +1,60 @@
use crate::*;
fn number (digits: &str) -> usize {
let mut value = 0;
for c in digits.chars() {
value = 10 * value + digit(c);
}
value
}
const fn digit (c: char) -> usize {
match c { '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, _ => unreachable!() }
}
#[derive(Debug)]
pub enum ParseError {
Empty,
Unexpected(char),
Incomplete
}
#[derive(Debug, Clone, Default, PartialEq)]
pub enum Item<T: AsRef<str>> {
#[default] Nil,
Num(usize),
Sym(T),
Key(T),
Exp(Vec<Item<T>>),
}
impl Item<String> {
pub fn read_all <'a> (mut source: &'a str) -> Result<Vec<Self>, ParseError> {
let mut items = vec![];
loop {
if source.len() == 0 {
break
}
let (remaining, token) = Token::chomp(source)?;
match Item::read(token)? { Item::Nil => {}, item => items.push(item) };
source = remaining
}
Ok(items)
}
pub fn read <'a> (token: Token<'a>) -> Result<Self, ParseError> {
use Token::*;
Ok(match token {
Nil => Item::Nil,
Num(chars, index, length) =>
Self::Num(number(&chars[index..index+length])),
Sym(chars, index, length) =>
Self::Sym(chars[index..index+length].to_string()),
Key(chars, index, length) =>
Self::Key(chars[index..index+length].to_string()),
Exp(chars, index, length, 0) =>
Self::Exp(Self::read_all(&chars[index+1..(index+length).saturating_sub(1)])?),
_ => panic!("unclosed delimiter")
})
}
}