User:Menasewi/toki ilo pona
toki ilo pona (.tok
) is a programming language. It was designed by kulupu Menasewi on August 14, 2023.
It is based on Toki Pona, a constructed human language by Sonja Lang. It takes inspiration from prior art on the wiki Esolang, mainly toki pi ilo nanpa, while trying to be closer to the spirit of Toki Pona.
toki ilo pona can be written in Latin text, separating words with spaces. Or, it can be written with Sitelen Pona glyphs. As of writing, these are encoded in the UCSUR. (If they are ever added to Unicode, those codepoints should also be supported.)
Keywords
37 keywords and one alias are used:
ala ale~ali ante anu awen e jasima kepeken la li lili lon luka mute nanpa ni nimi o pakala kin suli te to toki tu wan weka
Commands
A command ends at the end of a line, or at a period, interpunct, colon, exclamation point, or question mark. Other punctuation and glyph combiners are ignored outside of a string.
Print:
o toki e [value]
Many commands allow repeating the argument marker. (In Latin text, it is conventional to separate arguments with a comma.) Print two things in one command:
o toki e [value], e [value]
Commands are heavily limited in their syntax, but most can be joined. ni
references the subject of the following command, which is run first. (In Latin text, it is conventional to end ni
commands with a colon.)
Variables
Initialize or set a variable:
[type] [Name] o [value]
The variable name can be multiple words. (It should also follow Toki Pona phonotactics.) In Latin text, each word must start with an uppercase letter. In Sitelen Pona, the whole name must be written in a cartouche.
The type is treated as a part of the variable name, and always goes before it. This means that variables of different types can share a name, if absolutely necessary.
Values
A value can be preceded by its type with no change in meaning. For a numeric example, ala
is equivalent to nanpa ala
.
If the name is excluded from a variable declaration, the command will serve as a temporary value that can be substituted back into ni
.
Data types
lon
The lon
type stores a boolean. The boolean true is lon
.
The boolean false is ala
. Because a value can be preceded by its type, lon ala
also works.
The not operator is [boolean] ala
.
The or operator is [boolean] anu [boolean]
.
nanpa
The nanpa
type stores an integer. ala
is 0.
Other integers are formed with Toki Pona numerals, additive from highest to lowest value.
ale
orali
: 100
mute
: 20
luka
: 5
tu
: 2
wan
: 1
A negative number is formed with jasima
at the end.
An addend is formed with kin
after it. Increment a variable:
nanpa [Name] o wan kin
Print the sum of 1 + 2:
o toki e ni: nanpa o wan, o tu kin
(Out:
tu wan
)
A boolean is treated as a one-bit number. Cast a boolean to ala
or wan
:
nanpa [Name] o [boolean]
Casting a number to a boolean yields ala
if it is 0, and lon
otherwise.
nimi
The nimi
type stores a string. Strings are delimited with te … to
. Outer whitespace is trimmed. If the string continues to the end of the line, the to
can be dropped.
A string that concatenates onto a previous one is formed with kin
after it.
Hello, World!:
o toki e te toki, ma o!
(Out:
toki, ma o!
)
Cast a boolean to its name:
nimi [Name] o [boolean]
Casting a string to a boolean yields ala
if it is empty, and lon
otherwise.
Cast a number to a string:
nimi [Name] o [number]
Following a string with a number returns the character at that index. Strings are one-indexed.
Character at position i:
nimi [Name] o [string] nanpa I
Here, suli
can be used here as a number corresponding to the string's length. This string–number trick can cast to a number representing the index:
nanpa [Name] o [string] nanpa suli
Penultimate character:
nimi [Name] o [string] ni: nanpa o suli, o wan jasima kin
pakala
pakala
throws an error with a stack trace when used, unless a variable of its type is defined. Define a custom error message and throw it later:
pakala [Name] o [string]
o pakala [Name]
Built-in commands also use pakala
. For example, if a string that does not represent a number is cast to a number, it throws pakala
.
Control flow
Control flow uses la
.
Tabs are used to indent code blocks.
If–else conditional:
[condition] la
[then]
ante la
[else]
ni
cannot be used in the condition.
Comparison
Values can only be compared in a condition.
=
[value] li sama [value] la
≠
[value] li sama ala [value] la
<
[value] li lili tawa [value] la
>
[value] li suli tawa [value] la
≤
[value] li suli ala tawa [value] la
≥
[value] li lili ala tawa [value] la
Loops
o awen·
loops from the head of the latest block. It retries the condition, then continues down the corresponding branch.
The for loop can be implemented as a "while i meets a comparison" loop:
nanpa I o [min]
nanpa I li suli ala tawa [max] la
[commands]
nanpa I o wan kin
o awen
Infinite loop:
lon la
[commands]
o awen
o weka·
breaks out of a block.
Functions
Function definition:
nasin [Name] kepeken [argument] la
[commands]
A function can have multiple arguments.
Functions with different argument types can share a name.
Invoke a function:
o nasin [Name] e [argument]
A function must be defined before the place it is invoked. Otherwise, the computer will be confused because you haven't given it context for what the function is.
[value] o weka·
returns a value.
Print the product of 12 × 10, then the factorial 5!:
nasin Leko kepeken nanpa Tenpo, kepeken nanpa Suli, la
lon Jasima o ala
nanpa Tenpo li lili tawa ala la
nanpa Tenpo o nanpa Tenpo jasima
lon Jasima o lon
nanpa Leko o ala
nanpa Tenpo li suli tawa ala la
nanpa Leko o nanpa Suli kin
nanpa Tenpo o wan jasima kin
o awen
lon Jasima la
nanpa Leko o nanpa Leko jasima
nanpa Leko o weka
o toki e ni: o nasin Leko e luka luka tu, e luka luka
nasin Patowijo kepeken nanpa Suli la
nanpa Suli li lili tawa ala la
o pakala te nanpa Suli li ken ala lili tawa ala
nanpa Suli li suli ala tawa wan la
wan o weka
nanpa Suli o wan jasima kin
nanpa o nasin Leko e nanpa Suli, e ni, o weka:
o nasin Patowijo e nanpa Suli
o toki e ni: o nasin Patowijo e luka
(Out:
ale mute
ale mute
)
English translation:
(Out:
)