Skip to content

Commit 458d797

Browse files
authored
length primitive (#3259)
1 parent bc23f0f commit 458d797

File tree

23 files changed

+74
-368
lines changed

23 files changed

+74
-368
lines changed

native/src/list.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use stak_vm::{Error, Heap, Memory, PrimitiveSet, Type};
1+
use stak_vm::{Error, Heap, Memory, Number, PrimitiveSet, Type};
22
use winter_maybe_async::maybe_async;
33

44
/// A list primitive.
@@ -11,13 +11,16 @@ pub enum ListPrimitive {
1111
Memq,
1212
/// A `list-tail` procedure.
1313
Tail,
14+
/// A `length` procedure.
15+
Length,
1416
}
1517

1618
impl ListPrimitive {
1719
const ASSQ: usize = Self::Assq as _;
1820
const CONS: usize = Self::Cons as _;
1921
const MEMQ: usize = Self::Memq as _;
2022
const TAIL: usize = Self::Tail as _;
23+
const LENGTH: usize = Self::Length as _;
2124
}
2225

2326
/// A list primitive set.
@@ -97,6 +100,11 @@ impl<H: Heap> PrimitiveSet<H> for ListPrimitiveSet {
97100

98101
memory.push(xs)?;
99102
}
103+
ListPrimitive::LENGTH => {
104+
let xs = memory.pop()?;
105+
106+
memory.push(Number::from_i64(memory.list_length(xs.assume_cons())? as _).into())?;
107+
}
100108
_ => return Err(Error::IllegalPrimitive),
101109
}
102110

prelude.scm

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@
627627
(define cons (primitive 61))
628628
(define memq (primitive 62))
629629
(define list-tail (primitive 63))
630+
(define length (primitive 64))
630631
(define eqv? (primitive 70))
631632
(define equal-inner? (primitive 71))
632633
(define sqrt (primitive 504))
@@ -901,11 +902,6 @@
901902
'()
902903
(cons fill (loop (- length 1))))))
903904

