|
| 1 | +--- |
| 2 | +lastUpdated: true |
| 3 | +--- |
| 4 | + |
| 5 | +# Variable |
| 6 | + |
| 7 | +## Declaration of variables |
| 8 | + |
| 9 | +Same as other languages, you can declare some variables in MCFPP for store, transfer and process data. |
| 10 | + |
| 11 | +The declaration of data is like this: |
| 12 | + |
| 13 | +```mcfpp |
| 14 | +#Type of data Identifier of data (optional: = expression or value ) |
| 15 | +int i = 5; |
| 16 | +int b = i * 6; |
| 17 | +``` |
| 18 | + |
| 19 | +The identifier of variable can be the combination of any characters and numbers. In one scope, a name of variable only can declared once. |
| 20 | + |
| 21 | +## Type of variable |
| 22 | + |
| 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 himself. So on, the basic types MCFPP have is shown here: |
| 24 | + |
| 25 | +| Type name | Type description | Example | |
| 26 | +|-|-|-| |
| 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 | +|jtext| 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[limit=6]`| |
| 38 | + |
| 39 | +There’s a simple introduction for type, You can expand and view them one by one: |
| 40 | + |
| 41 | +::: 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. |
| 43 | +::: |
| 44 | + |
| 45 | +::: 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 [小豆的数学库完成](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. |
| 47 | +::: |
| 48 | + |
| 49 | +::: 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. |
| 51 | +::: |
| 52 | + |
| 53 | +::: 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. |
| 55 | +::: |
| 56 | + |
| 57 | +::: 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. |
| 59 | +::: |
| 60 | + |
| 61 | +::: 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. |
| 63 | +::: |
| 64 | + |
| 65 | +::: 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`. |
| 67 | +::: |
| 68 | + |
| 69 | +::: details string |
| 70 | +TODO |
| 71 | +::: |
| 72 | + |
| 73 | +::: details jtext |
| 74 | +TODO |
| 75 | +::: |
| 76 | + |
| 77 | +::: details entity |
| 78 | +TODO |
| 79 | +::: |
| 80 | + |
| 81 | +::: details selector |
| 82 | +TODO |
| 83 | +::: |
| 84 | + |
| 85 | +## var keyword |
| 86 | + |
| 87 | +`var` keyword is a special keyword in MCFPP, it can 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`keyword 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 | +## Variable modifiers |
| 100 | + |
| 101 | +Variable modifiers can used to represent the type of variables, including `dynamic`, `const`, `import` |
| 102 | + |
| 103 | +- dynamic |
| 104 | + |
| 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. |
| 106 | + |
| 107 | +- const |
| 108 | + |
| 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. |
| 114 | + |
| 115 | +## Optimization of variables |
| 116 | + |
| 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. |
| 118 | + |
| 119 | +```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 |
| 123 | +
|
| 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 |
| 127 | +``` |
0 commit comments