Compiler concept

Ignacious via Digitalmars-d digitalmars-d at puremagic.com
Mon Dec 12 17:03:54 PST 2016


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.


More information about the Digitalmars-d mailing list