rcompiler 0.0.0-stage → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/CONTRIBUTING.md +38 -0
- package/LICENSE +15 -0
- package/README.md +170 -2
- package/SECURITY.md +27 -0
- package/assets/placeholder.png +0 -0
- package/docs/ARCHITECTURE.md +74 -0
- package/docs/CLI.md +67 -0
- package/docs/COMPATIBILITY.md +29 -0
- package/docs/CONFIG.md +73 -0
- package/docs/DEBUGGING.md +46 -0
- package/docs/NATIVE.md +110 -0
- package/docs/ROADMAP.md +12 -0
- package/docs/TESTING.md +55 -0
- package/exts/args.js +142 -0
- package/exts/freya.js +430 -0
- package/exts/fs.js +317 -0
- package/exts/io.js +396 -0
- package/exts/path.js +208 -0
- package/exts/process.js +416 -0
- package/exts/rcompilerExt.js +1396 -0
- package/exts/vartypeany.js +131 -0
- package/package.json +50 -4
- package/src/analysis.js +571 -0
- package/src/assets.js +335 -0
- package/src/cli.js +359 -0
- package/src/emit.js +353 -0
- package/src/mods.js +146 -0
- package/src/preludes.js +4174 -0
- package/src/rcompiler.js +2785 -0
- package/src/standard.js +1452 -0
- package/src/targets.js +129 -0
- package/src/web.js +1593 -0
package/exts/fs.js
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
// rCompiler 本机能力扩展 — fs 文件系统
|
|
2
|
+
// 接口参考:Alia 语言标准库 fs(https://alia.0832.ink/docs/stdlib/fs)
|
|
3
|
+
// 编译期生成零外部依赖 Rust(std::fs):写入时自动创建父目录;
|
|
4
|
+
// 写类操作失败向 stderr 打印一行警告;读类操作失败返回空值。
|
|
5
|
+
// 项目使用:扩展列表加入 "fs"(本地 exts/fs.js 自动加载)。
|
|
6
|
+
(function (util) {
|
|
7
|
+
const ArgumentType = util.ArgumentType ? util.ArgumentType : { STRING: 'r', NUMBER: 'r', BOOLEAN: 'b' };
|
|
8
|
+
const isC = () => !!util.isCompiler;
|
|
9
|
+
|
|
10
|
+
// ---- 表达式辅助(与 standard.js 同约定)----
|
|
11
|
+
const stuOf = (x) => (x && typeof x.Stu === 'function') ? x.Stu() : ((typeof x === 'string' && x) ? x : '""');
|
|
12
|
+
const striOf = (x) => (x && typeof x.Stri === 'function') ? x.Stri() : ((typeof x === 'string' && x) ? x : 'String::new()');
|
|
13
|
+
|
|
14
|
+
// 菜单/字段值读取:fields 优先,inputs 影子兜底(两种 VM 存储形态)
|
|
15
|
+
const gf = (args, name) => {
|
|
16
|
+
const b = args.scope.block;
|
|
17
|
+
if (b.fields && b.fields[name] != null) return String(b.fields[name][0]);
|
|
18
|
+
const inp = b.inputs && b.inputs[name];
|
|
19
|
+
if (Array.isArray(inp)) {
|
|
20
|
+
const sid = (typeof inp[2] === 'string') ? inp[2] : (inp[0] === 1 && typeof inp[1] === 'string' ? inp[1] : null);
|
|
21
|
+
if (sid != null) {
|
|
22
|
+
const sh = args.scope.target.blocks[sid];
|
|
23
|
+
if (sh && sh.fields && sh.fields[name] != null) return String(sh.fields[name][0]);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// ---- 运行时(std::fs,零外部依赖;Set 去重,按需注册)----
|
|
30
|
+
const RT_FS = [
|
|
31
|
+
'fn rcompiler_fs_write(path: &str, content: &str) {',
|
|
32
|
+
'let p = std::path::Path::new(path);',
|
|
33
|
+
'if let Some(d) = p.parent() { let _ = std::fs::create_dir_all(d); }',
|
|
34
|
+
'if let Err(e) = std::fs::write(p, content) { eprintln!("fs: 写入失败 {}: {}", path, e); }',
|
|
35
|
+
'}',
|
|
36
|
+
'fn rcompiler_fs_append(path: &str, content: &str) {',
|
|
37
|
+
'let p = std::path::Path::new(path);',
|
|
38
|
+
'if let Some(d) = p.parent() { let _ = std::fs::create_dir_all(d); }',
|
|
39
|
+
'use std::io::Write;',
|
|
40
|
+
'match std::fs::OpenOptions::new().create(true).append(true).open(p) {',
|
|
41
|
+
'Ok(mut f) => { let _ = f.write_all(content.as_bytes()); }',
|
|
42
|
+
'Err(e) => eprintln!("fs: 追加失败 {}: {}", path, e),',
|
|
43
|
+
'}',
|
|
44
|
+
'}',
|
|
45
|
+
'fn rcompiler_fs_copy(src: &str, dst: &str) {',
|
|
46
|
+
'let p = std::path::Path::new(dst);',
|
|
47
|
+
'if let Some(d) = p.parent() { let _ = std::fs::create_dir_all(d); }',
|
|
48
|
+
'if let Err(e) = std::fs::copy(src, p) { eprintln!("fs: 复制失败 {} -> {}: {}", src, dst, e); }',
|
|
49
|
+
'}',
|
|
50
|
+
'fn rcompiler_fs_rename(src: &str, dst: &str) {',
|
|
51
|
+
'let p = std::path::Path::new(dst);',
|
|
52
|
+
'if let Some(d) = p.parent() { let _ = std::fs::create_dir_all(d); }',
|
|
53
|
+
'if let Err(e) = std::fs::rename(src, p) { eprintln!("fs: 重命名失败 {} -> {}: {}", src, dst, e); }',
|
|
54
|
+
'}',
|
|
55
|
+
'fn rcompiler_fs_remove(path: &str) {',
|
|
56
|
+
'if let Err(e) = std::fs::remove_file(path) { eprintln!("fs: 删除失败 {}: {}", path, e); }',
|
|
57
|
+
'}',
|
|
58
|
+
'fn rcompiler_fs_remove_dir(path: &str) {',
|
|
59
|
+
'if let Err(e) = std::fs::remove_dir_all(path) { eprintln!("fs: 删除目录失败 {}: {}", path, e); }',
|
|
60
|
+
'}',
|
|
61
|
+
'fn rcompiler_fs_mkdir(path: &str) {',
|
|
62
|
+
'if let Err(e) = std::fs::create_dir_all(path) { eprintln!("fs: 创建目录失败 {}: {}", path, e); }',
|
|
63
|
+
'}',
|
|
64
|
+
'fn rcompiler_fs_read(path: &str) -> String {',
|
|
65
|
+
'std::fs::read_to_string(path).unwrap_or_default()',
|
|
66
|
+
'}',
|
|
67
|
+
'fn rcompiler_fs_exist(path: &str) -> bool {',
|
|
68
|
+
'std::path::Path::new(path).exists()',
|
|
69
|
+
'}',
|
|
70
|
+
'fn rcompiler_fs_is_file(path: &str) -> bool {',
|
|
71
|
+
'std::fs::metadata(path).map(|m| m.is_file()).unwrap_or(false)',
|
|
72
|
+
'}',
|
|
73
|
+
'fn rcompiler_fs_is_dir(path: &str) -> bool {',
|
|
74
|
+
'std::fs::metadata(path).map(|m| m.is_dir()).unwrap_or(false)',
|
|
75
|
+
'}',
|
|
76
|
+
'fn rcompiler_fs_size(path: &str) -> f64 {',
|
|
77
|
+
'std::fs::metadata(path).map(|m| m.len() as f64).unwrap_or(0.0)',
|
|
78
|
+
'}',
|
|
79
|
+
'fn rcompiler_fs_modified(path: &str) -> f64 {',
|
|
80
|
+
'std::fs::metadata(path).ok()',
|
|
81
|
+
'.and_then(|m| m.modified().ok())',
|
|
82
|
+
'.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())',
|
|
83
|
+
'.map(|d| d.as_secs_f64())',
|
|
84
|
+
'.unwrap_or(0.0)',
|
|
85
|
+
'}',
|
|
86
|
+
'fn rcompiler_fs_list(path: &str) -> String {',
|
|
87
|
+
'let mut names: Vec<String> = Vec::new();',
|
|
88
|
+
'if let Ok(rd) = std::fs::read_dir(path) {',
|
|
89
|
+
'for e in rd.flatten() { names.push(e.file_name().to_string_lossy().into_owned()); }',
|
|
90
|
+
'}',
|
|
91
|
+
'names.sort();',
|
|
92
|
+
'names.join("\\n")',
|
|
93
|
+
'}',
|
|
94
|
+
'fn rcompiler_fs_list_recursive(root: &str) -> String {',
|
|
95
|
+
'let mut out: Vec<String> = Vec::new();',
|
|
96
|
+
'fn walk(dir: &std::path::Path, rel: &str, out: &mut Vec<String>) {',
|
|
97
|
+
'if let Ok(rd) = std::fs::read_dir(dir) {',
|
|
98
|
+
'let mut ents: Vec<std::fs::DirEntry> = rd.flatten().collect();',
|
|
99
|
+
'ents.sort_by(|a, b| a.file_name().cmp(&b.file_name()));',
|
|
100
|
+
'for e in ents {',
|
|
101
|
+
'let name = e.file_name().to_string_lossy().into_owned();',
|
|
102
|
+
'let is_dir = e.file_type().map(|t| t.is_dir()).unwrap_or(false);',
|
|
103
|
+
'let child = if rel.is_empty() { name.clone() } else { format!("{}/{}", rel, name) };',
|
|
104
|
+
'if is_dir { out.push(format!("{}/", child)); walk(&e.path(), &child, out); }',
|
|
105
|
+
'else { out.push(child); }',
|
|
106
|
+
'}',
|
|
107
|
+
'}',
|
|
108
|
+
'}',
|
|
109
|
+
'walk(std::path::Path::new(root), "", &mut out);',
|
|
110
|
+
'out.join("\\n")',
|
|
111
|
+
'}'
|
|
112
|
+
].join('\n');
|
|
113
|
+
const rt = () => util.Tools.setruntime([RT_FS]);
|
|
114
|
+
|
|
115
|
+
class fsExt {
|
|
116
|
+
id = 'fs';
|
|
117
|
+
blocks = [
|
|
118
|
+
// ---- 写类命令 ----
|
|
119
|
+
{
|
|
120
|
+
opcode: 'write',
|
|
121
|
+
blockType: util.BlockType.COMMAND,
|
|
122
|
+
text: '写文件 [content] 到 [path]',
|
|
123
|
+
arguments: {
|
|
124
|
+
content: { type: ArgumentType.STRING, defaultValue: 'Hello' },
|
|
125
|
+
path: { type: ArgumentType.STRING, defaultValue: 'out.txt' }
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
opcode: 'append',
|
|
130
|
+
blockType: util.BlockType.COMMAND,
|
|
131
|
+
text: '追加 [content] 到 [path]',
|
|
132
|
+
arguments: {
|
|
133
|
+
content: { type: ArgumentType.STRING, defaultValue: 'more' },
|
|
134
|
+
path: { type: ArgumentType.STRING, defaultValue: 'out.txt' }
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
opcode: 'copy',
|
|
139
|
+
blockType: util.BlockType.COMMAND,
|
|
140
|
+
text: '复制文件 [src] 到 [dst]',
|
|
141
|
+
arguments: {
|
|
142
|
+
src: { type: ArgumentType.STRING, defaultValue: 'a.txt' },
|
|
143
|
+
dst: { type: ArgumentType.STRING, defaultValue: 'b.txt' }
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
opcode: 'rename',
|
|
148
|
+
blockType: util.BlockType.COMMAND,
|
|
149
|
+
text: '重命名 [src] 为 [dst]',
|
|
150
|
+
arguments: {
|
|
151
|
+
src: { type: ArgumentType.STRING, defaultValue: 'a.txt' },
|
|
152
|
+
dst: { type: ArgumentType.STRING, defaultValue: 'b.txt' }
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
opcode: 'remove',
|
|
157
|
+
blockType: util.BlockType.COMMAND,
|
|
158
|
+
text: '删除文件 [path]',
|
|
159
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'a.txt' } }
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
opcode: 'remove_dir',
|
|
163
|
+
blockType: util.BlockType.COMMAND,
|
|
164
|
+
text: '删除目录 [path](含全部内容)',
|
|
165
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'build' } }
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
opcode: 'mkdir',
|
|
169
|
+
blockType: util.BlockType.COMMAND,
|
|
170
|
+
text: '创建目录 [path]',
|
|
171
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'build' } }
|
|
172
|
+
},
|
|
173
|
+
// ---- 读类报告器 ----
|
|
174
|
+
{
|
|
175
|
+
opcode: 'read',
|
|
176
|
+
blockType: util.BlockType.REPORTER,
|
|
177
|
+
text: '读取文件 [path]',
|
|
178
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'a.txt' } }
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
opcode: 'exist',
|
|
182
|
+
blockType: util.BlockType.BOOLEAN,
|
|
183
|
+
text: '存在 [path]?',
|
|
184
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'a.txt' } }
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
opcode: 'is_file',
|
|
188
|
+
blockType: util.BlockType.BOOLEAN,
|
|
189
|
+
text: '[path] 是文件?',
|
|
190
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'a.txt' } }
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
opcode: 'is_dir',
|
|
194
|
+
blockType: util.BlockType.BOOLEAN,
|
|
195
|
+
text: '[path] 是目录?',
|
|
196
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'build' } }
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
opcode: 'size',
|
|
200
|
+
blockType: util.BlockType.REPORTER,
|
|
201
|
+
text: '[path] 大小(字节)',
|
|
202
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'a.txt' } }
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
opcode: 'modified',
|
|
206
|
+
blockType: util.BlockType.REPORTER,
|
|
207
|
+
text: '[path] 修改时间(Unix 秒)',
|
|
208
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'a.txt' } }
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
opcode: 'list',
|
|
212
|
+
blockType: util.BlockType.REPORTER,
|
|
213
|
+
text: '列出目录 [path]',
|
|
214
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: '.' } }
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
opcode: 'list_recursive',
|
|
218
|
+
blockType: util.BlockType.REPORTER,
|
|
219
|
+
text: '递归列出 [path]',
|
|
220
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: '.' } }
|
|
221
|
+
}
|
|
222
|
+
];
|
|
223
|
+
getInfo() {
|
|
224
|
+
return {
|
|
225
|
+
id: this.id,
|
|
226
|
+
name: '文件系统',
|
|
227
|
+
color1: '#c98a3d',
|
|
228
|
+
color2: '#e0a75c',
|
|
229
|
+
blocks: this.blocks
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
// ---- VM 端(编辑器内运行)不实现,仅编译期生成 Rust ----
|
|
233
|
+
write(args) {
|
|
234
|
+
if (!isC()) return;
|
|
235
|
+
rt();
|
|
236
|
+
args.compiler += `rcompiler_fs_write(${stuOf(args.path)}, &${striOf(args.content)});\n`;
|
|
237
|
+
return args.compiler;
|
|
238
|
+
}
|
|
239
|
+
append(args) {
|
|
240
|
+
if (!isC()) return;
|
|
241
|
+
rt();
|
|
242
|
+
args.compiler += `rcompiler_fs_append(${stuOf(args.path)}, &${striOf(args.content)});\n`;
|
|
243
|
+
return args.compiler;
|
|
244
|
+
}
|
|
245
|
+
copy(args) {
|
|
246
|
+
if (!isC()) return;
|
|
247
|
+
rt();
|
|
248
|
+
args.compiler += `rcompiler_fs_copy(${stuOf(args.src)}, ${stuOf(args.dst)});\n`;
|
|
249
|
+
return args.compiler;
|
|
250
|
+
}
|
|
251
|
+
rename(args) {
|
|
252
|
+
if (!isC()) return;
|
|
253
|
+
rt();
|
|
254
|
+
args.compiler += `rcompiler_fs_rename(${stuOf(args.src)}, ${stuOf(args.dst)});\n`;
|
|
255
|
+
return args.compiler;
|
|
256
|
+
}
|
|
257
|
+
remove(args) {
|
|
258
|
+
if (!isC()) return;
|
|
259
|
+
rt();
|
|
260
|
+
args.compiler += `rcompiler_fs_remove(${stuOf(args.path)});\n`;
|
|
261
|
+
return args.compiler;
|
|
262
|
+
}
|
|
263
|
+
remove_dir(args) {
|
|
264
|
+
if (!isC()) return;
|
|
265
|
+
rt();
|
|
266
|
+
args.compiler += `rcompiler_fs_remove_dir(${stuOf(args.path)});\n`;
|
|
267
|
+
return args.compiler;
|
|
268
|
+
}
|
|
269
|
+
mkdir(args) {
|
|
270
|
+
if (!isC()) return;
|
|
271
|
+
rt();
|
|
272
|
+
args.compiler += `rcompiler_fs_mkdir(${stuOf(args.path)});\n`;
|
|
273
|
+
return args.compiler;
|
|
274
|
+
}
|
|
275
|
+
read(args) {
|
|
276
|
+
if (!isC()) return '';
|
|
277
|
+
rt();
|
|
278
|
+
return new util.TypeInput.Stri(`rcompiler_fs_read(${stuOf(args.path)})`);
|
|
279
|
+
}
|
|
280
|
+
exist(args) {
|
|
281
|
+
if (!isC()) return false;
|
|
282
|
+
rt();
|
|
283
|
+
return new util.TypeInput.Bool(`rcompiler_fs_exist(${stuOf(args.path)})`);
|
|
284
|
+
}
|
|
285
|
+
is_file(args) {
|
|
286
|
+
if (!isC()) return false;
|
|
287
|
+
rt();
|
|
288
|
+
return new util.TypeInput.Bool(`rcompiler_fs_is_file(${stuOf(args.path)})`);
|
|
289
|
+
}
|
|
290
|
+
is_dir(args) {
|
|
291
|
+
if (!isC()) return false;
|
|
292
|
+
rt();
|
|
293
|
+
return new util.TypeInput.Bool(`rcompiler_fs_is_dir(${stuOf(args.path)})`);
|
|
294
|
+
}
|
|
295
|
+
size(args) {
|
|
296
|
+
if (!isC()) return 0;
|
|
297
|
+
rt();
|
|
298
|
+
return new util.TypeInput.Num(`rcompiler_fs_size(${stuOf(args.path)})`);
|
|
299
|
+
}
|
|
300
|
+
modified(args) {
|
|
301
|
+
if (!isC()) return 0;
|
|
302
|
+
rt();
|
|
303
|
+
return new util.TypeInput.Num(`rcompiler_fs_modified(${stuOf(args.path)})`);
|
|
304
|
+
}
|
|
305
|
+
list(args) {
|
|
306
|
+
if (!isC()) return '';
|
|
307
|
+
rt();
|
|
308
|
+
return new util.TypeInput.Stri(`rcompiler_fs_list(${stuOf(args.path)})`);
|
|
309
|
+
}
|
|
310
|
+
list_recursive(args) {
|
|
311
|
+
if (!isC()) return '';
|
|
312
|
+
rt();
|
|
313
|
+
return new util.TypeInput.Stri(`rcompiler_fs_list_recursive(${stuOf(args.path)})`);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
util.extensions.register(new fsExt());
|
|
317
|
+
})(this.isCompiler ? this : Scratch);
|