kaish-kernel 0.8.1

Core kernel for kaish: lexer, parser, interpreter, and runtime
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
//! S-expression formatter for kaish AST.
//!
//! Converts AST nodes to the S-expression format used in test snapshots.
//! S-expressions provide a stable, readable format that's easier to diff
//! than Debug output.

use super::*;

/// Format a Program as an S-expression.
/// For single-statement programs, formats just the statement.
/// For multi-statement programs, formats as a sequence.
pub fn format_program(program: &Program) -> String {
    let stmts: Vec<_> = program
        .statements
        .iter()
        .filter(|s| !matches!(s, Stmt::Empty))
        .collect();

    match stmts.len() {
        0 => "(program)".to_string(),
        1 => format_stmt(stmts[0]),
        _ => {
            let parts: Vec<String> = stmts.iter().map(|s| format_stmt(s)).collect();
            format!("(program {})", parts.join(" "))
        }
    }
}

/// Format a statement as an S-expression.
pub fn format_stmt(stmt: &Stmt) -> String {
    match stmt {
        Stmt::Assignment(a) => format_assignment(a),
        Stmt::Command(cmd) => format_command(cmd),
        Stmt::Pipeline(p) => format_pipeline(p),
        Stmt::If(if_stmt) => format_if(if_stmt),
        Stmt::For(for_loop) => format_for(for_loop),
        Stmt::While(while_loop) => format_while(while_loop),
        Stmt::Case(case_stmt) => format_case(case_stmt),
        Stmt::Break(n) => match n {
            Some(level) => format!("(break {})", level),
            None => "(break)".to_string(),
        },
        Stmt::Continue(n) => match n {
            Some(level) => format!("(continue {})", level),
            None => "(continue)".to_string(),
        },
        Stmt::Return(expr) => match expr {
            Some(e) => format!("(return {})", format_expr(e)),
            None => "(return)".to_string(),
        },
        Stmt::Exit(expr) => match expr {
            Some(e) => format!("(exit {})", format_expr(e)),
            None => "(exit)".to_string(),
        },
        Stmt::ToolDef(tool) => format_tooldef(tool),
        Stmt::Test(test_expr) => format!("(test {})", format_test_expr(test_expr)),
        Stmt::AndChain { left, right } => {
            format!("(and-chain {} {})", format_stmt(left), format_stmt(right))
        }
        Stmt::OrChain { left, right } => {
            format!("(or-chain {} {})", format_stmt(left), format_stmt(right))
        }
        Stmt::Empty => "(empty)".to_string(),
    }
}

/// Format an assignment as an S-expression.
fn format_assignment(a: &Assignment) -> String {
    let value = format_expr(&a.value);
    format!("(assign {} {} local={})", a.name, value, a.local)
}

/// Format a command as an S-expression.
pub fn format_command(cmd: &Command) -> String {
    let mut parts = vec![format!("(cmd {}", cmd.name)];

    for arg in &cmd.args {
        parts.push(format_arg(arg));
    }

    for redir in &cmd.redirects {
        parts.push(format_redirect(redir));
    }

    format!("{})", parts.join(" "))
}

/// Format an argument as an S-expression.
fn format_arg(arg: &Arg) -> String {
    match arg {
        Arg::Positional(expr) => format!("(pos {})", format_expr(expr)),
        Arg::Named { key, value } => format!("(named {} {})", key, format_expr(value)),
        Arg::WordAssign { key, value } => format!("(wordassign {} {})", key, format_expr(value)),
        Arg::ShortFlag(f) => format!("(shortflag {})", f),
        Arg::LongFlag(f) => format!("(longflag {})", f),
        Arg::DoubleDash => "(doubledash)".to_string(),
    }
}

/// Format a redirect as an S-expression.
fn format_redirect(redir: &Redirect) -> String {
    let kind = match redir.kind {
        RedirectKind::StdoutOverwrite => ">",
        RedirectKind::StdoutAppend => ">>",
        RedirectKind::Stdin => "<",
        RedirectKind::HereDoc => "<<",
        RedirectKind::HereString => "<<<",
        RedirectKind::Stderr => "2>",
        RedirectKind::Both => "&>",
        RedirectKind::MergeStderr => "2>&1",
        RedirectKind::MergeStdout => "1>&2",
    };
    format!("(redir {} {})", kind, format_expr(&redir.target))
}

/// Format a pipeline as an S-expression.
pub fn format_pipeline(p: &Pipeline) -> String {
    let cmds: Vec<String> = p.commands.iter().map(format_command).collect();

    if p.background {
        if cmds.len() == 1 {
            format!("(background {})", cmds[0])
        } else {
            format!("(background (pipeline {}))", cmds.join(" "))
        }
    } else {
        format!("(pipeline {})", cmds.join(" "))
    }
}

