Aug 13, 2022Kuina-chan


This is the language specification 2 of Kuin Programming Language, and is about types and literals.

1Kuin Types And Literals

Kuin types are divided into two types: value type which handles the value itself, and reference type which allocates a memory area during execution and handles its reference.
The value types and reference types in Kuin are shown in Table 1-1.
Table 1-1: Kuin Types And Literals
Type Description Classification Default Value Example Of Literal
int Integer Type Value Type 0 256
float Floating-point Type Value Type 0.0 3.14159
char Character Type Value Type '\0' 'A'
bool Boolean Type Value Type false true
bit8
bit16
bit32
bit64
Bit Type Value Type 0b8
0b16
0b32
0b64
255b8
[] Array Reference Type null [1, 2, 3]
"abc"
list<> List Reference Type null  
stack<> Stack Reference Type null  
queue<> Queue Reference Type null  
dict<,> Dictionary Reference Type null  
func<()> Function Type Reference Type null funcName
enum Enumerated Type Value Type Element With A Value Of 0 %elementName
class Class Reference Type null  
A literal is a written value in the source code.
A default value is a value that is stored when a variable is defined and nothing is assigned to it.
Each type is explained in detail below.

2Integer Type

int is a type for handling integers, and its specification is as shown in Figure 2-1.
  • It is an 8-byte type that handles integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • The value will overflow if it exceeds the range it can handle.
Figure 2-1: Specification Of int
Reason For The Design

Even if integer types of various sizes are provided, as in the C language, there are few cases where memory consumption needs to be trimmed down to that level, so for the sake of simplicity, Kuin focuses on one type of integer for normal use.

The literal of int is shown in Figure 2-2.
  • A sequence of one or more numbers in "0123456789" is interpreted as a decimal integer and becomes an int literal.
  • A "0x" followed by one or more of the numbers "0123456789ABCDEF" is interpreted as a hexadecimal integer. Lowercase letters are not allowed.
Figure 2-2: Specification Of int Literals
Reason For The Design

The reason why the hexadecimal number "ABCDEF" only allows uppercase letters is that lowercase alphabets are used for exponential notation and bit-type literals.

3Floating-point Type

float is a floating-point type for handling decimals, and its specification is as shown in Figure 3-1.
  • It is an 8-byte type that handles floating-point numbers with 15 to 16 significant digits, approximately to .
  • The basic specifications follow IEEE 754.
  • It can handle positive and negative infinity, and will go to positive or negative infinity if the value overflows.
  • When the value needs to be rounded, it is rounded to the nearest zero.
  • If you perform a mathematically undefined operation such as "0.0/0.0", the result will be "NaN".
  • The result when NaN is used for the calculation is undefined.
Figure 3-1: Specification Of float
Reason For The Design

As with int, Kuin uses only one floating point type for brevity.

The literal of float is shown in Figure 3-2.
  • If there is only one "." between two or more numbers in "0123456789", it is interpreted as a decimal fraction and becomes a float literal.
  • If this notation is followed by "e+" or "e-", and then "0123456789", it is considered to be exponential notation.
  • When written as "inf", it is positive infinity.
Figure 3-2: Specification Of float Literals
Exponential notation is a method of notation in which writing "mantissa part e+ exponential part" is considered to be "mantissa part ^ exponential part." For example, "1.0e+5" is the same value as "10000.0".

4Character Type

char is a type for handling characters, and its specification is as shown in Figure 4-1.
  • A 2-byte type that handles UTF-16 character codes from 0x0000 to 0xFFFF.
Figure 4-1: Specification Of char
Reason For The Design

When considering surrogate pair characters, it would be more convenient to use UTF-32 with 4 bytes per character, but since the frequency of using surrogate pair characters is not high, I decided to use UTF-16, which is more compatible with Windows API and consumes less memory.

The literal of char is shown in Figure 4-2.
  • The part enclosed by the two "'" is a literal of char.
  • The literal cannot contain tab characters or line feeds without using escape sequences.
  • The combination of "\" and any single character in a literal is interpreted as a special character as an escape sequence.
Figure 4-2: Specification Of char Literals
The escape sequence, which is a combination of "\" and any single character, is shown in Table 4-1.
Table 4-1: Kuin Escape Sequence
Symbols Meaning
\\ The "\" symbol.
\" The '"' symbol.
\' The "'" symbol.
\0 Character whose code is 0.
\n Newline character.
\t Tab character.
\u???? Character of the specified code. "????" is a 4-digit hexadecimal number in either numbers or uppercase letters.
\{expression} The result of an expression as a string. It can only be used within a []char literal. For example, when 3 is stored in the int variable n, the statement '"\{n+5}"' will be interpreted as '"8"'. This is equivalent to "(expression).toStr()".
Reason For The Design

