Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit 855fd0a

Browse files
committed
更新build_io_lib.ps1脚本以支持release模式构建,添加目标目录创建逻辑;重构library_test.cn以简化测试用例,增加对时间和操作系统信息的测试;在namespace.rs中添加直接调用函数的支持,并实现库函数注册器;更新lib.rs以使用库函数注册器;扩展AST和解释器以支持命名空间和库函数调用;修复值类型以支持空值表示。
1 parent 90b68ae commit 855fd0a

File tree

25 files changed

+1766
-109
lines changed

25 files changed

+1766
-109
lines changed

build.ps1

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# build the main project
2+
Write-Host "building the main project"
3+
cargo build
4+
5+
# build all the library projects
6+
$libraries = @(
7+
"library_io",
8+
"library_common",
9+
"library_example",
10+
"library_os",
11+
"library_time"
12+
)
13+
14+
# create the target directory for release
15+
$targetDir = ".\target\release\library"
16+
if (-not (Test-Path $targetDir)) {
17+
Write-Host "create: $targetDir"
18+
New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
19+
}
20+
21+
# create the target directory for debug
22+
$debugTargetDir = ".\target\debug\library"
23+
if (-not (Test-Path $debugTargetDir)) {
24+
Write-Host "create: $debugTargetDir"
25+
New-Item -ItemType Directory -Path $debugTargetDir -Force | Out-Null
26+
}
27+
28+
foreach ($lib in $libraries) {
29+
Write-Host "building: $lib"
30+
cd .\$lib
31+
cargo build
32+
33+
# get the output file name
34+
$libName = $lib -replace "library_", ""
35+
36+
# 处理release版本
37+
$sourceFile = ".\target\release\$libName.dll"
38+
$targetFile = "..\target\release\library\$libName.dll"
39+
40+
# if the file exists, copy it
41+
if (Test-Path $sourceFile) {
42+
Write-Host "copy: $sourceFile -> $targetFile"
43+
Copy-Item -Path $sourceFile -Destination $targetFile -Force
44+
} else {
45+
Write-Host "warning: $sourceFile not found, skip copy"
46+
}
47+
48+
# 处理debug版本
49+
$sourceFile = ".\target\debug\$libName.dll"
50+
$targetFile = "..\target\debug\library\$libName.dll"
51+
52+
# if the file exists, copy it
53+
if (Test-Path $sourceFile) {
54+
Write-Host "copy: $sourceFile -> $targetFile"
55+
Copy-Item -Path $sourceFile -Destination $targetFile -Force
56+
} else {
57+
Write-Host "warning: $sourceFile not found, skip copy"
58+
}
59+
60+
# return to the root directory
61+
cd ..
62+
}
63+
64+
Write-Host "all libraries done"

build_io_lib.ps1

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,37 @@
44
cd .\library_io
55

66
# 编译库(release模式)
7-
Write-Host "正在编译library_io..."
7+
Write-Host "building the library_io..."
88
cargo build --release
99

1010
# 创建目标目录(如果不存在)
11-
$targetDir = "..\target\debug\library"
11+
$targetDir = "..\target\release\library"
1212
if (-not (Test-Path $targetDir)) {
13-
Write-Host "创建目录: $targetDir"
13+
Write-Host "create: $targetDir"
1414
New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
1515
}
1616

1717
# 复制DLL文件
1818
$sourceFile = ".\target\release\io.dll"
1919
$targetFile = "$targetDir\io.dll"
2020

21-
Write-Host "复制文件: $sourceFile -> $targetFile"
21+
22+
23+
# 创建目标目录(如果不存在)
24+
$targetDir = "..\target\debug\library"
25+
if (-not (Test-Path $targetDir)) {
26+
Write-Host "create: $targetDir"
27+
New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
28+
}
29+
30+
# 复制DLL文件
31+
$sourceFile = ".\target\debug\io.dll"
32+
$targetFile = "$targetDir\io.dll"
33+
34+
Write-Host "copy: $sourceFile -> $targetFile"
2235
Copy-Item -Path $sourceFile -Destination $targetFile -Force
2336

2437
# 返回原目录
2538
cd ..
2639

27-
Write-Host "完成!io.dll已更新"
40+
Write-Host "done: io.dll"

build_libraries.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Host "please use build.ps1 !"

kaizaoji.cn

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using lib <os>;
2+
using lib <io>;
3+
using lib <time>;
4+
using ns std;
5+
6+
fn main() : int {
7+
while (true) {
8+
println(" 🎃");
9+
println("██░░██");
10+
println(" ░░");
11+
sleep_seconds(0.2);
12+
exec("powershell", "-Command", "Clear-Host");
13+
println(" 🎃");
14+
println("░░██░░");
15+
println(" ██");
16+
sleep_seconds(0.2);
17+
exec("powershell", "-Command", "Clear-Host");
18+
};
19+
return 0;
20+
};

library_common/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

library_common/src/namespace.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ impl NamespaceBuilder {
6868
};
6969
self.functions.contains_key(&full_name)
7070
}
71+
72+
/// 向命名空间中添加直接调用函数(不带命名空间前缀)
73+
/// 这允许函数被直接调用,而不需要命名空间前缀
74+
///
75+
/// # 参数
76+
/// * `name` - 函数名称
77+
/// * `func` - 函数指针
78+
///
79+
/// # 返回
80+
/// 返回自身引用,支持链式调用
81+
pub fn add_direct_function(&mut self, name: &str, func: LibraryFunction) -> &mut Self {
82+
self.functions.insert(name.to_string(), func);
83+
self
84+
}
85+
86+
/// 获取函数映射的克隆
87+
pub fn get_functions(&self) -> HashMap<String, LibraryFunction> {
88+
self.functions.clone()
89+
}
7190
}
7291

