Sep 18, 2021Kuina-chan


This is the language specification 3 of Kuin Programming Language, and is about operators.

1Kuin Operators

The operators and their precedence in Kuin are shown in Table 1-1.
Table 1-1: Kuin Operators
Precedence Join Operator Description
1 Left f() Function Call
a[] Array Reference
a.b Member Reference
a $ t Cast
a $> t Binary Encoding
a $< t Binary Decoding
2 Right a ^ b Power
3 Right +a
-a
Sign
!a Logical Negation
^a Element Number Operator
#t Instance Creation
##a Deep Copy
4 Left a * b Multiplication
a / b Division
a % b Modulo Operation
5 Left a + b Addition
a - b Subtraction
6 Left a ~ b Array Concatenation
7 Left a = b
a <> b
a < b
a > b
a <= b
a >= b
Comparison Operator
a =& b
a <>& b
Reference Comparison Operator
a =$ t
a <>$ t
Type Comparison Operator
8 Left a & b Logical Conjunction
9 Left a | b Logical Disjunction
10 Right a ?(b, c) Conditional Operator
11 Right a :: b
a :+ b
a :- b
a :* b
a :/ b
a :% b
a :^ b
a :~ b
Assignment Operator
When operators are used without parentheses, the operator with the lowest precedence will be calculated first. If operators with the same precedence are lined up, the operator on the left will be calculated first in the case of left join, and the operator on the right will be calculated first in the case of right join.
Each of these operators is explained in detail below.

2Function Call

It is an operator that calls a function (Table 2-1)。
Table 2-1: Function Call Syntax

Function Name(Argument 1, Argument 2, ...)

The specification of function call is shown in Figure 2-1.
  • Writing "(Expression Of Argument 1, Expression Of Argument 2, ...)", the function will be called and the return value, if any, will be returned.
  • When passing an argument by reference, the variable name of the argument must be preceded by "&". For example, it is written as "f(&n)".
  • When you don't need to receive the result in a pass-by-reference argument, you can omit the variable name of the argument and just write "&". For example, write "f(&)".
  • The type and number of arguments passed to the function and the type of the return value must match those defined in the function.
Figure 2-1: Function Call Specification
If you write multiple function calls in a single expression, the order in which the functions are called is undefined. When calling multiple functions with side effects, it is necessary to make sure that the order of calls is not affected. However, in the "&", "|", and "?(,)" operators, expressions are always evaluated in order from the left, and unnecessary expressions are skipped, so the order of function calls can be written assuming that they are called from the left.

3Array Reference

It is an operator that refers to the elements of an array (Table 3-1).
Table 3-1: Array Reference Syntax

Array Name[Element Index]

The specification of array reference is shown in Figure 3-1.
  • If you use the int value n and write the array value followed by "[n]", the nth element of the array will be returned.
  • In this case, n must be greater than or equal to 0 and less than the number of elements in the array. If n is out of this range, an exception (0xE9170002) will be raised when running in debug.
Figure 3-1: Array Reference Specification

4Member Reference

It is an operator that refers to a member of a value (Table 4-1).
Table 4-1: Member Reference Syntax

Value.Member Name

The specification of member reference is shown in Figure 4-1.
  • When a value of any type is written with ".Member Name" after it, the member defined for that type will be returned.
Figure 4-1: Member Reference Specification
Members are the methods and properties of a class, as well as the built-in methods provided for each type.

5Cast

It is an operator that converts a value to a specified type (Table 5-1).
Table 5-1: Cast Syntax

Value $ Type Name

The specification of cast is shown in Figure 5-1.
  • When a value of any type is written with "$ Type Name" after it, the value converted to that type is returned.
  • If an instance of a class is cast to a type other than that class and its parent class, an exception (0xE9170001) will be raised at runtime.
  • If the class instance is null, no exception will be thrown and the cast will succeed.
Figure 5-1: Cast Specification
If the type of the class you are trying to cast is a type that cannot be the class or parent class in the first place, an error will occur at compile time.
There are restrictions on the types that can be converted, as shown in Table 5-2.
Table 5-2: Types That Can Be Cast
Type Before Conversion Possible Types After Conversion
int int, float, char, bool, bit types, enum
float int, float, bit types
char int, char, bit types
bool int, bool, bit types
bit types int, float, char, bool, bit types, enum
enum int, bit types, enum
class class

6Binary Encoding

It is an operator that converts a value into a binary sequence of type []bit8 (Table 6-1).
Table 6-1: Binary Encoding Syntax

Value $> []bit8

