Kitten is a minimalistic, dynamically typed, concatenative programming
language intended primarily for Web development. (This is an in-progress
implementation of that language.) Kitten is inspired by the Cat programming
language. The Kitten compiler (kitten
) compiles Kitten programs into C, which
can be compiled and linked against the Kitten runtime library (libkitten
) to
produce a standalone executable. Thus a Kitten program can be built anywhere you
can find a C compiler, making it ideal for shared hosts, where installing
development tools may not be possible. Kitten will eventually come with a
standard prelude of definitions to make your development life easier. To build
the compiler, you need GHC and Parsec; the runtime, GCC. Just download the
sources and run make
with your fingers crossed. The sources also include a
shell script named kittenc
that you can use to compile a Kitten program
meow.kitten
into an executable named meow
.
Kitten has three built-in types:
-
Integer: a signed 64-bit integral type.
-
Float: a double-precision floating-point number.
-
Quotation: a vector of boxed values, used to represent UTF-32 strings, heterogeneous arrays and structures, and anonymous functions.
The Kitten language itself is very simple:
<program> ::= <term>*
<term> ::= <integer> | <float> | <quotation> | <word> | <string>
| <definition> | <import>
<integer> ::= [0-9]+
<float> ::= [0-9]+ '.' [0-9]+
<quotation> ::= '[' <term>* ']'
<word> ::= [A-Za-z_][0-9A-Za-z_]*
<string> ::= '"' [^"]* '"'
<definition> ::= "define" <word> <quotation>
<import> ::= "import" <string>
Comments are given in parentheses (()
), and can be nested.
All terms essentially denote functions, and juxtaposition of terms denotes function composition. Thus all expressions are written in terms of data flow only, i.e., completely point-free.
There are only two special forms in the language: define
, which introduces a
new word definition, and import
, which imports a Kitten file for compilation
into the final executable.
Hello world:
"Hello world!\n" write
Hello user:
(It is customary to give a comment alongside definitions briefly describing
the expected inputs and outputs, of the form “inputs -- outputs”.)
define join ([a] [b] -- [a b])
[compose]
define greet (name --)
["Hello, " swap "!\n" join join]
define prompt (prompt -- input)
[write read_line]
"What is your name? " prompt greet
The following definitions are provided by the Kitten runtime library.
-
[A] apply
Evaluates the quotation at the top of the stack. -
[A] [B] compose
Returns the composition of the two quotations at the top of the stack. -
A dup
Duplicates the value at the top of the stack. -
A [B] [C] if
Applies[B]
ifA
is true (nonzero); otherwise, applies[C]
. -
A pop
Discards the top value on the stack. -
A quote
Returns[A]
. -
A B swap
Swaps the two topmost values on the stack.
-
A B add
Returns the sum ofA
andB
. -
A B div
Returns the quotient ofA
andB
. -
A B eq
Returns whetherA
andB
are equal. -
A B ge
Returns whetherA
is greater than or equal toB
. -
A B gt
Returns whetherA
is greater thanB
. -
A isf
Returns whetherA
is a float. -
A isi
Returns whetherA
is an integer. -
A isq
Returns whetherA
is a quotation. -
A B le
Returns whetherA
is less than or equal toB
. -
A B lt
Returns whetherA
is less thanB
. -
A B mod
Returns the modulus ofA
andB
. -
A B mul
Returns the product ofA
andB
. -
A B ne
Returns whetherA
is not equal toB
. -
A B sub
Returns the difference ofA
andB
.
-
A putc
WritesA
to standard output as a character. -
A write
WritesA
to standard output as a formatted value.
The following definitions are provided by the Kitten standard library:
read_line
Reads a line of text into a quotation.