JavaScript(Node.js),204字节
process.openStdin(f=a=>process.stdout.write((i=0,""+a).split` `.map(x=>([...x.slice(0,-1)].map(d=>({i:x=e=>i++,d:e=>i--,s:k=e=>i*=i,o:c=e=>e,x,k,c})[d](i=-1||i==256?i=0:0)),i))+"\n>> "),f``).on("data",f)
这可能是打高尔夫球的。Node.js再次证明它是奇怪的变相冗长。代码说明:
process.openStdin( // This function has to be called to take input, but doesn't have arguments
f=a=> // Define a function f. This is the deadfish interpreter. It takes an argument `a` which is a Buffer
process.stdout.write( // Same as console.log, but doesn't output trailing newline
(i = 0, "" + a) // Take advantage of comma operator to (A) define the accumulator i, and casts a (which is a Buffer) to a String
.split` ` // Split the string a at spaces, making it an array
.map( // Map through each element of the array
x=> // Map function, takes argument x, the value in the array (string)
([...x.slice(0,-1)] // Remove the last character (newline) and than use the spread operator to divide the string into an array of it's chars
.map(d=> // Map function, you know how this works
({ // Here I define the various deadfish commands
i: x = e => i++,
d: e => i--,
s: k = e => i*=i,
o: c = e => e,
// Take advantage of ES6 object notation. Equivilent to {"x": x, "k": k, "c", c}
x,
k,
c
})
[d] // Get the command to execute. If this is passed something which isn't valid, a giant error will appear
(
i==-1 || i==256 ? i = 0 : 0 // Take advantage of the fact that none of the command functions take arguments to handle the strange "overflow"
)
),
i)
) +
"\n>> "), // Display the prompt again, as well as a newline
f`` // Initalize the prompt by passing an empty script
)
.on("data",f) // Bind the f function to newline on STDIN