头图

Write in front

This is Ozelot. A college student who is learning to program and hopes to communicate and learn with you guys. design plan about the programming language Cesno, welcome to comment and exchange!

Tip: The final design has not been decided yet, there may be many design deficiencies that need to be optimized, so the content may be changed. Because I couldn't leave it unlabeled, I had to label it as "programmer". Hope it doesn't cause trouble to everyone.

Note: Because Cesno has not been produced yet, only the grammar specifications of Cesno are recorded here. Because of this, the highlighting of the code part may not be guaranteed to be displayed correctly every time. If it affects reading, I am very sorry!


The basis of variables

Here will be written about the pre-basic knowledge of variables.

Identifier

Identifier is an indispensable concept in programming languages. It represents the name of a "thing" (this thing can be a variable, function, class...).

Cesno's identifier is similar to other languages. There can be numbers, English letters, "$" signs, and "_" signs in the identifier, but the number cannot appear in the first character of the identifier. There is no limit on the length of the identifier, and other characters except some special characters and numbers are also allowed (because Cesno supports UTF-8 encoding format). But for reasons of readability, simplicity, etc., the identifier is not recommended for names that are too long (for example, more than 64 characters, or any length that is confusing to read), and it is not recommended to use other than ASCII characters. Characters are used as identifiers.

Start characterAfter the start character
number×
English alphabet
"$"、"_"

type

Type is a kind of "standard" about data-it can be used to distinguish the types of data, and it is convenient for us to store and read them.

The following are several types commonly used in Cesno:

Type name (English)Type name (Chinese)Brief explanation
intInteger type (or "integer type")Stores an integer from -2147483648 to 2147483647
floatFloating point double-precision floating decimal point in the IEEE standard
boolBoolean (or "authenticity")Store a true value ( true ) or a fake value ( false )
charCharacter typeStore a character encoded in UTF-8
stringString typeStore a series of characters encoded in UTF-8
objectObject typeBase class for all concrete types

The following table lists are used to identify special conditions in the type. They cannot be used to initialize a variable.

Type name (English)Type name (Chinese)Brief explanation
voidNo type (or "virtual type")Used to identify a function that has no return value
anyArbitrary (or "dynamic type")Used to identify no fixed type
neverVacancy type (or "vacancy type")Used to identify the type that will never appear ( void is actually no return value, and never more inclined to the logical level and there is no type information)

If you want to see more types or have information about types, please move to Cesno type (linked PLACEHOLDER).

Variable operation

Declare variable

To declare a variable, you need to write the name of the type first, followed by the name of the variable (variable names follow the identifier). for example:

int a;

This completes the declaration of an integer variable a When declaring variables, you can also give initial values at the same time, like this:

int b = 10;

The declaration and assignment of Cesno common types are as follows:

int    x = 100;
float  d = 0.5;
bool   t = true;
char   c = 'C';
string s = "Hello, everyone.";
object o = 20; // 事实上,object类变量可以接受任何具体类型的赋值
               // 比如上面的类型都可以填入object。

If the variable to be declared does not have a fixed type (ie "dynamic type"), the keyword var can be used. At this time, Cesno will not check whether the types match during assignment. Dynamic typing can sometimes provide convenience, so please use it appropriately. Cesno's dynamic typing does not mean that the variable "has no type". In fact, the variable type has always been static, but it appears to be dynamic.

var x = 3;  // 此时,x的类型由字面量 3 推导为 int 型
x += 7;     // x现在等于10,这种写法正确的
x = "str";  // x现在为 string 型,由字面量 "str" 推导而来
x = 1 / x;  // 这会报错。Cesno在此时能确定 x 在这里就是 string 型,
            // 而除号不适用于 int 与 string 一起运算。
            // 但如果程序流程包含了选择,Cesno便不能确定采用动态类型的变量的具体类型,
            // 而这可能会导致潜在的错误。所以请勿滥用 var 来定义变量。

Destroy variable

When you don't need a variable to continue to exist, you can destroy them to save your memory. Using the operator delete can easily cancel the variable declaration and release the memory space. For example:

int a = 10;
delete a;

Of course, you can also use delete release multiple variables at once, and you don't need to distinguish whether they are arrays:

int   a = 20;
int[] b = {30, 50, 100, 200};
delete a, b;

When a variable that uses polymorphism is about to be destroyed, there is no need to cast the type:

object x = 10;
delete x;    // 正确,会释放掉 int 大小的空间,并删除 x 的声明

The scope of variable work

Variable namespace

namespace can be used to prevent name conflicts. Programmers can use the namespace keyword plus an identifier to delimit a namespace. Use "." (member access operator) to access (including read and write) members in the namespace, so the inside and outside of the scope can access each other-to access the inside from the outside, use “.” to access the outside from the inside, Just write the name directly. Variables in the current namespace can be declared and destroyed in the namespace, but external variables cannot be destroyed.

int a = 10;
namespace n
{
    int b = 20;
    print(a + b);    // 将输出 30,其中 a = 10,b = 20,a 外 b 内
    a = 50;
    print(a + b);    // 将输出 70,其中 a = 50,b = 20,a 外 b 内
    int a = 60;
    print(a + b);    // 将输出 80,其中 a = 60,b = 20,a 内 b 内
}
print(a);      // a 是 10
print(n.a);    // a 是命名空间内成员,为 60

You can also create an anonymous namespace way not give namespace after additional identifier . The characteristic of this writing is that the content defined in the anonymous namespace cannot be accessed by the outer layer. For example, this code:

int a = 10;
namespace { int anonymous_x = 20; print(anonymous_x); } // 这样写是正确的
// 在这里无法访问 anonymous_x,因为这个命名空间没有名字,你无法写出访问它的式子

Variable scope

Cesno like other languages has scope concept (scope) of. An identifier can be directly written out of the range is its scope. Scope has a deep relationship with namespaces and functions.

A variable is valid in a namespace-including its internal namespace:

int a = 10;
namespace { print(a); /* 正确,可以访问 */ }
print(a); /* 同样正确,可以访问 */

The definition body (such as the function definition body) cannot access the variables of other definition bodies, unless it is a global variable:

global var n = 10;

class Test
{
    var member = n;    // 可以访问,n 是全局变量
}

function void changeGlobalVarN()
{
    n = "Some string";
}

void main
{
    var n = 5;               // 在 main 中覆盖了全局 n 的声明,这个 n 是局部的
    Test t1 = new Test();    // 成员 member 是 10
    changeGlovalVarN();
    Test t2 = new Test();    // 成员 member 是 "Some string"
}

From the above example, we can see: global variables should be used with caution, otherwise it is easy to cause errors.


Ozelot_Vanilla
11 声望3 粉丝

希望能为更多人做出一点贡献