Aug 13, 2022Kuina-chan


This is the language specification 5 of Kuin Programming Language, and is about statements and blocks.

1Kuin Statements And Blocks

The statements and blocks that can be written in a function in Kuin generally have the structure shown in Table 1-1.
Table 1-1: Statements And Blocks

Statement Name Statement Contents

Block Name Identifier(...)
    Block Contents
end Block Name

For example, as shown in Figure 1-1, variable definitions are structured as statements and function definitions are structured as blocks.
  1. var n: int
  2.  
  3. func f(x: float)
  4.   var y: float
  5. end func
Figure 1-1: Variable And Function Definitions
The difference is that a statement is written on a single line, while a block is written over multiple lines. You can write more statements and blocks inside the blocks.
Statements and blocks in Kuin are shown in Table 1-2.
Table 1-2: Kuin Statements And Blocks
Name Classification Description
alias Statement Type Alias Definition
assert Statement Assertion
break Statement Exit Block
const Statement Constant Definition
do Statement Execute Expression
excode Statement Direct Writing Of Post-compilation Code
include Statement Include Separate File
ret Statement Exit Function
skip Statement Skip Loop Once
throw Statement Raising Exception
var Statement Variable Definition
block Block Basic Block
class Block Class Definition
enum Block Enum Definition
for Block Counting Loop
func Block Function Definition
if Block Conditional Branch
switch Block Branching By Value
try Block Exception Handling
while Block Conditional Loop
Each statement and block is explained in detail below.
Reason For The Design

The reason for specifying the block name in the end of block "end block name" is for readability reasons, as it is difficult to tell which block it corresponds to when there are many end of block symbols in a row, such as "}" in C or "end" in Pascal.

As with type checking at compile time, it is better for the compiler to check if the code is what the user intended to reduce bugs, so I let the user specify the block name and check the correspondence at compile time, even if it is a bit verbose.

The syntax "end block name" comes from Visual Basic.

2alias Statement

The alias statement defines a new type by adding an alias to an existing type (Table 2-1).
Table 2-1: alias Statement Syntax

alias New Type Name: Existing Type Name

The alias statement can be written in a global area outside of a function, or even within a class.

3assert Statement

The assert statement is a statement that raises an exception (0xE9170000) when executed in debugging if the condition is not met (Table 3-1).
Table 3-1: assert Statement Syntax

assert Conditional Expression

The purpose of this feature is to increase the reliability of the program by writing conditions to be met. The condition needs to be written in bool.
The assert statement is skipped as non-existent in the release build, so there is no runtime processing load in the release build.

4break Statement

The break statement is a statement that exits the processing of a block (Table 4-1).
Table 4-1: break Statement Syntax

break Block Identifier

The specification of the break statement is shown in Figure 4-1.
  • It exits the process of block block, for block, if block, switch block, try block, and while block with the specified identifier.
  • Exiting the block process means jumping to the position immediately after the end of the block.
Figure 4-1: break Statement Specification
Reason For The Design

In many languages, only the closest block can be exited, so exiting a multiple loop required a roundabout way of adding a flag variable. This is why I have made it possible to specify the identifier of the exiting block in Kuin.

In Kuin, many blocks, including if blocks, can be exited with a break statement, so to avoid confusion over which block to exit, I decided that the identifier could not be omitted.

5const Statement

The const statement defines a constant. A constant is a variable whose value cannot be rewritten. The way it is defined is the same as for variables (Table 5-1).
Table 5-1: const Statement Syntax

const Constant Name: Type Name :: Value

The const statement can be written in a global area outside of a function, or even within a class.
The specification of the const statement is shown in Figure 5-1.
  • Defined constants are replaced with values at compile time and do not consume memory at run time.
  • The value specified in the const statement must be Compile-time Constant.
Figure 5-1: const Statement Specification

6do Statement

The do statement is a statement that evaluates an expression (Table 6-1).
Table 6-1: do Statement Syntax

do Expression

The specification of the do statement is shown in Figure 6-1.
  • The last operation to be evaluated must have side effects.
Figure 6-1: do Statement Specification
Reason For The Design

The reason for requiring the do syntax for the execution of a simple expression is to improve readability and compilation speed by enforcing a consistent rule that the meaning of the operation must be indicated at the beginning of the line.

However, since the execution of the expression is written with high frequency, I kept it to two characters to minimize the number of types as much as possible.

7excode Statement

The excode statement is a statement that can directly describe the code that will be generated after compilation (Table 7-1).
Table 7-1: excode Statement Syntax