7392
/// 创建并初始化多个命名空间
@@ -100,4 +119,75 @@ pub fn register_namespaces(namespaces: Vec<(&str, Vec<(&str, LibraryFunction)>)>
100119
/// 返回函数映射的原始指针
101120
pub fn create_library_pointer(functions: HashMap<String, LibraryFunction>) -> *mut HashMap<String, LibraryFunction> {
102121
Box::into_raw(Box::new(functions))
122+
}
123+
124+
/// 库函数注册器,用于统一管理库函数的注册
125+
pub struct LibraryRegistry {
126+
namespaces: HashMap<String, NamespaceBuilder>,
127+
direct_functions: HashMap<String, LibraryFunction>,
128+
}
129+
130+
impl LibraryRegistry {
131+
/// 创建一个新的库函数注册器
132+
pub fn new() -> Self {
133+
LibraryRegistry {
134+
namespaces: HashMap::new(),
135+
direct_functions: HashMap::new(),
136+
}
137+
}
138+
139+
/// 获取或创建命名空间构建器
140+
///
141+
/// # 参数
142+
/// * `namespace` - 命名空间名称
143+
///
144+
/// # 返回
145+
/// 返回命名空间构建器的可变引用
146+
pub fn namespace(&mut self, namespace: &str) -> &mut NamespaceBuilder {
147+
if !self.namespaces.contains_key(namespace) {
148+
self.namespaces.insert(namespace.to_string(), NamespaceBuilder::new(namespace));
149+
}
150+
self.namespaces.get_mut(namespace).unwrap()
151+
}
152+
153+
/// 添加直接调用函数(不带命名空间前缀)
154+
///
155+
/// # 参数
156+
/// * `name` - 函数名称
157+
/// * `func` - 函数指针
158+
///
159+
/// # 返回
160+
/// 返回自身引用,支持链式调用
161+
pub fn add_direct_function(&mut self, name: &str, func: LibraryFunction) -> &mut Self {
162+
self.direct_functions.insert(name.to_string(), func);
163+
self
164+
}
165+
166+
/// 构建最终的函数映射
167+
///
168+
/// # 返回
169+
/// 返回合并所有命名空间和直接函数后的函数映射
170+
pub fn build(&self) -> HashMap<String, LibraryFunction> {
171+
let mut all_functions = HashMap::new();
172+
173+
// 添加所有命名空间函数
174+
for (_, ns_builder) in &self.namespaces {
175+
ns_builder.register_all(&mut all_functions);
176+
}
177+
178+
// 添加所有直接函数
179+
for (name, func) in &self.direct_functions {
180+
all_functions.insert(name.clone(), *func);
181+
}
182+
183+
all_functions
184+
}
185+
186+
/// 构建并创建库指针
187+
///
188+
/// # 返回
189+
/// 返回函数映射的原始指针,用于库初始化
190+
pub fn build_library_pointer(&self) -> *mut HashMap<String, LibraryFunction> {
191+
create_library_pointer(self.build())
192+
}
103193
}

library_example/Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

library_io/src/lib.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ::std::io::{self, Write as IoWrite};
33
use ::std::fmt::Write as FmtWrite;
44

55
// 导入通用库
6-
use cn_common::namespace::{LibraryFunction, NamespaceBuilder, create_library_pointer};
6+
use cn_common::namespace::{LibraryFunction, NamespaceBuilder, create_library_pointer, LibraryRegistry};
77
use cn_common::string::process_escape_chars;
88

99
// 命名空间函数
@@ -136,19 +136,22 @@ mod std {
136136
// 初始化函数,返回函数映射
137137
#[no_mangle]
138138
pub extern "C" fn cn_init() -> *mut HashMap<String, LibraryFunction> {
139-
// 使用命名空间构建器注册std命名空间下的函数
140-
let mut std_ns = NamespaceBuilder::new("std");
139+
// 创建库函数注册器
140+
let mut registry = LibraryRegistry::new();
141+
142+
// 注册std命名空间下的函数
143+
let std_ns = registry.namespace("std");
141144
std_ns.add_function("print", std::cn_print)
142145
.add_function("println", std::cn_println)
143146
.add_function("read_line", std::cn_read_line)
144147
.add_function("printf", std::cn_printf);
145148

146-
// 创建函数映射
147-
let mut functions = HashMap::new();
148-
149-
// 注册所有函数到主函数映射
150-
std_ns.register_all(&mut functions);
149+
// 同时注册为直接函数,不需要命名空间前缀
150+
registry.add_direct_function("print", std::cn_print)
151+
.add_direct_function("println", std::cn_println)
152+
.add_direct_function("read_line", std::cn_read_line)
153+
.add_direct_function("printf", std::cn_printf);
151154

152-
// 将HashMap装箱并转换为原始指针
153-
create_library_pointer(functions)
155+
// 构建并返回库指针
156+
registry.build_library_pointer()
154157
}

0 commit comments

Comments
 (0)