aethershell 0.3.1

The world's first multi-agent shell with typed functional pipelines and multi-modal AI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! Bash → Aether Shell transpiler (compat mode).
//!
//! This is a pragmatic, line-oriented transpiler for a *useful subset* of Bash:
//!   - simple commands, args, and pipelines (`|`)
//!   - single quotes:  'no expansion'
//!   - double quotes:  expand $V and ${V} as Aether string interpolations `${V}`
//!   - $VAR as a standalone arg becomes an identifier `VAR`
//!   - simple assignments: NAME=value  →  let NAME = <expr>
//!
//! Fallback: if a line contains redirections or constructs we don't yet handle,
//! we emit:  sh(["bash","-lc", "<original line>"])
//!
//! Output: Aether source as a String. The `sh(...)` call is assumed to be a builtin
//! (or small helper) that runs an external command. If you prefer a different name,
//! adjust `render_external_call` below.

use anyhow::{anyhow, Result};

/// Transpile a whole Bash script (multi-line) to Aether code.
pub fn transpile_bash_to_ae(src: &str) -> Result<String> {
    let mut out = String::new();
    out.push_str("// Transpiled from Bash → Aether (compat mode)\n");

    for raw_line in src.lines() {
        let line = raw_line.trim();
        if line.is_empty() || line.starts_with('#') {
            // preserve comments as Aether comments
            if line.starts_with('#') {
                out.push_str(&format!("// {}\n", &line[1..].trim_start()));
            }
            continue;
        }

        // Quick detect: if there are redirections or unsupported operators,
        // fall back to `bash -lc`.
        if contains_redirection_or_complex(line) {
            out.push_str(&render_fallback_bash(line));
            out.push('\n');
            continue;
        }

        // Simple assignment?
        if let Some(assign) = parse_simple_assignment(line) {
            out.push_str(&assign);
            out.push('\n');
            continue;
        }

        // Try to parse as a pipeline of commands.
        match parse_pipeline(line) {
            Ok(cmds) if !cmds.is_empty() => {
                let aether_line = render_pipeline(&cmds);
                out.push_str(&aether_line);
                out.push('\n');
            }
            _ => {
                // Fallback if we failed to parse robustly
                out.push_str(&render_fallback_bash(line));
                out.push('\n');
            }
        }
    }

    Ok(out)
}

/* =============================================================================
Representation
============================================================================= */

#[derive(Debug, Clone)]
struct Command {
    program: Token,   // typically plain word (may include vars if unquoted/double-quoted)
    args: Vec<Token>, // arguments
}

#[derive(Debug, Clone)]
enum Token {
    /// Normal / double-quoted token with possible var expansions
    Interp(Vec<Piece>),
    /// Single-quoted string with no expansion
    Single(String),
}

#[derive(Debug, Clone)]
enum Piece {
    Text(String),
    Var(String), // VAR name (no leading '$')
}

/* =============================================================================
Top-level quick checks and simple forms
============================================================================= */

fn contains_redirection_or_complex(s: &str) -> bool {
    // quick n' dirty: redirect operators or process-substitution or background
    // If you want to support these, map them to Aether I/O or fallback to `bash -lc`.
    let suspicious = ['>', '<', '&'];
    s.chars().any(|c| suspicious.contains(&c))
        || s.contains("2>")
        || s.contains(">>")
        || s.contains("<<")
        || s.contains("|&")
        || s.contains("<<<")
        || s.contains(">|")
        || s.contains("$(")  // command substitution: not supported yet, fallback
        || s.contains("`") // backticks: not supported yet, fallback
}

fn render_fallback_bash(line: &str) -> String {
    // Escape the line into a single-quoted Aether string literal safely.
    // We'll build: sh(["bash","-lc","<line>"])
    let quoted = single_quote_literal(line);
    format!("sh([\"bash\",\"-lc\", {}]);", quoted)
}

