Skip to content

Commit

Permalink
Remove empty tuple from SExp (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity authored Aug 10, 2023
1 parent 999c4df commit f7b6331
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 33 deletions.
26 changes: 13 additions & 13 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bls12_381::{G1Affine, G1Projective, G2Affine, G2Projective};
pub type NodePtr = i32;

pub enum SExp {
Atom(),
Atom,
Pair(NodePtr, NodePtr),
}

Expand Down Expand Up @@ -252,7 +252,7 @@ impl Allocator {

pub fn g1(&self, node: NodePtr) -> Result<G1Projective, EvalErr> {
let blob = match self.sexp(node) {
SExp::Atom() => self.atom(node),
SExp::Atom => self.atom(node),
_ => {
return err(node, "pair found, expected G1 point");
}
Expand All @@ -271,7 +271,7 @@ impl Allocator {

pub fn g2(&self, node: NodePtr) -> Result<G2Projective, EvalErr> {
let blob = match self.sexp(node) {
SExp::Atom() => self.atom(node),
SExp::Atom => self.atom(node),
_ => {
return err(node, "pair found, expected G2 point");
}
Expand All @@ -293,7 +293,7 @@ impl Allocator {
let pair = self.pair_vec[node as usize];
SExp::Pair(pair.first, pair.rest)
} else {
SExp::Atom()
SExp::Atom
}
}

Expand All @@ -305,7 +305,7 @@ impl Allocator {
pub fn next(&self, n: NodePtr) -> Option<(NodePtr, NodePtr)> {
match self.sexp(n) {
SExp::Pair(first, rest) => Some((first, rest)),
SExp::Atom() => None,
SExp::Atom => None,
}
}

Expand Down Expand Up @@ -385,7 +385,7 @@ fn test_null() {
assert_eq!(a.atom(a.null()), b"");

let buf = match a.sexp(a.null()) {
SExp::Atom() => a.atom(a.null()),
SExp::Atom => a.atom(a.null()),
SExp::Pair(_, _) => panic!("unexpected"),
};
assert_eq!(buf, b"");
Expand All @@ -397,7 +397,7 @@ fn test_one() {
assert_eq!(a.atom(a.one()), b"\x01");
assert_eq!(
match a.sexp(a.one()) {
SExp::Atom() => a.atom(a.one()),
SExp::Atom => a.atom(a.one()),
SExp::Pair(_, _) => panic!("unexpected"),
},
b"\x01"
Expand All @@ -411,7 +411,7 @@ fn test_allocate_atom() {
assert_eq!(a.atom(atom), b"foobar");
assert_eq!(
match a.sexp(atom) {
SExp::Atom() => a.atom(atom),
SExp::Atom => a.atom(atom),
SExp::Pair(_, _) => panic!("unexpected"),
},
b"foobar"
Expand All @@ -427,7 +427,7 @@ fn test_allocate_pair() {

assert_eq!(
match a.sexp(pair) {
SExp::Atom() => panic!("unexpected"),
SExp::Atom => panic!("unexpected"),
SExp::Pair(left, right) => (left, right),
},
(atom1, atom2)
Expand All @@ -436,7 +436,7 @@ fn test_allocate_pair() {
let pair2 = a.new_pair(pair, pair).unwrap();
assert_eq!(
match a.sexp(pair2) {
SExp::Atom() => panic!("unexpected"),
SExp::Atom => panic!("unexpected"),
SExp::Pair(left, right) => (left, right),
},
(pair, pair)
Expand Down Expand Up @@ -552,21 +552,21 @@ fn test_sexp() {

assert_eq!(
match a.sexp(atom1) {
SExp::Atom() => 0,
SExp::Atom => 0,
SExp::Pair(_, _) => 1,
},
0
);
assert_eq!(
match a.sexp(atom2) {
SExp::Atom() => 0,
SExp::Atom => 0,
SExp::Pair(_, _) => 1,
},
0
);
assert_eq!(
match a.sexp(pair) {
SExp::Atom() => 0,
SExp::Atom => 0,
SExp::Pair(_, _) => 1,
},
1
Expand Down
4 changes: 2 additions & 2 deletions src/core_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn op_raise(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response
// as it'd potentially look the same as a throw of multiple arguments.
let throw_value = if let Ok([value]) = get_args::<1>(a, input, "") {
match a.sexp(value) {
SExp::Atom() => value,
SExp::Atom => value,
_ => input,
}
} else {
Expand All @@ -67,7 +67,7 @@ pub fn op_raise(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response
}

fn ensure_atom(a: &Allocator, n: NodePtr, op: &str) -> Result<(), EvalErr> {
if let SExp::Atom() = a.sexp(n) {
if let SExp::Atom = a.sexp(n) {
Ok(())
} else {
Err(EvalErr(n, format!("{op} on list")))
Expand Down
2 changes: 1 addition & 1 deletion src/more_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ pub fn op_concat(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) -> Respo
)?;
match a.sexp(arg) {
SExp::Pair(_, _) => return err(arg, "concat on list"),
SExp::Atom() => total_size += a.atom_len(arg),
SExp::Atom => total_size += a.atom_len(arg),
};
terms.push(arg);
}
Expand Down
12 changes: 6 additions & 6 deletions src/op_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fn test_get_varargs() {

pub fn nullp(a: &Allocator, n: NodePtr) -> bool {
match a.sexp(n) {
SExp::Atom() => a.atom_len(n) == 0,
SExp::Atom => a.atom_len(n) == 0,
_ => false,
}
}
Expand Down Expand Up @@ -222,7 +222,7 @@ fn test_rest() {

pub fn int_atom(a: &Allocator, args: NodePtr, op_name: &str) -> Result<(Number, usize), EvalErr> {
match a.sexp(args) {
SExp::Atom() => Ok((a.number(args), a.atom_len(args))),
SExp::Atom => Ok((a.number(args), a.atom_len(args))),
_ => err(args, &format!("{op_name} requires int args")),
}
}
Expand Down Expand Up @@ -254,7 +254,7 @@ fn test_int_atom_failure() {

pub fn atom_len(a: &Allocator, args: NodePtr, op_name: &str) -> Result<usize, EvalErr> {
match a.sexp(args) {
SExp::Atom() => Ok(a.atom_len(args)),
SExp::Atom => Ok(a.atom_len(args)),
_ => err(args, &format!("{op_name} requires an atom")),
}
}
Expand All @@ -281,7 +281,7 @@ pub fn uint_atom<const SIZE: usize>(
op_name: &str,
) -> Result<u64, EvalErr> {
let bytes = match a.sexp(args) {
SExp::Atom() => a.atom(args),
SExp::Atom => a.atom(args),
_ => {
return err(args, &format!("{op_name} requires int arg"));
}
Expand Down Expand Up @@ -409,7 +409,7 @@ fn test_uint_atom_8_pair() {

pub fn atom<'a>(a: &'a Allocator, n: NodePtr, op_name: &str) -> Result<&'a [u8], EvalErr> {
match a.sexp(n) {
SExp::Atom() => Ok(a.atom(n)),
SExp::Atom => Ok(a.atom(n)),
_ => err(n, &format!("{op_name} on list")),
}
}
Expand Down Expand Up @@ -534,7 +534,7 @@ fn test_u64_from_bytes() {

pub fn i32_atom(a: &Allocator, args: NodePtr, op_name: &str) -> Result<i32, EvalErr> {
let buf = match a.sexp(args) {
SExp::Atom() => a.atom(args),
SExp::Atom => a.atom(args),
_ => {
return err(args, &format!("{op_name} requires int32 args"));
}
Expand Down
4 changes: 2 additions & 2 deletions src/run_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<'a, D: Dialect> RunProgramContext<'a, D> {
// put a bunch of ops on op_stack
let (op_node, op_list) = match self.allocator.sexp(program) {
// the program is just a bitfield path through the env tree
SExp::Atom() => {
SExp::Atom => {
let r: Reduction =
traverse_path(self.allocator, self.allocator.atom(program), env)?;
self.push(r.1)?;
Expand All @@ -305,7 +305,7 @@ impl<'a, D: Dialect> RunProgramContext<'a, D> {
self.account_op_push();
Ok(APPLY_COST)
}
SExp::Atom() => self.eval_op_atom(op_node, op_list, env),
SExp::Atom => self.eval_op_atom(op_node, op_list, env),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/serde/object_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn treehash(
.get_from_cache(&right)
.map(|right_value| hash_blobs(&[&[2], left_value, right_value])),
},
SExp::Atom() => Some(hash_blobs(&[&[1], allocator.atom(node)])),
SExp::Atom => Some(hash_blobs(&[&[1], allocator.atom(node)])),
}
}

Expand All @@ -142,7 +142,7 @@ pub fn serialized_length(
.saturating_add(*right_value)
}),
},
SExp::Atom() => {
SExp::Atom => {
let buf = allocator.atom(node);
let lb: u64 = buf.len().try_into().unwrap_or(u64::MAX);
Some(if lb == 0 || (lb == 1 && buf[0] < 128) {
Expand Down Expand Up @@ -192,7 +192,7 @@ fn calculate_depth_simple(
.get_from_cache(&right)
.map(|right_value| 1 + max(*left_value, *right_value)),
},
SExp::Atom() => Some(0),
SExp::Atom => Some(0),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn node_to_stream<W: io::Write>(a: &Allocator, node: NodePtr, f: &mut W) ->
while let Some(v) = values.pop() {
let n = a.sexp(v);
match n {
SExp::Atom() => {
SExp::Atom => {
write_atom(f, a.atom(v))?;
}
SExp::Pair(left, right) => {
Expand Down
2 changes: 1 addition & 1 deletion src/serde/ser_br.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn node_to_stream_backrefs<W: io::Write>(
read_op_stack.push(ReadOp::Parse);
read_op_stack.push(ReadOp::Parse);
}
SExp::Atom() => {
SExp::Atom => {
let atom = allocator.atom(node_to_write);
write_atom(f, atom)?;
read_cache_lookup.push(*node_tree_hash);
Expand Down
2 changes: 1 addition & 1 deletion src/test_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub fn node_eq(allocator: &Allocator, s1: NodePtr, s2: NodePtr) -> bool {
(SExp::Pair(s1a, s1b), SExp::Pair(s2a, s2b)) => {
node_eq(allocator, s1a, s2a) && node_eq(allocator, s1b, s2b)
}
(SExp::Atom(), SExp::Atom()) => allocator.atom_eq(s1, s2),
(SExp::Atom, SExp::Atom) => allocator.atom_eq(s1, s2),
_ => false,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/traverse_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn traverse_path(allocator: &Allocator, node_index: &[u8], args: NodePtr) ->
while byte_idx > first_bit_byte_index || bitmask < last_bitmask {
let is_bit_set: bool = (node_index[byte_idx] & bitmask) != 0;
match allocator.sexp(arg_list) {
SExp::Atom() => {
SExp::Atom => {
return Err(EvalErr(arg_list, "path into atom".into()));
}
SExp::Pair(left, right) => {
Expand Down
2 changes: 1 addition & 1 deletion wasm/src/lazy_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl LazyNode {
#[wasm_bindgen(getter)]
pub fn atom(&self) -> Option<Vec<u8>> {
match &self.allocator.sexp(self.node) {
SExp::Atom() => Some(self.allocator.atom(self.node).into()),
SExp::Atom => Some(self.allocator.atom(self.node).into()),
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion wheel/src/lazy_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl LazyNode {
#[getter(atom)]
pub fn atom(&self, py: Python) -> Option<PyObject> {
match &self.allocator.sexp(self.node) {
SExp::Atom() => Some(PyBytes::new(py, self.allocator.atom(self.node)).into()),
SExp::Atom => Some(PyBytes::new(py, self.allocator.atom(self.node)).into()),
_ => None,
}
}
Expand Down

0 comments on commit f7b6331

Please sign in to comment.