!56 refactor: 重构关键字
Merge pull request !56 from zhangxun/feature/refactor-keyword
This commit is contained in:
commit
00f73ecb47
20
README.md
20
README.md
@ -44,7 +44,7 @@ SnowVM) 的完整编译-执行链路。
|
|||||||
|
|
||||||
Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的语法和严格的类型系统,以帮助 LLM 更好地理解程序。
|
Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的语法和严格的类型系统,以帮助 LLM 更好地理解程序。
|
||||||
|
|
||||||
语言使用显式的 `module` 声明来组织代码,用 `function`,`parameter`,`return_type`,`body` 等关键字分隔不同代码块,语法结构固定且易读。此外,Snow
|
语言使用显式的 `module` 声明来组织代码,用 `function`,`params`,`returns`,`body` 等关键字分隔不同代码块,语法结构固定且易读。此外,Snow
|
||||||
实现了语义分析来检查变量作用域和类型一致性,在编译阶段捕获错误并确保生成的中间代码正确无误。这种自上而下的编译流程,使得代码设计和生成更加模块化,可解释,也有利于调试和优化。
|
实现了语义分析来检查变量作用域和类型一致性,在编译阶段捕获错误并确保生成的中间代码正确无误。这种自上而下的编译流程,使得代码设计和生成更加模块化,可解释,也有利于调试和优化。
|
||||||
|
|
||||||
相关背景: [心路历程](docs/Snow-Lang-Journey/Snow-Lang-Journey.md)
|
相关背景: [心路历程](docs/Snow-Lang-Journey/Snow-Lang-Journey.md)
|
||||||
@ -111,7 +111,7 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
|
|||||||
module: Main
|
module: Main
|
||||||
import:Math
|
import:Math
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
Math.add(6,1)
|
Math.add(6,1)
|
||||||
return 0
|
return 0
|
||||||
@ -135,7 +135,7 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
|
|||||||
3 15 IDENTIFIER main
|
3 15 IDENTIFIER main
|
||||||
3 19 NEWLINE \n
|
3 19 NEWLINE \n
|
||||||
|
|
||||||
4 9 KEYWORD return_type
|
4 9 KEYWORD returns
|
||||||
4 20 COLON :
|
4 20 COLON :
|
||||||
4 22 TYPE int
|
4 22 TYPE int
|
||||||
4 25 NEWLINE \n
|
4 25 NEWLINE \n
|
||||||
@ -174,10 +174,10 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
|
|||||||
#### Math.snow
|
#### Math.snow
|
||||||
module: Math
|
module: Math
|
||||||
function: add
|
function: add
|
||||||
parameter:
|
params:
|
||||||
declare n1: int
|
declare n1: int
|
||||||
declare n2: int
|
declare n2: int
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
return n1 + n2
|
return n1 + n2
|
||||||
end body
|
end body
|
||||||
@ -195,7 +195,7 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
|
|||||||
2 15 IDENTIFIER add
|
2 15 IDENTIFIER add
|
||||||
2 18 NEWLINE \n
|
2 18 NEWLINE \n
|
||||||
|
|
||||||
3 9 KEYWORD parameter
|
3 9 KEYWORD params
|
||||||
3 18 COLON :
|
3 18 COLON :
|
||||||
3 19 NEWLINE \n
|
3 19 NEWLINE \n
|
||||||
|
|
||||||
@ -211,7 +211,7 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
|
|||||||
5 25 TYPE int
|
5 25 TYPE int
|
||||||
5 28 NEWLINE \n
|
5 28 NEWLINE \n
|
||||||
|
|
||||||
6 9 KEYWORD return_type
|
6 9 KEYWORD returns
|
||||||
6 20 COLON :
|
6 20 COLON :
|
||||||
6 22 TYPE int
|
6 22 TYPE int
|
||||||
6 25 NEWLINE \n
|
6 25 NEWLINE \n
|
||||||
@ -494,7 +494,7 @@ Snow 语言受到 LLM 驱动代码生成趋势的启发,强调简单而清晰的
|
|||||||
```snow
|
```snow
|
||||||
module: Math
|
module: Math
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
Math.factorial(6)
|
Math.factorial(6)
|
||||||
return 0
|
return 0
|
||||||
@ -502,9 +502,9 @@ module: Math
|
|||||||
end function
|
end function
|
||||||
|
|
||||||
function: factorial
|
function: factorial
|
||||||
parameter:
|
params:
|
||||||
declare n:int
|
declare n:int
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
declare num1:int = 1
|
declare num1:int = 1
|
||||||
loop:
|
loop:
|
||||||
|
|||||||
@ -76,8 +76,8 @@
|
|||||||
module: Main
|
module: Main
|
||||||
import:Math
|
import:Math
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
Math.factorial(6L,1L)
|
Math.factorial(6L,1L)
|
||||||
|
|
||||||
@ -90,10 +90,10 @@ end module
|
|||||||
## 源代码 (test.snow)
|
## 源代码 (test.snow)
|
||||||
module: Math
|
module: Math
|
||||||
function: factorial
|
function: factorial
|
||||||
parameter:
|
params:
|
||||||
declare n1: long
|
declare n1: long
|
||||||
declare n2: long
|
declare n2: long
|
||||||
return_type: long
|
returns: long
|
||||||
body:
|
body:
|
||||||
return n1+n2
|
return n1+n2
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
```snow
|
```snow
|
||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
|
|
||||||
return 1 + 1
|
return 1 + 1
|
||||||
@ -114,7 +114,7 @@ cond 可以是表达式(结果为 bool 类型)或者 bool 字面量
|
|||||||
```snow
|
```snow
|
||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
if 5 > 7 then
|
if 5 > 7 then
|
||||||
return 5
|
return 5
|
||||||
@ -152,7 +152,7 @@ end loop
|
|||||||
```snow
|
```snow
|
||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
declare sum: int = 0
|
declare sum: int = 0
|
||||||
loop:
|
loop:
|
||||||
@ -177,17 +177,17 @@ end module
|
|||||||
函数的形式如下:
|
函数的形式如下:
|
||||||
```snow
|
```snow
|
||||||
function: add
|
function: add
|
||||||
parameter:
|
params:
|
||||||
declare a: int
|
declare a: int
|
||||||
declare b: int
|
declare b: int
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
return a + b
|
return a + b
|
||||||
end body
|
end body
|
||||||
end function
|
end function
|
||||||
```
|
```
|
||||||
其中 add 是函数名,parameter 下面是参数列表(可省略),与变量的定义类似,但是不允许赋初值,
|
其中 add 是函数名,params 下面是参数列表(可省略),与变量的定义类似,但是不允许赋初值,
|
||||||
接着 return_type 设置返回值类型,最后的 body 为函数体。
|
接着 returns 设置返回值类型,最后的 body 为函数体。
|
||||||
|
|
||||||
## 模块
|
## 模块
|
||||||
|
|
||||||
@ -200,7 +200,7 @@ snow 会自动将同名模块的函数合并。
|
|||||||
```snow
|
```snow
|
||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
|
|
||||||
return 1 + 1
|
return 1 + 1
|
||||||
@ -216,10 +216,10 @@ end module
|
|||||||
// Math.snow
|
// Math.snow
|
||||||
module: Math
|
module: Math
|
||||||
function: add
|
function: add
|
||||||
parameter:
|
params:
|
||||||
declare a: int
|
declare a: int
|
||||||
declare b: int
|
declare b: int
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
return a + b
|
return a + b
|
||||||
end body
|
end body
|
||||||
@ -233,7 +233,7 @@ end module
|
|||||||
module: Main
|
module: Main
|
||||||
import: Math
|
import: Math
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
|
|
||||||
return Math.add(5, 7)
|
return Math.add(5, 7)
|
||||||
@ -248,7 +248,7 @@ end module
|
|||||||
module: Main
|
module: Main
|
||||||
import: Math, Time
|
import: Math, Time
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
|
|
||||||
return Math.add(5, 7)
|
return Math.add(5, 7)
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Math
|
module: Math
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
Math.factorial(6)
|
Math.factorial(6)
|
||||||
return 0
|
return 0
|
||||||
@ -9,9 +9,9 @@ module: Math
|
|||||||
end function
|
end function
|
||||||
|
|
||||||
function: factorial
|
function: factorial
|
||||||
parameter:
|
params:
|
||||||
declare n:int
|
declare n:int
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
declare num1:int = 1
|
declare num1:int = 1
|
||||||
loop:
|
loop:
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
loop:
|
loop:
|
||||||
init:
|
init:
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
declare abc:int =1
|
declare abc:int =1
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
import: os
|
import: os
|
||||||
function: main
|
function: main
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
loop:
|
loop:
|
||||||
init:
|
init:
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
import: os
|
import: os
|
||||||
function: main
|
function: main
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
// 合法
|
// 合法
|
||||||
declare b1: byte = 127b
|
declare b1: byte = 127b
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
import: ModuleB
|
import: ModuleB
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
return ModuleB.fun()
|
return ModuleB.fun()
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
module: ModuleA
|
module: ModuleA
|
||||||
function: fun
|
function: fun
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
return 123
|
return 123
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: ModuleB
|
module: ModuleB
|
||||||
import: ModuleA
|
import: ModuleA
|
||||||
function: fun
|
function: fun
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
return ModuleA.fun() + ModuleA.fun()
|
return ModuleA.fun() + ModuleA.fun()
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -3,8 +3,8 @@ module: Main
|
|||||||
globals:
|
globals:
|
||||||
declare sum: int = 0
|
declare sum: int = 0
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
sum = 20
|
sum = 20
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -3,8 +3,8 @@ module: Main
|
|||||||
globals:
|
globals:
|
||||||
declare sum: int = 123
|
declare sum: int = 123
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
os.print(sum)
|
os.print(sum)
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
import:Math
|
import:Math
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
Math.add(6,1)
|
Math.add(6,1)
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: Math
|
module: Math
|
||||||
function: add
|
function: add
|
||||||
parameter:
|
params:
|
||||||
declare n1: int
|
declare n1: int
|
||||||
declare n2: int
|
declare n2: int
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
return n1 + n2
|
return n1 + n2
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
declare res: boolean = 8L > 7L
|
declare res: boolean = 8L > 7L
|
||||||
if res then
|
if res then
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
return 65537
|
return 65537
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
foo()
|
foo()
|
||||||
|
|
||||||
@ -9,7 +9,7 @@ module: Main
|
|||||||
end function
|
end function
|
||||||
|
|
||||||
function: foo
|
function: foo
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
if false then
|
if false then
|
||||||
return 1
|
return 1
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
5 == 7
|
5 == 7
|
||||||
5 == 7s
|
5 == 7s
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
import: os
|
import: os
|
||||||
function: main
|
function: main
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
os.print(222)
|
os.print(222)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module: Main
|
|||||||
globals:
|
globals:
|
||||||
declare num2:int=10
|
declare num2:int=10
|
||||||
function: main
|
function: main
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
declare num1:int=11
|
declare num1:int=11
|
||||||
os.print(num1+num2+abc())
|
os.print(num1+num2+abc())
|
||||||
@ -11,7 +11,7 @@ module: Main
|
|||||||
end function
|
end function
|
||||||
|
|
||||||
function: abc
|
function: abc
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
return 1
|
return 1
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
import: os
|
import: os
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
loop:
|
loop:
|
||||||
init:
|
init:
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
import: os
|
import: os
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
loop:
|
loop:
|
||||||
init:
|
init:
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
import: os
|
import: os
|
||||||
function: main
|
function: main
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
loop:
|
loop:
|
||||||
init:
|
init:
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
import: os
|
import: os
|
||||||
function: main
|
function: main
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
declare n: int[][][][] = [
|
declare n: int[][][][] = [
|
||||||
[
|
[
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
return (1+2) / 3 * 4 + 2 *2
|
return (1+2) / 3 * 4 + 2 *2
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
import:os
|
import:os
|
||||||
function: main
|
function: main
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
declare arr: int[] = [1, 2, 3]
|
declare arr: int[] = [1, 2, 3]
|
||||||
arr[0] = 5
|
arr[0] = 5
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -3,8 +3,8 @@ module: Main
|
|||||||
globals:
|
globals:
|
||||||
declare sum: int = 123
|
declare sum: int = 123
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
declare arr: int[][][][][][][][][][][][][][][][][][][][][][] = [[[[[[[[[[[[[[[[[[[[[[1], [2], [3]]]]]]]]]]]]]]]]]]]]]]
|
declare arr: int[][][][][][][][][][][][][][][][][][][][][][] = [[[[[[[[[[[[[[[[[[[[[[1], [2], [3]]]]]]]]]]]]]]]]]]]]]]
|
||||||
loop:
|
loop:
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
module: os
|
module: os
|
||||||
import: os
|
import: os
|
||||||
function: print
|
function: print
|
||||||
parameter:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
return_type: void
|
returns: void
|
||||||
body:
|
body:
|
||||||
syscall("PRINT",i1)
|
syscall("PRINT",i1)
|
||||||
end body
|
end body
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
declare n1: int =1
|
declare n1: int =1
|
||||||
declare n2: int =2
|
declare n2: int =2
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: boolean
|
returns: boolean
|
||||||
body:
|
body:
|
||||||
declare b1: boolean =true
|
declare b1: boolean =true
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
declare b1: boolean = true
|
declare b1: boolean = true
|
||||||
loop:
|
loop:
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
declare b1 :int = -1
|
declare b1 :int = -1
|
||||||
return b1
|
return b1
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: boolean
|
returns: boolean
|
||||||
body:
|
body:
|
||||||
declare b1 :boolean = true
|
declare b1 :boolean = true
|
||||||
return !b1
|
return !b1
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Main
|
module: Main
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: long
|
returns: long
|
||||||
body:
|
body:
|
||||||
declare n: long
|
declare n: long
|
||||||
n = 2147483647
|
n = 2147483647
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
module: Math
|
module: Math
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
Math.factorial(6)
|
Math.factorial(6)
|
||||||
return 0
|
return 0
|
||||||
@ -9,9 +9,9 @@ module: Math
|
|||||||
end function
|
end function
|
||||||
|
|
||||||
function: factorial
|
function: factorial
|
||||||
parameter:
|
params:
|
||||||
declare n:int
|
declare n:int
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
declare num1:int = 1
|
declare num1:int = 1
|
||||||
loop:
|
loop:
|
||||||
|
|||||||
@ -26,7 +26,7 @@ public class TokenFactory {
|
|||||||
* 语言的保留关键字集合。
|
* 语言的保留关键字集合。
|
||||||
*/
|
*/
|
||||||
private static final Set<String> KEYWORDS = Set.of
|
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",
|
"if", "then", "else", "loop", "declare", "return", "import", "init",
|
||||||
"cond", "step", "globals", "break", "continue");
|
"cond", "step", "globals", "break", "continue");
|
||||||
|
|
||||||
|
|||||||
@ -71,7 +71,7 @@ public class ASTPrinter {
|
|||||||
NodeContext _
|
NodeContext _
|
||||||
) -> {
|
) -> {
|
||||||
System.out.println(pad + "function " + name
|
System.out.println(pad + "function " + name
|
||||||
+ "(params=" + parameters + ", return=" + returnType + ")");
|
+ "(params=" + parameters + ", returns=" + returnType + ")");
|
||||||
for (StatementNode stmt : body) {
|
for (StatementNode stmt : body) {
|
||||||
print(stmt, indent + 1);
|
print(stmt, indent + 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,8 +27,8 @@ import java.util.*;
|
|||||||
*
|
*
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>函数头(关键字 {@code function:} 与函数名)</li>
|
* <li>函数头(关键字 {@code function:} 与函数名)</li>
|
||||||
* <li>参数列表(parameter 区块)</li>
|
* <li>参数列表(params 区块)</li>
|
||||||
* <li>返回类型(return_type 区块)</li>
|
* <li>返回类型(returns 区块)</li>
|
||||||
* <li>函数体(body 区块)</li>
|
* <li>函数体(body 区块)</li>
|
||||||
* <li>函数结束(关键字 {@code end function})</li>
|
* <li>函数结束(关键字 {@code end function})</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
@ -107,7 +107,7 @@ public class FunctionParser implements TopLevelParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数定义中各区块的解析规则(parameter、return_type、body)。
|
* 构造函数定义中各区块的解析规则(params、returns、body)。
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* 每个 {@link SectionDefinition} 包含两个部分: 区块起始判断器(基于关键字)与具体的解析逻辑。
|
* 每个 {@link SectionDefinition} 包含两个部分: 区块起始判断器(基于关键字)与具体的解析逻辑。
|
||||||
@ -124,13 +124,13 @@ public class FunctionParser implements TopLevelParser {
|
|||||||
List<StatementNode> body) {
|
List<StatementNode> body) {
|
||||||
Map<String, SectionDefinition> map = new HashMap<>();
|
Map<String, SectionDefinition> map = new HashMap<>();
|
||||||
|
|
||||||
map.put("parameter", new SectionDefinition(
|
map.put("params", new SectionDefinition(
|
||||||
(TokenStream stream) -> stream.peek().getLexeme().equals("parameter"),
|
(TokenStream stream) -> stream.peek().getLexeme().equals("params"),
|
||||||
(ParserContext context, TokenStream stream) -> params.addAll(parseParameters(context))
|
(ParserContext context, TokenStream stream) -> params.addAll(parseParameters(context))
|
||||||
));
|
));
|
||||||
|
|
||||||
map.put("return_type", new SectionDefinition(
|
map.put("returns", new SectionDefinition(
|
||||||
(TokenStream stream) -> stream.peek().getLexeme().equals("return_type"),
|
(TokenStream stream) -> stream.peek().getLexeme().equals("returns"),
|
||||||
(ParserContext context, TokenStream stream) -> returnType[0] = parseReturnType(stream)
|
(ParserContext context, TokenStream stream) -> returnType[0] = parseReturnType(stream)
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -182,7 +182,7 @@ public class FunctionParser implements TopLevelParser {
|
|||||||
* <p>
|
* <p>
|
||||||
* 支持声明后附加注释,格式示例:
|
* 支持声明后附加注释,格式示例:
|
||||||
* <pre>
|
* <pre>
|
||||||
* parameter:
|
* params:
|
||||||
* declare x: int // 说明文字
|
* declare x: int // 说明文字
|
||||||
* declare y: float
|
* declare y: float
|
||||||
* </pre>
|
* </pre>
|
||||||
@ -194,7 +194,7 @@ public class FunctionParser implements TopLevelParser {
|
|||||||
private List<ParameterNode> parseParameters(ParserContext ctx) {
|
private List<ParameterNode> parseParameters(ParserContext ctx) {
|
||||||
TokenStream ts = ctx.getTokens();
|
TokenStream ts = ctx.getTokens();
|
||||||
|
|
||||||
ts.expect("parameter");
|
ts.expect("params");
|
||||||
ts.expect(":");
|
ts.expect(":");
|
||||||
skipComments(ts);
|
skipComments(ts);
|
||||||
ts.expectType(TokenType.NEWLINE);
|
ts.expectType(TokenType.NEWLINE);
|
||||||
@ -208,7 +208,7 @@ public class FunctionParser implements TopLevelParser {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
String lex = ts.peek().getLexeme();
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,14 +232,14 @@ public class FunctionParser implements TopLevelParser {
|
|||||||
* 解析返回类型声明。
|
* 解析返回类型声明。
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* 格式为 {@code return_type: TYPE},支持前置或行尾注释。
|
* 格式为 {@code returns: TYPE},支持前置或行尾注释。
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param ts 当前使用的 {@link TokenStream}。
|
* @param ts 当前使用的 {@link TokenStream}。
|
||||||
* @return 返回类型名称字符串。
|
* @return 返回类型名称字符串。
|
||||||
*/
|
*/
|
||||||
private String parseReturnType(TokenStream ts) {
|
private String parseReturnType(TokenStream ts) {
|
||||||
ts.expect("return_type");
|
ts.expect("returns");
|
||||||
ts.expect(":");
|
ts.expect(":");
|
||||||
skipComments(ts);
|
skipComments(ts);
|
||||||
Token typeToken = ts.expectType(TokenType.TYPE);
|
Token typeToken = ts.expectType(TokenType.TYPE);
|
||||||
|
|||||||
@ -24,8 +24,8 @@ public final class SnowExample {
|
|||||||
return """
|
return """
|
||||||
module: Math
|
module: Math
|
||||||
function: main
|
function: main
|
||||||
parameter:
|
params:
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
Math.factorial(6)
|
Math.factorial(6)
|
||||||
return 0
|
return 0
|
||||||
@ -33,9 +33,9 @@ public final class SnowExample {
|
|||||||
end function
|
end function
|
||||||
|
|
||||||
function: factorial
|
function: factorial
|
||||||
parameter:
|
params:
|
||||||
declare n: int
|
declare n: int
|
||||||
return_type: int
|
returns: int
|
||||||
body:
|
body:
|
||||||
declare num1: int = 1
|
declare num1: int = 1
|
||||||
loop:
|
loop:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user