fn parse_simple_assignment(line: &str) -> Option<String> {
    // Accept NAME=VALUE (no spaces around '='). Ignore export/readonly for now.
    // Examples:
    //   FOO=bar           -> let FOO = "bar";
    //   X=$HOME           -> let X = HOME;
    //   N=123             -> let N = 123;
    // We avoid compound assignments like PATH="$HOME:${PATH}" -> fallback (too complex).
    if line.starts_with("export ") || line.starts_with("readonly ") {
        return None; // skip here so that fallback or command path handles it
    }

    // No spaces allowed on either side for this "simple" matcher.
    if line.split_whitespace().count() != 1 {
        return None;
    }
    if let Some(eq) = line.find('=') {
        let (lhs, rhs) = line.split_at(eq);
        let lhs = lhs.trim();
        if lhs.is_empty() {
            return None;
        }
        if !is_ident(lhs) {
            return None;
        }
        let rhs = &rhs[1..].trim(); // skip '='

        // If RHS contains spaces or operators, it's not "simple"
        if rhs.is_empty() || rhs.contains(' ') || contains_redirection_or_complex(rhs) {
            return None;
        }

        // Decide how to render RHS:
        if rhs.starts_with('"') || rhs.starts_with('\'') {
            // Strip quotes and render appropriately.
            if let Ok(tok) = parse_single_token(rhs) {
                let expr = render_arg_expr(&tok);
                return Some(format!("let {} = {};", lhs, expr));
            } else {
                return None;
            }
        } else if rhs.starts_with('$') {
            if let Some(var) = parse_var_ref(rhs) {
                return Some(format!("let {} = {};", lhs, var));
            } else {
                return None;
            }
        } else if is_int_literal(rhs) {
            return Some(format!("let {} = {};", lhs, rhs));
        } else if is_float_literal(rhs) {
            return Some(format!("let {} = {};", lhs, rhs));
        } else {
            // treat as plain string
            return Some(format!("let {} = {};", lhs, single_quote_literal(rhs)));
        }
    }
    None
}

/* =============================================================================
Parsing a pipeline
============================================================================= */

fn parse_pipeline(s: &str) -> Result<Vec<Command>> {
    let parts = split_unquoted_pipes(s)?;
    let mut cmds = Vec::new();
    for p in parts {
        let toks = split_shell_words(&p)?;
        if toks.is_empty() {
            continue;
        }
        let program = toks[0].clone();
        let args = toks[1..].to_vec();
        cmds.push(Command { program, args });
    }
    Ok(cmds)
}

fn split_unquoted_pipes(s: &str) -> Result<Vec<String>> {
    let mut out = Vec::new();
    let mut cur = String::new();
    let mut dq = false;
    let mut sq = false;

    let mut chars = s.chars().peekable();
    while let Some(ch) = chars.next() {
        match ch {
            '\'' if !dq => {
                sq = !sq;
                cur.push(ch);
            }
            '"' if !sq => {
                dq = !dq;
                cur.push(ch);
            }
            '|' if !sq && !dq => {
                out.push(cur.trim().to_string());
                cur.clear();
            }
            _ => cur.push(ch),
        }
    }
    if !cur.trim().is_empty() {
        out.push(cur.trim().to_string());
    }
    Ok(out)
}

fn split_shell_words(s: &str) -> Result<Vec<Token>> {
    let mut toks: Vec<Token> = Vec::new();
    let mut cur = Vec::<Piece>::new();
    let mut literal_sq = None::<String>; // collecting for a 'single quoted' string
    let mut dq = false;
    let mut sq = false;
    let mut chars = s.chars().peekable();

    while let Some(ch) = chars.next() {
        match ch {
            '\'' if !dq => {
                if !sq {
                    sq = true;
                    literal_sq = Some(String::new());
                } else {
                    // closing single quote
                    let lit = literal_sq.take().unwrap_or_default();
                    toks.push(Token::Single(lit));
                    sq = false;
                }
            }
            '"' if !sq => {
                dq = !dq;
                if dq {
                    // opening double quote -> nothing special here
                } else {
                    // closing -> flush current Interp pieces as a token if any
                    if !cur.is_empty() {
                        toks.push(Token::Interp(std::mem::take(&mut cur)));
                    }
                }
            }
            c if sq => {
                // inside single quotes, no expansions
                if let Some(ref mut lit) = literal_sq {
                    lit.push(c);
                }
            }
            c if dq => {
                match c {
                    '$' => {
                        if let Some((var, consumed)) = read_var(&mut chars) {
                            cur.push(Piece::Var(var));
                            // consumed handled inside read_var
                            let _ = consumed;
                        } else {
                            cur.push(Piece::Text("$".into()));
                        }
                    }
                    _ => cur.push(Piece::Text(c.to_string())),
                }
            }
            c if c.is_whitespace() => {
                // token boundary (only if we have pieces pending)
                if !cur.is_empty() {
                    toks.push(Token::Interp(std::mem::take(&mut cur)));
                }
            }
            '$' => {
                if let Some((var, consumed)) = read_var(&mut chars) {
                    cur.push(Piece::Var(var));
                    let _ = consumed;
                } else {
                    cur.push(Piece::Text("$".into()));
                }
            }
            _ => {
                cur.push(Piece::Text(ch.to_string()));
            }
        }
    }

    // finalize last pieces if present
    if sq {
        // unterminated single quote: treat everything as literal
        let lit = literal_sq.take().unwrap_or_default();
        toks.push(Token::Single(lit));
    }
    if !cur.is_empty() {
        toks.push(Token::Interp(cur));
    }

    Ok(toks)
}

