5.9 KiB
title, id, localeTitle, challengeType
title | id | localeTitle | challengeType |
---|---|---|---|
Execute Brain**** | 59e0a8df964e4540d5abe599 | 59e0a8df964e4540d5abe599 | 5 |
Description
Escribe una función para implementar un intérprete de Brain ****. La función tomará una cadena como parámetro y debería devolver una cadena como salida. Se dan más detalles a continuación :
RCBF es un conjunto de compiladores e intérpretes Brainf *** escritos para Rosetta Code en varios idiomas.
A continuación hay enlaces a cada una de las versiones de RCBF.
Una implementación solo necesita implementar correctamente las siguientes instrucciones:
{|
!Mando
!Descripción
| -
| style = "text-align: center" | >
|| Mueve el puntero a la derecha
| -
| style = "text-align: center" | <
|| Mueve el puntero a la izquierda
| -
| style = "text-align: center" | +
|| Incrementar la celda de memoria debajo del puntero.
| -
| style = "text-align: center" | -
|| Disminuye la celda de memoria debajo del puntero.
| -
| style = "text-align: center" | .
|| Muestra el carácter que significa la celda en el puntero.
| -
| style = "text-align: center" | ,
|| Ingrese un carácter y guárdelo en la celda en el puntero
| -
| style = "text-align: center" | [
|| Salta la coincidencia ]
si la celda debajo del puntero es 0
| -
| style = "text-align: center" | ]
|| Saltar de nuevo a la coincidencia [
si la celda debajo del puntero no es cero
|}
Se permite cualquier tamaño de celda, la compatibilidad con EOF ( E nd- O - F ) es opcional, ya que tiene memoria limitada o no limitada.
Instructions
Tests
tests:
- text: <code>brain(bye)</code> debe retuen una cuerda
testString: 'assert(typeof brain(bye) === "string", "<code>brain(bye)</code> should return a string");'
- text: <html> ' <code>brain("++++++[>++++++++++<-]>+++++.")</code>
testString: 'assert.equal(brain("++++++[>++++++++++<-]>+++++."),"A", "<code>brain("++++++[>++++++++++<-]>+++++.")</code should return "A"");'
- text: ' <code>brain(bye)</code> debe devolver ¡ <code>Goodbye, World!\\r\\n</code> '
testString: 'assert.equal(brain(bye), "Goodbye, World!\r\n", "<code>brain(bye)</code> should return <code>Goodbye, World!\\r\\n</code>");'
- text: <code>brain(hello)</code> debería devolver <code>Hello World!\\n</code> '
testString: 'assert.equal(brain(hello), "Hello World!\n", "<code>brain(hello)</code> should return <code>Hello World!\\n</code>");'
- text: ' <code>brain(fib)</code> debe devolver <code>1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89</code> '
testString: 'assert.equal(brain(fib), "1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89", "<code>brain(fib)</code> should return <code>1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89</code>");'
Challenge Seed
function brain (prog) {
// Good luck!
}
Before Test
let fib=`+
++
+++
++++
+>+>>
>>++++
+++++++
++++++++
+++++++++
++++++++++
++++++>++++
++++++++++++
+++++++++++++
+++<<<<<<[>[>>
>>>>+>+<<<<<<<-
]>>>>>>>[<<<<<<<
+>>>>>>>-]<[>++++
++++++[-<-[>>+>+<<
<-]>>>[<<<+>>>-]+<[
>[-]<[-]]>[<<[>>>+<<
<-]>>[-]]<<]>>>[>>+>+
<<<-]>>>[<<<+>>>-]+<[>
[-]<[-]]>[<<+>>[-]]<<<<
<<<]>>>>>[++++++++++++++
+++++++++++++++++++++++++
+++++++++.[-]]++++++++++<[
->-<]>+++++++++++++++++++++
+++++++++++++++++++++++++++.
[-]<<<<<<<<<<<<[>>>+>+<<<<-]>
>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]
<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+
>-]>[<+>-]<<<-]`;
let hello='++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.'
let bye='++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>>+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++.<+++++++.--------.<<<<<+.<+++.---.';
Solution
function brain(prog){
var output="";
var code; // formatted code
var ip = 0; // current instruction within code
var nest = 0; // current bracket nesting (for Out button)
var ahead = []; // locations of matching brackets
var data = [0]; // data array (mod by +, -)
var dp = 0; // index into data (mod by <, >)
var inp = 0; // current input character (fetch with ,)
var quit = 0;
var commands = {
'>':function() { if (++dp >= data.length) data[dp]=0 },
'<':function() { if (--dp < 0) quit++ },
'+':function() { ++data[dp] },
'-':function() { --data[dp] },
'[':function() { if (!data[dp]) ip = ahead[ip]; else ++nest },
']':function() { if ( data[dp]) ip = ahead[ip]; else --nest },
',':function() {
var c = document.getElementById("input").value.charCodeAt(inp++);
data[dp] = isNaN(c) ? 0 : c; // EOF: other options are -1 or no change
},
'.':function() {
output+=String.fromCharCode(data[dp]);
/*var s = document.getElementById("output").innerHTML)
+ String.fromCharCode(data[dp]);
s = s.replace(/\n/g,"<br>").replace(/ /g,"&nbsp;");
document.getElementById("output").innerHTML = s;*/
},
};
let ar=prog.split('');
var st = [], back, error = -1;
for (ip=0; ip<ar.length; ip++) {
switch(ar[ip]) {
case '[':
st.push(ip);
break;
case ']':
if (st.length == 0) error = ip;
back = st.pop();
ahead[ip] = back;
ahead[back] = ip;
break;
}
}
for(ip=0;ip<ar.length;ip++){
if(commands.hasOwnProperty(ar[ip]))
commands[ar[ip]]();
}
return output;
}