The specification of binary encoding is shown in Figure 6-1.
  • When a value of any type is written with "$> []bit8" after it, the value converted to a binary sequence is returned. It is not possible to specify a type name other than []bit8.
  • Since it is recursively deep-copied and binaryized, the reference relationship is lost if the two variables hold the same reference.
  • When a function type is converted to binary, it is converted to a value that will be null when restored by the "$<" operator.
  • When null is binaryized, null is restored.
  • When you binaryize an instance that is in an inheritance relationship with a class, the type of the instance, not the class, is respected and restored correctly.
  • When list type is binaryized, the list pointer is also restored correctly.
  • Classes in the standard library that cannot be directly instantiated with the "#" operator may not be restored correctly.
Figure 6-1: Binary Encoding Specification

7Binary Decoding

It is an operator that restores a binary sequence of type []bit8, converted by a binary encoding operator, to a value (Table 7-1).
Table 7-1: Binary Decoding Syntax

Value $< Type Name

The specification of binary decoding is shown in Figure 7-1.
  • If you write "$< Type Name" after the value converted to a binary sequence, the value before the conversion to a binary sequence is returned.
  • The type name must specify the type before it is converted by the binary encoding operator.
  • When an instance of a class is restored, it is restored with the type of the instance before restoration, not with the type of the specified class.
  • Classes in the standard library that cannot be directly instantiated with the "#" operator may not be restored correctly.
Figure 7-1: Binary Decoding Specification

8Power

It is an operator that calculates a to the b power (Table 8-1).
Table 8-1: Power Syntax

a ^ b

The specification of power is shown in Figure 8-1.
  • For two int or float values a and b, "a ^ b" will return the value of a to the b power.
  • The type of a and b must be equal, and the result of the operation will be returned in that type.
  • When both a and b are 0, 1 is returned.
  • If the result is not a real number, it will be set to 0 for int, NaN for float, and no exception will be thrown.
Figure 8-1: Power Specification
Reason For The Design

This operator "^" comes from BASIC. Since power is such a familiar operation that it is used in everyday life, I thought it would be better to write it intuitively as an operator.

9Sign

It is an operator that represents a plus or minus sign (Table 9-1).
Table 9-1: Sign Syntax

+Value
-Value

The specification of sign is shown in Figure 9-1.
  • Writing an int, float, or bit type value with a "+" in front of it will return the value as is.
  • Writing an int, float, or bit type value with "-" in front of it will return the value with the positive or negative value reversed.
  • If it is an operation on a Compile-time Constant, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 9-1: Sign Specification

10Logical Negation

It is an operator that inverts between true and false (Table 10-1).
Table 10-1: Logical Negation Syntax

!Value

The specification of logical negation is shown in Figure 10-1.
  • Writing a bool value with a "!" in front of it will return the value with true and false reversed.
  • If it is an operation on a Compile-time Constant, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 10-1: Logical Negation Specification

11Element Number Operator

It is an operator to get the number of elements in an array, etc. (Table 11-1).
Table 11-1: Element Number Operator Syntax

^Value

The specification of element number operator is shown in Figure 11-1.
  • If you write an array, list, stack, queue, or dict value with a "^" in front of it, the number of elements will be returned as an int type. The values of these types hold the number of elements inside, and this operation is performed in a fast () manner.
Figure 11-1: Element Number Operator Specification

12Instance Creation

It is an operator that creates an instance of a class, etc. (Table 12-1).
Table 12-1: Instance Creation Syntax

#Type Name

The specification of instance creation is shown in Figure 12-1.
  • If you write a list, stack, queue, dict, or class type name with a "#" in front of it, an instance of it will be created and returned.
  • Some classes in the standard library cannot be directly instantiated with the "#" operator, resulting in compilation errors. See the documentation for each library for more information on this.
  • Writing "#[number of elements 1, number of elements 2, ..., number of elements n]" before any type name, an instance of an n-dimensional array with that type name as an element will be created and returned.
  • Each value in the generated array will be the default value of its type.
Figure 12-1: Instance Creation Specification
For example, writing "#[3,2]int" will return a two-dimensional array of type "[][]int" with 3 x 2 elements.

13Deep Copy

It is an operator that makes a deep copy of an instance (duplicating the value, not the reference) (Table 13-1).
Table 13-1: Deep Copy Syntax

##Value

The specification of deep copy is shown in Figure 13-1.
  • Writing an array, list, stack, queue, dict, or class value with "##" in front of it, the value will be copied to another memory area and its reference will be returned.
  • Since it is a recursive deep copy, the reference relationship is lost if the two variables hold the same reference.
  • When the value of a function type is deep-copied, the reference to the function is copied.
  • If you deep-copy null, null is copied.
  • If you deep-copy an instance that is in an inheritance relationship with a class, the type of the instance, not the class, is respected and copied correctly.
  • Deep-copying the list type will correctly copy the list pointer.
  • Classes in the standard library that cannot be directly instantiated with the "#" operator may not be copied correctly.
