Skip to content

Commit 9a76cd8

Browse files
committed
更新至最新语法
1 parent 9af2514 commit 9a76cd8

28 files changed

+165
-170
lines changed

docs/zh/quickstart/01project/01create-project.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ MCFPP目前提供了两种使用方法:直接使用命令行,或者借助Gra
2323
随后,我们创建一个简单的mcfpp文件,例如`example.mcfpp`
2424

2525
```mcfpp
26-
func hello(){
26+
func hello {
2727
print("Hello World");
2828
}
2929
```

docs/zh/quickstart/02base/01variables.md

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ lastUpdated: true
1111
变量的声明一般如下:
1212

1313
```mcfpp
14-
#<变量的类型> <变量的标识符> [= 表达式或值]
15-
int i = 5;
16-
int b = i * 6;
14+
#var <变量的标识符> [as <变量的类型>] [= 表达式或值]
15+
var i as int = 5;
16+
var b as int = i * 6;
17+
18+
#类型自动推断
19+
var j = 5; #j的类型会被推断为int
20+
var k = 5.0; #k的类型会被推断为float
21+
var i; # [!code error] #错误,缺少初始化表达式,无法推断
1722
```
1823

1924
变量的标识符可以为任意字母和数字的组合。在同一个作用域中,一个相同名称的变量只能被声明一次。
@@ -82,20 +87,6 @@ int b = i * 6;
8287
表示一个目标选择器。储存为一个字符串。
8388
:::
8489

85-
## var关键字
86-
87-
`var`关键字是MCFPP中的一个特殊关键字,它可以用来声明一个变量,而不需要指定变量的类型。编译器会根据变量的初始化表达式来推断变量的类型。
88-
89-
然而,如果要使用`var`关键字声明一个变量,那么变量的初始化表达式是必须的。
90-
91-
例如:
92-
93-
```mcfpp
94-
var i = 5; #i的类型会被推断为int
95-
var j = 5.0; #j的类型会被推断为float
96-
var i; # [!code error] #错误,缺少初始化表达式
97-
```
98-
9990
## 变量修饰符
10091

10192
变量修饰符可以用来表示变量的特性,包括`dynamic``const`
@@ -113,11 +104,11 @@ var i; # [!code error] #错误,缺少初始化表达式
113104
在编译的过程中,编译器会针对一些编译器已知的量进行优化处理。当一个变量被直接赋值为字面量的时候,编译器就会记住这个变量的名字,直到对这个变量失去跟踪。
114105

115106
```mcfpp
116-
int i = 5; #编译器记住了这个变量的值为5
107+
var i = 5; #编译器记住了这个变量的值为5
117108
i += 7; #编译器直接将i的值记为12,不会输出记分板命令
118-
int j = i; #编译器同样也知道了j的值
109+
var j = i; #编译器同样也知道了j的值
119110
120-
dynamic int x;
111+
dynamic var x as int;
121112
j = x; #编译器丢失了对j具体值的跟踪
122113
j += 1; #编译器会输出记分板命令
123114
```

