goby
goby copied to clipboard
Support String Interpolation in Goby
>> lang = 'Goby'
#>> Goby
>> message = "I love #{lang} lang!"
#>> I love Goby lang!
>> message2 = 'I love #{lang} lang!'
#>> I love #{lang} lang!
Only double quotation mark support string interpolation + escaped character; Single quotation mark parse escaped character only
@Maxwell-Alexius I'm thinking support formatting string in Goby first like:
s = String.fmt("I love %s lang!", "Goby")
puts(s) #=> I live Goby lang!
This is less convenient but easier to implement.
Update: Already implemented, see usage here
When you implement the fmt
package's Formatter
interface, you can utilize the golang's handling of formt. In the project monkey, I've implemented a fmt module, which does the formatting of the language's object, so you can have a look.
Just a memorandum: When the string interpolation is supported, I suppose disabling the concatenation String#+
intentionally might be good instead. String concatenation with +
sometimes would lead to inefficient codes and security holes like SQL injections.
The string interpolation should equip auto #to_s
(and if possible, auto sanitization as a default like below)
-
"Hello #{var}"
: auto#to_s
and auto sanitization -
"Hello ##{var}"
: auto#to_s
only
auto sanitization
SQL sanitization can't be perfomed by the language, as it's a functionality of the database (driver); each database (driver) can/does escape in a different way, also depending on the semantic of the value to escape: tables/schemas/columns and literals are/can be escaped in different ways.
I should've noticed that: thank you!
So, as someone who mostly uses ruby in a systems environment(replacement for bash, automation etc), this is one of the most important features of the language to me. As such I am happy to take on implementation of it.
I played around with making it work the other day, and decided it would be worth discussing how best to implement this.
Where should this code live
- Should the lexer be splitting the string into segments, and creating sub expressions. This could then be wrapped in a class by the parser that would do the string formatting in a
to_s
method. - Put all logic in the parser and put the logic in the regular string class. We could mark at compile time if the string contained interpolation, and each sub expression could be handled as a block.
@eliothedeman sounds like the first one is more close to how Ruby handles this? (I don't really know how string interpolation work though)
This might be the one? https://stackoverflow.com/questions/25488902/what-happens-when-you-use-string-interpolation-in-ruby
@hachi8833 Thanks for the pointer. Will read!
Yeah it looks like it is actual logic is done by the compiler, which makes sense.
x = 10
y = 20
puts "x=#{x} y=#{y}"
turns into (logically speaking)
x = 10
y = 20
puts "x=" + x.to_s + " y=" + y.to_s
There is plenty that could be done to make this after at runtime, but I think a good first pass is implementing it in the same way ruby does.