www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error in template instantiation from D-Cookbook example

reply ShadoLight <ettienne.gilbert gmail.com> writes:
Hi,

Trying to improve my CT-fu, I looked at a DSL example (chapter 9) 
in Adam's D-Cookbook. Although I am sure Adam's original examples 
would have worked, there were some errors in the example code in 
the book.

The example also contains some code to allow you to test the 
functions at RT, which had an error i.e. I had to change this...

	writeln(parse(code));		// to aid with debugging
	writeln(convertToD(parse(code)));	// debugging aid
..to..
	writeln(parse(code)[]);		// to aid with debugging
	writeln(convertToD(parse(code))[]);	// debugging aid

..to match the actual function arguments. So that worked fine.

However I could not get the CT version to work with the current 
version of DMD i.e. this [1]...

[1] https://run.dlang.io/is/dyElXg

...fails with:
onlineapp.d-mixin-36(38): Error: template argument expected 
following !
onlineapp.d(48): Error: template instance onlineapp.runDslCode!"5 
5 + 3 - 2 * 1 + 3 /" error instantiating.

I find the error message a bit lacking as to the real cause - I 
really cannot see where the "template argument expected following 
!" error is. It all looks correct to me.

In addition the RT version is working correctly (after fixing the 
above bug), so that is not helping in identifying the error - 
particularly since it is failing in the only CT specific function 
not used/called at RT. So I am not sure how to debug this.

Can somebody please put me out of my misery here?

Thx!
Feb 09 2018
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 9 February 2018 at 21:31:29 UTC, ShadoLight wrote:
 [1] https://run.dlang.io/is/dyElXg
There's missing quotes in there: Line 14: code ~= "push(call!"~piece~"(pop(), pop()));\n"; Should be: code ~= "push(call!\""~piece~"\"(pop(), pop()));\n"; That might have been an error in the original book. A lot of quotes and semicolons got mangled in the process of copying them from the actual compilable examples into the MS Word manuscript.. But to explain why this is wrong just consider the code generated: push(call!+(pop(), pop())); When the operator is pasted in without quotes, you get the above. And + isn't a valid template arg. But "+" is.
Feb 09 2018
prev sibling parent reply Meta <jared771 gmail.com> writes:
On Friday, 9 February 2018 at 21:31:29 UTC, ShadoLight wrote:
 	writeln(parse(code));		// to aid with debugging
 	writeln(convertToD(parse(code)));	// debugging aid
 ..to..
 	writeln(parse(code)[]);		// to aid with debugging
 	writeln(convertToD(parse(code))[]);	// debugging aid
The problem becomes apparent once you uncomment one of these and paste the offending string ("5 5 + 3 - 2 * 1 + 3 /") in. `writeln(convertToD(parse("5 5 + 3 - 2 * 1 + 3 /"))[]);` prints the following: push(5); push(5); push(call!+(pop(), pop())); push(3); push(call!-(pop(), pop())); push(2); push(call!*(pop(), pop())); push(1); push(call!+(pop(), pop())); push(3); push(call!/(pop(), pop())); If you look at the definition of call: int call(string op)(int a, int b) { return mixin("b"~op~"a"); } So it takes a string as its sole template argument. The problem is that the code in convertToD forgot to add the quotes around the operators that are supposed to be passed as strings to call. If you modify line 14 from the example you pasted to add these quotes to the mixin string, it works: code ~= "push(call!"~piece~"(pop(), pop()));\n"; becomes code ~= "push(call!\""~piece~"\"(pop(), pop()));\n"; Running the modified code, it prints: push(5); push(5); push(call!"+"(pop(), pop())); push(3); push(call!"-"(pop(), pop())); push(2); push(call!"*"(pop(), pop())); push(1); push(call!"+"(pop(), pop())); push(3); push(call!"/"(pop(), pop())); And `runDslCode!"5 5 + 3 - 2 * 1 + 3 /"();` prints `[5]`.
Feb 09 2018
parent ShadoLight <ettienne.gilbert gmail.com> writes:
On Friday, 9 February 2018 at 21:39:22 UTC, Adam D. Ruppe wrote:
 On Friday, 9 February 2018 at 21:31:29 UTC, ShadoLight wrote:
 [1] https://run.dlang.io/is/dyElXg
There's missing quotes in there: Line 14: code ~= "push(call!"~piece~"(pop(), pop()));\n"; Should be: code ~= "push(call!\""~piece~"\"(pop(), pop()));\n";
[snip] On Friday, 9 February 2018 at 21:58:51 UTC, Meta wrote:
 On Friday, 9 February 2018 at 21:31:29 UTC, ShadoLight wrote:
[snip]
 The problem becomes apparent once you uncomment one of these 
 and paste the offending string ("5 5 + 3 - 2 * 1 + 3 /") in.
[snip]
 push(call!+(pop(), pop()));
[snip]
 So it takes a string as its sole template argument. The problem 
 is that the code in convertToD forgot to add the quotes around 
 the operators that are supposed to be passed as strings to call.
[snip] Indeed, that makes sense! Thanks to you both! Adam, you are right - it was indeed incorrectly mangled... it is shown as... code ~= "push(call!'"~piece~"'(pop(), pop()));\n"; So the escaped quotes \" were mangled as '. I simply removed them since I assumed they were typos in the doc, similar to the other ones I had fixed. My bad. Man, I cannot believe how quickly you guys answered. Thanks again!
Feb 09 2018