Some preProcessor woes


#1

Trying to get my head tuned to the inner workings of Interpreter’s preProcessor. Maybe someone can explain why it always posts a nil in both the true func and the false func below?
The true func works besides the ugliness of the nil post. The false func should just send the code to the regular interpreter, treating it as a normal sc line. It doesn’t.

(
thisProcess.interpreter.preProcessor = {|codeBlock|
	var b = codeBlock.split($\n);
	b.do{|code|
		c = code.split($ );
		if(code.beginsWith("w"), {
			c.scramble.postln;
		}, {
			code;
		});
	};
};
)

(
why does the preProcessor return nil when executing this block? the scramble function obviously works.
why oh why? it looks bad in my post window.
)

"and why does this return nil and not just this string?"

#2

Is this false func thing about return context maybe? Returning outside of the do loop like this actually seems to work:

   (
    thisProcess.interpreter.preProcessor = {|codeBlock|
    	var o, b = codeBlock.split($\n);
    	b.do{|code|
    		c = code.split($ );
    		if(code.beginsWith("w"), {
    			c.scramble.postln;
    		}, {
    			o = code.replace("(").replace(")");
    		});
    	};
    	o;
    };
    )

    (
    why does the preProcessor return nil when executing this block?
    why oh why? it looks bad in my post window.
    )

    "and why does this return nil?"

#3

The preProcessor takes a string as input, and it should return one and only one string as its result.

The right approach is to split the string, do what you want to the component parts (collect) and then ‘join’ back into the final result.

codeBlock.split($\n).collect { |code|
	c = code.split($ );
    	if(code.beginsWith("w"), {
    		c.scramble.postln;
		"0;"  // you might need a dummy here?
    	}, {
    		code.replace("(").replace(")");
    	});
}
.join;

Note that I haven’t tested this, but my very large preprocessor project takes this approach. https://github.com/jamshark70/ddwChucklib-livecode/blob/master/preprocessor.scd#L83

“and why does this return nil?”

Ctrl-Return goes through a method called “interpretPrintCmdLine” – meaning that it is going to interpret something and print the result. It is always going to interpret something, and always going to print something. "".interpret --> nil.

The preprocessor function needs to return some code whose result will print something meaningful.

hjh


#4

Thank you! I actually had a look at your project while investigating this, but as you say, it’s huge. A bit difficult to immediately understand how you do it. I will study it further.