www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compiler concept

reply Ignacious <I.D.T ProjectMaya.com> writes:
Would it be difficult to implement the following, or something 
with similar capabilities, as a patch to dmd?

Create a compiler that lets one use actual code as templates. 
e.g., a text block of a function can be referenced by in the code 
using a "dom" to modify that code similar to how we can modify 
html.

int foo(x)
    if x > 1
       return 3;
    return 4;

int :foo(x) bar(x)
[
	_dom[return][0] = 8;	/* _dom is a keyword representing the code 
block, in a dom format, of the function foo. The rh expression is 
checked for validity just as if it were used in the function. 
Here we set the first return to the express 8. This gives the 
same function but returns 8 instead of 3 at this point.
	!_dom[return][1]; 		/* The bang kills the 2nd return statement 
removing it from the body as if it were never typed, at this 
point, bar would be invalid
]
{

	static if _dom.Lines == 3 && _dom.#x <= 6
		return 1;

	/* We have added back a valid return statement, Now bar returns 
1 when x <=1 else 3. Not a very useful function, but it 
demonstrates how a dom like typesafe syntax might be used
}


The main problem such a model is consistency as changing foo 
would generally break any dependencies quite easily. An IDE can 
remedy this quite well by simply checking if any of the 
corresponding _dom based functions of a function that has been 
modified are still consistent. Unfortunately there is no real way 
to keep consistency in behavior in general is this is even a 
problem with function overloading.
Dec 12 2016
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 13 December 2016 at 01:03:54 UTC, Ignacious wrote:
 Would it be difficult to implement the following, or something 
 with similar capabilities, as a patch to dmd?
It would be pretty hard to do with dmd because compiled code isn't designed for modification, but it wouldn't be too hard to do with an interpreter, which uses a tree of nodes already anyway. Dynamic (and JIT VM based) languages often use this kind of thing internally for optimizations and sometimes for self-modifying code.
Dec 12 2016
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 13/12/2016 2:57 PM, Adam D. Ruppe wrote:
 On Tuesday, 13 December 2016 at 01:03:54 UTC, Ignacious wrote:
 Would it be difficult to implement the following, or something with
 similar capabilities, as a patch to dmd?
It would be pretty hard to do with dmd because compiled code isn't designed for modification, but it wouldn't be too hard to do with an interpreter, which uses a tree of nodes already anyway. Dynamic (and JIT VM based) languages often use this kind of thing internally for optimizations and sometimes for self-modifying code.
As CTFE has proven, its do-able but we would be talking at least 6-9 months worth of work. With no way its going into upstream. Not to mention we'd need a whole host of AST nodes in druntime. So really not worth the effort.
Dec 12 2016
parent reply Jacob Carlborg <doob me.com> writes:
On 2016-12-13 04:38, rikki cattermole wrote:

 As CTFE has proven, its do-able but we would be talking at least 6-9
 months worth of work.
 With no way its going into upstream.

 Not to mention we'd need a whole host of AST nodes in druntime.
 So really not worth the effort.
Doing something like AST macros (which this sounds like) is not _that_ difficult. A bit time consuming, yes. But I don't think it would need to take 6-9 months for someone dedicated. -- /Jacob Carlborg
Dec 12 2016
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 13 December 2016 at 07:44:54 UTC, Jacob Carlborg 
wrote:
 Doing something like AST macros (which this sounds like) is not 
 _that_ difficult.
I was thinking doing it at run time... doing it at compile time indeed isn't that bad (it still looks like the interpreter tree at that point). The hard part for dmd would be more in documenting a stable api than the actual code part.
Dec 13 2016
prev sibling parent reply Swoorup Joshi <swoorupjoshi gmail.com> writes:
On Tuesday, 13 December 2016 at 01:03:54 UTC, Ignacious wrote:
 Would it be difficult to implement the following, or something 
 with similar capabilities, as a patch to dmd?

 [...]
Isn't this what string mixin is for
Dec 13 2016
parent bauss (wtf happend to my name took some old cached title LOL??) writes:
On Tuesday, 13 December 2016 at 12:58:42 UTC, Swoorup Joshi wrote:
 On Tuesday, 13 December 2016 at 01:03:54 UTC, Ignacious wrote:
 Would it be difficult to implement the following, or something 
 with similar capabilities, as a patch to dmd?

 [...]
Isn't this what string mixin is for
Not really, since you don't get a tree of each expression nodes. It's more like macros. Pretty similar to what Nim does. http://nim-lang.org/docs/macros.html
Dec 13 2016