Test custom iterator

This commit is contained in:
Andrew Morris
2023-05-29 16:27:42 +10:00
parent 87dfda4855
commit 21a0d182b2
2 changed files with 47 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
// test_output! [5,6,7,8,9]
export default function () {
let vals: number[] = [];
for (const x of new Range(5, 10)) {
vals.push(x);
}
return vals;
}
class Range {
start: number;
end: number;
constructor(start: number, end: number) {
this.start = start;
this.end = end;
}
[Symbol.iterator]() {
return new RangeIterator(this.start, this.end);
}
}
class RangeIterator {
value: number;
end: number;
constructor(value: number, end: number) {
this.value = value;
this.end = end;
}
next() {
const done = this.value >= this.end;
const res = { value: this.value, done };
if (!done) {
this.value++;
}
return res;
}
}

View File

@@ -921,6 +921,7 @@ impl<'a> AssemblyParser<'a> {
let name = self.parse_identifier();
Value::Pointer(Pointer { name })
}
'$' => Value::Builtin(self.assemble_builtin()),
'}' => {
self.pos.next();
break object;