diff --git a/src/main/java/org/jcnc/snow/pkg/tasks/GenerateTask.java b/src/main/java/org/jcnc/snow/pkg/tasks/GenerateTask.java index 4f43f51..589dc43 100644 --- a/src/main/java/org/jcnc/snow/pkg/tasks/GenerateTask.java +++ b/src/main/java/org/jcnc/snow/pkg/tasks/GenerateTask.java @@ -1,6 +1,7 @@ package org.jcnc.snow.pkg.tasks; import org.jcnc.snow.pkg.model.Project; +import org.jcnc.snow.pkg.utils.SnowExampleTemplate; import java.io.IOException; import java.nio.file.Files; @@ -50,39 +51,7 @@ public final class GenerateTask implements Task { /* -------- 创建示例入口文件 -------- */ Path mainSnow = root.resolve("src").resolve("main.snow"); if (Files.notExists(mainSnow)) { - Files.writeString(mainSnow, """ - 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 - """); + Files.writeString(mainSnow, SnowExampleTemplate.getMainModule()); System.out.println("[generate] created src/main.snow"); } diff --git a/src/main/java/org/jcnc/snow/pkg/utils/SnowExampleTemplate.java b/src/main/java/org/jcnc/snow/pkg/utils/SnowExampleTemplate.java new file mode 100644 index 0000000..e55fd4d --- /dev/null +++ b/src/main/java/org/jcnc/snow/pkg/utils/SnowExampleTemplate.java @@ -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 + """; + } +}