904-
(define (length xs)
905-
(do ((xs xs (cdr xs)) (y 0 (+ y 1)))
906-
((null? xs)
907-
y)))
908-
909905
(define (map* f xs)
910906
(if (null? xs)
911907
xs

r7rs/src/small.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ impl<H: Heap, D: Device, F: FileSystem, P: ProcessContext, C: Clock> PrimitiveSe
147147
Primitive::NULL | Primitive::PAIR => {
148148
maybe_await!(self.type_check.operate(memory, primitive - Primitive::NULL))?
149149
}
150-
Primitive::ASSQ | Primitive::CONS | Primitive::MEMQ | Primitive::TAIL => {
150+
Primitive::ASSQ
151+
| Primitive::CONS
152+
| Primitive::MEMQ
153+
| Primitive::TAIL
154+
| Primitive::LENGTH => {
151155
maybe_await!(self.list.operate(memory, primitive - Primitive::ASSQ))?
152156
}
153157
Primitive::EQV | Primitive::EQUAL_INNER => {

r7rs/src/small/primitive.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub(super) enum Primitive {
2424
Cons,
2525
Memq,
2626
Tail,
27+
Length,
2728
Eqv = 70,
2829
EqualInner,
2930
Read = 100,
@@ -78,6 +79,7 @@ impl Primitive {
7879
pub const CONS: usize = Self::Cons as _;
7980
pub const MEMQ: usize = Self::Memq as _;
8081
pub const TAIL: usize = Self::Tail as _;
82+
pub const LENGTH: usize = Self::Length as _;
8183
pub const EQV: usize = Self::Eqv as _;
8284
pub const EQUAL_INNER: usize = Self::EqualInner as _;
8385
pub const READ: usize = Self::Read as _;

snapshots/bench/src/eval/main.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@
136136
- constant 63
137137
- call 1 #f primitive
138138
- set list-tail
139+
- constant 64
140+
- call 1 #f primitive
141+
- set length
139142
- constant 70
140143
- call 1 #f primitive
141144
- set eqv?
@@ -676,25 +679,6 @@
676679
- get 3
677680
- call 1 #f 1
678681
- set make-list
679-
- constant procedure 1 #f
680-
- constant #f
681-
- constant procedure 2 #f
682-
- get 1
683-
- call 1 #f null?
684-
- if
685-
- get 0
686-
- get 1
687-
- call 1 #f cdr
688-
- get 1
689-
- constant 1
690-
- call 2 #f ||
691-
- call 2 #f 5
692-
- call 1 #f $$close
693-
- set 1
694-
- get 1
695-
- constant 0
696-
- call 2 #f 2
697-
- set length
698682
- constant procedure 2 #f
699683
- get 0
700684
- call 1 #f null?

snapshots/bench/src/fibonacci/main.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@
9191
- constant 63
9292
- call 1 #f ||
9393
- set ||
94+
- constant 64
95+
- call 1 #f ||
96+
- set ||
9497
- constant 71
9598
- call 1 #f ||
9699
- set ||
@@ -318,25 +321,6 @@
318321
- constant procedure 0 #t
319322
- get 0
320323
- set ||
321-
- constant procedure 1 #f
322-
- constant #f
323-
- constant procedure 2 #f
324-
- get 1
325-
- call 1 #f ||
326-
- if
327-
- get 0
328-
- get 1
329-
- call 1 #f ||
330-
- get 1
331-
- constant 1
332-
- call 2 #f ||
333-
- call 2 #f 5
334-
- call 1 #f ||
335-
- set 1
336-
- get 1
337-
- constant 0
338-
- call 2 #f 2
339-
- set ||
340324
- constant procedure 2 #f
341325
- get 0
342326
- call 1 #f ||

snapshots/bench/src/hello/main.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
- constant 63
8989
- call 1 #f ||
9090
- set ||
91+
- constant 64
92+
- call 1 #f ||
93+
- set ||
9194
- constant procedure 3 #f
9295
- get 1
9396
- get 1
@@ -242,25 +245,6 @@
242245
- call 1 #f ||
243246
- call 1 #f ||
244247
- set ||
245-
- constant procedure 1 #f
246-
- constant #f
247-
- constant procedure 2 #f
248-
- get 1
249-
- call 1 #f ||
250-
- if
251-
- get 0
252-
- get 1
253-
- call 1 #f ||
254-
- get 1
255-
- constant 1
256-
- call 2 #f ||
257-
- call 2 #f 5
258-
- call 1 #f ||
259-
- set 1
260-
- get 1
261-
- constant 0
262-
- call 2 #f 2
263-
- set ||
264248
- constant procedure 2 #f
265249
- get 0
266250
- call 1 #f ||

snapshots/bench/src/read/main.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
- constant 63
8989
- call 1 #f ||
9090
- set ||
91+
- constant 64
92+
- call 1 #f ||
93+
- set ||
9194
- constant 70
9295
- call 1 #f ||
9396
- set ||
@@ -299,25 +302,6 @@
299302
- constant procedure 0 #t
300303
- get 0
301304
- set ||
302-
- constant procedure 1 #f
303-
- constant #f
304-
- constant procedure 2 #f
305-
- get 1
306-
- call 1 #f ||
307-
- if
308-
- get 0
309-
- get 1
310-
- call 1 #f ||
311-
- get 1
312-
- constant 1
313-
- call 2 #f ||
314-
- call 2 #f 5
315-
- call 1 #f ||
316-
- set 1
317-
- get 1
318-
- constant 0
319-
- call 2 #f 2
320-
- set ||
321305
- constant procedure 2 #f
322306
- get 0
323307
- call 1 #f ||

snapshots/bench/src/sum/main.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@
9191
- constant 63
9292
- call 1 #f ||
9393
- set ||
94+
- constant 64
95+
- call 1 #f ||
96+
- set ||
9497
- constant 71
9598
- call 1 #f ||
9699
- set ||
@@ -318,25 +321,6 @@
318321
- constant procedure 0 #t
319322
- get 0
320323
- set ||
321-
- constant procedure 1 #f
322-
- constant #f
323-
- constant procedure 2 #f
324-
- get 1
325-
- call 1 #f ||
326-
- if
327-
- get 0
328-
- get 1
329-
- call 1 #f ||
330-
- get 1
331-
- constant 1
332-
- call 2 #f ||
333-
- call 2 #f 5
334-
- call 1 #f ||
335-
- set 1
336-
- get 1
337-
- constant 0
338-
- call 2 #f 2
339-
- set ||
340324
- constant procedure 2 #f
341325
- get 0
342326
- call 1 #f ||

snapshots/bench/src/tak/main.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@
9191
- constant 63
9292
- call 1 #f ||
9393
- set ||
94+
- constant 64
95+
- call 1 #f ||
96+
- set ||
9497
- constant 71
9598
- call 1 #f ||
9699
- set ||
@@ -318,25 +321,6 @@
318321
- constant procedure 0 #t
319322
- get 0
320323
- set ||
321-
- constant procedure 1 #f
322-
- constant #f
323-
- constant procedure 2 #f
324-
- get 1
325-
- call 1 #f ||
326-
- if
327-
- get 0
328-
- get 1
329-
- call 1 #f ||
330-
- get 1
331-
- constant 1
332-
- call 2 #f ||
333-
- call 2 #f 5
334-
- call 1 #f ||
335-
- set 1
336-
- get 1
337-
- constant 0
338-
- call 2 #f 2
339-
- set ||
340324
- constant procedure 2 #f
341325
- get 0
342326
- call 1 #f ||

0 commit comments

Comments
 (0)