8
2
You have to write an interpreter for a cool language called Chicken!
You should read a Chicken program from a file, standard input, program or function arguments, or whatever is most convenient for your language, as well as input to the program.
You should print or return the result of interpreting the program according to the Chicken language specification.
More description about the language.
Chicken Program Overview
Chicken operates on a single stack, which composes its entire memory model. As instructions are executed, the program will push and pop values from the stack, but there are also instructions that allow the program to modify other parts of the stack at will.
There are three segments in the stack:
- The registers, at indices 0 and 1. Index 0 is a reference to the stack itself, and index 1 is a reference to the user input. Mostly used for instruction 6 (see below).
- The loaded code: for each line of code there is cell in this segment that contains the number of "chicken"s in the line. This is padded with a 0 (opcode for terminate program) at the end.
- The actual program stack, where values are pushed/popped as the program runs. Note that the segments are not isolated, which means that it is possible to create self-modifying code or execute code from this segment of the stack space.
The Chicken ISA
Chicken's instruction set is based around the number of times the word "chicken" appears on each line of the program. An empty line terminates the program and prints the topmost value in the stack.
The Chicken instruction set, by number of "chicken"s per line:
- Push the literal string "chicken" to the stack
- Add top two stack values as natural numbers and push the result.
- Subtract top two values as natural numbers and push the result.
- Multiply top two values as natural numbers and push the result.
- Compare two top values for equality, push 1 if they are equal and 0 otherwise.
- Look at the next instruction to determine which source to load from: 0 loads from stack, 1 loads from user input. Top of stack points to address/index to load from the given source; load that value and push it onto stack. Since this is a double wide instruction, the instruction pointer skips the instruction used to determine the source.
- Top of stack points to address/index to store to. The value below that will be popped and stored in the stack at the given index.
- Top of stack is a relative offset to jump to. If the value below that is truthy, then the program jumps by the offset.
- Interprets the top of the stack as ascii and pushes the corresponding character.
- (10 + N) Pushes the literal number n-10 onto the stack.
Example
Assume the program is:
chicken chicken chicken chicken chicken chicken chicken chicken chicken chicken chicken
chicken chicken chicken chicken chicken chicken
(an empty line)
(A cat program. Note that the empty line is required because of the preceding line having 6 "chicken".)
Input provided to Chicken program
Chicken
Output
Chicken
The Chicken.js reference implementation.
Error detection
The interpreter should leave an error and terminate when any word not "chicken" is present in the source.
Good luck!
3You need to copy the language specifications into the question. Questions shouldn't rely on external links. – mbomb007 – 2016-06-22T14:43:52.650
1Original JS interpreter – mbomb007 – 2016-06-22T14:52:35.113
Well, Why not do that yourself too? – None – 2016-06-22T14:55:54.480
1It's your question. You determine the specs. – mbomb007 – 2016-06-22T14:58:56.430
5The file input restricts this challenge to specific languages. For instance, it makes it impossible to produce a chicken answer, which I'm sure you'll agree is disappointing. – Aaron – 2016-06-22T15:11:04.783
Languages that don't use file input, can use user inputs. since the chicken program ends when there are no "chicken" in a line, You can use that. – None – 2016-06-22T15:14:47.547
@Aaron Anyway, good point. – None – 2016-06-22T15:16:12.197
How to handle invalid code? Can we just assume that all input programs are valid chicken programs? – Bassdrop Cumberwubwubwub – 2016-06-22T15:20:00.287
Good point. The program should have an error if it has any word except "chicken". anything else is a valid chicken program. – None – 2016-06-22T15:23:59.253
You should update your post with the points noted in the comments. As of now, this challenge is quite vague and will most likely either be downvoted, or closed as unclear. – Bassdrop Cumberwubwubwub – 2016-06-22T15:25:39.270
Also your
8 chicken
and9 chicken
are the same instruction. Maybe it's wise to copy the instructions from the esolang page – Bassdrop Cumberwubwubwub – 2016-06-22T15:26:40.507Oh lol that is wierd – None – 2016-06-22T15:27:56.833
Languages that don't use file input, can use user inputs. since the chicken program ends when there are no "chicken" in a line, You can use that.
That's a very important distinction. You should [edit] that into the original post. – James – 2016-06-22T16:35:10.623I already did that. – None – 2016-06-23T08:16:37.547
it will be awesome if somebody write this in chicken – user902383 – 2016-06-23T13:46:22.507
@user902383 Yeah. – None – 2016-06-23T16:25:43.647
sorry if this is off-topic but can anyone explain me what 6 7 8 instructions do ? i don't understand them from esolang wiki and this article - also is 9 instruction something like getchar() ? – None – 2016-06-24T16:05:29.410
I've edited the challenge to open it up a little, and clarify some points. The error to be given was not specified, so I think just termination should be fine. – cat – 2016-06-24T18:24:37.267
Can you clarify what six
chicken
s does? "Double wide instruction. Next instruction indicates source to load from. 0 loads from stack, 1 loads from user input. Top of stack points to address/index to load onto stack." What does "double wide" mean? What does "load" mean? What does "points to address/index" mean? Edit: #7 & #8 are unclear also. Not everyone is a compsci major. At least link to an explanation, if it's too long to include. – msh210 – 2016-06-24T20:18:51.823I have included an esolang site. but I have not found a site that does explain it better. – None – 2016-06-25T01:23:34.170
Here is a nice less-obfuscated Javascript implementation that should probably help. – Value Ink – 2016-06-25T01:27:25.287
Yeah, that should help. – None – 2016-06-25T01:28:24.507
I tried to clarify the spec more based on that implementation. – Value Ink – 2016-06-25T02:25:49.553
Good.Now that makes more sense. – None – 2016-06-25T02:52:24.900