fn parse_single_token(s: &str) -> Result<Token> {
    // Parse a single token that may be quoted; reuse the splitting and expect exactly one token.
    let toks = split_shell_words(s)?;
    if toks.len() == 1 {
        Ok(toks
            .into_iter()
            .next()
            .ok_or_else(|| anyhow!("Expected single token in bash value"))?)
    } else {
        Err(anyhow!("expected single token, got {}", toks.len()))
    }
}

/* =============================================================================
Rendering Aether code
============================================================================= */

fn render_pipeline(cmds: &[Command]) -> String {
    let mut parts = Vec::new();
    for (i, c) in cmds.iter().enumerate() {
        let is_first = i == 0;
        parts.push(render_command(c, is_first));
    }
    parts.join(" | ")
}

fn render_command(cmd: &Command, _is_first: bool) -> String {
    // If the program can be mapped to an Aether builtin, render directly; else `sh([...])`.
    let prog_name = token_to_plain_word(&cmd.program);

    if let Some(name) = prog_name.as_deref() {
        if let Some(builtin) = map_builtin(name) {
            let args = cmd
                .args
                .iter()
                .map(render_arg_expr)
                .collect::<Vec<_>>()
                .join(", ");
            return if args.is_empty() {
                format!("{}()", builtin)
            } else {
                format!("{}({})", builtin, args)
            };
        }
    }

    // External: sh(["prog","arg1","arg2", ...])
    render_external_call(cmd)
}

fn render_external_call(cmd: &Command) -> String {
    let mut arr_items = Vec::new();

    // Program token as a string literal if we can't get a simple word;
    // otherwise use its text directly (still a string literal).
    let prog_text = match token_to_plain_word(&cmd.program) {
        Some(s) => s,
        None => token_to_string(&cmd.program),
    };
    arr_items.push(json_string_literal(&prog_text));

    for a in &cmd.args {
        arr_items.push(json_string_literal(&token_to_string(a)));
    }
    format!("sh([{}])", arr_items.join(", "))
}

fn render_arg_expr(tok: &Token) -> String {
    match tok {
        Token::Single(s) => double_quote_with_escapes(s),
        Token::Interp(pcs) => {
            // If it's exactly one Var, render as an identifier (VAR) not a string.
            if pcs.len() == 1 {
                if let Piece::Var(v) = &pcs[0] {
                    if is_ident(v) {
                        return v.clone();
                    }
                }
            }
            // Otherwise, produce an interpolated string: "text${VAR}more"
            let mut s = String::from("\"");
            for p in pcs {
                match p {
                    Piece::Text(t) => s.push_str(&escape_in_double_quotes(t)),
                    Piece::Var(v) => {
                        s.push_str("${");
                        s.push_str(v);
                        s.push('}');
                    }
                }
            }
            s.push('"');
            s
        }
    }
}

/* =============================================================================
Utilities: token → strings
============================================================================= */

/// If the token is a simple plain word w/o expansions and not quoted, return it; else None.
fn token_to_plain_word(tok: &Token) -> Option<String> {
    match tok {
        Token::Single(_) => None,
        Token::Interp(pcs) => {
            // plain word if all pieces are Text and none contain spaces
            if pcs.iter().all(|p| matches!(p, Piece::Text(_))) {
                let s = pcs
                    .iter()
                    .map(|p| match p {
                        Piece::Text(t) => t,
                        _ => unreachable!(),
                    })
                    .cloned()
                    .collect::<String>();
                if !s.is_empty() && !s.chars().any(|c| c.is_whitespace()) {
                    return Some(s);
                }
            }
            None
        }
    }
}

