Hi,
Long story short; I've always been a fan of the Python script language due to it's readability, simplicity and capability. I've also been looking forward to the day a compiled alternative to Python that can produce native binaries. I tried Nim for a while; however, it didn't feel similar enough to me. I've been messing around with the D programming language and its meta-programming and have been really enjoying it's capabilities. While D felt like a scripting language; I felt like I wanted to remove the need for brackets and work off indentation / colons and remove semi-colons, so I created my own parser in C# called Desolve.
So I created a small program
import std.stdio;
void main:
for(auto i = 0; i < 10; i++):
writeln(i)
if i > 5 && < 8:
writeln("higher than five and lower than 8")
else if i > 8:
writeln("higher than eight")
writeln("Done")
Then I decided, to make the parser also automatically import std.stdio, wrap my code in the main method and replace certain key-words such as:
var -> auto
writeln() -> print()
readln() -> readline()
# -> //
#[ -> /*
#] -> */
After that, I decided to explore how I would go using D to do a lot of work for me by using my parser to generate some meta-code for me. Say for example, I wanted to create a Python-like function, like:
Parser input:
def foo(name):
var reply = "Hello, " + name
return reply
Parser generated D:
auto foo(T)(auto ref T name)
{
auto reply = "Hello, " ~ name;
return reply;
}
It works the same way for multiple parameter functions
Parser input:
def doStuff(x, y):
print("ok")
Parser generated D:
auto doStuff(T,S)(auto ref T x,auto ref S y)
{
writeln("ok");
}
To append a string to another string in D, you use the ~ operator. To avoid this:
Parser input:
var foo = "hello, "
var bar = " world"
var x = foo + bar
print(x)
Parser generated D:
auto foo = "hello, ";
auto bar = " world!";
static if (is(typeof(foo) == string))
{
auto x = foo ~ bar;
}
else
{
auto x = foo + bar;
}
writeln(x);
And these are just a few examples of what I've come up with. The parser is by no means generating efficient code or even meant for use outside of my own experimentation/fun, but I sure hope someone in the near future utilises D for certain projects such as this one. My point of this post is really: has anyone here done something similar to this? and if so, for what purpose?