< Home | Recrafted | Blog | About >

Pre-emption 2: Electric Boogaloo
  On December 10, 2021, I published a writeup of the pre-emption method I use in Cynosure.

  While writing that article, I came across a fairly critical bug that in said article I glossed over because fixing it meant rewriting the entire code processor.  The time has come, though, and I had to write a preemption implementation for Cynosure 2.

  The code for that is here.  You may follow along if you wish.

  So what's different about this new code?

How It Works
  As with the previous implementation, there is a table of patterns; the primary difference is that there are more of them.  They also use Cynosure 2's randomized sysyield function name to prevent overriding the preemption bits.

  After that table there is the gsub function, which takes a chunk of code and replaces all the patterns in it.

  Then there is the wrap function.

  This function takes in a chunk of code.  It iterates through that chunk of code and finds either a quote ('") or an open bracket ([).

  When it finds a quote, it will flip the internal variable in_str.  There's some extra logic around matching up quote pairs.

  When it finds an open bracket:
    - If that open bracket is immediately followed by another one, this is the beginning of a multiline string.  It will find the closest following ]] and attach that to the result string.
    - If the open bracket is followed by some number of =s and then an open bracket, it matches the balance of that and adds the result to the result string: e.g. for [===[ it will find ]===] and treat that as a string.
    - Otherwise, it ignores the bracket and continues.

  This logic is a little simpler than the previous implementation, and doesn't freak out over multiline strings with quotes in them.  All-in-all I think it's a much-needed improvement.

  Aside from the above stuff, everything in the previous article still applies to the original Cynosure.

Conclusion
  I hope you learned something from this article.  If you have any questions, message me at Ocawesome101#5343 on Discord or through #oc on EsperNet.