How'd we implement the mini-langs (other than its "text patterns" described yesterday) supported by Lua's `string` namespace? Using the Scanner upon which we bootstrapped a Lua parser?
`string.gsub` also supports a "%"-digit syntax referencing the captures from the pattern-matching which triggered the substitution. Combined with intermediate haystack & substitution characters, needs to be efficiently concatenated. Which I made sure our opcodes allows!
1/3?
`string.pack`/`string.unpack`/`string.packsize` interprets a mini-lang which (ignoring whitespace) precedes chars indicating a type with "<"/"="/">" as endianness & "!"-prefixed alignment.
Usually the type-char indicates an integer's byte-width & whether its signed, though "d" & "f" represents floats whilst "c", "z", & "s" copies however many bytes out as strings. "x" & prefix-"X" ignores the bytes as padding.
Interpret using Assembly, ideally using our maybe-ops to avoid branching!
2/3?
You're probably already familiar with `string.format`'s mini-lang!
Interpreting it will gather all strings up to "%" to be concatenated into the result. A following "-" switches to right-padding. "+" or space sets the positive-sign char. "0" sets the padding char. & "'" enable the thousands-separator.
The following digits represents the min-width, & any after "." represents the precision. A type-char determines additional formatting for the number, or string. Then resumes literal text.
3/4!
We'd need to ensure we have we have an internal variant of numbers' `tostring` (described earlier) implementation which exposes those formatting options to `strnig.format`. Maybe this would be the `__tostring` metamethod?
Sometimes pointers would be need to be cast to numbers for formatting.
Regardless we'd gather plenty of substrings to hand off to our CONCAT opcode!
4/4 Fin for today! Tomorrow: UTF-8!
(type-casting could be the main util we need to implement Lua's standard lib in Lua...)
P.S. I haven't stated what endianness I'd use for the underlying CPU for this interpreter. It'd be nice if there were (a bit of) a Control/Status Register (CSR) allowing me to configure this! Shouldn't complicate the circuitry much...
@alcinnz the MIPS architecture had this. I don't think it was ever used much. An operating system picked an endinanness and stuck with it.