Aug 13, 2022Kuina-chan


This is the language specification 9 of Kuin Programming Language, and is about scopes and files.

1Kuin Scope

In Kuin, when you want to specify a local identifier, you write the name as it is, and when you want to specify a global identifier, you prefix the name with "@" (Figure 1-1).
  1. func globalFunc()
  2. end func
  3.  
  4. func main()
  5.   func localFunc()
  6.   end func
  7.  
  8.   do @globalFunc()
  9.   do localFunc()
  10. end func
Figure 1-1: Global And Local Identifiers
In this example, when the global function "globalFunc" is called, it is written as "@globalFunc()", and when the local function "localFunc" is called, it is written as "localFunc()".
The global things can be referenced from anywhere, but the local things can only be referenced from the same or more inner blocks.
For example, if there is a block inside a block, the inner thing can refer to the outer block, but the outer thing cannot refer to the inner (Figure 1-2).
  1. func main()
  2.   for i(1, 10)
  3.     var m: int :: 2 + i
  4.     for j(1, 10)
  5.       var n: int :: i + j + m
  6.       var o: int :: n
  7.     end for
  8.   end for
  9. end func
Figure 1-2: Block Scope
In this example, "i+j+m" refers inside the for blocks i and j, and "2+i" refers inside the for block i, but it is not possible to rewrite "2+i" as "2+j" or "2+n" because j and n are in the inner blocks.

2Kuin File

2.1Splitting A File



In Kuin, source code can be loaded separately in different source files.
Earlier, I explained that local things are referred to with their identifiers as they are, and global things are referred to with "@". A global reference to another source is written as "\source name@identifier", while a global reference to a standard library is written as "source name@identifier" (Figure 2-1).
  1. func main()
  2.   do \other@f()
  3.   do lib@sleep(1000)
  4. end func
Figure 2-1: Function Call In Another Source File
In this example, the global function "f()" of another file created as other.kn is called. It also calls "sleep(1000)", a function of Kuin's standard library lib.kn.
If the source name is prefixed with "\", the source will be loaded from the same directory as the source to be compiled, and if it is not prefixed with "\", it will be loaded from the standard library directory.
User-created sources can also be loaded by diving into a directory, as in "\dir1\dir2\other@f()". It can't go up the directory like "\..\".
The only characters that can be used in file and directory names are lowercase letters, numbers, and "_", which must start with a non-numeric character.
By the way, to prevent unintended access, by default everything is inaccessible from another file, and you need to put a "+" at the beginning of the definition for things you want to make accessible. For example, the function f defined in other.kn earlier needs to be defined as shown in Figure 2-2.
  1. +func f()
  2. end func
Figure 2-2: Public Function
In addition to func, there are global var, const, alias, enum, and class that can be published to a separate file with a "+".
The above can be summarized as shown in Table 2-1.
Table 2-1: How To Refer To Kuin Identifier
Kind Reference Method Example
Local identifier do f()
Global In Its Own Source @identifier do @f()
Global In Another Source \source name@identifier do \src@f()
Standard Library source name@identifier do src@f()

2.2include Statement



If you want to split the source code into separate files while keeping the source names to the left of "@" the same, use the include statement.
For example, if you divide the contents of main.kn into two files, main.part1.kn and main.part2.kn, and write them in main.kn as shown in Figure 2-3, the contents of the two files will be included in main.kn and treated as if you had written the contents of main.part1.kn and main.part2.kn directly in main.kn.
  1. include part1
  2. include part2
  3.  
  4. func main()
  5. end func
Figure 2-3: include Statement
1660379763enf