Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411423 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 18, 2024, 04:39:47 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Desolve
Pages: [1]
Print
Author Topic: Desolve  (Read 731 times)
Gleade
Level 0
*


View Profile
« on: July 05, 2017, 09:22:28 PM »

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

Code:
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:

Code:
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:
Code:
def foo(name):
var reply = "Hello, " + name
return reply

Parser generated D:
Code:
auto foo(T)(auto ref T name)
{
auto reply = "Hello, " ~ name;
return reply;
}

It works the same way for multiple parameter functions

Parser input:
Code:
def doStuff(x, y):
print("ok")

Parser generated D:
Code:
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:
Code:

var foo = "hello, "
var bar = " world"

var x = foo + bar

print(x)


Parser generated D:
Code:
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?
Logged

Find my game Dire on:
JWki
Level 4
****


View Profile
« Reply #1 on: July 06, 2017, 03:24:52 AM »

Well that's how languages often start I guess.
FWIF I know that hardcore C game programmers often use this to generate C code going off a higher level syntax, often domain specific. Shawn McGrath did something similiar where he rolled his own inheritance type system, allowing him to define types with inheritance relationships in a syntax that is similiar to C++ classes, while generating C code that would implement the type system using tagged unions.
Logged
Gleade
Level 0
*


View Profile
« Reply #2 on: July 06, 2017, 02:50:22 PM »

Well that's how languages often start I guess.

That's very true.

FWIF I know that hardcore C game programmers often use this to generate C code going off a higher level syntax, often domain specific.

That makes sense. The main reason I began programming was because I wanted to develop games; I was using C++, which was an excellent start of point for myself. After 5 or 6 years I started working on "big" solo projects in C++ and just got over having to manage headers and source files separately. Luckily for me I had picked up C#/Java during my course which eventually lead me onto the road of D. The end goal for Desolve will be to adapt my C# game engine to Desolve's standard library.

Shawn McGrath did something similiar where he rolled his own inheritance type system, allowing him to define types with inheritance relationships in a syntax that is similiar to C++ classes, while generating C code that would implement the type system using tagged unions.

That's really cool! I'll have to look into that.

Eventually I want to look into packing .dlls / dylibs / .so into the executable and extract them + the executable to a temporary sandbox folder depending on the OS.
Logged

Find my game Dire on:
TheGiant
Level 0
**



View Profile
« Reply #3 on: July 07, 2017, 12:08:22 AM »

This would be a great plugin for Visual Studio. I hate semicolons.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic