12
2
Introduction
Kipple is a stack-based, esoteric programming language invented by Rune Berg in March 2003.
Kipple has 27 stacks, 4 operators, and a control structure.
Stacks
The stacks are named a
-z
and contain 32-bit signed integers. There is also a special stack, @
, to make outputting numbers more convenient. When a number is pushed onto @
, the ASCII values of that number's digits are in fact pushed instead. (For example, if you push 12 to @
, it will push 49 and then 50 to @
instead.)
Input is pushed onto the input stack i
before the program is executed. The interpreter will ask for values to store in i
before execution. After execution finishes, anything on the output stack o
is popped to output as ASCII character . Since this is Kipple's only IO mechanism, interacting with a Kipple program is impossible.
Operators
An operand is either a stack identifier or a signed 32 bit integer.
Push: >
or <
Syntax: Operand>StackIndentifier
or StackIndentifier<Operand
The Push operator takes the operand to the left and pushes it onto the specified stack. For example, 12>a
will push the value 12 onto stack a
. a>b
will pop the topmost value from stack a
and push it onto stack b
. Popping an empty stack always returns 0. a<b
is equivalent to b>a
. a<b>c
pops topmost value from b
and pushes to both c
and a
.
Add: +
Syntax: StackIndentifier+Operand
The Add operator pushes the sum of the topmost item on the stack and the operand onto the stack. If the operand is a stack, then the value is popped from it. For example, if the topmost value of stack a
is 1, then a+2
will push 3 onto it. If a
is empty, then a+2
will push 2 onto it. If the topmost values of stack a
and b
are 1 and 2, then a+b
will pop the value 2 from stack b
and push 3 onto stack a
.
Subtract: -
Syntax: StackIndentifier-Operand
The Subtract operator works exactly like the Add operator, except that it subtracts instead of adding.
Clear: ?
Syntax: StackIndentifier?
The Clear operator empties the stack if its topmost item is 0.
The interpreter will ignore anything that isn't next to an operator, so the following program would work: a+2 this will be ignored c<i
. However, the proper way to add comments is by using the #
character. Anything between a #
and an end-of-line character is removed before execution. ASCII character #10 is defined as end-of-line in Kipple.
Operands may be shared by two operators, e.g. a>b c>b c?
may be written as a>b<c?
.
The program 1>a<2 a+a
will result in a
containing the values [1 4]
(from bottom to top) and not [1 3]
. Likewise for the -
operator.
The Control Structure
There is only one control structure in Kipple: the loop.
Syntax: (StackIndentifier code )
As long as the specified stack is not empty, the code within the matching parentheses will be repeated. Loops may contain other loops. For example, (a a>b)
will move all the values of stack a
onto stack b
, though the order will be reversed. A functionally identical, but more elegant way to do this is (a>b)
.
Examples
100>@ (@>o)
This will output 100
33>o 100>o 108>o 114>o 111>o 87>o 32>o 111>o 108>o 108>o 101>o 72>o
This will print "Hello World!"
. When the o
stack is being output, it starts to pop characters from top of the stack to bottom.
#prime.k by Jannis Harder
u<200
#change 200
k<2>m
u-2
(u-1 u>t u>z u<t
(k>e e+0 e>r)
(e>k)
m+1
m>t
m>z
m<t
t<0>z? t?
1>g
(r>b
m+0 m>a
b+0 b>w
(a-1
b+0 b>j
j?
1>s
(j<0>s j?)
s?
(s<0 w+0 w>b s?)
a>t
a>z
t>a
b-1
b>t
b>z
t>b
z<0>t? z?
a?)
b?
1>p
(b<0 b? 0>p)
p?
(p 0>r? 0>p? 0>g)
)
g?
(g m+0 m>k 0>g?)
u?)
(k>@
10>o
(@>o)
)
This is a prime number generator, but I'm not sure how it works.
Rules
You must write a program/function that interprets Kipple. This program/function may get a Kipple program via a source file, or get it via STDIN directly from the user. If STDIN is not available, it must get it from keyboard input, and continue getting input until a specific unprintable character is entered. For example, if your interpreter is written in x86 machine code, it would get the Kipple program character by character from keyboard, and continue to do so until esc (or any other key other that does not emit a printable character) is pressed.
If there is an error, e.g. a syntax error or stack overflow, it must acknowledge it in some way, for example by returning 10 instead of 0 or error messages produced by the interpreter/compiler, BUT NOT PRINTING ERROR MESSAGES.
Any other regular rules for code golf apply for this challenge.
Your code will be tested with some of the examples in Kipple's samples archive
This is a code-golf . Shortest code in bytes will win. Good Luck!
Note that there is an optional operator in Kipple, "
, but it is not a part of the specification and just an extra feature in official interpreter. I haven't mentioned it here so it does not need to be supported in your submission.
If you have any doubt about any part of specification , you can examine it with official interpreter written in Java . This will download a zip file containing compiled program and source code . It's licensed under the GPL.
1Do we have to use 32-bit signed integers or can we go with the host implementation's natural integer type? (The most important cases are probably unsigned 32-bit integers, signed or unsigned 8-bit integers and arbitrary-precision integers.) – Martin Ender – 2016-07-08T15:21:47.707
well , it was what i found on esotric wiki . yes , because your interpreter may be incompatible with other kipple programs which their mechanism are based on this feature – None – 2016-07-08T15:26:30.497
Can you be more specific about the behavior in the case of errors? So we can return an incorrect answer or emit an error, but we can't print the error? – Alex A. – 2016-07-08T18:18:05.087
@Alex A. Yes , because it can be considered as program's output , and you can make a kipple program which can have same output as error message . Also it is "cheaper" (uses less characters) not to have a function/statement that prints an error message . – None – 2016-07-08T18:22:48.057
Because of how the
@
stack works, what happens if you run@+5
? – Value Ink – 2016-07-08T18:27:31.313@kevin lau - not kenny , it will just push topmost value +5 , like when applyed to other stacks – None – 2016-07-08T18:34:36.080
3What whitespace can occur in a source program? How can I ask for input
i
if I take the source program from stdin? – orlp – 2016-07-08T20:11:15.230@orlp the things that come to mind are: 1. Take a file name as the first line of stdin, or 2. Pass the file name as command line. Or, for maximum craziness, pass the entire program as a string into command line? I wonder if that's possible – Value Ink – 2016-07-08T23:40:41.567
The spec says there are 29 stacks, but the only stacks I see are the 26
a-z
stacks and@
. Where are the last two stacks? – Value Ink – 2016-07-08T23:42:44.420@KevinLau-notKenny I believe that's a typo, although my answer uses 28 stacks, I use the same data structure that I use for stacks for my call stack :) – orlp – 2016-07-08T23:47:38.810
@orlp "i" stack is like the input stack in fish . when the interpreter is excuted and the code is entered , before running the code , it will ask useres to give intial value to "i" stack . user can fill the stack , as much as needed (from bottom to top), and when excuted , program can get that user entered information from "i" stack , and extra values are left on stack. for example , if program needs 2 inputs and user enters "3 2 1" , "i" stack will be [3 2 1] from bottom to top . then 1 then 2 will be used as input and 3 will be left on stack , though programmer can clear or use "i" stack . – None – 2016-07-09T05:12:28.687
@KevinLau-notKenny , that was an error , there are 27 stacks - i've edited the main post . – None – 2016-07-09T05:17:11.463
@orlp as said in specification "The interpreter will ignore anything that isn't next to an operator, so the following program would work:
a+2 this will be ignored c<i
" so there can be any type of white space in program . – None – 2016-07-09T05:21:54.897Not clearly specified 1: the output from
o
stack is a string made of ascii char codes. It's only in the examples – edc65 – 2016-07-09T19:55:01.987Not clearly specified 2: input number format. Is -12 a valid language token for the signed integer
-12
? – edc65 – 2016-07-09T19:55:54.800Not clearly specified 3: stack underflow ?
Popping an empty stack always returns 0
– edc65 – 2016-07-09T20:18:45.050Checking again at https://esolangs.org/wiki/Kipple, this is the right answer: "An operand can either be a stack identifier or a nonnegative integer (0 to 2147483647)."
– edc65 – 2016-07-11T08:03:08.573Just to be double-triple sure, when you say the program shouldn't print error messages, you mean that errors can be thrown, but should not appear on screen, yes? – Value Ink – 2016-07-11T08:55:44.587
Also, is the
?
always the last operator in a group of commands, or isa?<b
possible? – Value Ink – 2016-07-11T09:21:46.607What's the defined behavior for
a<b>c
? Do they both use the top value ofb
, or doesa
take the top value andc
take the next one? – Value Ink – 2016-07-11T23:06:33.587@KevinLau-notKenny I've deleted my last comment , sorry ,
a<b>c
pops topmost value fromb
and pushes to bothc
anda
. – None – 2016-07-12T12:54:48.063All right, thanks for the tip. You should update your spec to clarify it for other people, just in case. Also, what of my other question, regarding if
a?<b
is possible? – Value Ink – 2016-07-12T17:51:39.363Not clearly specified 4:
a<b>c pops topmost value from b and pushes to both c and a
Missing this is a basic flaw (both here and in the original Kipple spec). Basically it's impossible to write a working interpreter based on this specifications - you have to check how the reference java interpreter works. That's why I downvote – edc65 – 2016-07-13T06:08:21.663@GLASSIC Nope; it pushes the top value of
b
toa
, and the second-to-top value ofb
toc
. It's just likea<bb>c
, or, more clearly,a<b c<b
. It's just that Operands may be shared by two operators. No, the operand doesn't get a value before being evaluated, thus it will act differently ona<b>c
the first and second times. – Erik the Outgolfer – 2016-07-15T13:15:14.110