goby-lang / goby

Goby - Yet another programming language written in Go
MIT License
3.49k stars 171 forks source link

Support String Interpolation in Goby #330

Open Alexius-Huang opened 7 years ago

Alexius-Huang commented 7 years ago
>> 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

st0012 commented 7 years ago

@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

haifenghuang commented 7 years ago

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.

hachi8833 commented 6 years ago

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)

64kramsystem commented 6 years ago

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.

hachi8833 commented 6 years ago

I should've noticed that: thank you!

eliothedeman commented 6 years ago

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

st0012 commented 6 years ago

@eliothedeman sounds like the first one is more close to how Ruby handles this? (I don't really know how string interpolation work though)

hachi8833 commented 6 years ago

This might be the one? https://stackoverflow.com/questions/25488902/what-happens-when-you-use-string-interpolation-in-ruby

eliothedeman commented 6 years ago

@hachi8833 Thanks for the pointer. Will read!

eliothedeman commented 6 years ago

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.