curimit / SugarCpp

SugarCpp is a language which can compile to C++11.
135 stars 13 forks source link

Ruby style string formater via stringstream. #25

Open curimit opened 11 years ago

curimit commented 11 years ago

In ruby we can format string in this way:

k = 123
str = "x=#{k+1}" 

So in SugarCpp, we can just write this

import "iostream"

int main()
    k := 123
    str := "x=#{k+1}"

Which compiles into this

#include <iostream>
#include <sstream>

int main()
{
    auto k = 123;
    auto str = ({
        std::stringstream _t_sstream;
        _t_sstream << "x=" << k + 1;
        _t_sstream.str();
    });
}
ppwwyyxx commented 11 years ago

Amazing feature.!

vuryleo commented 11 years ago

Amazing! plz pay attention to the left part for that sometimes it's easy to use these if you brought out this feature.

cout << "x=#{k + 1}"

It shall be compiled to

cout << ({
        std::stringstream _t_sstream;
        _t_sstream << "x=" << k + 1;
        _t_sstream.str();
});

Seems it's easy for only dealing with "#{}", plz make sure there are some way to get exactly #{.*}.

Please check it when finished, THX.

curimit commented 11 years ago

这一坨feature等我完成编译器自举以后再继续写……

ozra commented 9 years ago

I've used this all the time in LS, but there's this one thing that's always nagged me about it: The assymetry of the notation - I find it often is a bit cluttered and unclear. A notation I do find to be very clear in this regard is:

interpolation := "elephant"
text := "room"
 "Here we have an {{interpolation}} in the {{text}}"

Ofcourse it doesn't follow the Ruby or LiveScript syntax, but still, it is much clearer imho.

curimit commented 9 years ago

Seems good, I'll check this.

ozra commented 9 years ago

Yeah, the where clauses from Haskell is neat, would also be for making localized functions etc. Generally. In the case of the interpolations, I mean ofcourse it should interpolate variables in the scope as usual, just with the change (or addition) of the double braces notation..

2014-12-10 4:42 GMT+01:00 curimit notifications@github.com:

How about this?

text := "Here we have an {{interpolation}} in the {{text}}" where interpolation := "elephant" text := "room"

— Reply to this email directly or view it on GitHub https://github.com/curimit/SugarCpp/issues/25#issuecomment-66400441.