feat: 实现 BuiltinUtils 标准库并更新相关机制

- 重构 BuiltinTypeRegistry 类,扩展内置类型和模块的注册功能
- 新增 BuiltinUtils 标准库模块,提供 print 和 println 函数
- 实现 syscall 内核函数,供 BuiltinUtils 内部使用
- 更新测试用例,添加 Demo14 项目以验证新功能
This commit is contained in:
Luke 2025-07-19 17:04:49 +08:00
parent 4e3de185b8
commit 64cefebee5
4 changed files with 85 additions and 36 deletions

View File

@ -3,7 +3,6 @@
<toRun name="Demo1" type="Application" />
<toRun name="Demo10" type="Application" />
<toRun name="Demo11" type="Application" />
<toRun name="Demo11" type="Application" />
<toRun name="Demo12" type="Application" />
<toRun name="Demo13" type="Application" />
<toRun name="Demo2" type="Application" />

View File

@ -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

View File

@ -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

View File

@ -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} 是内置类型和内置模块的集中注册中心
* <p>
* 本类主要负责:
* 语言全部内置类型 / 模块 / 函数的注册中心
*
* <p>目前同时注册</p>
* <ul>
* <li>定义语言中所有可识别的基础类型 intfloatstring </li>
* <li>在语义分析初始化时将内置模块 {@code BuiltinUtils}注册到上下文中</li>
* <li>提供对内置类型的快速查找支持</li>
* <li>所有基础类型byteshortint </li>
* <li>标准库模块 <b>BuiltinUtils</b> 仅声明函数签名真正实现写在 Snow 源码中</li>
* <li>运行时内核函数 <b>syscall</b> 供标准库内部实现调用</li>
* </ul>
* 该类为纯工具类所有成员均为静态不可实例化
*/
public final class BuiltinTypeRegistry {
/**
* 内置类型映射表: 将类型名称字符串映射到对应的 {@link Type} 实例
* <p>
* 用于类型解析过程如解析变量声明或函数返回类型
* 将用户源码中的类型字符串转换为语义类型对象
*/
public static final Map<String, Type> 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<String, Type> BUILTIN_TYPES;
static {
Map<String, Type> 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() { }
/**
* 初始化语义上下文中与内置模块相关的内容
* <p>
* 当前实现将内置模块 {@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);
}
}