refactor: snow 模块代码模板提取到 SnowExampleTemplate.java

This commit is contained in:
Luke 2025-06-24 11:41:05 +08:00
parent e0b4e2432f
commit 493b970d9a
2 changed files with 54 additions and 33 deletions

View File

@ -1,6 +1,7 @@
package org.jcnc.snow.pkg.tasks; package org.jcnc.snow.pkg.tasks;
import org.jcnc.snow.pkg.model.Project; import org.jcnc.snow.pkg.model.Project;
import org.jcnc.snow.pkg.utils.SnowExampleTemplate;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
@ -50,39 +51,7 @@ public final class GenerateTask implements Task {
/* -------- 创建示例入口文件 -------- */ /* -------- 创建示例入口文件 -------- */
Path mainSnow = root.resolve("src").resolve("main.snow"); Path mainSnow = root.resolve("src").resolve("main.snow");
if (Files.notExists(mainSnow)) { if (Files.notExists(mainSnow)) {
Files.writeString(mainSnow, """ Files.writeString(mainSnow, SnowExampleTemplate.getMainModule());
module: Math
function: main
parameter:
return_type: int
body:
Math.factorial(6)
return 0
end body
end function
function: factorial
parameter:
declare n:int
return_type: int
body:
declare num1:int = 1
loop:
initializer:
declare counter:int = 1
condition:
counter <= n
update:
counter = counter + 1
body:
num1 = num1 * counter
end body
end loop
return num1
end body
end function
end module
""");
System.out.println("[generate] created src/main.snow"); System.out.println("[generate] created src/main.snow");
} }

View File

@ -0,0 +1,52 @@
package org.jcnc.snow.pkg.utils;
/**
* 提供示例 .snow 模块代码模板
*/
public final class SnowExampleTemplate {
private SnowExampleTemplate() {
// 工具类不允许实例化
}
/**
* 返回 main.snow 示例模块内容
*
* @return 示例模块代码字符串
*/
public static String getMainModule() {
return """
module: Math
function: main
parameter:
return_type: int
body:
Math.factorial(6)
return 0
end body
end function
function: factorial
parameter:
declare n:int
return_type: int
body:
declare num1:int = 1
loop:
initializer:
declare counter:int = 1
condition:
counter <= n
update:
counter = counter + 1
body:
num1 = num1 * counter
end body
end loop
return num1
end body
end function
end module
""";
}
}