!56 refactor: 重构关键字

Merge pull request !56 from zhangxun/feature/refactor-keyword
This commit is contained in:
Luke 2025-08-04 09:23:38 +00:00 committed by Gitee
commit 00f73ecb47
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
52 changed files with 122 additions and 122 deletions

View File

@ -44,7 +44,7 @@ SnowVM) 的完整编译-执行链路。
Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的语法和严格的类型系统,以帮助 LLM 更好地理解程序。
语言使用显式的 `module` 声明来组织代码,用 `function`,`parameter`,`return_type`,`body` 等关键字分隔不同代码块,语法结构固定且易读。此外,Snow
语言使用显式的 `module` 声明来组织代码,用 `function`,`params`,`returns`,`body` 等关键字分隔不同代码块,语法结构固定且易读。此外,Snow
实现了语义分析来检查变量作用域和类型一致性,在编译阶段捕获错误并确保生成的中间代码正确无误。这种自上而下的编译流程,使得代码设计和生成更加模块化,可解释,也有利于调试和优化。
相关背景: [心路历程](docs/Snow-Lang-Journey/Snow-Lang-Journey.md)
@ -111,7 +111,7 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
module: Main
import:Math
function: main
return_type: int
returns: int
body:
Math.add(6,1)
return 0
@ -135,7 +135,7 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
3 15 IDENTIFIER main
3 19 NEWLINE \n
4 9 KEYWORD return_type
4 9 KEYWORD returns
4 20 COLON :
4 22 TYPE int
4 25 NEWLINE \n
@ -174,10 +174,10 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
#### Math.snow
module: Math
function: add
parameter:
params:
declare n1: int
declare n2: int
return_type: int
returns: int
body:
return n1 + n2
end body
@ -195,7 +195,7 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
2 15 IDENTIFIER add
2 18 NEWLINE \n
3 9 KEYWORD parameter
3 9 KEYWORD params
3 18 COLON :
3 19 NEWLINE \n
@ -211,7 +211,7 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
5 25 TYPE int
5 28 NEWLINE \n
6 9 KEYWORD return_type
6 9 KEYWORD returns
6 20 COLON :
6 22 TYPE int
6 25 NEWLINE \n
@ -494,7 +494,7 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
```snow
module: Math
function: main
return_type: int
returns: int
body:
Math.factorial(6)
return 0
@ -502,9 +502,9 @@ module: Math
end function
function: factorial
parameter:
params:
declare n:int
return_type: int
returns: int
body:
declare num1:int = 1
loop:

View File

@ -76,8 +76,8 @@
module: Main
import:Math
function: main
parameter:
return_type: int
params:
returns: int
body:
Math.factorial(6L,1L)
@ -90,10 +90,10 @@ end module
## 源代码 (test.snow)
module: Math
function: factorial
parameter:
params:
declare n1: long
declare n2: long
return_type: long
returns: long
body:
return n1+n2
end body

View File

@ -7,7 +7,7 @@
```snow
module: Main
function: main
return_type: int
returns: int
body:
return 1 + 1
@ -114,7 +114,7 @@ cond 可以是表达式(结果为 bool 类型)或者 bool 字面量
```snow
module: Main
function: main
return_type: int
returns: int
body:
if 5 > 7 then
return 5
@ -152,7 +152,7 @@ end loop
```snow
module: Main
function: main
return_type: int
returns: int
body:
declare sum: int = 0
loop:
@ -177,17 +177,17 @@ end module
函数的形式如下:
```snow
function: add
parameter:
params:
declare a: int
declare b: int
return_type: int
returns: int
body:
return a + b
end body
end function
```
其中 add 是函数名parameter 下面是参数列表(可省略),与变量的定义类似,但是不允许赋初值,
接着 return_type 设置返回值类型,最后的 body 为函数体。
其中 add 是函数名params 下面是参数列表(可省略),与变量的定义类似,但是不允许赋初值,
接着 returns 设置返回值类型,最后的 body 为函数体。
## 模块
@ -200,7 +200,7 @@ snow 会自动将同名模块的函数合并。
```snow
module: Main
function: main
return_type: int
returns: int
body:
return 1 + 1
@ -216,10 +216,10 @@ end module
// Math.snow
module: Math
function: add
parameter:
params:
declare a: int
declare b: int
return_type: int
returns: int
body:
return a + b
end body
@ -233,7 +233,7 @@ end module
module: Main
import: Math
function: main
return_type: int
returns: int
body:
return Math.add(5, 7)
@ -248,7 +248,7 @@ end module
module: Main
import: Math, Time
function: main
return_type: int
returns: int
body:
return Math.add(5, 7)

View File

@ -1,7 +1,7 @@
module: Math
function: main
parameter:
return_type: int
params:
returns: int
body:
Math.factorial(6)
return 0
@ -9,9 +9,9 @@ module: Math
end function
function: factorial
parameter:
params:
declare n:int
return_type: int
returns: int
body:
declare num1:int = 1
loop:

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -1,6 +1,6 @@
function: main
parameter:
return_type: int
params:
returns: int
body:
loop:
init:

View File

@ -1,6 +1,6 @@
module: Main
function: main
return_type: void
returns: void
body:
declare abc:int =1
end body

View File

@ -1,7 +1,7 @@
module: Main
import: os
function: main
return_type: void
returns: void
body:
loop:
init:

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -1,7 +1,7 @@
module: Main
import: os
function: main
return_type: void
returns: void
body:
// 合法
declare b1: byte = 127b

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -1,7 +1,7 @@
module: Main
import: ModuleB
function: main
return_type: int
returns: int
body:
return ModuleB.fun()
end body

View File

@ -1,6 +1,6 @@
module: ModuleA
function: fun
return_type: int
returns: int
body:
return 123
end body

View File

@ -1,7 +1,7 @@
module: ModuleB
import: ModuleA
function: fun
return_type: int
returns: int
body:
return ModuleA.fun() + ModuleA.fun()
end body

View File

@ -3,8 +3,8 @@ module: Main
globals:
declare sum: int = 0
function: main
parameter:
return_type: int
params:
returns: int
body:
sum = 20

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -3,8 +3,8 @@ module: Main
globals:
declare sum: int = 123
function: main
parameter:
return_type: int
params:
returns: int
body:
os.print(sum)
return 0

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -1,7 +1,7 @@
module: Main
import:Math
function: main
return_type: int
returns: int
body:
Math.add(6,1)
return 0

View File

@ -1,9 +1,9 @@
module: Math
function: add
parameter:
params:
declare n1: int
declare n2: int
return_type: int
returns: int
body:
return n1 + n2
end body

View File

@ -1,5 +1,5 @@
function: main
return_type: int
returns: int
body:
declare res: boolean = 8L > 7L
if res then

View File

@ -1,5 +1,5 @@
function: main
return_type: int
returns: int
body:
return 65537
end body

View File

@ -1,6 +1,6 @@
module: Main
function: main
return_type: int
returns: int
body:
foo()
@ -9,7 +9,7 @@ module: Main
end function
function: foo
return_type: int
returns: int
body:
if false then
return 1

View File

@ -1,6 +1,6 @@
module: Main
function: main
return_type: int
returns: int
body:
5 == 7
5 == 7s

View File

@ -1,7 +1,7 @@
module: Main
import: os
function: main
return_type: void
returns: void
body:
os.print(222)
end body

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -3,7 +3,7 @@ module: Main
globals:
declare num2:int=10
function: main
return_type: void
returns: void
body:
declare num1:int=11
os.print(num1+num2+abc())
@ -11,7 +11,7 @@ module: Main
end function
function: abc
return_type: int
returns: int
body:
return 1
end body

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -1,7 +1,7 @@
module: Main
import: os
function: main
return_type: int
returns: int
body:
loop:
init:

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -1,7 +1,7 @@
module: Main
import: os
function: main
return_type: int
returns: int
body:
loop:
init:

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -1,7 +1,7 @@
module: Main
import: os
function: main
return_type: int
returns: int
body:
loop:
init:

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -1,7 +1,7 @@
module: Main
import: os
function: main
return_type: void
returns: void
body:
declare n: int[][][][] = [
[

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -1,7 +1,7 @@
module: Main
function: main
parameter:
return_type: int
params:
returns: int
body:
return (1+2) / 3 * 4 + 2 *2
end body

View File

@ -1,7 +1,7 @@
module: Main
import:os
function: main
return_type: void
returns: void
body:
declare arr: int[] = [1, 2, 3]
arr[0] = 5

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -3,8 +3,8 @@ module: Main
globals:
declare sum: int = 123
function: main
parameter:
return_type: int
params:
returns: int
body:
declare arr: int[][][][][][][][][][][][][][][][][][][][][][] = [[[[[[[[[[[[[[[[[[[[[[1], [2], [3]]]]]]]]]]]]]]]]]]]]]]
loop:

View File

@ -1,9 +1,9 @@
module: os
import: os
function: print
parameter:
params:
declare i1: int
return_type: void
returns: void
body:
syscall("PRINT",i1)
end body

View File

@ -1,7 +1,7 @@
module: Main
function: main
parameter:
return_type: int
params:
returns: int
body:
declare n1: int =1
declare n2: int =2

View File

@ -1,7 +1,7 @@
module: Main
function: main
parameter:
return_type: boolean
params:
returns: boolean
body:
declare b1: boolean =true

View File

@ -1,7 +1,7 @@
module: Main
function: main
parameter:
return_type: int
params:
returns: int
body:
declare b1: boolean = true
loop:

View File

@ -1,7 +1,7 @@
module: Main
function: main
parameter:
return_type: int
params:
returns: int
body:
declare b1 :int = -1
return b1

View File

@ -1,7 +1,7 @@
module: Main
function: main
parameter:
return_type: boolean
params:
returns: boolean
body:
declare b1 :boolean = true
return !b1

View File

@ -1,7 +1,7 @@
module: Main
function: main
parameter:
return_type: long
params:
returns: long
body:
declare n: long
n = 2147483647

View File

@ -1,7 +1,7 @@
module: Math
function: main
parameter:
return_type: int
params:
returns: int
body:
Math.factorial(6)
return 0
@ -9,9 +9,9 @@ module: Math
end function
function: factorial
parameter:
params:
declare n:int
return_type: int
returns: int
body:
declare num1:int = 1
loop:

View File

@ -26,7 +26,7 @@ public class TokenFactory {
* 语言的保留关键字集合
*/
private static final Set<String> KEYWORDS = Set.of
("module", "function", "parameter", "return_type", "body", "end",
("module", "function", "params", "returns", "body", "end",
"if", "then", "else", "loop", "declare", "return", "import", "init",
"cond", "step", "globals", "break", "continue");

View File

@ -71,7 +71,7 @@ public class ASTPrinter {
NodeContext _
) -> {
System.out.println(pad + "function " + name
+ "(params=" + parameters + ", return=" + returnType + ")");
+ "(params=" + parameters + ", returns=" + returnType + ")");
for (StatementNode stmt : body) {
print(stmt, indent + 1);
}

View File

@ -27,8 +27,8 @@ import java.util.*;
*
* <ul>
* <li>函数头关键字 {@code function:} 与函数名</li>
* <li>参数列表parameter 区块</li>
* <li>返回类型return_type 区块</li>
* <li>参数列表params 区块</li>
* <li>返回类型returns 区块</li>
* <li>函数体body 区块</li>
* <li>函数结束关键字 {@code end function}</li>
* </ul>
@ -107,7 +107,7 @@ public class FunctionParser implements TopLevelParser {
}
/**
* 构造函数定义中各区块的解析规则parameterreturn_typebody
* 构造函数定义中各区块的解析规则paramsreturnsbody
*
* <p>
* 每个 {@link SectionDefinition} 包含两个部分: 区块起始判断器基于关键字与具体的解析逻辑
@ -124,13 +124,13 @@ public class FunctionParser implements TopLevelParser {
List<StatementNode> body) {
Map<String, SectionDefinition> map = new HashMap<>();
map.put("parameter", new SectionDefinition(
(TokenStream stream) -> stream.peek().getLexeme().equals("parameter"),
map.put("params", new SectionDefinition(
(TokenStream stream) -> stream.peek().getLexeme().equals("params"),
(ParserContext context, TokenStream stream) -> params.addAll(parseParameters(context))
));
map.put("return_type", new SectionDefinition(
(TokenStream stream) -> stream.peek().getLexeme().equals("return_type"),
map.put("returns", new SectionDefinition(
(TokenStream stream) -> stream.peek().getLexeme().equals("returns"),
(ParserContext context, TokenStream stream) -> returnType[0] = parseReturnType(stream)
));
@ -182,7 +182,7 @@ public class FunctionParser implements TopLevelParser {
* <p>
* 支持声明后附加注释格式示例:
* <pre>
* parameter:
* params:
* declare x: int // 说明文字
* declare y: float
* </pre>
@ -194,7 +194,7 @@ public class FunctionParser implements TopLevelParser {
private List<ParameterNode> parseParameters(ParserContext ctx) {
TokenStream ts = ctx.getTokens();
ts.expect("parameter");
ts.expect("params");
ts.expect(":");
skipComments(ts);
ts.expectType(TokenType.NEWLINE);
@ -208,7 +208,7 @@ public class FunctionParser implements TopLevelParser {
continue;
}
String lex = ts.peek().getLexeme();
if (lex.equals("return_type") || lex.equals("body") || lex.equals("end")) {
if (lex.equals("returns") || lex.equals("body") || lex.equals("end")) {
break;
}
@ -232,14 +232,14 @@ public class FunctionParser implements TopLevelParser {
* 解析返回类型声明
*
* <p>
* 格式为 {@code return_type: TYPE}支持前置或行尾注释
* 格式为 {@code returns: TYPE}支持前置或行尾注释
* </p>
*
* @param ts 当前使用的 {@link TokenStream}
* @return 返回类型名称字符串
*/
private String parseReturnType(TokenStream ts) {
ts.expect("return_type");
ts.expect("returns");
ts.expect(":");
skipComments(ts);
Token typeToken = ts.expectType(TokenType.TYPE);

View File

@ -24,8 +24,8 @@ public final class SnowExample {
return """
module: Math
function: main
parameter:
return_type: int
params:
returns: int
body:
Math.factorial(6)
return 0
@ -33,9 +33,9 @@ public final class SnowExample {
end function
function: factorial
parameter:
params:
declare n: int
return_type: int
returns: int
body:
declare num1: int = 1
loop: