digitalmars.D - Compiler concept
- Ignacious (35/35) Dec 12 2016 Would it be difficult to implement the following, or something
- Adam D. Ruppe (7/9) Dec 12 2016 It would be pretty hard to do with dmd because compiled code
- rikki cattermole (6/14) Dec 12 2016 As CTFE has proven, its do-able but we would be talking at least 6-9
- Jacob Carlborg (6/11) Dec 12 2016 Doing something like AST macros (which this sounds like) is not _that_
- Adam D. Ruppe (7/9) Dec 13 2016 I was thinking doing it at run time... doing it at compile time
- Swoorup Joshi (2/5) Dec 13 2016 Isn't this what string mixin is for
- bauss (wtf happend to my name took some old cached title LOL??) (4/10) Dec 13 2016 Not really, since you don't get a tree of each expression nodes.
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
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
On 13/12/2016 2:57 PM, Adam D. Ruppe wrote:On Tuesday, 13 December 2016 at 01:03:54 UTC, Ignacious 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.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
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
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
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
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: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.htmlWould 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