docs/zh/quickstart/02base/03logic-statements.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ do{
6969
`break`语句用来跳出整个循环,`continue`语句用来跳过本次循环。例如:
7070

7171
```mcfpp
72-
for (int i = 0; i < 10; i++){
72+
for (var i = 0; i < 10; i++){
7373
if (i == 5){
7474
break;
7575
}

docs/zh/quickstart/02base/05original-command.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ MCFPP支持Minecraft原生命令的使用。有两种方法可以使用原生命
1111
`/`开头的语句将会被认为是一个原生命令。例如:
1212

1313
```mcfpp
14-
func test(){
14+
func test {
1515
/summon minecraft:armor_stand ~ ~ ~
1616
}
1717
```
@@ -23,7 +23,7 @@ func test(){
2323
`insert`函数可以将一个字符串插入到当前位置。例如:
2424

2525
```mcfpp
26-
func test(){
26+
func test {
2727
insert("summon minecraft:armor_stand ~ ~ ~");
2828
}
2929
```

docs/zh/quickstart/03namespace/01namespace.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ MCFPP中的命名空间和MC中的命名空间是同一种东西。这也意味
1111
```mcfpp
1212
namespace test;
1313
14-
func test(){ # test:test函数
14+
func test { # test:test函数
1515
print(i);
1616
}
1717
```

docs/zh/quickstart/04function/01define-and-call.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,25 @@ func functionName(parameter1, parameter2, ...) -> returnType{
1717
`func`是函数的关键字,`functionName`是函数的标识符或者说名字,而紧随其后的`parameter1, parameter2, ...`则是函数的参数列表,`returnType`是可选的,即函数的返回类型。函数体则是由`{}`包裹的一系列语句。下面是一个实际的例子:
1818

1919
```mcfpp
20-
func add(int a, int b) -> int{
20+
func add(a as int, b as int) -> int{
2121
return a + b;
2222
}
2323
```
2424

2525
这个函数的名字是`add`,它有两个整数类型的参数`a``b`,返回值也是一个整数类型。这个函数的作用是把两个参数相加并返回结果。
2626

27+
若函数没有参数,你可以省略参数列表
28+
29+
```mcfpp
30+
func getRandom -> int{
31+
return random(0, 100);
32+
}
33+
34+
func sayHi {
35+
print("hi");
36+
}
37+
```
38+
2739
## return
2840

2941
`return`和Minecraft中的`return`命令作用相同,都是用于返回函数的返回值。它的语法即为`return expression;`,其中`expression`是一个表达式,它的值就是函数的返回值。
@@ -37,7 +49,7 @@ func add(int a, int b) -> int{
3749
MCFPP中,函数的调用语法和C/Java一样,即`functionName(parameter1, parameter2, ...);`。其中,`functionName`是函数的名字,`parameter1, parameter2, ...`是要传递给函数的参数。下面是一个实际的例子:
3850

3951
```mcfpp
40-
func test(){
52+
func test {
4153
print(add(1, 2)); #上面定义的add函数
4254
}
4355
```
@@ -49,11 +61,11 @@ func test(){
4961
函数中,对参数的修改不会影响到函数外部的变量。例如:
5062

5163
```mcfpp
52-
func test(int a){
64+
func test(int a) {
5365
a = 5;
5466
}
5567
56-
void main(){
68+
void main {
5769
int a = 0;
5870
test(a);
5971
print(a); #输出0

docs/zh/quickstart/04function/02static-params.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ lastUpdate: true
77
`static`关键字用于声明一个静态参数。静态参数表示,在参数传递的过程中,是传递的参数本身而不是参数的值,因此在函数中对参数的修改会影响外部的变量。例如:
88

99
```mcfpp
10-
func test(static int a){
10+
func test(static a as int){
1111
a = 5;
1212
}
1313
14-
void main(){
14+
void main {
1515
int a = 0;
1616
test(a);
1717
print(a); #输出5

docs/zh/quickstart/04function/03inline-function.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ inline func functionName(parameter1, parameter2, ...) -> returnType{
2121
在调用内联函数的地方,编译器会直接将内联函数的代码插入,而不是通过函数调用的方式来执行。例如下面的例子:
2222

2323
```mcfpp
24-
inline func add(int a, int b) -> int{
24+
inline func add(a as int, b as int) -> int{
2525
return a + b;
2626
}
2727
28-
func main(){
28+
func main {
2929
print(add(1, 2));
3030
}
3131
```
3232

3333
编译的时候,上述代码会相当于:
3434

3535
```mcfpp
36-
func main(){
37-
int a = 1;
38-
int b = 2;
39-
int ret = a + b;
36+
func main {
37+
var a = 1;
38+
var b = 2;
39+
var ret = a + b;
4040
print(ret);
4141
}
4242
```

docs/zh/quickstart/05class/01define-and-instantiate.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class SuperCreeper{
1212
1313
text name;
1414
15-
constructor(text name){
15+
constructor(name as text){
1616
this.name = name;
1717
}
1818
19-
override func tick(){
19+
override func tick {
2020
effect(@a[distance=..5], Effects.Poison);
2121
title(@a[distance=..5], this.name);
2222
}
@@ -60,8 +60,8 @@ SuperCreeper p = SuperCreeper("")[
6060
事实上,实体模板初始化器可以用在任何地方,而不只是在实体模板的初始化的时候。比如
6161

6262
```mcfpp
63-
func main(){
64-
Test t = Test();
63+
func main {
64+
var t = Test();
6565
t = t[a = 100];
6666
print(t.toText());
6767
}

docs/zh/quickstart/05class/02member.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,24 @@ lastUpdate: true
1212

1313
```mcfpp
1414
class A{
15-
int a;
16-
int b = 5;
15+
a as int;
16+
b as int = 5;
1717
}
1818
```
1919

2020
上述代码定义了一个实体模板`A`,它有两个属性`a``b``a`是一个整数类型的属性,没有初始化;`b`是一个整数类型的属性,初始化为`5`
2121

22+
:::tip
23+
实体模板和数据模板的成员定义都不需要写`var`关键字,直接写成员的类型和名字即可。
24+
:::
25+
2226
## 方法
2327

2428
方法是实体模板的函数成员,用来操作对象的数据。方法的定义语法如下:
2529

2630
```mcfpp
2731
class A{
28-
void test(){
32+
func test {
2933
print("Hello, World!");
3034
}
3135
}
@@ -35,8 +39,8 @@ class A{
3539

3640
```mcfpp
3741
class A{
38-
int a;
39-
void setA(int a){
42+
a as int;
43+
func setA(a as int){
4044
this.a = a;
4145
}
4246
}
@@ -52,21 +56,21 @@ MCFPP中,实体模板的成员可以使用`public`、`protected`、`private`
5256

5357
```mcfpp
5458
class A{
55-
public int a;
56-
protected int b;
57-
private int c;
59+
public a as int;
60+
protected b as int;
61+
private c as int;
5862
}
5963
6064
class B: A{
61-
void test(){
65+
func test {
6266
a = 1; #合法
6367
b = 2; #合法
6468
c = 3; #编译错误
6569
}
6670
}
6771
68-
func test(){
69-
A obj = A();
72+
func test {
73+
var obj = A();
7074
obj.a = 1; #合法
7175
obj.b = 2; #编译错误
7276
obj.c = 3; #编译错误

docs/zh/quickstart/05class/03object.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ lastUpdate: true
1111
```mcfpp
1212
object class Singleton {
1313
# 成员
14-
int value = 0;
14+
value as int = 0;
1515
1616
# 方法
17-
func setValue(int v) {
18-
value = v
17+
func setValue(value as int) {
18+
this.value = value
1919
}
2020
}
2121
@@ -30,7 +30,7 @@ Singleton.setValue(10)
3030
```mcfpp
3131
class Person {
3232
33-
int id = 0;
33+
id as int = 0;
3434
3535
constructor() {
3636
this.id = Person.nextId();
@@ -39,9 +39,9 @@ class Person {
3939
4040
object class Person {
4141
42-
private int id = 0;
42+
private id as int = 0;
4343
44-
int nextId() {
44+
func nextId -> int {
4545
id = id + 1;
4646
return id;
4747
}

docs/zh/quickstart/05class/04inheritance-abstract.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ MCFPP中,类支持继承和抽象。
1212

1313
```mcfpp
1414
class Parent{
15-
protect int a;
16-
func test(){
15+
protect a as int;
16+
func test {
1717
print("Parent");
1818
print(a);
1919
}
2020
}
2121
2222
class Child: Parent{
23-
int b;
24-
override func test(){
23+
b as int;
24+
override func test {
2525
print("Child");
2626
print(super.a);
2727
}
@@ -38,7 +38,7 @@ abstract class A{
3838
}
3939
4040
class B: A{
41-
override func test(){
41+
override func test {
4242
print("Hello, World!");
4343
}
4444
}

docs/zh/quickstart/05class/05miscellaneous.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ lastUpdate: true
2424

2525
```mcfpp
2626
27-
SuperCreeper p = SuperCreeper("Super Creeper");
27+
var p = SuperCreeper("Super Creeper");
2828
2929
p.dispose();
3030

docs/zh/quickstart/07generic/01concrete_var.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ lastUpdate: true
77
在编译过程中,编译器会对一些编译器可以确定的变量进行优化,例如
88

99
```mcfpp
10-
int i = 5;
11-
int j = 5;
10+
var i = 5;
11+
var j = 5;
1212
1313
print(i + j);
1414
```
@@ -18,8 +18,8 @@ print(i + j);
1818
这种变量,在MCFPP中叫做编译确定量(`Concrete Var`)。编译确定量和普通变量是可以相互转换的,例如:
1919

2020
```mcfpp
21-
int i = 5;
22-
dynamic j;
21+
var i = 5;
22+
dynamic var j as int;
2323
i = j;
2424
```
2525

0 commit comments

Comments
 (0)