Aug 13, 2022Kuina-chan
This is the language specification 9 of Kuin Programming Language, and is about scopes and files.
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).
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).
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.
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).
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.
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.
|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()|
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.