/// Turn a token into a string by evaluating only *static* parts; variables become literal `$VAR`.
fn token_to_string(tok: &Token) -> String {
    match tok {
        Token::Single(s) => s.clone(),
        Token::Interp(pcs) => {
            let mut s = String::new();
            for p in pcs {
                match p {
                    Piece::Text(t) => s.push_str(t),
                    Piece::Var(v) => {
                        s.push('$');
                        s.push_str(v);
                    }
                }
            }
            s
        }
    }
}

/* =============================================================================
Small lex helpers
============================================================================= */

fn read_var(chars: &mut std::iter::Peekable<std::str::Chars<'_>>) -> Option<(String, usize)> {
    // Supports $NAME and ${NAME}; returns (name, consumed_chars) excluding the leading '$'
    let mut consumed = 0usize;
    if let Some('{') = chars.peek().copied() {
        // ${NAME}
        let _ = chars.next(); // consume '{'
        consumed += 1;
        let mut name = String::new();
        while let Some(&ch) = chars.peek() {
            consumed += 1;
            let _ = chars.next();
            if ch == '}' {
                break;
            }
            name.push(ch);
        }
        if name.is_empty() {
            return None;
        }
        Some((name, consumed))
    } else {
        // $NAME
        let mut name = String::new();
        while let Some(&ch) = chars.peek() {
            if ch == '_' || ch.is_ascii_alphanumeric() {
                name.push(ch);
                let _ = chars.next();
                consumed += 1;
            } else {
                break;
            }
        }
        if name.is_empty() {
            None
        } else {
            Some((name, consumed))
        }
    }
}

fn parse_var_ref(s: &str) -> Option<String> {
    if let Some(rest) = s.strip_prefix("${") {
        if let Some(end) = rest.find('}') {
            let name = &rest[..end];
            if is_ident(name) {
                return Some(name.to_string());
            }
        }
        None
    } else if let Some(name) = s.strip_prefix('$') {
        if is_ident(name) {
            Some(name.to_string())
        } else {
            None
        }
    } else {
        None
    }
}

fn is_ident(s: &str) -> bool {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) if c == '_' || c.is_ascii_alphabetic() => {}
        _ => return false,
    }
    for ch in chars {
        if !(ch == '_' || ch.is_ascii_alphanumeric()) {
            return false;
        }
    }
    true
}

fn is_int_literal(s: &str) -> bool {
    !s.is_empty() && s.chars().all(|c| c.is_ascii_digit())
}

fn is_float_literal(s: &str) -> bool {
    // very loose
    s.contains('.')
        && s.chars().filter(|c| *c == '.').count() == 1
        && s.chars().all(|c| c.is_ascii_digit() || c == '.')
}

/* =============================================================================
Quoting helpers
============================================================================= */

fn single_quote_literal(s: &str) -> String {
    // Aether strings are double-quoted; but for array-to-json-ish we need JSON-like "..." too.
    // We'll produce a JSON string literal here for `sh([...])`.
    json_string_literal(s)
}

fn json_string_literal(s: &str) -> String {
    // Simple JSON string escaper for ASCII-oriented input.
    let mut out = String::from("\"");
    for ch in s.chars() {
        match ch {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if c.is_control() => {
                out.push_str(&format!("\\u{:04x}", c as u32));
            }
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

fn double_quote_with_escapes(s: &str) -> String {
    // Aether string literal with minimal escapes, *no* interpolation.
    let mut out = String::from("\"");
    for ch in s.chars() {
        match ch {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

fn escape_in_double_quotes(s: &str) -> String {
    let mut out = String::new();
    for ch in s.chars() {
        match ch {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            _ => out.push(ch),
        }
    }
    out
}

/* =============================================================================
Builtin mapping
============================================================================= */

fn map_builtin(name: &str) -> Option<&'static str> {
    // Extend as Aether adds more native builtins.
    match name {
        "echo" => Some("echo"),
        "pwd" => Some("pwd"),
        "cd" => Some("cd"),
        // "cat" => Some("read_text"), // optional: would change behavior (returns text)
        _ => None,
    }
}