Skip to content

Commit

Permalink
Introduce polyglot magic to choose execution language dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
jak-ing committed Apr 30, 2019
1 parent 2eeac5c commit ea2a0ef
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions lib/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,32 @@ process.on("SIGINT", function() {
});


function polyglotirize(rawCode) {
return `Polyglot.eval('python', '${rawCode}')`;
function polygloterize(rawCode) {
const rawCodeLines = rawCode.split('\n');
if(rawCodeLines.length < 2) {
return rawCode;
}

const firstLine = rawCodeLines[0]
const otherLines = rawCodeLines.slice(1)

if(!firstLine.startsWith('%polyglot ')) {
return rawCode;
}

const languageIdentifier = firstLine.split(' ')[1]

return `
try {
Polyglot.eval(
'${languageIdentifier}',
'${otherLines.join('\n')}'
);
} catch(e) {
console.error('An error occured while trying to execute Polyglot code:');
console.error(e);
}
`
}

/**
Expand All @@ -125,7 +149,7 @@ function parseCommandArguments() {
startupCallback: function() {
log("startupCallback:", this.startupCallback);
},
transpile: polyglotirize,
transpile: polygloterize,
};

var usage = (
Expand Down

0 comments on commit ea2a0ef

Please sign in to comment.