|
2 | 2 | lastUpdated: true |
3 | 3 | --- |
4 | 4 |
|
5 | | -# Variable |
| 5 | +# Variables |
6 | 6 |
|
7 | | -## Defination of variables |
| 7 | +:::warning |
| 8 | +This content has been translated by AI. We welcome native speakers to help improve the translation. |
| 9 | +::: |
| 10 | + |
| 11 | +## Declaring variables |
8 | 12 |
|
9 | | -Same as other languages, you can define some variables in MCFPP for store, transfer and process data. |
| 13 | +As in other languages, you can declare variables in MCFPP to store, pass and process data. |
10 | 14 |
|
11 | | -The defination of data is like this: |
| 15 | +Typical declaration: |
12 | 16 |
|
13 | 17 | ```mcfpp |
14 | | -#<type> <identifier> [= <expression>] |
15 | | -int i = 5; |
16 | | -int b = i * 6; |
| 18 | +#var <identifier> [as <type>] [= expression or value] |
| 19 | +var i as int = 5; |
| 20 | +var b as int = i * 6; |
| 21 | +
|
| 22 | +#type inferred |
| 23 | +var j = 5; # j is inferred as int |
| 24 | +var k = 5.0; # k is inferred as float |
| 25 | +var i; # [!code error] # error: missing initializer, cannot infer |
17 | 26 | ``` |
18 | 27 |
|
19 | | -The identifier of variable can be the combination of any characters and numbers. In one scope, a name of variable only can defined once. |
| 28 | +Identifiers may contain letters and digits. In the same scope a name can be declared only once. |
20 | 29 |
|
21 | | -## Type of variable |
| 30 | +## Variable types |
22 | 31 |
|
23 | | -In MCFPP, variable have basic data type and combined data type. Basic data type is hard-coding and built-in, but combined data types, such as class, template and so on, can be determined by the developer themselves. So on, the basic types MCFPP have is shown here: |
| 32 | +MCFPP has primitive and composite types. Primitives are built-in; composite types (classes, templates, etc.) are user-defined. Current primitives: |
24 | 33 |
|
25 | | -| Type name | Type description | Example | |
| 34 | +|Type|Description|Example| |
26 | 35 | |-|-|-| |
27 | | -|int| The most basic data type, represent an integer |`1`,`114514`,`-5`| |
28 | | -|float| Represent a single-precision floating-point |`2.5`,`1.0`,`9.5e6`| |
29 | | -|bool| Represent a Boolean data |`true`,`false`| |
30 | | -|nbt| Represents a NBT data |`"a":{"b":"c"}`,`"a":[1,2,3,4]`| |
31 | | -|list| Represent a list |`[1,2,3,4]`,`["a","b","c"]`| |
32 | | -|dict| Represent a dictionary |`{"a":1,"b":2,"c":3}`| |
33 | | -|map|A high-level implement of dictionary, have much more functions than dictionary | Same as dictionary | |
34 | | -|string| Represent a string |`"mcfpp"`,`"qwq"`| |
35 | | -|text| Represents the raw json text |`"mcfpp"`,`{"text":"mcfpp","color":"#114514"}`| |
36 | | -|entity| Represent an entity. Store the UUID of an entity | omit | |
37 | | -|selector| Represent a entity selector |`@a`,`@p`| |
38 | | - |
39 | | -There's a simple introduction for type, You can expand and view them one by one: |
| 36 | +|int|integer|`1`,`114514`,`-5`| |
| 37 | +|float|single-precision float|`2.5`,`1.0`,`9.5e6`| |
| 38 | +|bool|boolean|`true`,`false`| |
| 39 | +|nbt|NBT data|`"a":{"b":"c"}`,`"a":[1,2,3,4]`| |
| 40 | +|list|list|`[1,2,3,4]`,`["a","b","c"]`| |
| 41 | +|dict|dictionary|`{"a":1,"b":2,"c":3}`| |
| 42 | +|map|richer map with more features|same as above| |
| 43 | +|string|string (NBT string tag)|`"mcfpp"`,`"qwq"`| |
| 44 | +|text|text components|`"mcfpp"`,`{"text":"mcfpp","color":"#114514"}`| |
| 45 | +|entity|single entity (stores UUID)|—| |
| 46 | +|selector|target selector|`@a`,`@p`| |
| 47 | + |
| 48 | +Brief descriptions (expandable): |
40 | 49 |
|
41 | 50 | ::: details int |
42 | | -**int** type is the most basic type in MCFPP, represent a integer. It can be positive, negative, zero. Also can be decimal , binary, octal and hexadecimal. An int type data will be stored as a score board variable, So it’s size is same as the score board. |
| 51 | +int is the basic integer type. It can be positive, negative, or zero, and supports decimal, binary, octal, hex, etc. int values are stored as scoreboard variables, so they have scoreboard size limits. |
43 | 52 | ::: |
44 | 53 |
|
45 | 54 | ::: details float |
46 | | -**float** type is a single-precision floating-point type in MCFPP. It can be positive, negative, zero. And also can be decimal, scientific notation and so on. The float calculation of MCFPP relies on [XiaoDou's MathLib](https:#github.com/xiaodou8593/math2.0). The calculation of float calculation is purely score board calculation, so the memory usage won’t be so big. |
| 55 | +A floating‑point number with single precision. Supports decimal and scientific notation. MCFPP float ops rely on Xiaodou's math library. Float operations are implemented via scoreboard math and have modest overhead. |
47 | 56 | ::: |
48 | 57 |
|
49 | 58 | ::: details bool |
50 | | -**bool** type is a type of Boolean data in MCFPP. It only have two values: `true` and `false`. Bool type data would be stored as a score board variable, so it’s size is same as the score board. |
| 59 | +Boolean with values `true` or `false`. Stored as a scoreboard variable. |
51 | 60 | ::: |
52 | 61 |
|
53 | 62 | ::: details nbt |
54 | | -**nbt** type represent a NBT data. But in fact, most times NBT type data just stores a NBT path, so in fact it can be called as NBT Pointer. It’s worth mentioning, **nbt** type variables is the basic of most basic types. Like `list`, `map` are both archive by the NBT data operations. |
| 63 | +NBT data, often acting as an NBT pointer (stores an NBT path). Many composite types (list, map, etc.) are based on NBT operations. |
55 | 64 | ::: |
56 | 65 |
|
57 | 66 | ::: details list |
58 | | -**list** type represent a list, `list` type achieved most functions of `ArrayList` in Java, more details can read the API of the standard library. `list`would be stored as a NBT list. |
| 67 | +List type. Implements most ArrayList methods from Java; stored as an NBT list. |
59 | 68 | ::: |
60 | 69 |
|
61 | 70 | ::: details dict |
62 | | -**dict** type represent to a dictionary, which stored as a combined NBT tag. Because the limit of Minecraft, `dict` type only can insert and delete to basic key values, can’t iterating over an array. You can use `map` to have more operations. |
| 71 | +Dictionary stored as an NBT compound. Due to Minecraft limits, dict supports basic key insert/remove but not iteration. Use map for more operations. |
63 | 72 | ::: |
64 | 73 |
|
65 | 74 | ::: details map |
66 | | -**map** type high-level implement of `dict`, have much more functions than `dict`. `map` type achieve most functions of `HashMap` in Java, more details are in the API of the standard library. But it’s worth mentioning that the higher implement of `map` means `map` have more cost than `dict`. |
| 75 | +A richer wrapper over dict with more features (implements most HashMap methods). Provides more functionality at the cost of additional overhead. |
67 | 76 | ::: |
68 | 77 |
|
69 | 78 | ::: details string |
70 | | -Represent a string, which is a string tag in NBT. |
| 79 | +String type (NBT string tag). |
71 | 80 | ::: |
72 | 81 |
|
73 | 82 | ::: details text |
74 | | -Represent a raw JSON text, compared to `string` type, `text` type can contain more format information, such as color, bold and so on. `text` type data will be stored as a combined NBT tag. |
| 83 | +Raw JSON text. Unlike string, text can include formatting (color, bold, etc.). Stored as an NBT compound. |
75 | 84 | ::: |
76 | 85 |
|
77 | 86 | ::: details entity |
78 | | -Reference to a single entity. Stored as a UUID integer NBT array. |
| 87 | +Represents a single entity. Stored as a UUID integer NBT array. |
79 | 88 | ::: |
80 | 89 |
|
81 | 90 | ::: details selector |
82 | | -Represent a entity selector. Stored as a string. |
| 91 | +Represents a target selector. Stored as a string. |
83 | 92 | ::: |
84 | 93 |
|
85 | | -## `var` |
86 | | - |
87 | | -Keyword `var` can be used to declare a variable, without specifying variable types. Compiler would infer the type of the variable by the initialization expression. |
88 | | - |
89 | | -However, if we use `var` to declare a variable, then the initialization expression is necessary. |
90 | | - |
91 | | -For example: |
92 | | - |
93 | | -```mcfpp |
94 | | -var i = 5; # type of i will be inferred as int |
95 | | -var j = 5.0; # type of j will be inferred as float |
96 | | -var i; # [!code error] # Error, there’s no initialization expression |
97 | | -``` |
98 | | - |
99 | 94 | ## Variable modifiers |
100 | 95 |
|
101 | | -Variable modifiers can used to represent the type of variables, including `dynamic`, `const`, `import` |
| 96 | +Modifiers express variable properties: dynamic, const. |
102 | 97 |
|
103 | 98 | - dynamic |
104 | 99 |
|
105 | | -During Compiling, If any variable has been defined as literal, like `int i = 5;`, compiler will optimize this variable, such as `i += 7` will record `i` as 12 but not compile as scoreboard command. And `dynamic` is used to represent the variable is always dynamic calculating during compilation, though it’s a literal. Such as `dynamic int i = 5;`, `i` will be seen as a dynamic variable during compilation, and won’t be optimized. |
| 100 | +If a variable is a literal (e.g. `int i = 5;`), the compiler may optimize it (e.g. `i += 7` becomes 12 at compile time instead of scoreboard commands). `dynamic` forces runtime/dynamic behavior even for literals. Example: `dynamic int i = 5;` will not be optimized away. |
106 | 101 |
|
107 | 102 | - const |
108 | 103 |
|
109 | | -`const` means a variable is always constant. That is, its value is given during compilation, and never changes. Such as `const int i = 5;`, `i` will be seem as constant during compiling. Constant is always static during compiling, and its value must be clear when we declare it, can’t assign after declaring. |
110 | | - |
111 | | -- import |
112 | | - |
113 | | -`import` means a variable is import variable, it’s value is imported from other place. Such as `import int i;`, `i` will be seen as import variable after compile. Often, variable only can be used after assignment, but if we use `import` modifier, Then variable can be used after declaration without assignment. |
| 104 | +`const` marks a compile-time constant whose value is fixed at compile time and cannot change. It must be initialized on declaration. |
114 | 105 |
|
115 | | -## Optimization of variables |
| 106 | +## Compile-time optimizations |
116 | 107 |
|
117 | | -During compilation , compiler will optimize to some known values. When a variable’s value be set as a literal, compiler would record it’s name, until lose track to this variable. |
| 108 | +The compiler tracks compile-time-known values and optimizes accordingly. When a variable is assigned a literal, the compiler remembers its value until tracking is lost. |
118 | 109 |
|
119 | 110 | ```mcfpp |
120 | | -int i = 5; # Compiler record it’s value is 5 |
121 | | -i += 7; # Compiler set i’s value as 12 directly, won’t output score board command |
122 | | -int j = i; # Compiler also knows j’s value now |
| 111 | +var i = 5; # compiler records i = 5 |
| 112 | +i += 7; # compiler treats i as 12; no scoreboard commands emitted |
| 113 | +var j = i; # compiler also knows j's value |
123 | 114 |
|
124 | | -import int x; |
125 | | -j = x; # Compiler lose track to what j’s value actually is |
126 | | -j += 1; # Compiler will output score board command |
| 115 | +dynamic var x as int; |
| 116 | +j = x; # compiler loses precise value for j |
| 117 | +j += 1; # compiler will emit scoreboard commands |
127 | 118 | ``` |
0 commit comments