mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Test custom iterator
This commit is contained in:
46
inputs/passing/customIterator.ts
Normal file
46
inputs/passing/customIterator.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user