rewrite_gmegabuf_writes

Function rewrite_gmegabuf_writes 

Source
pub(super) fn rewrite_gmegabuf_writes(s: &str) -> String
Expand description

Rewrite gmegabuf(<idx>) = <val> (and megabuf) into gmegabuf_set(<idx>, <val>). evalexpr’s = operator rejects a function-call LHS with a confusing “Expected String, got Float” error, which cascades across the rest of the equation. The two-arg call shape is well-formed; gmegabuf_set / megabuf_set are registered as buffer-backed functions in math_functions.rs.

Walker-based, replacing an earlier regex-only implementation. Three gains over the regex:

  1. Nested parens in <idx>. The old regex’s [^()=]+? class rejected any ( / ) inside the index expression, so gmegabuf((bd_src*bd_oct+oct)*2) = bd_qual silently fell through and tripped evalexpr’s “Expected String, got Float” error.
  2. Compound assignments. gmegabuf(idx) += val / *= / -= / /= are common in the corpus and now expand to gmegabuf_set(idx, gmegabuf(idx) <op> val).
  3. Allows whitespace between name and (. Corpus presets write gmegabuf (n+1) with a space.