Figure 13-1: Deep Copy Specification
Reason For The Design

We often want to copy the content instead of copying the reference, but there are few languages that provide this, and we needed to implement the copy process ourselves, so Kuin provides it as a standard feature.

14Multiplication

It is an operator that calculates (Table 14-1).
Table 14-1: Multiplication Syntax

a * b

The specification of multiplication is shown in Figure 14-1.
  • For two values a and b of either int, float, or bit type, "a * b" will return the value of .
  • The type of a and b must be equal, and the result of the operation will be returned in that type.
  • If it is an operation between Compile-time Constant values, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 14-1: Multiplication Specification

15Division

It is an operator that calculates (Table 15-1).
Table 15-1: Division Syntax

a / b

The specification of division is shown in Figure 15-1.
  • For two values a and b of either int, float, or bit type, "a / b" will return the value of .
  • The type of a and b must be equal, and the result of the operation will be returned in that type.
  • For int or bit type operations, b must not be zero. In this case, a compile error or exception may occur, or an undefined value may result.
  • When both a and b are 0.0 in a float operation, a compile error will occur or the result will be NaN.
  • If it is an operation between Compile-time Constant values, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 15-1: Division Specification

16Modulo Operation

It is an operator that calculates the remainder of (Table 16-1).
Table 16-1: Modulo Operation Syntax

a % b

The specification of modulo operation is shown in Figure 16-1.
  • For two values a and b of either int, float, or bit type, "a % b" will return the value of the remainder of .
  • The type of a and b must be equal, and the result of the operation will be returned in that type.
  • For int or bit type operations, b must not be zero. In this case, a compile error or exception may occur, or an undefined value may result.
  • When both a and b are 0.0 in a float operation, a compile error will occur or the result will be NaN.
  • If it is an operation between Compile-time Constant values, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 16-1: Modulo Operation Specification

17Addition

It is an operator that calculates (Table 17-1).
Table 17-1: Addition Syntax

a + b

The specification of addition is shown in Figure 17-1.
  • For two values a and b of either int, float, or bit type, "a + b" will return the value of .
  • The type of a and b must be equal, and the result of the operation will be returned in that type.
  • If it is an operation between Compile-time Constant values, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 17-1: Addition Specification

18Subtraction

It is an operator that calculates (Table 18-1).
Table 18-1: Subtraction Syntax

a - b

The specification of subtraction is shown in Figure 18-1.
  • For two values a and b of either int, float, or bit type, "a - b" will return the value of .
  • The type of a and b must be equal, and the result of the operation will be returned in that type.
  • If it is an operation between Compile-time Constant values, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 18-1: Subtraction Specification

19Array Concatenation

It is an operator that concatenates two arrays (Table 19-1).
Table 19-1: Array Concatenation Syntax

a ~ b

The specification of array concatenation is shown in Figure 19-1.
  • For the two values a and b, which are arrays, "a ~ b" will return an array concatenating a and b, allocated in a new memory area.
  • The types of a and b must be equal.
  • a and b must not be null. A compile error occurs or an exception is thrown at runtime.
  • If it is an operation between Compile-time Constant values, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 19-1: Array Concatenation Specification
Reason For The Design

This operator "~" is derived from the D language. The "+" found in Java and C# is ambiguous as adding numbers, and the "." in PHP is ambiguous as calling a member, so "~" was adopted.

20Comparison Operator

It is an operator that compares two values (Table 20-1).
Table 20-1: Comparison Operator Syntax

a = b
a <> b
a < b
a > b
a <= b
a >= b

The specification of comparison operator is shown in Figure 20-1.
  • For two comparable values a and b, either "a = b", "a <> b", "a < b", "a > b", "a <= b", or "a >= b" will return the result of the value comparison in bool.
  • Comparable types are int, float, char, bit type, []char, enumerated type, and class in the case of "a < b", "a > b", "a <= b", and "a >= b", and bool is added to these in the case of "a = b" and "a <> b".
  • a and b must not be null. A compile error occurs or an exception is thrown at runtime.
  • The types of a and b must be equal.
  • The expressions "a = b", "a < b", "a > b", "a <= b", and "a >= b" have the same meaning as comparisons in mathematics. The "a <> b" corresponds to in mathematics.
  • If it is an operation between Compile-time Constant values, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 20-1: Comparison Operator Specification
Reason For The Design

