Change implementation strategy of cashassembly, add scripts, comments support and tests

This commit is contained in:
Kuldeep
2026-09-11 10:02:20 +00:00
parent bd2f1044e7
commit c3dd0162d9
15 changed files with 2035 additions and 332 deletions
+35 -55
View File
@@ -1,61 +1,41 @@
/**
* Detects whether a string is a pure CashAssembly expression.
*
* CashAssembly expressions look like `$(<variable>)` or `$(<a> <b>)`. This pattern checks
* that the entire string is one such expression and nothing else. It will not match if
* there is other text surrounding the expression.
*
* For example:
* `$(<fee>)` matches (a full expression)
* `OP_DUP $(<fee>)` does not match (extra text before it)
* `$()` does not match (empty expression)
*/
export const CASHASSEMBLY_EXPRESSION_PATTERN = /^\$\([^)]+\)$/;
/**
* Finds all CashAssembly evaluations embedded in a larger string.
*
* An evaluation looks like `$(...)`, for example `$(<fee>)` or `$(<a> <b>)`. This pattern
* locates every occurrence in the input and returns them all (global flag `g`).
* Empty evaluations `$()` are intentionally excluded because they reference no variables.
*
* For example, scanning `"OP_DUP <$(<pubkeyHash>)> OP_HASH160 $(<fee>)"` would return
* `['$(<pubkeyHash>)', '$(<fee>)']`.
*/
export const CASHASSEMBLY_EVALUATION_PATTERN = /\$\([^)]+\)/g;
/**
* Extracts variable names from angle-bracket references inside a CashAssembly evaluation.
*
* Inside an evaluation like `$(<pubkeyHash> <fee>)`, variables are referenced as `<name>`.
* This pattern captures the name between the brackets. The global flag `g` allows iterating
* over every variable reference in a single evaluation string.
*
* For example, running this against `$(<pubkeyHash> <fee>)` would return the variable names
* `["pubkeyHash", "fee"]`.
*/
export const CASHASSEMBLY_VARIABLE_PATTERN = /<([^>]+)>/g;
/**
* Identifies CashAssembly literal tokens that appear inside angle-bracket push statements.
*
* Inside an evaluation, not everything between `<` and `>` is a variable name. Literals are
* also valid push contents: numeric literals (e.g. `<0>`, `<32>`), hex literals (`<0x02>`),
* binary literals (`<0b1010>`), and string literals (`<"minting">`, `<'hello'>`). This pattern
* matches any captured token that starts with a digit or a quote character.
*/
export const CASHASSEMBLY_LITERAL_TOKEN_PATTERN = /^[0-9"']/;
/**
* Matches a single dot variable method reference inside an angle-bracket identifier.
* Matches a single dot variable method reference inside an angle bracket identifier.
*
* Used to detect primitive method references such as `expiry.toIso8601`.
*
* For example:
* `expiry.toIso8601` matches (base `expiry`, method `toIso8601`)
* `requestedSatoshis` does not match (no method)
* `key.schnorr_signature.all_outputs` does not match (more than one dot)
* `key.public_key` matches the pattern shape but it is only resolved
* when `type` maps to a primitive
* For example `expiry.toIso8601` matches with base expiry and method toIso8601.
* `requestedSatoshis` does not match because it has no method.
* `key.schnorr_signature.all_outputs` does not match because it has more than one dot.
*/
export const CASHASSEMBLY_VARIABLE_METHOD_REFERENCE_PATTERN = /^([^.]+)\.([^.]+)$/;
/**
* Character count of the `$(` evaluation opener, which is two characters long.
*
* Adding this length moves the read position to the first character after that sequence so scanning
* starts on the evaluation contents rather than on the opening parenthesis.
*/
export const CASHASSEMBLY_EVALUATION_START_LENGTH = 2;
/**
* Character count of the `//` and `/*` comment openers, which are both two characters long.
*
* Adding this length moves the read position to the first character after that sequence so the '*'
* of a block comment opener cannot be reused as the '*' of its closer.
*/
export const CASHASSEMBLY_COMMENT_START_LENGTH = 2;
/**
* Character count of the block comment closer, which is two characters long.
*
* Adding this length moves the read position to the first character after that sequence so the
* closing slash cannot pair with the next character and open another comment.
*/
export const CASHASSEMBLY_BLOCK_COMMENT_END_LENGTH = 2;
/**
* `$()` with nothing between the parentheses.
*
* Callers use this to detect an evaluation that has no contents to compile.
*/
export const EMPTY_CASHASSEMBLY_EVALUATION = '$()';