/// Format an if statement as an S-expression.
fn format_if(if_stmt: &IfStmt) -> String {
    let cond = format_expr(&if_stmt.condition);
    let then_stmts: Vec<String> = if_stmt
        .then_branch
        .iter()
        .filter(|s| !matches!(s, Stmt::Empty))
        .map(format_stmt)
        .collect();
    let then_part = format!("(then {})", then_stmts.join(" "));

    match &if_stmt.else_branch {
        Some(else_stmts) => {
            let else_inner: Vec<String> = else_stmts
                .iter()
                .filter(|s| !matches!(s, Stmt::Empty))
                .map(format_stmt)
                .collect();
            if else_inner.is_empty() {
                format!("(if {} {} (else))", cond, then_part)
            } else {
                format!("(if {} {} (else {}))", cond, then_part, else_inner.join(" "))
            }
        }
        None => format!("(if {} {} (else))", cond, then_part),
    }
}

/// Format a for loop as an S-expression.
fn format_for(for_loop: &ForLoop) -> String {
    let items: Vec<String> = for_loop.items.iter().map(format_expr).collect();
    let body_stmts: Vec<String> = for_loop
        .body
        .iter()
        .filter(|s| !matches!(s, Stmt::Empty))
        .map(format_stmt)
        .collect();
    format!(
        "(for {} (in {}) (do {}))",
        for_loop.variable,
        items.join(" "),
        body_stmts.join(" ")
    )
}

/// Format a while loop as an S-expression.
fn format_while(while_loop: &WhileLoop) -> String {
    let cond = format_expr(&while_loop.condition);
    let body_stmts: Vec<String> = while_loop
        .body
        .iter()
        .filter(|s| !matches!(s, Stmt::Empty))
        .map(format_stmt)
        .collect();
    format!("(while {} (do {}))", cond, body_stmts.join(" "))
}

/// Format a case statement as an S-expression.
fn format_case(case_stmt: &CaseStmt) -> String {
    let expr = format_expr(&case_stmt.expr);
    let branches: Vec<String> = case_stmt
        .branches
        .iter()
        .map(format_case_branch)
        .collect();
    format!("(case {} ({}))", expr, branches.join(" "))
}

/// Format a case branch as an S-expression.
fn format_case_branch(branch: &CaseBranch) -> String {
    let patterns = branch.patterns.join("|");
    let body_stmts: Vec<String> = branch
        .body
        .iter()
        .filter(|s| !matches!(s, Stmt::Empty))
        .map(format_stmt)
        .collect();
    format!("(branch \"{}\" ({}))", patterns, body_stmts.join(" "))
}

/// Format a tool definition as an S-expression.
fn format_tooldef(tool: &ToolDef) -> String {
    let params: Vec<String> = tool.params.iter().map(format_param).collect();
    let body_stmts: Vec<String> = tool
        .body
        .iter()
        .filter(|s| !matches!(s, Stmt::Empty))
        .map(format_stmt)
        .collect();
    format!(
        "(tooldef {} ({}) ({}))",
        tool.name,
        params.join(" "),
        body_stmts.join(" ")
    )
}

/// Format a parameter definition as an S-expression.
fn format_param(param: &ParamDef) -> String {
    let type_str = param
        .param_type
        .as_ref()
        .map(|t| match t {
            ParamType::String => "string",
            ParamType::Int => "int",
            ParamType::Float => "float",
            ParamType::Bool => "bool",
        })
        .unwrap_or("any");

    match &param.default {
        Some(default) => format!("(param {} {} {})", param.name, type_str, format_expr(default)),
        None => format!("(param {} {})", param.name, type_str),
    }
}

/// Format an expression as an S-expression.
pub fn format_expr(expr: &Expr) -> String {
    match expr {
        Expr::Literal(value) => format_value(value),
        Expr::VarRef(path) => format!("(varref {})", format_varpath(path)),
        Expr::Interpolated(parts) => {
            let parts_str: Vec<String> = parts
                .iter()
                .map(format_string_part)
                .collect();
            format!("(interpolated {})", parts_str.join(" "))
        }
        Expr::HereDocBody { parts, strip_tabs } => {
            let parts_str: Vec<String> = parts
                .iter()
                .map(|sp| format_string_part(&sp.part))
                .collect();
            format!(
                "(heredoc-body strip-tabs={} {})",
                strip_tabs,
                parts_str.join(" ")
            )
        }
        Expr::BinaryOp { left, op, right } => {
            let op_str = match op {
                BinaryOp::And => "and",
                BinaryOp::Or => "or",
            };
            format!("({} {} {})", op_str, format_expr(left), format_expr(right))
        }
        Expr::CommandSubst(pipeline) => {
            format!("(cmdsubst {})", format_pipeline(pipeline))
        }
        Expr::Test(test_expr) => format!("(test {})", format_test_expr(test_expr)),
        Expr::Positional(n) => format!("(positional {})", n),
        Expr::AllArgs => "(all-args)".to_string(),
        Expr::ArgCount => "(arg-count)".to_string(),
        Expr::VarLength(name) => format!("(var-length {})", name),
        Expr::VarWithDefault { name, default } => {
            let default_parts: Vec<String> = default.iter().map(format_string_part).collect();
            format!("(var-default {} ({}))", name, default_parts.join(" "))
        }
        Expr::Arithmetic(expr_str) => format!("(arithmetic \"{}\")", expr_str),
        Expr::Command(cmd) => format_command(cmd),
        Expr::LastExitCode => "(last-exit-code)".to_string(),
        Expr::CurrentPid => "(current-pid)".to_string(),
        Expr::GlobPattern(s) => format!("(glob \"{}\")", s),
    }
}

