adder
adder copied to clipboard
Defining Functions and Executing them Externally
Love this project, it looks like it could be perfect for my needs.
I read through the docs and I couldn't find an answer to the question I have.
Is it possible, and if so how would one go about doing so, for a user script to define a function and for that function to be executed externally in Node.js.
For example, could I have an adder script like so:
# Click handler
def onClick():
print("Clicked");
And then call that function from Node.js:
// Call event handler
program.execute("onClick");
Or something along those lines? I couldn't find anything in the docs about this. Thanks for the help!
I've possibly answered my own question, at least partially, looking through the source code in /interpreter/interpreter.js, I see this method:
callFunction: function(func, args, object)
Seems fairly self explanatory, posting my own answer here for now in case others find this with similar query, if anyone wishes to pitch in any further advice it would be welcome. I'll try out the method and see how I go.
OK I figured this out and so I'm going to leave an example here for anyone else who has the same question later on. Turns out I guessed correctly and program.execute() does take the name of a function to call!
So here's an example of a node.js script:
const AdderScript = require('adder-script');
const fs = require('fs');
// Load the script
let script = fs.readFileSync('example.adder', 'utf8');
// Init AdderScript
AdderScript.init();
// Compile script
compiledCode = AdderScript.compile(script);
// Make program
var program = AdderScript.newProgram(compiledCode);
// Execute it (on first run it will define the function)
program.execute();
// Will output 'Hello World'
// Execute the defined function 'test'.
program.execute('test');
// Will Output 'Test'
And the corresponding adder script it loads:
print("Hello World");
def test():
print("Test");