Kana

From Esolang
Jump to navigation Jump to search
This is still a work in progress. It may be changed in the future.

Kana is a programming language created by User:LEOMOK. It is a prefix-notation language which uses Japanese kana symbols as instructions.

Literals

In Kana, there are four data types: numbers, strings, booleans and lists.

Numbers

Numbers can be integer or floating-point. They are created from the following digits:

Digit Value
0
1
2
3
4
5
6
7
8
9

A nonnegative integer is represented by a string of these digits, for example 157 is 一五七. To make a negative integer, insert 負 before the number (I know that in Japanese you can also use マイナス, but katakana characters are used for different purposes). Floats are written with 点 for the decimal point.

Strings

Strings of printable ASCII are written in the quotes 「」, with characters in these strings represented by characters in the Katakana Unicode block. To convert a printable ASCII character to a Kana character, add 0x30A0 to its character code than subtract 0x20. For example, the letter "A" (0x41) is represented as 0x41 + 0x30A0 - 0x20 = 0x30C1, which coressponds to the character チ. (The character ー (U+30FC, representing "|" in a string) is interchangeable with 一 (U+4E01, the digit 1).

In addition, a newline can be represented with 〜, and a tab with 〆.

There are also "raw strings" written in the quotes 『』, which represent the literal content inside the quotes. They are useful if your string contains non-printable ASCII characters.

Booleans

The characters 真 and 偽 are used for true and false, respectively.

Lists

Lists are represented by wrapping their contents in the delimiters 〔〕. Both 〔〕 and 空 represent the empty list.

Variables

There are 47 usable variables in Kana. They are represented with the following hiragana characters:

   あ  い  う  え  お  か  き  く  け  こ
   さ  し  す  せ  そ  た  ち  つ  て  と
   な  に  ぬ  ね  の  は  ひ  ふ      ほ
   ま  み  む  め  も  や  ゆ  よ  
   ら  り  る  れ  ろ  わ  を  ん  ゐ  ゑ

Each variable has an initial value of 0. You may also ゝ to refer to the return value of the last expression (it is 0 in the first expression). To make things easier, some of these values are pre-initialized to other values:

Variable Value
First input value / function argument
Second input value / function argument
Third input value / function argument
Fourth input value / function argument
Fifth input value / function argument
A list of all input variables / function arguments
Pi
Tau (2 * pi)
Euler's number
ASCII uppercase alphabet A to Z, as string
ASCII lowercase alphabet a to z, as string
Digits 0 to 9, as string
ASCII alphanumeric characters, as string
ASCII printable characters (U+0020 to U+007E), as string
Regex to match a signed integer, /\-\d+/
Regex to match a float, /\-\d+(\.\d+)?/
Regex to match an unsigned integer, /\d+/
Regex to match any amount of whitespace, /\s*/

There is a special variable, ゟ, which is the seed of the random number generator used in the functions ヰ and ヱ.

Functions

The 48 katakana characters are used for functions.

When values are the incorrect type for a function, they are coerced as follows:

Convert ↓ to → Number String Boolean
Number - ASCII decimal representation 0 → false, nonzero → true
String Parse as ASCII decimal number (may error) - empty → false, nonempty → true
Boolean true → 1, false → 0 true → "true", false → "false" -

A list may be converted to a string, returning its values in brackets, separated with commas, with strings quoted and escaped, for example [1, 2, "text\n", [3, 4, true]], and to a boolean with empty lists becoming false and nonempty lists becoming true. However, converting a list into a number will raise an error.

When the type hint of an argument says "integer", an error will be raised if an argument is a noninteger.

When a function requires a regex to be expressed as a list, the list must contain exactly two values, both must be strings, which are the regex content and the flags, for example ["\-\d+(\.\d+)?", "gm"].

Here is what each of them do (all indexing is 0-based, negative indices count from the end like in Python):

Function Arguments What it does
number a, number b Returns a + b.
number a, number b Returns a - b.
number a, number b Returns a * b.
number a, number b Returns a / b.
number a, number b Returns a modulo b, with the sign of b.
number a, number b Returns a to the power of b.
0 to the power of 0 is 1.
number a Returns the absolute value of a.
number a Returns the floor of a.
number a Returns the ceiling of a.
number a Returns a rounded to the nearest integer.
string a, string b
or: list a, list b
Returns a concatenated to b.
string/list a, integer b Returns a repeated b times. If b is negative, returns an empty string.
string/list a, integer b Returns character/item at index b of a.
string/list a, integer b, integer c Returns characters/items at indices b through c of a, both ends inclusive.
string a, string b
or: list a, any b
Returns the index of b in a, -1 if not found.
string a, string b Split a using b as a delimiter.
list a, string b Convert all values in a to strings, then join them with b as a delimiter.
string a, string b, string c Replace all instances of b with c in a.
string a, string b, string c, integer d Replace all instances of b with c in a, starting from index d.
string a, string b, string c, integer d Replace all instances of b with c in a, up to index d.
list a (all numbers or all strings) Return the maximum value in a.
list a (all numbers or all strings) Return the minimum value in a.
list a (all numbers or all strings) Return a sorted ascending.
string/list a Return a reversed.
string/list a Return a shuffled.
list a, any b Return a with b added onto the end.
list a, any b, integer c Return a with b inserted at index c.
list a, integer b Return a with the value at index b deleted.
list a, any b Return a with the first instance of b deleted.
list a, any b Return a with every instance of b deleted.
string a, string a Return whether a contains b, as a boolean.
string a, string a Return whether a starts with b, as a boolean.
string a, string a Return whether a ends with b, as a boolean.
string a Convert a to uppercase.
string a Convert a to lowercase.
number a, number b
or: string a, string b
Return whether a > b as a boolean.
Strings are compared lexicographically in Unicode order.
number a, number b
or: string a, string b
Return whether a < b as a boolean.
Strings are compared lexicographically in Unicode order.
any a, any b
Return whether a == b as a boolean.
Two lists are equal if they have the same values in the same order.
string a, list b Return a list of all matches of regex b on a.
Returns an empty list if no matches.
string a, list b Split a by each match of regex b.
string a, list b, string b Replace every match of regex b in a with c.
To reference a specific capture of the regex b in c, use:
  • \D - matches 0 to 9 (where D is the digit)
  • \g{N} - any numbered match, usually used for multidigit indices (where N is the number)
string a Return a list containing the Unicode values of each character of a.
list a (all numbers) Return a string with the values in a as the codepoints.
boolean a, boolean b Return a AND b, short-circuiting.
boolean a, boolean b Return a OR b, short-circuiting.
boolean a Return NOT a.
number a, number b Return a random integer between a and b, both ends inclusive.
string/list a Return a random value/character in a.