中文 | English
rop
is a TypeScript/JavaScript library that parses and evaluates expressions via tagged template literals. It supports operator overloading for custom and built-in types, enabling custom behaviors for JS operators and Python-style array slicing.
Before reading the Quick Tutorial, these examples below can help you understand what rop
can do:
o`2 + 3`; // 5
// Value embedding
o`${2} + 3`; // 5
Python-style array slicing syntax:
Rop.INST.bind({ arr: [1, 2, 3, 4, 5] });
// Basic slicing
o`arr[1:3]`; // [2, 3]
// Negative indices
o`arr[:-2]`; // [1, 2, 3]
// With step
o`arr[::2]`; // [1, 3, 5]
// Reverse
o`arr[::-1]`; // [5, 4, 3, 2, 1]
// Multi-dimensional slicing (for custom types)
o`${tensor}[2:5, 1:5, 4:7]`;
o`${[2, 3]} + ${[4, 5]}`; // [2, 3, 4, 5]
Rop.INST.bind({
a: new Set([1, 2, 3]),
b: new Set([3, 4, 5]),
});
o`a + b`; // Set { 1, 2, 3, 4, 5 }
Rop.INST.bind({
obj: { name: 'Alice' },
arr: [1, 2, 3],
});
// access property `name` on `obj`
o`obj.name`; // Alice
// index `obj` with `name`
o`obj['name']`; // Alice
o`arr[1]`; // 2
// bind identifiers `a` and `b`, so you can use them in the expression.
rop.bind({
a: new Vec2(2, 3),
b: new Vec2(3, 4),
});
rop.o<Vec2>`a + b`; // Vec2 { x: 5, y: 7 }