Skip to content

Commit

Permalink
recover syntax interning roundtrip proptest
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpaulino committed Aug 9, 2023
1 parent b8ebcbc commit a709ee4
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 169 deletions.
1 change: 1 addition & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum Expression<F: LurkField> {
Str(Ptr<F>, Ptr<F>),
Thunk(Thunk<F>),
RootSym,
RootKey,
Sym(Ptr<F>, Ptr<F>),
Key(Ptr<F>, Ptr<F>),
Char(char),
Expand Down
1 change: 1 addition & 0 deletions src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum ParseErrorKind<F: LurkField> {
ParseIntErr(ParseIntError),
InvalidChar(String),
Nom(ErrorKind),
InterningError(String),
}

impl<F: LurkField> fmt::Display for ParseErrorKind<F> {
Expand Down
19 changes: 14 additions & 5 deletions src/parser/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,21 @@ fn intern_path<'a, F: LurkField>(
path: &[String],
keyword: Option<bool>,
) -> ParseResult<'a, F, SymbolRef> {
use nom::Err::Failure;
match keyword {
Some(keyword) => match state.borrow_mut().intern_path(&path, keyword) {
Some(keyword) => match state.borrow_mut().intern_path(path, keyword) {
Ok(symbol) => Ok((upto, symbol)),
Err(e) => todo!(),
Err(e) => Err(Failure(ParseError::new(
upto,
ParseErrorKind::InterningError(format!("{e}")),
))),
},
None => match state.borrow_mut().intern_relative_path(&path) {
None => match state.borrow_mut().intern_relative_path(path) {
Ok(symbol) => Ok((upto, symbol)),
Err(e) => todo!(),
Err(e) => Err(Failure(ParseError::new(
upto,
ParseErrorKind::InterningError(format!("{e}")),
))),
},
}
}
Expand Down Expand Up @@ -168,7 +175,7 @@ pub fn parse_symbol<F: LurkField>(
parse_absolute_symbol(state.clone()),
parse_relative_symbol(state.clone()),
))(from)?;
Ok((upto, Syntax::Symbol(Pos::from_upto(from, upto), sym.into())))
Ok((upto, Syntax::Symbol(Pos::from_upto(from, upto), sym)))
}
}

