Skip to content

Commit 8b6d79e

Browse files
committed
rename null() to nil() for better alignment with LISP
1 parent 293e9ab commit 8b6d79e

19 files changed

+138
-146
lines changed

benches/run-program.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use std::fs::read_to_string;
66
use std::time::Instant;
77

88
fn long_strings(a: &mut Allocator) -> NodePtr {
9-
let mut list = a.null();
9+
let mut list = a.nil();
1010
for _i in 0..1000 {
1111
let item = a
1212
.new_atom(b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
1313
.expect("new_atom");
1414
list = a.new_pair(item, list).expect("new_pair");
1515
}
1616

17-
a.new_pair(list, a.null()).expect("new_pair")
17+
a.new_pair(list, a.nil()).expect("new_pair")
1818
}
1919

2020
fn large_tree_impl(a: &mut Allocator, depth: i32) -> NodePtr {
@@ -37,11 +37,11 @@ fn long_string(a: &mut Allocator) -> NodePtr {
3737
atom.extend(b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
3838
}
3939
let item = a.new_atom(&atom[..]).expect("new_atom");
40-
a.new_pair(item, a.null()).expect("new_pair")
40+
a.new_pair(item, a.nil()).expect("new_pair")
4141
}
4242

4343
fn tuple3<const N: i32, const K: i32>(a: &mut Allocator) -> NodePtr {
44-
let list = a.null();
44+
let list = a.nil();
4545
let item = a.new_number(K.into()).expect("new_atom");
4646
let list = a.new_pair(item, list).expect("new_pair");
4747
let item = a.new_number(N.into()).expect("new_atom");
@@ -51,7 +51,7 @@ fn tuple3<const N: i32, const K: i32>(a: &mut Allocator) -> NodePtr {
5151
}
5252

5353
fn pair<const N: i32>(a: &mut Allocator) -> NodePtr {
54-
let list = a.null();
54+
let list = a.nil();
5555
let item = a.new_number(N.into()).expect("new_atom");
5656
let list = a.new_pair(item, list).expect("new_pair");
5757
let item = a
@@ -61,18 +61,18 @@ fn pair<const N: i32>(a: &mut Allocator) -> NodePtr {
6161
}
6262

6363
fn single_value<const N: i32>(a: &mut Allocator) -> NodePtr {
64-
let list = a.null();
64+
let list = a.nil();
6565
let item = a.new_number(N.into()).expect("new_atom");
6666
a.new_pair(item, list).expect("new_pair")
6767
}
6868

6969
fn generate_list<const N: i32>(a: &mut Allocator) -> NodePtr {
70-
let mut list = a.null();
70+
let mut list = a.nil();
7171
for _i in 0..N {
7272
let item = a.new_number(42.into()).expect("new_atom");
7373
list = a.new_pair(item, list).expect("new_pair");
7474
}
75-
a.new_pair(list, a.null()).expect("new_pair")
75+
a.new_pair(list, a.nil()).expect("new_pair")
7676
}
7777

7878
fn large_block(a: &mut Allocator) -> NodePtr {
@@ -81,7 +81,7 @@ fn large_block(a: &mut Allocator) -> NodePtr {
8181
buffer.push((i & 0xff) as u8);
8282
}
8383

84-
let mut list = a.null();
84+
let mut list = a.nil();
8585
for i in 0..1000 {
8686
let hex_key1 = hex::encode(&buffer[i..i + 32]);
8787
let hex_key2 = hex::encode(&buffer[i / 2..i / 2 + 32]);
@@ -118,13 +118,13 @@ ff83\
118118
}
119119

120120
fn matrix<const W: i32, const H: i32>(a: &mut Allocator) -> NodePtr {
121-
let mut args = a.null();
121+
let mut args = a.nil();
122122

123123
for _l in 0..2 {
124-
let mut col = a.null();
124+
let mut col = a.nil();
125125

126126
for _k in 0..H {
127-
let mut row = a.null();
127+
let mut row = a.nil();
128128
for _i in 0..W {
129129
let val = a.new_atom(b"ccba9401").expect("new_atom");
130130
row = a.new_pair(val, row).expect("new_pair");
@@ -177,11 +177,11 @@ ffff01ffff33ffa06b7a83babea1eec790c947db4464ab657dbe9b887fe9acc2\
177177
}
178178

179179
fn none(a: &mut Allocator) -> NodePtr {
180-
a.null()
180+
a.nil()
181181
}
182182

183183
fn point_pow(a: &mut Allocator) -> NodePtr {
184-
let list = a.null();
184+
let list = a.nil();
185185
let item = a.new_number(1337.into()).expect("new_atom");
186186
let list = a.new_pair(item, list).expect("new_pair");
187187
let item = a.new_atom(&hex::decode("b3b8ac537f4fd6bde9b26221d49b54b17a506be147347dae5d081c0a6572b611d8484e338f3432971a9823976c6a232b").expect("invalid point hex")).expect("new_atom");

fuzz/fuzz_targets/fuzzing_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const BUFFER: [u8; 63] = [
5656

5757
pub fn make_tree(a: &mut Allocator, cursor: &mut BitCursor, short_atoms: bool) -> NodePtr {
5858
match cursor.read_bits(1) {
59-
None => a.null(),
59+
None => a.nil(),
6060
Some(0) => {
6161
let first = make_tree(a, cursor, short_atoms);
6262
let second = make_tree(a, cursor, short_atoms);
@@ -65,12 +65,12 @@ pub fn make_tree(a: &mut Allocator, cursor: &mut BitCursor, short_atoms: bool) -
6565
Some(_) => {
6666
if short_atoms {
6767
match cursor.read_bits(8) {
68-
None => a.null(),
68+
None => a.nil(),
6969
Some(val) => a.new_atom(&[val]).unwrap(),
7070
}
7171
} else {
7272
match cursor.read_bits(6) {
73-
None => a.null(),
73+
None => a.nil(),
7474
Some(len) => a.new_atom(&BUFFER[..len as usize]).unwrap(),
7575
}
7676
}

fuzz/fuzz_targets/run_program.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fuzz_target!(|data: &[u8]| {
1818
}
1919
Ok(r) => r,
2020
};
21-
let args = allocator.null();
21+
let args = allocator.nil();
2222

2323
let allocator_checkpoint = allocator.checkpoint();
2424

src/allocator.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ enum ObjectType {
1919

2020
// The top 6 bits of the NodePtr indicate what type of object it is
2121
impl NodePtr {
22-
pub fn null() -> Self {
22+
pub fn nil() -> Self {
2323
Self::new(ObjectType::Bytes, 0)
2424
}
2525

@@ -56,7 +56,7 @@ impl NodePtr {
5656

5757
impl Default for NodePtr {
5858
fn default() -> Self {
59-
Self::null()
59+
Self::nil()
6060
}
6161
}
6262

@@ -170,11 +170,11 @@ impl Allocator {
170170
pub fn new_atom(&mut self, v: &[u8]) -> Result<NodePtr, EvalErr> {
171171
let start = self.u8_vec.len() as u32;
172172
if (self.heap_limit - start as usize) < v.len() {
173-
return err(self.null(), "out of memory");
173+
return err(self.nil(), "out of memory");
174174
}
175175
let idx = self.atom_vec.len();
176176
if idx == MAX_NUM_ATOMS {
177-
return err(self.null(), "too many atoms");
177+
return err(self.nil(), "too many atoms");
178178
}
179179
self.u8_vec.extend_from_slice(v);
180180
let end = self.u8_vec.len() as u32;
@@ -197,15 +197,15 @@ impl Allocator {
197197
pub fn new_pair(&mut self, first: NodePtr, rest: NodePtr) -> Result<NodePtr, EvalErr> {
198198
let idx = self.pair_vec.len();
199199
if idx == MAX_NUM_PAIRS {
200-
return err(self.null(), "too many pairs");
200+
return err(self.nil(), "too many pairs");
201201
}
202202
self.pair_vec.push(IntPair { first, rest });
203203
Ok(NodePtr::new(ObjectType::Pair, idx))
204204
}
205205

206206
pub fn new_substr(&mut self, node: NodePtr, start: u32, end: u32) -> Result<NodePtr, EvalErr> {
207207
if self.atom_vec.len() == MAX_NUM_ATOMS {
208-
return err(self.null(), "too many atoms");
208+
return err(self.nil(), "too many atoms");
209209
}
210210
let (ObjectType::Bytes, idx) = node.node_type() else {
211211
return err(node, "(internal error) substr expected atom, got pair");
@@ -231,11 +231,11 @@ impl Allocator {
231231

232232
pub fn new_concat(&mut self, new_size: usize, nodes: &[NodePtr]) -> Result<NodePtr, EvalErr> {
233233
if self.atom_vec.len() == MAX_NUM_ATOMS {
234-
return err(self.null(), "too many atoms");
234+
return err(self.nil(), "too many atoms");
235235
}
236236
let start = self.u8_vec.len();
237237
if self.heap_limit - start < new_size {
238-
return err(self.null(), "out of memory");
238+
return err(self.nil(), "out of memory");
239239
}
240240
self.u8_vec.reserve(new_size);
241241

@@ -258,7 +258,7 @@ impl Allocator {
258258
if counter != new_size {
259259
self.u8_vec.truncate(start);
260260
return err(
261-
self.null(),
261+
self.nil(),
262262
"(internal error) concat passed invalid new_size",
263263
);
264264
}
@@ -353,7 +353,7 @@ impl Allocator {
353353
}
354354
}
355355

356-
pub fn null(&self) -> NodePtr {
356+
pub fn nil(&self) -> NodePtr {
357357
NodePtr::new(ObjectType::Bytes, 0)
358358
}
359359

@@ -429,7 +429,7 @@ fn test_node_as_index() {
429429
#[test]
430430
fn test_atom_eq() {
431431
let mut a = Allocator::new();
432-
let a0 = a.null();
432+
let a0 = a.nil();
433433
let a1 = a.one();
434434
let a2 = a.new_atom(&[1]).unwrap();
435435
let a3 = a.new_atom(&[0x5, 0x39]).unwrap();
@@ -473,12 +473,12 @@ fn test_atom_eq() {
473473
}
474474

475475
#[test]
476-
fn test_null() {
476+
fn test_nil() {
477477
let a = Allocator::new();
478-
assert_eq!(a.atom(a.null()), b"");
478+
assert_eq!(a.atom(a.nil()), b"");
479479

480-
let buf = match a.sexp(a.null()) {
481-
SExp::Atom => a.atom(a.null()),
480+
let buf = match a.sexp(a.nil()) {
481+
SExp::Atom => a.atom(a.nil()),
482482
SExp::Pair(_, _) => panic!("unexpected"),
483483
};
484484
assert_eq!(buf, b"");
@@ -549,7 +549,7 @@ fn test_allocate_heap_limit() {
549549
fn test_allocate_atom_limit() {
550550
let mut a = Allocator::new();
551551
// we can allocate 5 atoms total
552-
// keep in mind that we always have 2 pre-allocated atoms for null and one,
552+
// keep in mind that we always have 2 pre-allocated atoms for nil and one,
553553
// so with a limit of 5, we only have 3 slots left at this point.
554554
let _atom = a.new_atom(b"foo").unwrap();
555555
let _atom = a.new_atom(b"bar").unwrap();
@@ -563,7 +563,7 @@ fn test_allocate_atom_limit() {
563563
}
564564
assert_eq!(a.new_atom(b"foobar").unwrap_err().1, "too many atoms");
565565

566-
// the pre-allocated null() and one() also count against the limit, and they
566+
// the pre-allocated nil() and one() also count against the limit, and they
567567
// use 0 and 1 bytes respectively
568568
assert_eq!(a.u8_vec.len(), MAX_NUM_ATOMS * 3 - 3 - 2);
569569
}
@@ -798,7 +798,7 @@ fn test_point_size_error(#[case] fun: TestFun, #[case] size: usize, #[case] expe
798798
#[case(test_g2, "pair found, expected G2 point")]
799799
fn test_point_atom_pair(#[case] fun: TestFun, #[case] expected: &str) {
800800
let mut a = Allocator::new();
801-
let n = a.new_pair(a.null(), a.one()).unwrap();
801+
let n = a.new_pair(a.nil(), a.one()).unwrap();
802802
let r = fun(&a, n);
803803
assert_eq!(r.0, n);
804804
assert_eq!(r.1, expected.to_string());

src/bls_ops.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::allocator::{Allocator, NodePtr};
22
use crate::cost::{check_cost, Cost};
33
use crate::err_utils::err;
44
use crate::op_utils::{
5-
atom, first, get_args, get_varargs, int_atom, mod_group_order, new_atom_and_cost, nullp, rest,
5+
atom, first, get_args, get_varargs, int_atom, mod_group_order, new_atom_and_cost, nilp, rest,
66
MALLOC_COST_PER_BYTE,
77
};
88
use crate::reduction::{EvalErr, Reduction, Response};
@@ -272,7 +272,7 @@ pub fn op_bls_pairing_identity(a: &mut Allocator, input: NodePtr, max_cost: Cost
272272
let mut items = Vec::<(G1Element, G2Element)>::new();
273273

274274
let mut args = input;
275-
while !nullp(a, args) {
275+
while !nilp(a, args) {
276276
cost += BLS_PAIRING_COST_PER_ARG;
277277
check_cost(a, cost, max_cost)?;
278278
let g1 = a.g1(first(a, args)?)?;
@@ -285,7 +285,7 @@ pub fn op_bls_pairing_identity(a: &mut Allocator, input: NodePtr, max_cost: Cost
285285
if !aggregate_pairing(items) {
286286
err(input, "bls_pairing_identity failed")
287287
} else {
288-
Ok(Reduction(cost, a.null()))
288+
Ok(Reduction(cost, a.nil()))
289289
}
290290
}
291291

@@ -306,7 +306,7 @@ pub fn op_bls_verify(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respo
306306
args = rest(a, args)?;
307307

308308
let mut items = Vec::<(PublicKey, &[u8])>::new();
309-
while !nullp(a, args) {
309+
while !nilp(a, args) {
310310
let pk = a.g1(first(a, args)?)?;
311311
args = rest(a, args)?;
312312
let msg = atom(a, first(a, args)?, "bls_verify message")?;
@@ -323,6 +323,6 @@ pub fn op_bls_verify(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Respo
323323
if !aggregate_verify(&signature, items) {
324324
err(input, "bls_verify failed")
325325
} else {
326-
Ok(Reduction(cost, a.null()))
326+
Ok(Reduction(cost, a.nil()))
327327
}
328328
}

src/core_ops.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::allocator::{Allocator, NodePtr, SExp};
22
use crate::cost::Cost;
33
use crate::err_utils::err;
4-
use crate::op_utils::{first, get_args, nullp, rest};
4+
use crate::op_utils::{first, get_args, nilp, rest};
55
use crate::reduction::{EvalErr, Reduction, Response};
66

77
const FIRST_COST: Cost = 30;
@@ -17,11 +17,7 @@ const EQ_COST_PER_BYTE: Cost = 1;
1717

1818
pub fn op_if(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
1919
let [cond, affirmative, negative] = get_args::<3>(a, input, "i")?;
20-
let chosen_node = if nullp(a, cond) {
21-
negative
22-
} else {
23-
affirmative
24-
};
20+
let chosen_node = if nilp(a, cond) { negative } else { affirmative };
2521
Ok(Reduction(IF_COST, chosen_node))
2622
}
2723

@@ -45,7 +41,7 @@ pub fn op_listp(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response
4541
let [n] = get_args::<1>(a, input, "l")?;
4642
match a.sexp(n) {
4743
SExp::Pair(_, _) => Ok(Reduction(LISTP_COST, a.one())),
48-
_ => Ok(Reduction(LISTP_COST, a.null())),
44+
_ => Ok(Reduction(LISTP_COST, a.nil())),
4945
}
5046
}
5147

@@ -80,5 +76,5 @@ pub fn op_eq(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
8076
ensure_atom(a, s1, "=")?;
8177
let eq = a.atom_eq(s0, s1);
8278
let cost = EQ_BASE_COST + (a.atom_len(s0) as Cost + a.atom_len(s1) as Cost) * EQ_COST_PER_BYTE;
83-
Ok(Reduction(cost, if eq { a.one() } else { a.null() }))
79+
Ok(Reduction(cost, if eq { a.one() } else { a.nil() }))
8480
}

src/cost.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub type Cost = u64;
55

66
pub fn check_cost(a: &Allocator, cost: Cost, max_cost: Cost) -> Result<(), EvalErr> {
77
if cost > max_cost {
8-
Err(EvalErr(a.null(), "cost exceeded".into()))
8+
Err(EvalErr(a.nil(), "cost exceeded".into()))
99
} else {
1010
Ok(())
1111
}

0 commit comments

Comments
 (0)