Aug 13, 2022Kuina-chan


This is the language specification 4 of Kuin Programming Language, and is about variables and functions.

1Kuin Variable Definition

In Kuin, variables are defined as shown in Table 1-1.
Table 1-1: Variable Definition

var Variable Name: Type Name

var Variable Name: Type Name :: Initial Value

For local variables, if you specify an initial value, that value will be assigned when the variable is created.
In the case of global variables, only Compile-time Constant can be specified as the initial value, and that value will be assigned when the program starts.
If no initial value is specified, the variable will be assigned the default value (see Kuin Types And Literals).
Figure 1-1 shows an example of defining a variable str of type []char and specifying '"abcde"' as the initial value.
  1. var str: []char :: "abcde"
Figure 1-1: Example Of Variable Definition
When variables are defined outside of a function, they become global variables and are created in a static area of memory. When variables are defined in a function, they become local variables and are created in the stack area of memory. When variables are defined in a class, they become properties (member variables).
Reason For The Design

Variable definitions are written in much the same way as in Pascal. I adopted it because we can understand that it is a variable definition at the beginning of the line, and it improves readability and compilation speed.

2Kuin Function Definition

In Kuin, functions are defined as shown in Table 2-1.
Table 2-1: Function Definition

func Function Name(Argument Name 1: Type Name 1, Argument Name 2: Type Name 2, ...)
    Function Contents
end func

func Function Name(Argument Name 1: Type Name 1, Argument Name 2: Type Name 2, ...): Type Name Of Return Value
    Function Contents
end func

For example, when defining a function f that takes arguments a and b of type int and returns a value of type bool, it is shown in Figure 2-1.
  1. func f(a: int, b: int): bool
  2. end func
Figure 2-1: Example Of Function Definition
When functions are defined in a class, they become methods (member functions).
Reason For The Design

Function definitions are written in much the same way as in Pascal. I adopted it because we can understand that it is a function definition at the beginning of the line, and it improves readability and compilation speed.

2.1Inner Function



You can also define a function within a function (Figure 2-2).
  1. func f(a: int, b: int): bool
  2.   func g(a: int, b: int): bool
  3.   end func
  4. end func
Figure 2-2: Example Of Defining Inner Function
However, inner functions have the restrictions shown in Figure 2-3.
  • It is not possible to access local or me variables defined in outer functions.
Figure 2-3: Restrictions On Inner Functions

2.2Pass By Reference



Writing "&" in front of the type of the dummy argument will result in reference passing, where the reference to the variable is passed to the argument.
When you pass a variable as a pass-by-reference argument, you must also prefix the actual argument variable with "&" (Figure 2-4).
  1. func main()
  2.   var x: int
  3.   var y: int
  4.   do f(&x, &y)
  5.  
  6.   func f(a: &int, b: &int)
  7.     do a :: 3
  8.     do b :: 5
  9.   end func
  10. end func
Figure 2-4: Pass-by-reference Example
In this example, when "f(&x, &y)" is called, the reference to the variables x and y is passed and the values 3 and 5 are assigned in the function f, so x becomes 3 and y becomes 5.
If you just write "&" without writing the variable name in the argument of pass-by-reference, a temporary variable will be created and passed automatically (Figure 2-5).
  1. func main()
  2.   do f(&, &)
  3.  
  4.   func f(a: &int, b: &int)
  5.     do a :: 3
  6.     do b :: 5
  7.   end func
  8. end func
Figure 2-5: Shorthand Notation For Pass-by-reference
This notation is useful when the return value of a function is being returned by pass-by-reference and you do not need to receive it.
The value of the temporary variable passed when you just write "&" is initialized with the default value of the type just before the function call.
1660379546enf