nitrogen icon indicating copy to clipboard operation
nitrogen copied to clipboard

Interpolated strings

Open lfkeitel opened this issue 6 years ago • 0 comments

Similar to double quoted strings in PHP, f strings in Python 3, and template strings in JavaScript.

Syntax Notes

"This string contains ${some_var}, cool huh?" or `This string contains ${some_var}, cool huh?`

Do like PHP or JavaScript where double quotes or backticks denote an interpolated string? While single quotes are not interpreted. Or like Python where there's a marker before the string? Backticks aren't being used for anything so they could denote this type of string. Perhaps make it the most flexible allowing new lines and escape sequences.

Curly braces are required.

Are the expressions allowed inside the braces or only identifiers? Can more elaborate expressions be used such as math? What about array/map indexing? Is that allowed?

Implementation Notes

Interpreted strings begin as an AST node with text and variables parts. The parts are reduced at runtime to a single string when the string is first encountered. Any non-string variables are converted to a string. Classes will use a toString method if available, other values will have their String() methods called in Go.

VM could be implemented a couple ways:

  1. Use standard string concatenation. Each variable is passed to toString for conversion and concated to the part before it. This method would require no new opcodes but would be less performant due to multiple concatenations and the multiple calls from Nitrogen to the runtime for string conversion.
  2. Create a new runtime object and opcode that takes that object to generate a string. The runtime object would contain the parts from the syntax tree. The opcode would take the object, and only that object, from the stack and "execute" it to create a flat string. This method would be the cleanest and most performant since the entire conversion and concatenation step would happen in the runtime.

lfkeitel avatar Feb 16 '19 02:02 lfkeitel