code generation

code-generation
Fabien Benetou 1 year ago
parent 58ca14f26e
commit 37b6e05882
  1. 32
      index.html

@ -1426,14 +1426,16 @@ function interpretJXR( code ){
commandhistory.push( {date: +Date.now(), uninterpreted: uninterpreted, interpreted: parseCode} )
console.log( parseCode )
let res
try {
eval( parseCode )
res = eval( parseCode )
} catch (error) {
console.error(`Evaluation failed with ${error}`);
}
// unused keyboard shortcuts (e.g BrowserSearch) could be used too
// opt re-run it by moving the corresponding text in target volume
return res
}
AFRAME.registerComponent('toolbox', { // ununsed
@ -2332,6 +2334,34 @@ function jxrhighlight(){
// see also generalization via https://shiki.matsu.io custom renderers
}
function generateCode(mycode = 'function randomArray(){', callback=console.log){
fetch("https://bigcode-santacoder-demo.hf.space/run/predict", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
data: [ mycode, 48, 0.2, 0, ] // see https://bigcode-santacoder-demo.hf.space/?view=api for payload and result
})})
.then(r => r.json())
.then(
r => {
let generatedCode = r.data[0].replace(/.*#727cd6;'>/, '').replace(/<\/span.*\n/,'\n').replace(/<\/span.*/,'');
parseResult( generatedCode )
}
)
function parseResult(resultingCode){
callback(resultingCode) // assume we have a-console for review
if ( resultingCode.match(/^function /) ){
if (resultingCode.substr(-1)!="}") resultingCode+="}" // happens often without the <FILL-HERE> token
// assumes sticking to JavaScript function definition that can then be called via jxr, safer and more manipulable
var generatedCodeToEval = resultingCode.replaceAll('\n','')
eval?.( generatedCodeToEval ) // forcing global scope but fails, at least result is undefined in that scope
addNewNote("jxr "+ resultingCode.replace(/^function /, '').replaceAll('\n','').replace(/{.*/, ''))
}
// example of callback, could instead let user review it first, name it and only evaluate if seemingly correct
}
}
</script>
<div id="observablehq-key">
<div id="observablehq-viewof-offsetExample-ab4c1560"></div>

Loading…
Cancel
Save