The mismatch operator "<>" is found in BASIC, Pascal, SQL, etc. If the precedence of the operator can be determined when the first character of the operator is read, compilation will be faster. Therefore, we adopted "<>" instead of "!=", whose first character overlaps with "!".

21Reference Comparison Operator

It is an operator that compares two references (Table 21-1).
Table 21-1: Reference Comparison Operator Syntax

a =& b
a <>& b

The specification of reference comparison operator is shown in Figure 21-1.
  • Performing the operation "a =& b" or "a <>& b" on two values a and b in any of the following: array, list, stack, queue, dict, class, func, and null, returns a reference match or reference mismatch of type bool, respectively.
  • The types of a and b must be equal.
Figure 21-1: Reference Comparison Operator Specification

22Type Comparison Operator

It is an operator that determines whether a class instance is of the specified class type or its parent class type (Table 22-1).
Table 22-1: Type Comparison Operator Syntax

Instance =$ Type Name
Instance <>$ Type Name

The specification of type comparison operator is shown in Figure 22-1.
  • When the "a =$ t" or "a <>$ t" operation is performed on a class value a with a class type t, a type match of the class or its parent class or a type mismatch of the class or its parent class is returned by bool, respectively.
  • The instance must not be null. A compile error occurs or an exception is thrown at runtime.
Figure 22-1: Type Comparison Operator Specification
Since an instance of an inherited child class can be assigned to a variable of its parent class, this operator is used to find out which type the assigned instance is of.

23Logical Conjunction

It is an operator that calculates logical conjunction (Table 23-1).
Table 23-1: Logical Conjunction Syntax

a & b

The specification of logical conjunction is shown in Figure 23-1.
  • For the two bool values a and b, "a & b" will return true if both a and b are true, and false otherwise.
  • Since the result is determined to be false when a is false, the evaluation of expression b is skipped when a is false. The equation is always evaluated in the order of a, b.
  • If it is an operation between Compile-time Constant values, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 23-1: Logical Conjunction Specification

24Logical Disjunction

It is an operator that calculates logical disjunction (Table 24-1).
Table 24-1: Logical Disjunction Syntax

a | b

The specification of logical disjunction is shown in Figure 24-1.
  • For the two bool values a and b, "a | b" will return false if both a and b are false, and true otherwise.
  • Since the result is determined to be true when a is true, the evaluation of expression b is skipped when a is true. The equation is always evaluated in the order of a, b.
  • If it is an operation between Compile-time Constant values, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 24-1: Logical Disjunction Specification

25Conditional Operator

It is an operator that returns a value depending on the truth of the condition (Table 25-1).
Table 25-1: Conditional Operator Syntax

Condition ?(Value When True, Value When False)

The specification of conditional operator is shown in Figure 25-1.
  • For a bool value a and two values of any type b and c, "a ?(b, c)" will return b when a is true, and c when a is false.
  • There should be no space between "?" and "(".
  • The types of b and c must be equal.
  • The expression that is not a return value of either b or c is skipped evaluation. The equations are always evaluated in the order of a, b or a, c.
  • If it is an operation between Compile-time Constant values, it is guaranteed to be calculated at compile time and the result will also be a constant.
Figure 25-1: Conditional Operator Specification
Reason For The Design

This operator "?(,)" comes from the C language. In the C language, it is "?:", but since it is difficult to understand the order of precedence when the expression becomes long or when multiple "?:" are combined, I prepared explicit parentheses and changed it to "?(,)". The reason why there should be no space between "?" and "(" is because if there is a space here, this parenthesis can easily be confused with the parenthesis of a mathematical expression.

26Assignment Operator

It is an operator that assigns a value to a variable (Table 26-1).
Table 26-1: Assignment Operator Syntax

a :: b
a :+ b
a :- b
a :* b
a :/ b
a :% b
a :^ b
a :~ b

The specification of assignment operator is shown in Figure 26-1.
  • For a variable of any type a and a value b, "a :: b" will assign b to a.
  • The types of a and b must be equal.
  • This operator does not return a value.
  • "a :+ b" is equal to "a :: a + b". Similarly, "a :- b", "a :* b", "a :/ b", "a :% b", "a :^ b", and "a :~ b" are all equal to "a :: a Operator b".
  • The expression a is evaluated only once. Even if a is a function call, the function will not be called twice.
Figure 26-1: Assignment Operator Specification
Reason For The Design

The assignment operator "::" is one of Kuin's most quirky designs, but it comes from the Pascal assignment operator ":=". If the order of precedence can be determined as soon as the first character of the operator is read, compilation is faster. So, I decided to use "::", which is not "==", whose first character overlaps with "=", and is easier to type than ":=".

1631936714enf