Rohansi / Mond

A scripting language for .NET Core
https://mond.rohan.dev/
MIT License
367 stars 24 forks source link

Language support for Interpolated Strings #83

Open foobit opened 4 years ago

foobit commented 4 years ago

Really neat language! Would adding support for Interpolated Strings be difficult?

Building strings can get wordy using the + operator (pun intended): "The area of interest is bounded by " + p1.X + "," + p1.Y + " and " + p2.X + "," + p2.Y + "."

IMO, having the values inlined instead of at the end (like string.format) makes it easier to read.

Possible suggested styles:

C#6

$"The area of interest is bounded by {p1.X}, {p1.Y} and {p2.X}, {p2.Y}"

Javascript

'The area of interest is bounded by ${p1.X}, ${p1.Y} and ${p2.X}, ${p2.Y}'

Swift

'The area of interest is bounded by \(p1.X), \(p1.Y) and \(p2.X), \(p2.Y)'

Rohansi commented 4 years ago

Hey, thanks! Agree that it'd be awesome to have. Don't really have a preference for syntax but I do like JavaScript's tagged templates.

Implementation shouldn't be too bad - the most complicated part might be the lexer changes required. It'll need to save its state upon reaching ${ and then return to being a string when it reaches the matching }. And handle nested string interpolation, of course!

If you're up for it just let me know if you need help with anything.

foobit commented 4 years ago

I agree tagged templates are very nice. However, I can't see myself using them for everyday strings. I'm testing Mond using lots of user facing strings.

A related use case for me is for localization. Positional arguments string.format is helpful, but named arguments would be better. However, that is kind of out of scope for the base language. Having, good string handling is key for writing higher level sentence generation.

I'll try implementing one of the basic interpolation strings first to learn the internals of Mond. Thanks.