excode String Value

It is an advanced feature.
Its specification varies depending on the environment specified in the "-e" compilation option.

8include Statement

The include statement is a statement that includes the source code written in a separate file (Table 8-1).
Table 8-1: include Statement Syntax

include Part Of The File Name

If the name of the source code that describes the include statement is "sample.kn" and part of the file name specified in the include statement is "part", the file "sample.part.kn" will be read and included.
It is not possible to include multiple steps, such as "sample.part.part2.kn".
The included source code will be treated as if you had written it directly in the source code describing the include statement.

9ret Statement

The ret statement is a statement that exits the function(Table 9-1).
Table 9-1: ret Statement Syntax

ret

ret Return Value

The ret statement must return a value for functions that specify a return type, and must not return a value for functions that do not specify a return type.
The specification of the ret statement is shown in Figure 9-1.
  • If the process exits from a function that specifies the type of the return value without using the ret statement, the default value of the type will be returned as the return value.
Figure 9-1: ret Statement Specification
Reason For The Design

In many languages it is "return", but in x86 assembly the abbreviation "ret" is used, and I adopted the shorter form.

10skip Statement

The skip is a statement that skips a loop once (Table 10-1).
Table 10-1: skip Statement Syntax

skip Block Identifier

The specification of the skip statement is shown in Figure 10-1.
  • It skips one loop of the for and while blocks for the specified identifier.
  • Skipping the loop of a block means jumping to just before the end of that block.
Figure 10-1: skip Statement Specification
Reason For The Design

In many languages it is "continue", but since it is long and cumbersome to type, I decided on "skip".

11throw Statement

The throw statement is a statement that raises an exception (Table 11-1).
Table 11-1: throw Statement Syntax

throw Exception Code

The exception code must be specified as int. See Kuin Exception Codes for specific values.
When an exception occurs, the application exits one function after another and terminates. However, with the try block, you can catch exceptions that occur in the middle of execution and continue processing. You can raise an exception where an error occurs and use a try block where you want to handle the error.
Reason For The Design

Many languages allow users to inherit exception classes to perform advanced exception handling, but this is often cumbersome and not used properly, so I decided to focus on exception codes that are easy to handle practically.

12var Statement

The var statement defines a variable. See Kuin Variable Definition for details.
The var statement can be written in a global area outside of a function, or even within a class.

13block Block

A block block is a block that does nothing (Table 13-1).
Table 13-1: block Block Syntax

block
    Processing
end block

block Block Name
    Processing
end block

It is used for splitting scopes and exiting with break.

14class Block

The class block defines a class. See Classes for details.
The class block can be written in a global area outside of a function, or even within a class.

15enum Block

The enum block defines an enum. See Enumerated Types for details.
The enum block can be written in a global area outside of a function, or even within a class.

16for Block

The for block is a block that loops while counting (Table 16-1).
Table 16-1: for Block Syntax

for(Initial Value, Last Value)
    Processing
end for

for Block Name(Initial Value, Last Value)
    Processing
end for

for(Initial Value, Last Value, Increase/Decrease Value)
    Processing
end for

In the for block, repeat from the initial value to the last value, increasing or decreasing by the increase/decrease value. The increase/decrease value is interpreted as 1 when omitted.
The specification of the for block is shown in Figure 16-1.
  • The initial value, last value, and increase/decrease value must all be int.
  • The increase/decrease value must be Compile-time Constant and a non-zero value.
  • The count value of the for block can be obtained by referring to the block name as an int variable.
  • The condition to repeat the loop is "count value<=last value" when "increase/decrease value>0", and "count value>=last value" when "increase/decrease value<0".
  • The expressions specified by the initial value, last value, and increase/decrease value are evaluated just before entering the for block, and are not re-evaluated during the repeated loop.
Figure 16-1: for Block Specification
Reason For The Design

In many languages, for requires the user to specify variables for the initial, last, and increase/decrease values, and specifying the wrong variables could lead to bugs, so I designed Kuin to be simpler and more focused on counting.

This makes it impossible to do things like increase by 2 as in other languages, but I think that while should be used for such complex operations.

17func Block

The func block defines a function. See Kuin Function Definition for details.
The func block can be written in a global area outside of a function, or even within a class.

18if Block

The if block is a block that branches the process depending on the condition Table 18-1).
Table 18-1: if Block Syntax

if(Condition)
    Processing
end if

if Block Name(Condition)
    Processing
end if

if(Condition 1)
    Processing
elif(Condition 2)
    Processing
elif(Condition 3)
    Processing
