Evaluate expression with operator overloading and Python-style array slicing using tagged template literal.
npm install @leawind/roprop 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:
``ts2 + 3
o; // 5`
Python-style array slicing syntax:
`ts
Rop.INST.bind({ arr: [1, 2, 3, 4, 5] });
// Basic slicing
oarr[1:3]; // [2, 3]
// Negative indices
oarr[:-2]; // [1, 2, 3]
// With step
oarr[::2]; // [1, 3, 5]
// Reverse
oarr[::-1]; // [5, 4, 3, 2, 1]
// Multi-dimensional slicing (for custom types)
o${tensor}[2:5, 1:5, 4:7];`
`ts${[2, 3]} + ${[4, 5]}
o; // [2, 3, 4, 5]`
`tsa + b
Rop.INST.bind({
a: new Set([1, 2, 3]),
b: new Set([3, 4, 5]),
});
o; // Set { 1, 2, 3, 4, 5 }`
`ts
Rop.INST.bind({
obj: { name: 'Alice' },
arr: [1, 2, 3],
});
// access property name on objobj.name
o; // Alice
// index obj with nameobj['name']
o; // Alicearr[1]
o; // 2`
`tsa
// bind identifiers and b, so you can use them in the expression.a + b
rop.bind({
a: new Vec2(2, 3),
b: new Vec2(3, 4),
});
rop.o; // Vec2 { x: 5, y: 7 }``