rcompiler 2.1.0 → 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 +11 -0
- package/README.md +15 -2
- package/docs/CONFIG.md +1 -1
- package/docs/NATIVE.md +110 -0
- package/exts/args.js +142 -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/package.json +1 -1
- package/src/analysis.js +8 -0
- package/src/assets.js +2 -2
- package/src/cli.js +2 -0
- package/src/emit.js +3 -3
- package/src/preludes.js +845 -122
- package/src/rcompiler.js +328 -60
- package/src/standard.js +176 -76
- package/src/web.js +0 -10
- /package/exts/{rcrcHelper.js → rcompilerExt.js} +0 -0
package/exts/io.js
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
// rCompiler 本机能力扩展 — io 输入输出
|
|
2
|
+
// 接口参考:Alia 语言标准库 io(https://alia.0832.ink/docs/stdlib/io)
|
|
3
|
+
// 编译期生成零外部依赖 Rust(std::io + ANSI 转义序列):
|
|
4
|
+
// - print/println/eprint/eprintln 直接内联宏;format 支持 {} 占位符;
|
|
5
|
+
// - 终端控制(清屏/光标/颜色/样式)为纯 ANSI 实现,Windows Terminal 及主流终端可用;
|
|
6
|
+
// 光标坐标 0-based,与 Alia 一致;颜色/样式菜单值编译期映射为 ANSI 代码;
|
|
7
|
+
// - read_line 读取前先冲刷 stdout(配合 print 提示符);EOF 返回空串;
|
|
8
|
+
// read_all 适用于管道输入(cat file | prog);
|
|
9
|
+
// - 暂缺:原始模式按键读取(readKey)、终端尺寸、鼠标事件(需 termios/libc)。
|
|
10
|
+
(function (util) {
|
|
11
|
+
const ArgumentType = util.ArgumentType ? util.ArgumentType : { STRING: 'r', NUMBER: 'r', BOOLEAN: 'b' };
|
|
12
|
+
const isC = () => !!util.isCompiler;
|
|
13
|
+
|
|
14
|
+
const stuOf = (x) => (x && typeof x.Stu === 'function') ? x.Stu() : ((typeof x === 'string' && x) ? x : '""');
|
|
15
|
+
const striOf = (x) => (x && typeof x.Stri === 'function') ? x.Stri() : ((typeof x === 'string' && x) ? x : 'String::new()');
|
|
16
|
+
const numOf = (x) => (x && typeof x.Num === 'function') ? x.Num() : '0.0';
|
|
17
|
+
|
|
18
|
+
// 菜单/字段值读取:fields 优先,inputs 影子兜底(两种 VM 存储形态)
|
|
19
|
+
const gf = (args, name) => {
|
|
20
|
+
const b = args.scope.block;
|
|
21
|
+
if (b.fields && b.fields[name] != null) return String(b.fields[name][0]);
|
|
22
|
+
const inp = b.inputs && b.inputs[name];
|
|
23
|
+
if (Array.isArray(inp)) {
|
|
24
|
+
const sid = (typeof inp[2] === 'string') ? inp[2] : (inp[0] === 1 && typeof inp[1] === 'string' ? inp[1] : null);
|
|
25
|
+
if (sid != null) {
|
|
26
|
+
const sh = args.scope.target.blocks[sid];
|
|
27
|
+
if (sh && sh.fields && sh.fields[name] != null) return String(sh.fields[name][0]);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// ANSI 代码映射(编译期已知菜单值直接内联)
|
|
34
|
+
const FG = { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, gray: 90, bright_red: 91, bright_green: 92, bright_yellow: 93, bright_blue: 94, bright_magenta: 95, bright_cyan: 96, bright_white: 97 };
|
|
35
|
+
const BG = { black: 40, red: 41, green: 42, yellow: 43, blue: 44, magenta: 45, cyan: 46, white: 47, gray: 100, bright_red: 101, bright_green: 102, bright_yellow: 103, bright_blue: 104, bright_magenta: 105, bright_cyan: 106, bright_white: 107 };
|
|
36
|
+
const STYLE = { bold: 1, italic: 3, underline: 4, strikethrough: 9, inverse: 7, reset: 0 };
|
|
37
|
+
|
|
38
|
+
// ---- 运行时(零外部依赖;仅动态颜色/读输入需要)----
|
|
39
|
+
const RT_IO = [
|
|
40
|
+
'pub fn rcompiler_io_read_line() -> String {',
|
|
41
|
+
'let _ = std::io::Write::flush(&mut std::io::stdout());',
|
|
42
|
+
'let mut s = String::new();',
|
|
43
|
+
'match std::io::stdin().read_line(&mut s) {',
|
|
44
|
+
'Ok(0) | Err(_) => String::new(),',
|
|
45
|
+
'Ok(_) => s.trim_end_matches(|c| c == \'\\n\' || c == \'\\r\').to_string(),',
|
|
46
|
+
'}',
|
|
47
|
+
'}',
|
|
48
|
+
'pub fn rcompiler_io_read_all() -> String {',
|
|
49
|
+
'use std::io::Read;',
|
|
50
|
+
'let mut s = String::new();',
|
|
51
|
+
'let _ = std::io::stdin().read_to_string(&mut s);',
|
|
52
|
+
's',
|
|
53
|
+
'}',
|
|
54
|
+
'pub fn rcompiler_io_format(fmt: &str, v: &str) -> String {',
|
|
55
|
+
'fmt.replace("{}", v)',
|
|
56
|
+
'}',
|
|
57
|
+
'pub fn rcompiler_io_format2(fmt: &str, a: &str, b: &str) -> String {',
|
|
58
|
+
'match fmt.find("{}") {',
|
|
59
|
+
'Some(i) => {',
|
|
60
|
+
'let mut out = String::with_capacity(fmt.len() + a.len() + b.len());',
|
|
61
|
+
'out.push_str(&fmt[..i]);',
|
|
62
|
+
'out.push_str(a);',
|
|
63
|
+
'out.push_str(&rcompiler_io_format(&fmt[i + 2..], b));',
|
|
64
|
+
'out',
|
|
65
|
+
'}',
|
|
66
|
+
'None => fmt.to_string(),',
|
|
67
|
+
'}',
|
|
68
|
+
'}',
|
|
69
|
+
'pub fn rcompiler_io_fg(color: &str) {',
|
|
70
|
+
'let code = match color {',
|
|
71
|
+
'"black" => 30, "red" => 31, "green" => 32, "yellow" => 33,',
|
|
72
|
+
'"blue" => 34, "magenta" => 35, "cyan" => 36, "white" => 37, "gray" => 90,',
|
|
73
|
+
'"bright_red" => 91, "bright_green" => 92, "bright_yellow" => 93,',
|
|
74
|
+
'"bright_blue" => 94, "bright_magenta" => 95, "bright_cyan" => 96, "bright_white" => 97,',
|
|
75
|
+
'_ => return,',
|
|
76
|
+
'};',
|
|
77
|
+
'print!("\\x1b[{}m", code);',
|
|
78
|
+
'}',
|
|
79
|
+
'pub fn rcompiler_io_bg(color: &str) {',
|
|
80
|
+
'let code = match color {',
|
|
81
|
+
'"black" => 40, "red" => 41, "green" => 42, "yellow" => 43,',
|
|
82
|
+
'"blue" => 44, "magenta" => 45, "cyan" => 46, "white" => 47, "gray" => 100,',
|
|
83
|
+
'"bright_red" => 101, "bright_green" => 102, "bright_yellow" => 103,',
|
|
84
|
+
'"bright_blue" => 104, "bright_magenta" => 105, "bright_cyan" => 106, "bright_white" => 107,',
|
|
85
|
+
'_ => return,',
|
|
86
|
+
'};',
|
|
87
|
+
'print!("\\x1b[{}m", code);',
|
|
88
|
+
'}',
|
|
89
|
+
'pub fn rcompiler_io_style(s: &str) {',
|
|
90
|
+
'let code = match s {',
|
|
91
|
+
'"bold" => 1, "italic" => 3, "underline" => 4, "strikethrough" => 9, "inverse" => 7, "reset" => 0,',
|
|
92
|
+
'_ => return,',
|
|
93
|
+
'};',
|
|
94
|
+
'print!("\\x1b[{}m", code);',
|
|
95
|
+
'}'
|
|
96
|
+
].join('\n');
|
|
97
|
+
const rt = () => util.Tools.setruntime([RT_IO]);
|
|
98
|
+
|
|
99
|
+
const colorMenus = (prefix) => [
|
|
100
|
+
{ text: '黑', value: 'black' }, { text: '红', value: 'red' },
|
|
101
|
+
{ text: '绿', value: 'green' }, { text: '黄', value: 'yellow' },
|
|
102
|
+
{ text: '蓝', value: 'blue' }, { text: '品红', value: 'magenta' },
|
|
103
|
+
{ text: '青', value: 'cyan' }, { text: '白', value: 'white' },
|
|
104
|
+
{ text: '灰', value: 'gray' },
|
|
105
|
+
{ text: '亮红', value: 'bright_red' }, { text: '亮绿', value: 'bright_green' },
|
|
106
|
+
{ text: '亮黄', value: 'bright_yellow' }, { text: '亮蓝', value: 'bright_blue' },
|
|
107
|
+
{ text: '亮品红', value: 'bright_magenta' }, { text: '亮青', value: 'bright_cyan' },
|
|
108
|
+
{ text: '亮白', value: 'bright_white' }
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
class ioExt {
|
|
112
|
+
id = 'io';
|
|
113
|
+
blocks = [
|
|
114
|
+
// ---- 基础输出 ----
|
|
115
|
+
{
|
|
116
|
+
opcode: 'print',
|
|
117
|
+
blockType: util.BlockType.COMMAND,
|
|
118
|
+
text: '输出 [text]',
|
|
119
|
+
arguments: { text: { type: ArgumentType.STRING, defaultValue: 'Hello' } }
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
opcode: 'println',
|
|
123
|
+
blockType: util.BlockType.COMMAND,
|
|
124
|
+
text: '输出行 [text]',
|
|
125
|
+
arguments: { text: { type: ArgumentType.STRING, defaultValue: 'Hello' } }
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
opcode: 'eprint',
|
|
129
|
+
blockType: util.BlockType.COMMAND,
|
|
130
|
+
text: '输出错误 [text]',
|
|
131
|
+
arguments: { text: { type: ArgumentType.STRING, defaultValue: 'error' } }
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
opcode: 'eprintln',
|
|
135
|
+
blockType: util.BlockType.COMMAND,
|
|
136
|
+
text: '输出错误行 [text]',
|
|
137
|
+
arguments: { text: { type: ArgumentType.STRING, defaultValue: 'error' } }
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
opcode: 'format',
|
|
141
|
+
blockType: util.BlockType.REPORTER,
|
|
142
|
+
text: '格式化 [fmt] 用 [value]',
|
|
143
|
+
arguments: {
|
|
144
|
+
fmt: { type: ArgumentType.STRING, defaultValue: 'value = {}' },
|
|
145
|
+
value: { type: ArgumentType.STRING, defaultValue: '1' }
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
opcode: 'format2',
|
|
150
|
+
blockType: util.BlockType.REPORTER,
|
|
151
|
+
text: '格式化 [fmt] 用 [v1] [v2]',
|
|
152
|
+
arguments: {
|
|
153
|
+
fmt: { type: ArgumentType.STRING, defaultValue: '{} + {}' },
|
|
154
|
+
v1: { type: ArgumentType.STRING, defaultValue: '1' },
|
|
155
|
+
v2: { type: ArgumentType.STRING, defaultValue: '2' }
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
// ---- 输入 ----
|
|
159
|
+
{
|
|
160
|
+
opcode: 'read_line',
|
|
161
|
+
blockType: util.BlockType.REPORTER,
|
|
162
|
+
text: '读取一行输入'
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
opcode: 'read_all',
|
|
166
|
+
blockType: util.BlockType.REPORTER,
|
|
167
|
+
text: '读取全部输入(管道)'
|
|
168
|
+
},
|
|
169
|
+
// ---- 终端控制 ----
|
|
170
|
+
{
|
|
171
|
+
opcode: 'clear',
|
|
172
|
+
blockType: util.BlockType.COMMAND,
|
|
173
|
+
text: '清屏'
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
opcode: 'clear_line',
|
|
177
|
+
blockType: util.BlockType.COMMAND,
|
|
178
|
+
text: '清除当前行'
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
opcode: 'move_cursor',
|
|
182
|
+
blockType: util.BlockType.COMMAND,
|
|
183
|
+
text: '移动光标 行 [row] 列 [col]',
|
|
184
|
+
arguments: {
|
|
185
|
+
row: { type: ArgumentType.NUMBER, defaultValue: '0' },
|
|
186
|
+
col: { type: ArgumentType.NUMBER, defaultValue: '0' }
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
opcode: 'cursor_up',
|
|
191
|
+
blockType: util.BlockType.COMMAND,
|
|
192
|
+
text: '光标上移 [n]',
|
|
193
|
+
arguments: { n: { type: ArgumentType.NUMBER, defaultValue: '1' } }
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
opcode: 'cursor_down',
|
|
197
|
+
blockType: util.BlockType.COMMAND,
|
|
198
|
+
text: '光标下移 [n]',
|
|
199
|
+
arguments: { n: { type: ArgumentType.NUMBER, defaultValue: '1' } }
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
opcode: 'cursor_forward',
|
|
203
|
+
blockType: util.BlockType.COMMAND,
|
|
204
|
+
text: '光标右移 [n]',
|
|
205
|
+
arguments: { n: { type: ArgumentType.NUMBER, defaultValue: '1' } }
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
opcode: 'cursor_back',
|
|
209
|
+
blockType: util.BlockType.COMMAND,
|
|
210
|
+
text: '光标左移 [n]',
|
|
211
|
+
arguments: { n: { type: ArgumentType.NUMBER, defaultValue: '1' } }
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
opcode: 'hide_cursor',
|
|
215
|
+
blockType: util.BlockType.COMMAND,
|
|
216
|
+
text: '隐藏光标'
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
opcode: 'show_cursor',
|
|
220
|
+
blockType: util.BlockType.COMMAND,
|
|
221
|
+
text: '显示光标'
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
opcode: 'set_color',
|
|
225
|
+
blockType: util.BlockType.COMMAND,
|
|
226
|
+
text: '前景色 [color]',
|
|
227
|
+
usefield: ['0'],
|
|
228
|
+
arguments: { color: { type: ArgumentType.STRING, menu: 'colors', defaultValue: 'red' } }
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
opcode: 'set_bg_color',
|
|
232
|
+
blockType: util.BlockType.COMMAND,
|
|
233
|
+
text: '背景色 [color]',
|
|
234
|
+
usefield: ['0'],
|
|
235
|
+
arguments: { color: { type: ArgumentType.STRING, menu: 'colors', defaultValue: 'black' } }
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
opcode: 'reset_color',
|
|
239
|
+
blockType: util.BlockType.COMMAND,
|
|
240
|
+
text: '重置颜色'
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
opcode: 'style',
|
|
244
|
+
blockType: util.BlockType.COMMAND,
|
|
245
|
+
text: '样式 [style]',
|
|
246
|
+
usefield: ['0'],
|
|
247
|
+
arguments: { style: { type: ArgumentType.STRING, menu: 'styles', defaultValue: 'bold' } }
|
|
248
|
+
}
|
|
249
|
+
];
|
|
250
|
+
getInfo() {
|
|
251
|
+
return {
|
|
252
|
+
id: this.id,
|
|
253
|
+
name: '输入输出',
|
|
254
|
+
color1: '#c93d7a',
|
|
255
|
+
color2: '#e05c96',
|
|
256
|
+
blocks: this.blocks,
|
|
257
|
+
menus: {
|
|
258
|
+
colors: colorMenus(),
|
|
259
|
+
styles: [
|
|
260
|
+
{ text: '粗体', value: 'bold' }, { text: '斜体', value: 'italic' },
|
|
261
|
+
{ text: '下划线', value: 'underline' }, { text: '删除线', value: 'strikethrough' },
|
|
262
|
+
{ text: '反色', value: 'inverse' }, { text: '重置样式', value: 'reset' }
|
|
263
|
+
]
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
// ---- VM 端不实现,仅编译期生成 Rust ----
|
|
268
|
+
print(args) {
|
|
269
|
+
if (!isC()) return;
|
|
270
|
+
args.compiler += `print!("{}", ${stuOf(args.text)});\n`;
|
|
271
|
+
return args.compiler;
|
|
272
|
+
}
|
|
273
|
+
println(args) {
|
|
274
|
+
if (!isC()) return;
|
|
275
|
+
args.compiler += `println!("{}", ${stuOf(args.text)});\n`;
|
|
276
|
+
return args.compiler;
|
|
277
|
+
}
|
|
278
|
+
eprint(args) {
|
|
279
|
+
if (!isC()) return;
|
|
280
|
+
args.compiler += `eprint!("{}", ${stuOf(args.text)});\n`;
|
|
281
|
+
return args.compiler;
|
|
282
|
+
}
|
|
283
|
+
eprintln(args) {
|
|
284
|
+
if (!isC()) return;
|
|
285
|
+
args.compiler += `eprintln!("{}", ${stuOf(args.text)});\n`;
|
|
286
|
+
return args.compiler;
|
|
287
|
+
}
|
|
288
|
+
format(args) {
|
|
289
|
+
if (!isC()) return '';
|
|
290
|
+
rt();
|
|
291
|
+
return new util.TypeInput.Stri(`rcompiler_io_format(${stuOf(args.fmt)}, &${striOf(args.value)})`);
|
|
292
|
+
}
|
|
293
|
+
format2(args) {
|
|
294
|
+
if (!isC()) return '';
|
|
295
|
+
rt();
|
|
296
|
+
return new util.TypeInput.Stri(`rcompiler_io_format2(${stuOf(args.fmt)}, &${striOf(args.v1)}, &${striOf(args.v2)})`);
|
|
297
|
+
}
|
|
298
|
+
read_line(args) {
|
|
299
|
+
if (!isC()) return '';
|
|
300
|
+
rt();
|
|
301
|
+
return new util.TypeInput.Stri('rcompiler_io_read_line()');
|
|
302
|
+
}
|
|
303
|
+
read_all(args) {
|
|
304
|
+
if (!isC()) return '';
|
|
305
|
+
rt();
|
|
306
|
+
return new util.TypeInput.Stri('rcompiler_io_read_all()');
|
|
307
|
+
}
|
|
308
|
+
clear(args) {
|
|
309
|
+
if (!isC()) return;
|
|
310
|
+
args.compiler += 'print!("\\x1b[2J\\x1b[H");\n';
|
|
311
|
+
return args.compiler;
|
|
312
|
+
}
|
|
313
|
+
clear_line(args) {
|
|
314
|
+
if (!isC()) return;
|
|
315
|
+
args.compiler += 'print!("\\x1b[2K");\n';
|
|
316
|
+
return args.compiler;
|
|
317
|
+
}
|
|
318
|
+
move_cursor(args) {
|
|
319
|
+
if (!isC()) return;
|
|
320
|
+
args.compiler += [
|
|
321
|
+
'{',
|
|
322
|
+
`let __r = ((${numOf(args.row)}) as i32).max(0) + 1;`,
|
|
323
|
+
`let __c = ((${numOf(args.col)}) as i32).max(0) + 1;`,
|
|
324
|
+
'print!("\\x1b[{};{}H", __r, __c);',
|
|
325
|
+
'}'
|
|
326
|
+
].join('\n') + '\n';
|
|
327
|
+
return args.compiler;
|
|
328
|
+
}
|
|
329
|
+
cursor_up(args) {
|
|
330
|
+
if (!isC()) return;
|
|
331
|
+
args.compiler += `print!("\\x1b[{}A", ((${numOf(args.n)}) as i32).max(1));\n`;
|
|
332
|
+
return args.compiler;
|
|
333
|
+
}
|
|
334
|
+
cursor_down(args) {
|
|
335
|
+
if (!isC()) return;
|
|
336
|
+
args.compiler += `print!("\\x1b[{}B", ((${numOf(args.n)}) as i32).max(1));\n`;
|
|
337
|
+
return args.compiler;
|
|
338
|
+
}
|
|
339
|
+
cursor_forward(args) {
|
|
340
|
+
if (!isC()) return;
|
|
341
|
+
args.compiler += `print!("\\x1b[{}C", ((${numOf(args.n)}) as i32).max(1));\n`;
|
|
342
|
+
return args.compiler;
|
|
343
|
+
}
|
|
344
|
+
cursor_back(args) {
|
|
345
|
+
if (!isC()) return;
|
|
346
|
+
args.compiler += `print!("\\x1b[{}D", ((${numOf(args.n)}) as i32).max(1));\n`;
|
|
347
|
+
return args.compiler;
|
|
348
|
+
}
|
|
349
|
+
hide_cursor(args) {
|
|
350
|
+
if (!isC()) return;
|
|
351
|
+
args.compiler += 'print!("\\x1b[?25l");\n';
|
|
352
|
+
return args.compiler;
|
|
353
|
+
}
|
|
354
|
+
show_cursor(args) {
|
|
355
|
+
if (!isC()) return;
|
|
356
|
+
args.compiler += 'print!("\\x1b[?25h");\n';
|
|
357
|
+
return args.compiler;
|
|
358
|
+
}
|
|
359
|
+
set_color(args) {
|
|
360
|
+
if (!isC()) return;
|
|
361
|
+
const v = gf(args, 'color');
|
|
362
|
+
if (v != null && FG[v] != null) args.compiler += `print!("\\x1b[${FG[v]}m");\n`;
|
|
363
|
+
else {
|
|
364
|
+
rt();
|
|
365
|
+
args.compiler += `rcompiler_io_fg(${stuOf(args.color)});\n`;
|
|
366
|
+
}
|
|
367
|
+
return args.compiler;
|
|
368
|
+
}
|
|
369
|
+
set_bg_color(args) {
|
|
370
|
+
if (!isC()) return;
|
|
371
|
+
const v = gf(args, 'color');
|
|
372
|
+
if (v != null && BG[v] != null) args.compiler += `print!("\\x1b[${BG[v]}m");\n`;
|
|
373
|
+
else {
|
|
374
|
+
rt();
|
|
375
|
+
args.compiler += `rcompiler_io_bg(${stuOf(args.color)});\n`;
|
|
376
|
+
}
|
|
377
|
+
return args.compiler;
|
|
378
|
+
}
|
|
379
|
+
reset_color(args) {
|
|
380
|
+
if (!isC()) return;
|
|
381
|
+
args.compiler += 'print!("\\x1b[0m");\n';
|
|
382
|
+
return args.compiler;
|
|
383
|
+
}
|
|
384
|
+
style(args) {
|
|
385
|
+
if (!isC()) return;
|
|
386
|
+
const v = gf(args, 'style');
|
|
387
|
+
if (v != null && STYLE[v] != null) args.compiler += `print!("\\x1b[${STYLE[v]}m");\n`;
|
|
388
|
+
else {
|
|
389
|
+
rt();
|
|
390
|
+
args.compiler += `rcompiler_io_style(${stuOf(args.style)});\n`;
|
|
391
|
+
}
|
|
392
|
+
return args.compiler;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
util.extensions.register(new ioExt());
|
|
396
|
+
})(this.isCompiler ? this : Scratch);
|
package/exts/path.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// rCompiler 本机能力扩展 — path 路径
|
|
2
|
+
// 编译期生成零外部依赖 Rust(std::path 仅取 MAIN_SEPARATOR,其余为手写纯字符串操作):
|
|
3
|
+
// - 统一接受 '/' 与 '\\' 分隔;join/abs/sep 输出使用平台主分隔符;
|
|
4
|
+
// - normalize 纯字符串规范化(处理 . / .. 与多余分隔符),不解引用符号链接;
|
|
5
|
+
// - 语义对照 POSIX dirname/basename(无分隔符的父目录为 ".",根目录为 "/")。
|
|
6
|
+
(function (util) {
|
|
7
|
+
const ArgumentType = util.ArgumentType ? util.ArgumentType : { STRING: 'r', NUMBER: 'r', BOOLEAN: 'b' };
|
|
8
|
+
const isC = () => !!util.isCompiler;
|
|
9
|
+
|
|
10
|
+
const stuOf = (x) => (x && typeof x.Stu === 'function') ? x.Stu() : ((typeof x === 'string' && x) ? x : '""');
|
|
11
|
+
const numOf = (x) => (x && typeof x.Num === 'function') ? x.Num() : '0.0';
|
|
12
|
+
|
|
13
|
+
// ---- 运行时(零外部依赖)----
|
|
14
|
+
const RT_PATH = [
|
|
15
|
+
'pub fn rcompiler_path_is_abs(p: &str) -> bool {',
|
|
16
|
+
'if p.starts_with(\'/\') { return true; }',
|
|
17
|
+
'let b = p.as_bytes();',
|
|
18
|
+
'b.len() >= 3 && b[1] == b\':\' && (b[2] == b\'/\' || b[2] == b\'\\\\\') && b[0].is_ascii_alphabetic()',
|
|
19
|
+
'}',
|
|
20
|
+
'pub fn rcompiler_path_join(a: &str, b: &str) -> String {',
|
|
21
|
+
'if a.is_empty() { return b.to_string(); }',
|
|
22
|
+
'if b.is_empty() { return a.to_string(); }',
|
|
23
|
+
'if rcompiler_path_is_abs(b) { return b.to_string(); }',
|
|
24
|
+
'let sep = std::path::MAIN_SEPARATOR;',
|
|
25
|
+
'let ta = a.trim_end_matches(|c| c == \'/\' || c == \'\\\\\');',
|
|
26
|
+
'if ta.is_empty() { return format!("{}{}", sep, b); }',
|
|
27
|
+
'format!("{}{}{}", ta, sep, b)',
|
|
28
|
+
'}',
|
|
29
|
+
'pub fn rcompiler_path_normalize(p: &str) -> String {',
|
|
30
|
+
'// 盘符前缀(C:/)或根前缀单独保留,其余按 . / .. 规范化',
|
|
31
|
+
'let b = p.as_bytes();',
|
|
32
|
+
'let (prefix, rest) = if b.len() >= 2 && b[1] == b\':\' && b[0].is_ascii_alphabetic() {',
|
|
33
|
+
'let mut i = 2;',
|
|
34
|
+
'while i < b.len() && (b[i] == b\'/\' || b[i] == b\'\\\\\') { i += 1; }',
|
|
35
|
+
'(format!("{}:/", &p[..2]), &p[i..])',
|
|
36
|
+
'} else if p.starts_with(\'/\') {',
|
|
37
|
+
'("/".to_string(), &p[1..])',
|
|
38
|
+
'} else {',
|
|
39
|
+
'(String::new(), p)',
|
|
40
|
+
'};',
|
|
41
|
+
'let abs = !prefix.is_empty();',
|
|
42
|
+
'let mut parts: Vec<&str> = Vec::new();',
|
|
43
|
+
'for seg in rest.split(|c| c == \'/\' || c == \'\\\\\') {',
|
|
44
|
+
'match seg {',
|
|
45
|
+
'"" | "." => {}',
|
|
46
|
+
'".." => {',
|
|
47
|
+
'if !parts.is_empty() && *parts.last().unwrap() != ".." { parts.pop(); }',
|
|
48
|
+
'else if !abs { parts.push(".."); }',
|
|
49
|
+
'}',
|
|
50
|
+
's => parts.push(s),',
|
|
51
|
+
'}',
|
|
52
|
+
'}',
|
|
53
|
+
'let joined = parts.join("/");',
|
|
54
|
+
'if abs { format!("{}{}", prefix, joined) }',
|
|
55
|
+
'else if joined.is_empty() { ".".to_string() }',
|
|
56
|
+
'else { joined }',
|
|
57
|
+
'}',
|
|
58
|
+
'pub fn rcompiler_path_dir(p: &str) -> String {',
|
|
59
|
+
'let t = p.trim_end_matches(|c| c == \'/\' || c == \'\\\\\');',
|
|
60
|
+
'if t.is_empty() { return p.to_string(); }',
|
|
61
|
+
'match t.rfind(|c| c == \'/\' || c == \'\\\\\') {',
|
|
62
|
+
'Some(0) => "/".to_string(),',
|
|
63
|
+
'Some(i) => t[..i].to_string(),',
|
|
64
|
+
'None => ".".to_string(),',
|
|
65
|
+
'}',
|
|
66
|
+
'}',
|
|
67
|
+
'pub fn rcompiler_path_name(p: &str) -> String {',
|
|
68
|
+
'let t = p.trim_end_matches(|c| c == \'/\' || c == \'\\\\\');',
|
|
69
|
+
'if t.is_empty() { return p.to_string(); }',
|
|
70
|
+
'match t.rfind(|c| c == \'/\' || c == \'\\\\\') {',
|
|
71
|
+
'Some(i) => t[i + 1..].to_string(),',
|
|
72
|
+
'None => t.to_string(),',
|
|
73
|
+
'}',
|
|
74
|
+
'}',
|
|
75
|
+
'pub fn rcompiler_path_ext(p: &str) -> String {',
|
|
76
|
+
'let f = rcompiler_path_name(p);',
|
|
77
|
+
'match f.rfind(\'.\') { Some(i) if i > 0 => f[i + 1..].to_string(), _ => String::new() }',
|
|
78
|
+
'}',
|
|
79
|
+
'pub fn rcompiler_path_stem(p: &str) -> String {',
|
|
80
|
+
'let f = rcompiler_path_name(p);',
|
|
81
|
+
'match f.rfind(\'.\') { Some(i) if i > 0 => f[..i].to_string(), _ => f }',
|
|
82
|
+
'}',
|
|
83
|
+
'pub fn rcompiler_path_abs(p: &str) -> String {',
|
|
84
|
+
'if rcompiler_path_is_abs(p) { return rcompiler_path_normalize(p); }',
|
|
85
|
+
'let cwd = std::env::current_dir().map(|d| d.to_string_lossy().into_owned()).unwrap_or_default();',
|
|
86
|
+
'rcompiler_path_normalize(&rcompiler_path_join(&cwd, p))',
|
|
87
|
+
'}'
|
|
88
|
+
].join('\n');
|
|
89
|
+
const rt = () => util.Tools.setruntime([RT_PATH]);
|
|
90
|
+
|
|
91
|
+
class pathExt {
|
|
92
|
+
id = 'path';
|
|
93
|
+
blocks = [
|
|
94
|
+
{
|
|
95
|
+
opcode: 'join',
|
|
96
|
+
blockType: util.BlockType.REPORTER,
|
|
97
|
+
text: '拼接路径 [a] [b]',
|
|
98
|
+
arguments: {
|
|
99
|
+
a: { type: ArgumentType.STRING, defaultValue: 'src' },
|
|
100
|
+
b: { type: ArgumentType.STRING, defaultValue: 'main.rs' }
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
opcode: 'dir',
|
|
105
|
+
blockType: util.BlockType.REPORTER,
|
|
106
|
+
text: '父目录 [path]',
|
|
107
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'src/main.rs' } }
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
opcode: 'name',
|
|
111
|
+
blockType: util.BlockType.REPORTER,
|
|
112
|
+
text: '文件名 [path]',
|
|
113
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'src/main.rs' } }
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
opcode: 'stem',
|
|
117
|
+
blockType: util.BlockType.REPORTER,
|
|
118
|
+
text: '文件名去扩展名 [path]',
|
|
119
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'src/main.rs' } }
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
opcode: 'ext',
|
|
123
|
+
blockType: util.BlockType.REPORTER,
|
|
124
|
+
text: '扩展名 [path]',
|
|
125
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'src/main.rs' } }
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
opcode: 'is_abs',
|
|
129
|
+
blockType: util.BlockType.BOOLEAN,
|
|
130
|
+
text: '是绝对路径 [path]?',
|
|
131
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: '/tmp' } }
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
opcode: 'normalize',
|
|
135
|
+
blockType: util.BlockType.REPORTER,
|
|
136
|
+
text: '规范化路径 [path]',
|
|
137
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'a/b/../c' } }
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
opcode: 'abs',
|
|
141
|
+
blockType: util.BlockType.REPORTER,
|
|
142
|
+
text: '绝对路径 [path]',
|
|
143
|
+
arguments: { path: { type: ArgumentType.STRING, defaultValue: 'a.txt' } }
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
opcode: 'sep',
|
|
147
|
+
blockType: util.BlockType.REPORTER,
|
|
148
|
+
text: '路径分隔符'
|
|
149
|
+
}
|
|
150
|
+
];
|
|
151
|
+
getInfo() {
|
|
152
|
+
return {
|
|
153
|
+
id: this.id,
|
|
154
|
+
name: '路径',
|
|
155
|
+
color1: '#8a3dc9',
|
|
156
|
+
color2: '#a75ce0',
|
|
157
|
+
blocks: this.blocks
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
// ---- VM 端不实现,仅编译期生成 Rust ----
|
|
161
|
+
join(args) {
|
|
162
|
+
if (!isC()) return '';
|
|
163
|
+
rt();
|
|
164
|
+
return new util.TypeInput.Stri(`rcompiler_path_join(${stuOf(args.a)}, ${stuOf(args.b)})`);
|
|
165
|
+
}
|
|
166
|
+
dir(args) {
|
|
167
|
+
if (!isC()) return '';
|
|
168
|
+
rt();
|
|
169
|
+
return new util.TypeInput.Stri(`rcompiler_path_dir(${stuOf(args.path)})`);
|
|
170
|
+
}
|
|
171
|
+
name(args) {
|
|
172
|
+
if (!isC()) return '';
|
|
173
|
+
rt();
|
|
174
|
+
return new util.TypeInput.Stri(`rcompiler_path_name(${stuOf(args.path)})`);
|
|
175
|
+
}
|
|
176
|
+
stem(args) {
|
|
177
|
+
if (!isC()) return '';
|
|
178
|
+
rt();
|
|
179
|
+
return new util.TypeInput.Stri(`rcompiler_path_stem(${stuOf(args.path)})`);
|
|
180
|
+
}
|
|
181
|
+
ext(args) {
|
|
182
|
+
if (!isC()) return '';
|
|
183
|
+
rt();
|
|
184
|
+
return new util.TypeInput.Stri(`rcompiler_path_ext(${stuOf(args.path)})`);
|
|
185
|
+
}
|
|
186
|
+
is_abs(args) {
|
|
187
|
+
if (!isC()) return false;
|
|
188
|
+
rt();
|
|
189
|
+
return new util.TypeInput.Bool(`rcompiler_path_is_abs(${stuOf(args.path)})`);
|
|
190
|
+
}
|
|
191
|
+
normalize(args) {
|
|
192
|
+
if (!isC()) return '';
|
|
193
|
+
rt();
|
|
194
|
+
return new util.TypeInput.Stri(`rcompiler_path_normalize(${stuOf(args.path)})`);
|
|
195
|
+
}
|
|
196
|
+
abs(args) {
|
|
197
|
+
if (!isC()) return '';
|
|
198
|
+
rt();
|
|
199
|
+
return new util.TypeInput.Stri(`rcompiler_path_abs(${stuOf(args.path)})`);
|
|
200
|
+
}
|
|
201
|
+
sep(args) {
|
|
202
|
+
if (!isC()) return '/';
|
|
203
|
+
rt();
|
|
204
|
+
return new util.TypeInput.Stri('std::path::MAIN_SEPARATOR.to_string()');
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
util.extensions.register(new pathExt());
|
|
208
|
+
})(this.isCompiler ? this : Scratch);
|