else
    Processing
end if

In an if block, multiple elif clauses can be lined up or an else clause can be written at the end, both of which can be omitted.
The specification of the if block is shown in Figure 18-1.
  • The conditions must be bool.
  • Evaluate the expressions for the conditions in turn, and if any of the conditions become true, execute the contents and exit the block.
  • If none of the conditions are true, the contents of the else clause will be executed. If there is no else clause at this point, exit the block without doing anything.
  • If the condition is Compile-time Constant and therefore the process that is always executed (the contents of if, elif, or else) is determined to be one, the if block is replaced with a block block, and the process that is never executed is deleted at compile time.
Figure 18-1: if Block Specification
Reason For The Design

elif is an abbreviation for the so-called else if used in some languages such as Python.

19switch Block

The switch block is a block that branches the process depending on the value (Table 19-1).
Table 19-1: switch Block Syntax

switch(Value To Be Compared)
case Value 1
    Processing
case Value 2
    Processing
end switch

switch Block Name(Value To Be Compared)
case Value 1
    Processing
case Value 2
    Processing
end switch

switch(Value To Be Compared)
case Value 1, Value 2
    Processing
case Lower Limit Of Value 3 to Upper Limit Of Value 3
    Processing
end switch

switch(Value To Be Compared)
case Value 1
    Processing
case Value 2
    Processing
default
    Processing
end switch

In an switch block, multiple case clauses can be lined up or a default clause can be written at the end. The default clause can be omitted.
You can specify multiple values in the case clause by separating them with ",", or you can specify a range between a and b by writing "a to b".
The specification of the switch block is shown in Figure 19-1.
  • The value must be of a type that allows comparison between large and small.
  • There must be at least one case clause.
  • It checks if there is a case that matches the value to be compared, and if there is, executes its contents and exits the block.
  • If it matches more than one case, the first written case is adopted.
  • If it does not match any case, the contents of the default clause are executed. If there is no default clause at this time, exit the block without doing anything.
  • When the block name is referenced as if it were a variable, the value to be compared is returned in that type.
Figure 19-1: switch Block Specification
Reason For The Design

In C and other languages, if you don't write break at the end of a case, the process will go into the next case, but there are few situations where you want the process to go into the next case, and it is easy to cause bugs. So I designed Kuin to automatically exit the block without a break.

Instead, I have enriched the way we write conditions in the case clause so that we can write the same process for multiple values.

20try Block

The try block is a block that handles any exceptions that occur (Table 20-1).
Table 20-1: try Block Syntax

try
    Processing
catch
    Processing
end try

try Block Name
    Processing
catch
    Processing
end try

try
    Processing
catch Exception Code 1, Exception Code 2
    Processing
catch Lower Limit Of Exception Code to Upper Limit Of Exception Code
    Processing
end try

try
    Processing
finally
    Processing
end try

try
    Processing
catch
    Processing
finally
    Processing
end try

If an exception occurs in the try clause, the process jumps to the corresponding catch clause. Also, the finally clause will always be executed whether or not an exception occurs.
When an exception occurs, the application exits one function after another and terminates. However, the catch clause can be used to catch exceptions, and the finally clause can be used to write processes that should be handled without omission, such as resource release.
You can specify multiple values in the catch clause by separating them with ",", or you can specify a range between a and b by writing "a to b". The values can also be omitted.
You can write a finally clause at the end, or omit it.
The specification of the try block is shown in Figure 20-1.
  • At least one of the catch and finally clauses must exist.
  • It checks if there is a catch that matches the exception code of the exception that occurred, and if there is, executes its contents and exits the block.
  • If it matches more than one catch, the first written catch is adopted.
  • The finally clause is always executed just before exiting the block, regardless of whether the catch clause has been executed or not.
  • If no exception code in any catch clause corresponds to the exception when it is raised, the exception will recur immediately after the block is exited.
  • Do not use ret, break, skip, etc. within a finally clause to leave its own try block. This is because the exception that was raised will be dropped without recurrence and the process will continue.
  • When the block name is referenced as a variable, the exception code is returned as int.
Figure 20-1: try Block Specification

21while Block

The while block is a block that keeps looping as long as a condition is met (Table 21-1).
Table 21-1: while Block Syntax

while(Condition)
    Processing
end while

while Block Name(Condition)
    Processing
end while

while(Condition, skip)
    Processing
end while

If skip is specified, the first conditional decision is skipped and the content is always processed at least once.
Reason For The Design

This skip is equivalent to the do-while in C and other languages.

1660379591enf