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

View file

@ -2,122 +2,6 @@ use std::sync::{Arc, RwLock};
use std::collections::BTreeMap;
pub use clojure_reader::edn::Edn;
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")
})
}
}
#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub enum Token<'a> {
#[default] Nil,
Num(&'a str, usize, usize),
Sym(&'a str, usize, usize),
Key(&'a str, usize, usize),
Exp(&'a str, usize, usize, usize),
}
impl<'a> Token<'a> {
fn chomp (source: &'a str) -> Result<(&'a str, Self), ParseError> {
use Token::*;
let mut state = Self::default();
for (index, c) in source.char_indices() {
state = match state {
// must begin expression
Nil => match c {
' '|'\n'|'\r'|'\t' => Nil,
'(' => Exp(source, index, 1, 1),
':' => Sym(source, index, 1),
'0'..='9' => Num(source, index, 1),
'a'..='z' => Key(source, index, 1),
_ => return Err(ParseError::Unexpected(c))
},
Num(_, _, 0) => unreachable!(),
Sym(_, _, 0) => unreachable!(),
Key(_, _, 0) => unreachable!(),
Num(source, index, length) => match c {
'0'..='9' => Num(source, index, length + 1),
' '|'\n'|'\r'|'\t' => return Ok((&source[index+length..], Num(source, index, length))),
_ => return Err(ParseError::Unexpected(c))
},
Sym(source, index, length) => match c {
'a'..='z'|'0'..='9'|'-' => Sym(source, index, length + 1),
' '|'\n'|'\r'|'\t' => return Ok((&source[index+length..], Sym(source, index, length))),
_ => return Err(ParseError::Unexpected(c))
},
Key(source, index, length) => match c {
'a'..='z'|'0'..='9'|'-'|'/' => Key(source, index, length + 1),
' '|'\n'|'\r'|'\t' => return Ok((&source[index+length..], Key(source, index, length))),
_ => return Err(ParseError::Unexpected(c))
},
Exp(source, index, length, 0) => match c {
' '|'\n'|'\r'|'\t' => return Ok((&source[index+length..], Exp(source, index, length, 0))),
_ => return Err(ParseError::Unexpected(c))
},
Exp(source, index, length, depth) => match c {
')' => Exp(source, index, length + 1, depth - 1),
'(' => Exp(source, index, length + 1, depth + 1),
_ => Exp(source, index, length + 1, depth)
},
}
}
Ok(("", state))
}
}
//#[derive(Debug, Copy, Clone, Default, PartialEq)]
//pub struct Items<'a>(&'a [Item<'a>]);