|
| 1 | +functionprocessResult(result,error,output){ |
| 2 | +if(result[0]===1){ |
| 3 | +error.innerText=result[1]; |
| 4 | +output.innerText=""; |
| 5 | +}else{ |
| 6 | +error.innerText=""; |
| 7 | +output.innerText=result[1]; |
| 8 | +} |
| 9 | +} |
| 10 | + |
| 11 | +functionprocessLocal(input,format,output,error){ |
| 12 | +letresult=apidsl.parse("input.api.h",input); |
| 13 | +if(result[0]===0){ |
| 14 | +if(format==="haskell"){ |
| 15 | +result=apidsl.haskell("MyAPI",result[1]); |
| 16 | +}else{ |
| 17 | +result=apidsl[format](result[1]); |
| 18 | +} |
| 19 | +} |
| 20 | + |
| 21 | +processResult(result,error,output); |
| 22 | +} |
| 23 | + |
| 24 | +functionprocessRemote(input,format,output,error){ |
| 25 | +constapiUrl="https://apidsl2.herokuapp.com"; |
| 26 | +fetch(apiUrl+"/parse",{ |
| 27 | +method:"POST", |
| 28 | +body:JSON.stringify(["Request",input]) |
| 29 | +}).then((data)=>data.json()).then((result)=>{ |
| 30 | +if(result[0]===0){ |
| 31 | +fetch(apiUrl+"/"+format,{ |
| 32 | +method:"POST", |
| 33 | +body:JSON.stringify(["Request",result[1]]) |
| 34 | +}).then((data)=>data.json()).then((result)=>{ |
| 35 | +processResult(result,error,output); |
| 36 | +}); |
| 37 | +}else{ |
| 38 | +processResult(result,error,output); |
| 39 | +} |
| 40 | +}); |
| 41 | +} |
| 42 | + |
| 43 | +functionexceptionType(err){ |
| 44 | +if(errinstanceofArray){ |
| 45 | +if(err[1]instanceofObject&&"c"inerr[1]){ |
| 46 | +returnerr[1].c; |
| 47 | +} |
| 48 | +} |
| 49 | +returnnull; |
| 50 | +} |
| 51 | + |
| 52 | +functionisStackOverflow(err){ |
| 53 | +returnexceptionType(err)==="Stack_overflow"|| |
| 54 | +(exceptionType(err)==="Js_of_ocaml__Js.Error"&& |
| 55 | +err[2].includes("Maximum call stack size exceeded")); |
| 56 | +} |
| 57 | + |
| 58 | +functionprocess(){ |
| 59 | +leterror=document.getElementById("error"); |
| 60 | +letinput=document.getElementById("input").value; |
| 61 | +letoutput=document.getElementById("output"); |
| 62 | +letformat=document.querySelector("input[name="format"]:checked").value; |
| 63 | + |
| 64 | +try{ |
| 65 | +processLocal(input,format,output,error); |
| 66 | +}catch(err){ |
| 67 | +if(isStackOverflow(err)){ |
| 68 | +processRemote(input,format,output,error); |
| 69 | +}else{ |
| 70 | +console.log("Unhandled exception ",err); |
| 71 | +} |
| 72 | +} |
| 73 | +} |
| 74 | + |
| 75 | +functionload(snippet){ |
| 76 | +constsnippets={ |
| 77 | +"simple":"static int main();", |
| 78 | +"comment": |
| 79 | +`/** |
| 80 | + * The main function. |
| 81 | + */ |
| 82 | +static int main();`, |
| 83 | +"namespace": |
| 84 | +`namespace foo { |
| 85 | + /** |
| 86 | + * This is $main. |
| 87 | + */ |
| 88 | + static int32_t main(); |
| 89 | +}`, |
| 90 | +"large": |
| 91 | +`class foo { |
| 92 | + /** |
| 93 | + * The "this" type for all non-static functions. |
| 94 | + */ |
| 95 | + struct this; |
| 96 | +
|
| 97 | + /** |
| 98 | + * This is $main. |
| 99 | + */ |
| 100 | + int32_t main(); |
| 101 | +
|
| 102 | + namespace bar { |
| 103 | + uint32_t blep(); |
| 104 | + } |
| 105 | +
|
| 106 | + uint8_t some_property { get(); set(); } |
| 107 | +}` |
| 108 | +}; |
| 109 | + |
| 110 | +constfileUrl="https://raw.githubusercontent.com/TokTok/c-toxcore/master/toxcore/tox.api.h"; |
| 111 | + |
| 112 | +letinput=document.getElementById("input"); |
| 113 | +if(snippet==="tox"){ |
| 114 | +fetch(fileUrl).then(data=>data.text()).then(text=>{ |
| 115 | +input.value=text; |
| 116 | +process(); |
| 117 | +}); |
| 118 | +}else{ |
| 119 | +input.value=snippets[snippet]; |
| 120 | +process(); |
| 121 | +} |
| 122 | +} |