/// Format a test expression as an S-expression.
pub fn format_test_expr(test: &TestExpr) -> String {
    match test {
        TestExpr::FileTest { op, path } => {
            let op_str = match op {
                FileTestOp::Exists => "-e",
                FileTestOp::IsFile => "-f",
                FileTestOp::IsDir => "-d",
                FileTestOp::Readable => "-r",
                FileTestOp::Writable => "-w",
                FileTestOp::Executable => "-x",
            };
            format!("(file {} {})", op_str, format_expr(path))
        }
        TestExpr::StringTest { op, value } => {
            let op_str = match op {
                StringTestOp::IsEmpty => "-z",
                StringTestOp::IsNonEmpty => "-n",
            };
            format!("(string {} {})", op_str, format_expr(value))
        }
        TestExpr::Comparison { left, op, right } => {
            let op_str = match op {
                TestCmpOp::Eq => "==",
                TestCmpOp::NotEq => "!=",
                TestCmpOp::Match => "=~",
                TestCmpOp::NotMatch => "!~",
                TestCmpOp::Gt => ">",
                TestCmpOp::Lt => "<",
                TestCmpOp::GtEq => ">=",
                TestCmpOp::LtEq => "<=",
                TestCmpOp::NumEq => "-eq",
                TestCmpOp::NumNotEq => "-ne",
                TestCmpOp::NumGt => "-gt",
                TestCmpOp::NumLt => "-lt",
                TestCmpOp::NumGtEq => "-ge",
                TestCmpOp::NumLtEq => "-le",
            };
            format!(
                "(cmp {} {} {})",
                op_str,
                format_expr(left),
                format_expr(right)
            )
        }
        TestExpr::And { left, right } => {
            format!("(and {} {})", format_test_expr(left), format_test_expr(right))
        }
        TestExpr::Or { left, right } => {
            format!("(or {} {})", format_test_expr(left), format_test_expr(right))
        }
        TestExpr::Not { expr } => {
            format!("(not {})", format_test_expr(expr))
        }
    }
}

/// Format a StringPart as an S-expression.
fn format_string_part(part: &StringPart) -> String {
    match part {
        StringPart::Literal(s) => format!("\"{}\"", escape_for_display(s)),
        StringPart::Var(path) => format!("(varref {})", format_varpath(path)),
        StringPart::VarWithDefault { name, default } => {
            let default_parts: Vec<String> = default.iter().map(format_string_part).collect();
            format!("(vardefault {} ({}))", name, default_parts.join(" "))
        }
        StringPart::VarLength(name) => format!("(varlength {})", name),
        StringPart::Positional(n) => format!("(positional {})", n),
        StringPart::AllArgs => "(allargs)".to_string(),
        StringPart::ArgCount => "(argcount)".to_string(),
        StringPart::Arithmetic(expr) => format!("(arith \"{}\")", expr),
        StringPart::CommandSubst(pipeline) => format!("(cmdsubst {})", format_pipeline(pipeline)),
        StringPart::LastExitCode => "(last-exit-code)".to_string(),
        StringPart::CurrentPid => "(current-pid)".to_string(),
    }
}

/// Escape control characters for display in test output.
fn escape_for_display(s: &str) -> String {
    s.replace('\n', "\\n")
        .replace('\t', "\\t")
        .replace('\r', "\\r")
}

/// Format a value as an S-expression.
pub fn format_value(value: &Value) -> String {
    match value {
        Value::Null => "(null)".to_string(),
        Value::Bool(b) => format!("(bool {})", b),
        Value::Int(n) => format!("(int {})", n),
        Value::Float(f) => format!("(float {})", f),
        Value::String(s) => format!("(string \"{}\")", escape_for_display(s)),
        Value::Json(json) => format!("(json {})", json),
        Value::Blob(blob) => format!("(blob id={} size={} type={})", blob.id, blob.size, blob.content_type),
    }
}

/// Format a variable path as an S-expression.
pub fn format_varpath(path: &VarPath) -> String {
    path.segments
        .iter()
        .map(|seg| match seg {
            VarSegment::Field(name) => name.clone(),
        })
        .collect::<Vec<_>>()
        .join(".")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format_simple_int() {
        assert_eq!(format_value(&Value::Int(42)), "(int 42)");
    }

    #[test]
    fn format_simple_string() {
        assert_eq!(format_value(&Value::String("hello".to_string())), "(string \"hello\")");
    }

    #[test]
    fn format_varpath_simple() {
        let path = VarPath::simple("X");
        assert_eq!(format_varpath(&path), "X");
    }

    #[test]
    fn format_varpath_nested() {
        let path = VarPath {
            segments: vec![
                VarSegment::Field("VAR".to_string()),
                VarSegment::Field("field".to_string()),
            ],
        };
        assert_eq!(format_varpath(&path), "VAR.field");
    }
}