Expand Down Expand Up @@ -298,12 +305,14 @@ pub fn parse_list<F: LurkField>(
move |from: Span<'_>| {
let (i, _) = tag("(")(from)?;
let (i, xs) = if meta {
// parse the head symbol in the meta package
let saved_package = state.borrow().get_current_package_name().clone();
state
.borrow_mut()
.set_current_package(meta_package_symbol().into())
.expect("meta package is available");
let (i, h) = preceded(parse_space, parse_symbol(state.clone()))(i)?;
// then recover the previous package
state
.borrow_mut()
.set_current_package(saved_package)
Expand Down
51 changes: 33 additions & 18 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,17 +703,22 @@ impl<F: LurkField> Store<F> {
}

pub fn fetch_symbol(&self, ptr: &Ptr<F>) -> Option<Symbol> {
if ptr.tag == ExprTag::Nil {
return Some(lurk_sym("nil"));
}
let mut ptr = *ptr;
let mut path = Vec::new();
while let Some((car, cdr)) = self.fetch_symcons(&ptr) {
let string = self.fetch_string(&car)?;
path.push(string);
ptr = cdr
match ptr.tag {
ExprTag::Nil => Some(lurk_sym("nil")),
ExprTag::Sym | ExprTag::Key => {
let is_key = ptr.tag == ExprTag::Key;
let mut ptr = *ptr;
let mut path = Vec::new();
while let Some((car, cdr)) = self.fetch_symcons(&ptr) {
let string = self.fetch_string(&car)?;
path.push(string);
ptr = cdr
}
path.reverse();
Some(Symbol::new_from_vec(path, is_key))
}
_ => None,
}
Some(Symbol::sym(&path.into_iter().rev().collect::<Vec<_>>()))
}

pub fn fetch_strcons(&self, ptr: &Ptr<F>) -> Option<(Ptr<F>, Ptr<F>)> {
Expand Down Expand Up @@ -807,6 +812,7 @@ impl<F: LurkField> Store<F> {
ExprTag::Sym => self
.fetch_symcons(ptr)
.map(|(car, cdr)| Expression::Sym(car, cdr)),
ExprTag::Key if ptr.raw.is_null() => Some(Expression::RootKey),
ExprTag::Key => self
.fetch_symcons(ptr)
.map(|(car, cdr)| Expression::Key(car, cdr)),
Expand Down Expand Up @@ -1019,6 +1025,10 @@ impl<F: LurkField> Store<F> {
ZExpr::RootSym.z_ptr(&self.poseidon_cache),
Some(ZExpr::RootSym),
),
Some(Expression::RootKey) => (
ZExpr::RootKey.z_ptr(&self.poseidon_cache),
Some(ZExpr::RootKey),
),
Some(Expression::Sym(car, cdr)) => {
let (z_car, _) = self.get_z_expr(&car, z_store)?;
let (z_cdr, _) = self.get_z_expr(&cdr, z_store)?;
Expand Down Expand Up @@ -1494,6 +1504,11 @@ impl<F: LurkField> Store<F> {
self.create_z_expr_ptr(ptr, *z_ptr.value());
Some(ptr)
}
(ExprTag::Key, Some(RootSym)) => {
let ptr = self.intern_symnil(true);
self.create_z_expr_ptr(ptr, *z_ptr.value());
Some(ptr)
}
(ExprTag::Sym, Some(Sym(symcar, symcdr))) => {
let symcar = self.intern_z_expr_ptr(&symcar, z_store)?;
let symcdr = self.intern_z_expr_ptr(&symcdr, z_store)?;
Expand Down Expand Up @@ -1546,7 +1561,7 @@ impl<F: LurkField> Store<F> {
Some(ptr)
}
_ => {
//println!("Failed to get ptr for zptr: {:?}", z_ptr);
// println!("Failed to get ptr for zptr: {:?}", z_ptr);
None
}
}
Expand Down Expand Up @@ -1687,9 +1702,9 @@ impl<F: LurkField> Store<F> {
}

impl<F: LurkField> Expression<F> {
pub fn is_keyword_sym(&self) -> bool {
matches!(self, Expression::Key(_, _))
}
// pub fn is_keyword_sym(&self) -> bool {
// matches!(self, Expression::Key(_, _))
// }

pub const fn as_str(&self) -> Option<&str> {
match self {
Expand Down Expand Up @@ -1915,11 +1930,11 @@ impl<F: LurkField> ZStore<F> {
pub fn to_store_with_z_ptr(&self, z_ptr: &ZExprPtr<F>) -> Result<(Store<F>, Ptr<F>), Error> {
let mut store = Store::new();

for ptr in self.expr_map.keys() {
store.intern_z_expr_ptr(ptr, self);
for z_ptr in self.expr_map.keys() {
store.intern_z_expr_ptr(z_ptr, self);
}
for ptr in self.cont_map.keys() {
store.intern_z_cont_ptr(ptr, self);
for z_ptr in self.cont_map.keys() {
store.intern_z_cont_ptr(z_ptr, self);
}
match store.intern_z_expr_ptr(z_ptr, self) {
Some(ptr_ret) => Ok((store, ptr_ret)),
Expand Down
10 changes: 5 additions & 5 deletions src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Symbol {
}
}

pub fn new_of_vec(path: Vec<String>, keyword: bool) -> Self {
pub fn new_from_vec(path: Vec<String>, keyword: bool) -> Self {
Self { path, keyword }
}

Expand All @@ -94,13 +94,13 @@ impl Symbol {
}

#[inline]
pub fn sym_of_vec(path: Vec<String>) -> Self {
Self::new_of_vec(path, false)
pub fn sym_from_vec(path: Vec<String>) -> Self {
Self::new_from_vec(path, false)
}

#[inline]
pub fn key_of_vec(path: Vec<String>) -> Self {
Self::new_of_vec(path, true)
pub fn key_from_vec(path: Vec<String>) -> Self {
Self::new_from_vec(path, true)
}

/// Creates a new Symbol with the path extended by the given vector of path segments.
Expand Down
Loading

0 comments on commit a709ee4

Please sign in to comment.