#JavaScript (sample answer)
Interpreting JavaScript using JavaScript is somewhat useless, but this shows what your interpreter snippets should roughly behave like.
Here is the bare bones version, intended for quick use in other posts. To use it with preinitialized code and input you only need to copy the snippet and reassign the code
and input
variables in the first script tag. You'll unfortunately have to escape newlines and double quotes.
BEGIN_CODE
// Put JavaScript code here
// it may be on multiple lines
END_CODE_BEGIN_INPUT
Put input here
it may be on multiple lines
END_INPUT
<script>i=document.body.innerHTML;document.body.innerHTML=''</script>
<script>
c=i.match(/BEGIN_CODE([\s\S]*)END_CODE_BEGIN_INPUT/)
c=c&&c.length>1?c[1]:'Error reading code. Make sure it is between BEGIN_CODE and END_CODE_BEGIN_INPUT.'
i=i.match(/END_CODE_BEGIN_INPUT([\s\S]*)END_INPUT/)
i=i&&i.length>1?i[1]:'Error reading input. Make sure it is between END_CODE_BEGIN_INPUT and END_INPUT.'
</script>JavaScript:<br><tt>function(x) {</tt><br><textarea id='c'rows='4'cols='60'></textarea><br><tt>}</tt><br><br>Input string (variable x):<br><textarea id='i'rows='4'cols='60'></textarea><p><input type='button'value='Run'onclick='r()'></p>Output (return value):<br><textarea id='o'rows='4'cols='60'style='background-color:#f0f0f0;'></textarea><script>document.getElementById('c').value=c;document.getElementById('i').value=i;function r(){var r,c,i;r=c=i=null;eval('var f=function(x) {'+document.getElementById('c').value+'}');document.getElementById('o').value=String(f(document.getElementById('i').value))}</script>
The raw Markdown for including this Stack Snippet in a post is:
<!-- begin snippet: js hide: false -->
<!-- language: lang-html -->
<script>
code = "//PUT JAVASCRIPT CODE HERE"
input = "PUT INPUT HERE"
//IGNORE EVERYTHING BELOW
</script>JavaScript:<br><tt>function(x) {</tt><br><textarea id='c'rows='4'cols='60'></textarea><br><tt>}</tt><br><br>Input string (global variable x):<br><textarea id='i'rows='4'cols='60'></textarea><p><input type='button'value='Run'onclick='r()'></p>Output (global variable y):<br><textarea id='o'rows='4'cols='60'style='background-color:#f0f0f0;'></textarea><script>document.getElementById('c').value=code;document.getElementById('i').value=input;function r(){var r=null,code=null,input=null;eval('var f=function(x) {'+document.getElementById('c').value+'}');document.getElementById('o').value=String(f(document.getElementById('i').value))}</script>
<!-- end snippet -->
Below is the ungolfed general version, complete with example and a bit more styling.
function example() {
document.getElementById('code').value = "var lines = x.split('\\n')\nvar sum = 0\nfor (var i = 0; i < lines.length; i++) {\n var val = parseInt(lines[i])\n if (!isNaN(val)) {\n sum += val\n }\n}\nreturn sum"
document.getElementById('input').value = '1\n2\n3'
run()
}
function run() {
var run = null, example = null; //shadow other globals
eval('var f=function(x) {' + document.getElementById('code').value + '}')
document.getElementById('output').value = String(f(document.getElementById('input').value))
}
#output {
background-color: #f0f0f0;
}
#run {
font-size: 120%;
}
JavaScript:<br>
<tt>function(x) {</tt>
<br>
<textarea id='code' rows='12' cols='60'></textarea>
<br><tt>}</tt><br><br>
Input string (variable x):
<br>
<textarea id='input' rows='6' cols='60'></textarea>
<p><input id='run' type='button' value='Run' onclick='run()'></p>
Output (return value):
<br>
<textarea id='output' rows='6' cols='60'></textarea>
<p><a id='example' href='javascript:void(0)' onclick='example()'>Example: Sum Numbers</a></p>