Aug 13, 2022Kuina-chan


This is the reverse lookup dictionary 5 for exe execution environment of Kuin Programming Language, and is about how to handle variables, constants, and functions.

Handling Variable

Handling Constant

Handling Function

Index

1Handling Variable

1.1Create Variable



To create a variable, write "var Variable Name: Type Name" (Figure 1-1).
  1. var a: int
  2.  
  3. func main()
  4.   var b: int
  5.   var c: int :: 7
  6.  
  7.   do @a :: 3
  8.   do b :: 5
  9. end func
Figure 1-1: Create Variable
When a variable is created outside of a function, as in the 1st line, it becomes a global variable that is created when the program starts and destroyed when it exits. When a variable is created in a function as in lines 4-5, it becomes a local variable that is created when the function is called and destroyed when it exits.
You can assign values to global and local variables at the same time as the variables are created, as shown in line 5.
In lines 7-8, the program accesses the variables that were created. When accessing global variables, you need to prefix the variable name with "@" as shown in line 7.
From the time the variable is created until it is assigned a value, it contains the default value of the type. For example, the int type will contain 0.

2Handling Constant

A constant is a value with an easy-to-understand name.

2.1Use Constant



To define a constant, write "const Constant Name: Type Name :: Value" (Figure 2-1).
  1. const three: int :: 3
  2.  
  3. func main()
  4.   const five: int :: 5
  5.  
  6.   var n: int
  7.   do n :: @three {3}
  8.   do n :: five {5}
  9. end func
Figure 2-1: Creating Constant
Constants do not consume memory at runtime because they are compiled as if you had written their values directly into the program.

3Handling Function

3.1Creating Function



To create a function, write commands between "func Function Name(Argument 1, Argument 2, ...): Return Type" and "end func" (Figure 3-1).
  1. func add(a: int, b: int): int
  2.   ret a + b
  3. end func
  4.  
  5. func main()
  6.   func sub(a: int, b: int): int
  7.     ret a - b
  8.   end func
  9.  
  10.   var n: int
  11.   do n :: @add(5, 3) {8}
  12.   do n :: sub(5, 3) {2}
  13.   ret
  14. end func
Figure 3-1: Creating Function
The function add is created in lines 1 to 3, and the function sub is created in lines 6 to 8. The main function in lines 5 to 14 is also a function.
To call a function created outside of a function, prefix the function name with "@" and call it as shown in line 11. To call a function created in a function, call it as shown in line 12. When a function is created within a function, it can only be called within that function, preventing unexpected calls.
If you write "ret Return Value" as in line 2 or 7, the process will exit the function while returning the return value. To create a function with no return value, write the definition without the return type, as in the main function on line 5, and to exit the function in the middle, just write "ret" as on line 13.
If the process exits the function naturally without using the ret statement, the default value of the return type will be returned.

3.2Rewriting Value Of Variable Passed As Argument



When a variable is specified as the argument of a function, only the value of the variable is passed, so the value of the variable does not change even if it is rewritten in the function. To make the value of the original variable also be rewritten when it is rewritten in a function, prefix the argument type with "&" (Figure 3-2).
  1. func setZero1(n: int)
  2.   do n :: 0
  3. end func
  4.  
  5. func setZero2(n: &int)
  6.   do n :: 0
  7. end func
  8.  
  9. func main()
  10.   var a: int :: 5
  11.   do @setZero1(a) {a = 5}
  12.   do @setZero2(&a) {a = 0}
  13.   do @setZero2(&)
  14. end func
Figure 3-2: Call By Reference
In the setZero1 function in the 1st line, the value of n is set to 0 in the 2nd line, but the value of a in the caller remains unchanged.
The setZero2 function in line 5 has "&" in front of the type of the argument n, so if you set the value of n to 0 in line 6, the value of a in the caller will also be rewritten. When passing a variable as such an argument, it is necessary to specify that it will be rewritten in the function by adding "&" even before the variable, as in line 12.
A function can return only one return value, but by using this "&" argument, it can return multiple return values.
If the argument with "&" in front of the type is called with just "&" as in line 13, a disposable variable with a default value will be created and passed to the function. This notation is useful when the "&" argument returns a return value and you don't need to receive that return value.

3.3Assigning Function To Variable



A function can be assigned to a variable, and its type is "func<(Type Of Argument 1, Type Of Argument 2, ...): Return Value Type>" (Figure 3-3).
  1. func add(a: int, b: int): int
  2.   ret a + b
  3. end func
  4.  
  5. func main()
  6.   var f: func<(int, int): int>
  7.   do f :: @add
  8.   var n: int :: f(5, 3) {8}
  9. end func
Figure 3-3: Function Type
In line 7, the variable f is assigned the function @add, and in line 8, f is called as a function. In this case, @add is actually called.
This approach is used, for example, when you want a callback function to be passed as an argument to a function.
1660382602enf