diff --git a/.run/测试.run.xml b/.run/测试.run.xml index 5000363..c37e7f3 100644 --- a/.run/测试.run.xml +++ b/.run/测试.run.xml @@ -3,7 +3,6 @@ - diff --git a/playground/Demo/Demo14/BuiltinUtils.snow b/playground/Demo/Demo14/BuiltinUtils.snow new file mode 100644 index 0000000..d915ee8 --- /dev/null +++ b/playground/Demo/Demo14/BuiltinUtils.snow @@ -0,0 +1,19 @@ +module: BuiltinUtils + function: print + parameter: + declare msg: int + return_type: void + body: + syscall("PRINT",1) + end body + end function + + function: println + parameter: + declare msg: int + return_type: void + body: + syscall("PRINTLN",1) + end body + end function +end module diff --git a/playground/Demo/Demo14/Main.snow b/playground/Demo/Demo14/Main.snow new file mode 100644 index 0000000..baf23e4 --- /dev/null +++ b/playground/Demo/Demo14/Main.snow @@ -0,0 +1,11 @@ +module: Main + import: BuiltinUtils + + function: main + return_type: void + body: + BuiltinUtils.print(1) + BuiltinUtils.println(2) + end body + end function +end module \ No newline at end of file diff --git a/src/main/java/org/jcnc/snow/compiler/semantic/core/BuiltinTypeRegistry.java b/src/main/java/org/jcnc/snow/compiler/semantic/core/BuiltinTypeRegistry.java index d1566ab..3e69f38 100644 --- a/src/main/java/org/jcnc/snow/compiler/semantic/core/BuiltinTypeRegistry.java +++ b/src/main/java/org/jcnc/snow/compiler/semantic/core/BuiltinTypeRegistry.java @@ -1,54 +1,74 @@ package org.jcnc.snow.compiler.semantic.core; -import org.jcnc.snow.compiler.semantic.type.*; +import org.jcnc.snow.compiler.semantic.type.BuiltinType; +import org.jcnc.snow.compiler.semantic.type.FunctionType; +import org.jcnc.snow.compiler.semantic.type.Type; -import java.util.Map; +import java.util.*; /** - * {@code BuiltinTypeRegistry} 是内置类型和内置模块的集中注册中心。 - *

- * 本类主要负责: + * 语言全部内置类型 / 模块 / 函数的注册中心。 + * + *

目前同时注册:

*
    - *
  • 定义语言中所有可识别的基础类型(如 int、float、string 等);
  • - *
  • 在语义分析初始化时,将内置模块(如 {@code BuiltinUtils})注册到上下文中;
  • - *
  • 提供对内置类型的快速查找支持。
  • + *
  • 所有基础类型(byte、short、int …)
  • + *
  • 标准库模块 BuiltinUtils —— 仅声明函数签名,真正实现写在 Snow 源码中
  • + *
  • 运行时内核函数 syscall —— 供标准库内部实现调用
  • *
- * 该类为纯工具类,所有成员均为静态,不可实例化。 */ public final class BuiltinTypeRegistry { - /** - * 内置类型映射表: 将类型名称字符串映射到对应的 {@link Type} 实例。 - *

- * 用于类型解析过程(如解析变量声明或函数返回类型)中, - * 将用户源码中的类型字符串转换为语义类型对象。 - */ - public static final Map BUILTIN_TYPES = Map.of( - "int", BuiltinType.INT, - "long", BuiltinType.LONG, - "short", BuiltinType.SHORT, - "byte", BuiltinType.BYTE, - "float", BuiltinType.FLOAT, - "double", BuiltinType.DOUBLE, - "string", BuiltinType.STRING, - "boolean", BuiltinType.BOOLEAN, - "void", BuiltinType.VOID - ); + /** 基础类型表:名称 → Type */ + public static final Map BUILTIN_TYPES; + static { + Map t = new HashMap<>(); + t.put("byte", BuiltinType.BYTE); + t.put("short", BuiltinType.SHORT); + t.put("int", BuiltinType.INT); + t.put("long", BuiltinType.LONG); + t.put("float", BuiltinType.FLOAT); + t.put("double", BuiltinType.DOUBLE); + t.put("string", BuiltinType.STRING); + t.put("void", BuiltinType.VOID); + BUILTIN_TYPES = Collections.unmodifiableMap(t); + } - /** - * 私有构造函数,禁止实例化。 - */ private BuiltinTypeRegistry() { } /** - * 初始化语义上下文中与内置模块相关的内容。 - *

- * 当前实现将内置模块 {@code BuiltinUtils} 注册至上下文模块表中, - * 使其在用户代码中可被访问(如 {@code BuiltinUtils.to_string(...)})。 - * - * @param ctx 当前语义分析上下文 + * 供语义分析阶段调用:向上下文注入所有内置模块 / 函数声明。 */ public static void init(Context ctx) { + /* ---------- BuiltinUtils ---------- */ + ModuleInfo utils = new ModuleInfo("BuiltinUtils"); + // print(string): void + utils.getFunctions().put( + "print", + new FunctionType( + Collections.singletonList(BuiltinType.STRING), + BuiltinType.VOID + ) + ); + + // println(string): void + utils.getFunctions().put( + "println", + new FunctionType( + Collections.singletonList(BuiltinType.STRING), + BuiltinType.VOID + ) + ); + + // syscall(string, string): void —— 供 BuiltinUtils 内部使用 + utils.getFunctions().put( + "syscall", + new FunctionType( + Arrays.asList(BuiltinType.STRING, BuiltinType.STRING), + BuiltinType.VOID + ) + ); + + ctx.getModules().putIfAbsent("BuiltinUtils", utils); } }