-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,035 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
--- | ||
lastUpdated: true | ||
--- | ||
|
||
# 逻辑语句 | ||
|
||
::: tip | ||
MCFPP中的逻辑语句和C/Java中的逻辑语句完全一致。如果你对其他语言足够熟悉,你可以跳过这一节。 | ||
::: | ||
|
||
## if语句 | ||
|
||
`if`语句是一种条件语句,它用来判断一个条件是否成立。如果条件成立,那么`if`语句中的代码块将会被执行。`if`语句的语法如下: | ||
|
||
```mcfpp | ||
if (condition){ | ||
#code | ||
} | ||
``` | ||
|
||
`condition`是一个布尔表达式,它的值为`true`或`false`。如果`condition`的值为`true`,那么`#code`中的代码块将会被执行。 | ||
|
||
`if`语句还可以和`else`语句一起使用,`else`语句用来在`if`语句的条件不成立时执行代码块。`if-else`语句的语法如下: | ||
|
||
```mcfpp | ||
if (condition){ | ||
#code1 | ||
}else{ | ||
#code2 | ||
} | ||
``` | ||
|
||
`condition`是一个布尔表达式,它的值为`true`或`false`。如果`condition`的值为`true`,那么`#code1`中的代码块将会被执行;否则,`#code2`中的代码块将会被执行。 | ||
|
||
可以使用`else if`语句用来在`if`语句的条件不成立时判断另一个条件。`if-else if-else`语句的语法如下: | ||
|
||
```mcfpp | ||
if (condition1){ | ||
#code1 | ||
}else if (condition2){ | ||
#code2 | ||
}else{ | ||
#code3 | ||
} | ||
``` | ||
|
||
## while语句和do-while语句 | ||
|
||
`while`语句是一种循环语句,它用来重复执行一个代码块,直到条件不成立。`while`语句的语法如下: | ||
|
||
```mcfpp | ||
while (condition){ | ||
#code | ||
} | ||
``` | ||
|
||
`condition`是一个布尔表达式。如果`condition`的值为`true`,那么则执行`#code`代表的代码块。此后,再次判断`condition`的值,如果`condition`的值为`true`,那么`#code`代表代码块将会被执行;如此循环,直到`condition`的值为`false`。 | ||
|
||
`do-while`语句和`while`类似,但是无论条件是否成立,它都会先执行因此循环体中的语句,而后再判断条件来决定是否继续进行。`do-while`语句的语法如下: | ||
|
||
```mcfpp | ||
do{ | ||
#code | ||
}while (condition); | ||
``` | ||
|
||
## for语句 | ||
|
||
`for`语句是循环的一种稍复杂的版本,它的语法如下: | ||
|
||
```mcfpp | ||
for (forinit; condition; forupdate){ | ||
#code | ||
} | ||
``` | ||
|
||
`forinit`是一个初始化表达式,它用来初始化循环变量。`condition`是一个布尔表达式,它用来判断循环是否继续。`forupdate`是一个更新表达式,它用来更新循环变量。`#code`代表了循环体,即循环体中的代码。在运行的时候,`for`语句的执行过程如下: | ||
|
||
1. 执行`forinit`,初始化循环变量。 | ||
2. 判断`condition`的值,如果`condition`的值为`true`,则执行`#code`代表的代码块,然后执行`forupdate`,更新循环变量,再次判断`condition`的值。 | ||
3. 如果`condition`的值为`false`,则退出循环。 | ||
|
||
`for`循环中,`forinit`声明的变量只在`for`循环中有效。 | ||
|
||
## break和continue语句 | ||
|
||
`break`语句用来跳出整个循环,`continue`语句用来跳过本次循环。例如: | ||
|
||
```mcfpp | ||
for (int i = 0; i < 10; i++){ | ||
if (i == 5){ | ||
break; | ||
} | ||
if (i == 3){ | ||
continue; | ||
} | ||
#code | ||
} | ||
``` | ||
|
||
在上面的例子中,当`i`的值为`5`时,`break`语句会跳出整个循环;当`i`的值为`3`时,`continue`语句会跳过本次循环,直接进行下一次循环。因此,i在每次循环中的变化为:`0`,`1`,`2`,`4`,`5`,最后跳出循环。 | ||
|
||
`break`和`continue`语句只能在循环中使用。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
lastUpdated: true | ||
--- | ||
|
||
# 顶层语句 | ||
|
||
在MCFPP中,允许在文件的开始直接编写语句而无需额外定义函数,即顶层语句。顶层语句处于一个隐式的函数中,这个函数每个文件有且只有一个,且不能被外部调用。它的返回值类型为`void`。 | ||
|
||
```mcfpp | ||
print("Top statement"); | ||
func main(){ | ||
print("Function"); | ||
} | ||
``` | ||
|
||
在编译后,会生成两个函数——分别对应main函数以及顶层语句对应的默认函数。 | ||
|
||
顶层语句只能在文件的开始编写,即在函数定义或类定义之前。顶层语句可以调用文件中声明的其他函数和类 | ||
|
||
```mcfpp | ||
main(); | ||
func main(){ | ||
print("Function"); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
lastUpdate: true | ||
--- | ||
|
||
# 命名空间 | ||
|
||
MCFPP中的命名空间和MC中的命名空间是同一种东西。这也意味着,命名空间只能是小写字母、数字,点和下划线的组合。 | ||
|
||
你可以在文件中声明一个命名空间,这样文件中的所有函数和变量都会被放置在这个命名空间中。例如: | ||
|
||
```mcfpp | ||
namespace test; | ||
func test(){ # test:test函数 | ||
print(i); | ||
} | ||
``` | ||
|
||
一个文件中只能声明一次命名空间。 | ||
|
||
同样的,你也可以在项目配置文件中声明这个命名空间。 | ||
|
||
```json | ||
{ | ||
"file":[ | ||
"*" | ||
"D:/workspace/mcfpp/project/*" | ||
], | ||
"version":"1.19.4", | ||
"include":[ | ||
"D:/workspace/mcfpp/another_project.json" | ||
], | ||
"targetPath":"./out", | ||
//工程的默认命名空间。可选,默认为default // [!code focus] | ||
"namespace":"mcfpp" // [!code focus] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
lastUpdate: true | ||
--- | ||
|
||
# 定义和调用 | ||
|
||
MCFPP函数的定义方式和C/Java有较大的区别,而更加接近于Python的语法。 | ||
|
||
## 函数定义 | ||
|
||
MCFPP中,函数的定义语法如下: | ||
|
||
```mcfpp | ||
func functionName(parameter1, parameter2, ...) -> returnType{ | ||
#函数体 | ||
} | ||
``` | ||
|
||
`func`是函数的关键字,`functionName`是函数的标识符或者说名字,而紧随其后的`parameter1, parameter2, ...`则是函数的参数列表,`returnType`是可选的,即函数的返回类型。函数体则是由`{}`包裹的一系列语句。下面是一个实际的例子: | ||
|
||
```mcfpp | ||
func add(int a, int b) -> int{ | ||
return a + b; | ||
} | ||
``` | ||
|
||
这个函数的名字是`add`,它有两个整数类型的参数`a`和`b`,返回值也是一个整数类型。这个函数的作用是把两个参数相加并返回结果。 | ||
|
||
## return | ||
|
||
`return`和Minecraft中的`return`命令作用相同,都是用于返回函数的返回值。它的语法即为`return expression;`,其中`expression`是一个表达式,它的值就是函数的返回值。 | ||
|
||
如果一个函数定义了返回值类型,那么它的每一个分支都必须有`return`语句,即函数必定返回一个值。且`return`语句返回的值必须和函数的返回值类型相同,或者是返回值类型的子类型。 | ||
|
||
如果一个函数没有定义返回值类型,那么默认为`void`,即不会返回任何值。这个时候,`return`语句仍然是可用的,但是它的语法变为`return;`,即不带任何表达式。它将会起到立刻终止函数运行的作用。 | ||
|
||
## 函数的调用 | ||
|
||
MCFPP中,函数的调用语法和C/Java一样,即`functionName(parameter1, parameter2, ...);`。其中,`functionName`是函数的名字,`parameter1, parameter2, ...`是要传递给函数的参数。下面是一个实际的例子: | ||
|
||
```mcfpp | ||
func test(){ | ||
print(add(1, 2)); #上面定义的add函数 | ||
} | ||
``` | ||
|
||
这个例子中,`test`函数调用了`add`函数,并传递了两个参数`1`和`2`。`add`函数返回了`3`,因此`test`函数将会打印出`3`。 | ||
|
||
## 函数的传参 | ||
|
||
函数中,对参数的修改不会影响到函数外部的变量。例如: | ||
|
||
```mcfpp | ||
func test(int a){ | ||
a = 5; | ||
} | ||
void main(){ | ||
int a = 0; | ||
test(a); | ||
print(a); #输出0 | ||
} | ||
``` | ||
|
||
在这个例子中,`test`函数对传入的参数`a`进行了修改,但是`main`函数中的`a`并没有受到影响。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
lastUpdate: true | ||
--- | ||
|
||
# static关键字 | ||
|
||
`static`关键字用于声明一个静态参数。静态参数表示,在参数传递的过程中,是传递的参数本身而不是参数的值,因此在函数中对参数的修改会影响外部的变量。例如: | ||
|
||
```mcfpp | ||
func test(static int a){ | ||
a = 5; | ||
} | ||
void main(){ | ||
int a = 0; | ||
test(a); | ||
print(a); #输出5 | ||
} | ||
``` |
Oops, something went wrong.