In Windows, newline codes are mainly expressed as "\r\n" (0x0d, 0x0a) in the C language escape sequence, but since "\r" is not necessary in some situations and is complicated, newlines in Kuin are always expressed as "\n" and internally converted to "\r\n" as appropriate.

5Boolean Type

bool is a logical type for handling true and false, and its specification is shown in Figure 5-1.
  • It is a 1-byte logical type that takes a value of "false" or "true".
Figure 5-1: Specification Of bool
The literal of bool is shown in Figure 5-2.
  • Only "false" and "true" are literals of type bool.
  • "false" means false, "true" means true.
Figure 5-2: Specification Of bool Literals

6Bit Type

bit8, bit16, bit32, and bit64 are bit types used for bitwise operations, and their specifications are shown in Figure 6-1.
  • The bit8 type handles 8-bit integers (0x00 to 0xFF), the bit16 type handles 16-bit integers (0x0000 to 0xFFFF), the bit32 type handles 32-bit integers (0x00000000 to 0xFFFFFFFF), and the bit64 type handles 64-bit integers (0x0000000000000000 to 0xFFFFFFFFFFFFFFFF).
Figure 6-1: Specification Of bit
Reason For The Design

Since I designed the int type as an integer type that can be handled universally, I prepared these bit types separately as integer types for cases where I have to be aware of their representation in memory.

As the name implies, they can also handle bitwise operations, so we can use them differently from the int type depending on the situation.

The literal of bit is shown in Figure 6-2.
  • An int literal followed by one of "b8", "b16", "b32", or "b64" is a literal of type bit8, bit16, bit32, or bit64, respectively.
Figure 6-2: Specification Of bit Literals
For example, if you want to express the number of "AC" in hexadecimal as bit8 type, write "0xACb8".

7Array

Kuin's array is a dynamic array whose number of elements can be determined at runtime, and its specification is shown in Figure 7-1.
  • Arrays are written as "[]type name". For example, it can be expressed as "[]int" or as an array of arrays as "[][]char".
  • Strings in Kuin are handled as "[]char".
Figure 7-1: Specification Of Array
Reason For The Design

The reason why strings are made into an array of char instead of a dedicated type is that the various functions provided for arrays can also be used.

The literal of array is shown in Figure 7-2.
  • The literal of the array should be "[value1, value2, ..., valuen]".
  • The type of each value must be the same. Based on the type of the value, the type of the array literal is determined.
  • Setting all values of an array literal to null is not allowed because the type of the array literal cannot be determined. If you want them all to be null, you need to explicitly cast one of them.
  • In addition to these, the part enclosed in two '"' is a []char literal. The notation rules for []char literals are the same as for char literals.
Figure 7-2: Specification Of Array Literals

8List

A list is similar to an array, but it is a data structure that allows fast addition, insertion, and deletion of elements. The specification of list is shown in Figure 8-1.
  • A list is a data structure in which each element is connected by a pointer, and is expressed as "list<type name>". For example, it is written as "list<int>".
  • While it can add, insert, and delete elements instantly compared to arrays, the process of accessing the nth element is slower.
  • Lists are bi-directionally linked, so they can be traced in order from the beginning or in reverse order from the end.
Figure 8-1: Specification Of List

9Stack

A stack is a data structure that resembles a stack of elements that are put in first and taken out later. The specification of stack is shown in Figure 9-1.
  • A stack is expressed as "stack<type name>". For example, it is written as "stack<int>".
Figure 9-1: Specification Of Stack

10Queue

A queue is a waiting queue-like data structure where the first element in is taken out first. The specification of queue is shown in Figure 10-1.
  • A queue is expressed as "queue<type name>". For example, it is written as "queue<int>".
Figure 10-1: Specification Of Queue

11Dictionary

A dictionary is a data structure that stores a number of key-value pairs, and is used when you want to quickly search for a value from a key. The specification of dictionary is shown in Figure 11-1.
  • A dictionary is expressed as "dict<type name of key, type name of value>". For example, it is written as "dict<[]char, int>".
  • Because binary search is used to look up keys, dictionary searching is much faster than searching from the top in an array or list.
Figure 11-1: Specification Of Dictionary

12Function Type

In Kuin, all functions are handled as function types. You can then assign the function to a variable of the function type. The specification of function is shown in Figure 12-1.
  • A function type is expressed as "func<(type of argument 1, type of argument 2, ..., type of argument n): return type)>". For example, it is written as "func<(int, char): bool>".
  • The value of a function type is the function itself, and can be called as a function.
Figure 12-1: Specification of Function

13Enumerated Type And Class

Enums and classes are types whose contents can be defined freely by the user.
For more information on enums, see Enumerated Types, and for more information on classes, see Classes.
1660379449enf