Skip to main content

kaish_kernel/ast/
sexpr.rs

1//! S-expression formatter for kaish AST.
2//!
3//! Converts AST nodes to the S-expression format used in test snapshots.
4//! S-expressions provide a stable, readable format that's easier to diff
5//! than Debug output.
6
7use super::*;
8
9/// Format a Program as an S-expression.
10/// For single-statement programs, formats just the statement.
11/// For multi-statement programs, formats as a sequence.
12pub fn format_program(program: &Program) -> String {
13    let stmts: Vec<_> = program
14        .statements
15        .iter()
16        .filter(|s| !matches!(s, Stmt::Empty))
17        .collect();
18
19    match stmts.len() {
20        0 => "(program)".to_string(),
21        1 => format_stmt(stmts[0]),
22        _ => {
23            let parts: Vec<String> = stmts.iter().map(|s| format_stmt(s)).collect();
24            format!("(program {})", parts.join(" "))
25        }
26    }
27}
28
29/// Format a statement as an S-expression.
30pub fn format_stmt(stmt: &Stmt) -> String {
31    match stmt {
32        Stmt::Assignment(a) => format_assignment(a),
33        Stmt::Command(cmd) => format_command(cmd),
34        Stmt::Pipeline(p) => format_pipeline(p),
35        Stmt::If(if_stmt) => format_if(if_stmt),
36        Stmt::For(for_loop) => format_for(for_loop),
37        Stmt::While(while_loop) => format_while(while_loop),
38        Stmt::Case(case_stmt) => format_case(case_stmt),
39        Stmt::Break(n) => match n {
40            Some(level) => format!("(break {})", level),
41            None => "(break)".to_string(),
42        },
43        Stmt::Continue(n) => match n {
44            Some(level) => format!("(continue {})", level),
45            None => "(continue)".to_string(),
46        },
47        Stmt::Return(expr) => match expr {
48            Some(e) => format!("(return {})", format_expr(e)),
49            None => "(return)".to_string(),
50        },
51        Stmt::Exit(expr) => match expr {
52            Some(e) => format!("(exit {})", format_expr(e)),
53            None => "(exit)".to_string(),
54        },
55        Stmt::ToolDef(tool) => format_tooldef(tool),
56        Stmt::Test(test_expr) => format!("(test {})", format_test_expr(test_expr)),
57        Stmt::AndChain { left, right } => {
58            format!("(and-chain {} {})", format_stmt(left), format_stmt(right))
59        }
60        Stmt::OrChain { left, right } => {
61            format!("(or-chain {} {})", format_stmt(left), format_stmt(right))
62        }
63        Stmt::EnvScoped { assignments, body } => {
64            let assigns: Vec<String> = assignments.iter().map(format_assignment).collect();
65            format!("(env-scoped ({}) {})", assigns.join(" "), format_stmt(body))
66        }
67        Stmt::Empty => "(empty)".to_string(),
68    }
69}
70
71/// Format an assignment as an S-expression.
72fn format_assignment(a: &Assignment) -> String {
73    let value = format_expr(&a.value);
74    format!("(assign {} {} local={})", a.name, value, a.local)
75}
76
77/// Format a command as an S-expression.
78pub fn format_command(cmd: &Command) -> String {
79    let mut parts = vec![format!("(cmd {}", cmd.name)];
80
81    for arg in &cmd.args {
82        parts.push(format_arg(arg));
83    }
84
85    for redir in &cmd.redirects {
86        parts.push(format_redirect(redir));
87    }
88
89    format!("{})", parts.join(" "))
90}
91
92/// Format an argument as an S-expression.
93fn format_arg(arg: &Arg) -> String {
94    match arg {
95        Arg::Positional(expr) => format!("(pos {})", format_expr(expr)),
96        Arg::Named { key, value } => format!("(named {} {})", key, format_expr(value)),
97        Arg::WordAssign { key, value } => format!("(wordassign {} {})", key, format_expr(value)),
98        Arg::ShortFlag(f) => format!("(shortflag {})", f),
99        Arg::LongFlag(f) => format!("(longflag {})", f),
100        Arg::DoubleDash => "(doubledash)".to_string(),
101    }
102}
103
104/// Format a redirect as an S-expression.
105fn format_redirect(redir: &Redirect) -> String {
106    let kind = match redir.kind {
107        RedirectKind::StdoutOverwrite => ">",
108        RedirectKind::StdoutAppend => ">>",
109        RedirectKind::Stdin => "<",
110        RedirectKind::HereDoc => "<<",
111        RedirectKind::HereString => "<<<",
112        RedirectKind::Stderr => "2>",
113        RedirectKind::Both => "&>",
114        RedirectKind::MergeStderr => "2>&1",
115        RedirectKind::MergeStdout => "1>&2",
116    };
117    format!("(redir {} {})", kind, format_expr(&redir.target))
118}
119
120/// Format a pipeline as an S-expression.
121/// Format a command-substitution body — a block of statements — as an s-expr.
122/// A one-statement block formats as that statement; multiple are wrapped in a
123/// `(block …)` so the sequence is visible in snapshots.
124pub fn format_stmt_block(stmts: &[Stmt]) -> String {
125    if stmts.len() == 1 {
126        format_stmt(&stmts[0])
127    } else {
128        let inner: Vec<String> = stmts.iter().map(format_stmt).collect();
129        format!("(block {})", inner.join(" "))
130    }
131}
132
133pub fn format_pipeline(p: &Pipeline) -> String {
134    let cmds: Vec<String> = p.commands.iter().map(format_command).collect();
135
136    if p.background {
137        if cmds.len() == 1 {
138            format!("(background {})", cmds[0])
139        } else {
140            format!("(background (pipeline {}))", cmds.join(" "))
141        }
142    } else {
143        format!("(pipeline {})", cmds.join(" "))
144    }
145}
146
147/// Format an if statement as an S-expression.
148fn format_if(if_stmt: &IfStmt) -> String {
149    let cond = format_expr(&if_stmt.condition);
150    let then_stmts: Vec<String> = if_stmt
151        .then_branch
152        .iter()
153        .filter(|s| !matches!(s, Stmt::Empty))
154        .map(format_stmt)
155        .collect();
156    let then_part = format!("(then {})", then_stmts.join(" "));
157
158    match &if_stmt.else_branch {
159        Some(else_stmts) => {
160            let else_inner: Vec<String> = else_stmts
161                .iter()
162                .filter(|s| !matches!(s, Stmt::Empty))
163                .map(format_stmt)
164                .collect();
165            if else_inner.is_empty() {
166                format!("(if {} {} (else))", cond, then_part)
167            } else {
168                format!("(if {} {} (else {}))", cond, then_part, else_inner.join(" "))
169            }
170        }
171        None => format!("(if {} {} (else))", cond, then_part),
172    }
173}
174
175/// Format a for loop as an S-expression.
176fn format_for(for_loop: &ForLoop) -> String {
177    let items: Vec<String> = for_loop.items.iter().map(format_expr).collect();
178    let body_stmts: Vec<String> = for_loop
179        .body
180        .iter()
181        .filter(|s| !matches!(s, Stmt::Empty))
182        .map(format_stmt)
183        .collect();
184    format!(
185        "(for {} (in {}) (do {}))",
186        for_loop.variable,
187        items.join(" "),
188        body_stmts.join(" ")
189    )
190}
191
192/// Format a while loop as an S-expression.
193fn format_while(while_loop: &WhileLoop) -> String {
194    let cond = format_expr(&while_loop.condition);
195    let body_stmts: Vec<String> = while_loop
196        .body
197        .iter()
198        .filter(|s| !matches!(s, Stmt::Empty))
199        .map(format_stmt)
200        .collect();
201    format!("(while {} (do {}))", cond, body_stmts.join(" "))
202}
203
204/// Format a case statement as an S-expression.
205fn format_case(case_stmt: &CaseStmt) -> String {
206    let expr = format_expr(&case_stmt.expr);
207    let branches: Vec<String> = case_stmt
208        .branches
209        .iter()
210        .map(format_case_branch)
211        .collect();
212    format!("(case {} ({}))", expr, branches.join(" "))
213}
214
215/// Format a case branch as an S-expression.
216fn format_case_branch(branch: &CaseBranch) -> String {
217    let patterns = branch.patterns.join("|");
218    let body_stmts: Vec<String> = branch
219        .body
220        .iter()
221        .filter(|s| !matches!(s, Stmt::Empty))
222        .map(format_stmt)
223        .collect();
224    format!("(branch \"{}\" ({}))", patterns, body_stmts.join(" "))
225}
226
227/// Format a tool definition as an S-expression.
228fn format_tooldef(tool: &ToolDef) -> String {
229    let params: Vec<String> = tool.params.iter().map(format_param).collect();
230    let body_stmts: Vec<String> = tool
231        .body
232        .iter()
233        .filter(|s| !matches!(s, Stmt::Empty))
234        .map(format_stmt)
235        .collect();
236    format!(
237        "(tooldef {} ({}) ({}))",
238        tool.name,
239        params.join(" "),
240        body_stmts.join(" ")
241    )
242}
243
244/// Format a parameter definition as an S-expression.
245fn format_param(param: &ParamDef) -> String {
246    let type_str = param
247        .param_type
248        .as_ref()
249        .map(|t| match t {
250            ParamType::String => "string",
251            ParamType::Int => "int",
252            ParamType::Float => "float",
253            ParamType::Bool => "bool",
254        })
255        .unwrap_or("any");
256
257    match &param.default {
258        Some(default) => format!("(param {} {} {})", param.name, type_str, format_expr(default)),
259        None => format!("(param {} {})", param.name, type_str),
260    }
261}
262
263/// Format an expression as an S-expression.
264pub fn format_expr(expr: &Expr) -> String {
265    match expr {
266        Expr::Literal(value) => format_value(value),
267        Expr::VarRef(path) => format!("(varref {})", format_varpath(path)),
268        Expr::Interpolated(parts) => {
269            let parts_str: Vec<String> = parts
270                .iter()
271                .map(format_string_part)
272                .collect();
273            format!("(interpolated {})", parts_str.join(" "))
274        }
275        Expr::HereDocBody { parts, strip_tabs } => {
276            let parts_str: Vec<String> = parts
277                .iter()
278                .map(|sp| format_string_part(&sp.part))
279                .collect();
280            format!(
281                "(heredoc-body strip-tabs={} {})",
282                strip_tabs,
283                parts_str.join(" ")
284            )
285        }
286        Expr::BinaryOp { left, op, right } => {
287            let op_str = match op {
288                BinaryOp::And => "and",
289                BinaryOp::Or => "or",
290            };
291            format!("({} {} {})", op_str, format_expr(left), format_expr(right))
292        }
293        Expr::CommandSubst(stmts) => {
294            format!("(cmdsubst {})", format_stmt_block(stmts))
295        }
296        Expr::Test(test_expr) => format!("(test {})", format_test_expr(test_expr)),
297        Expr::Positional(n) => format!("(positional {})", n),
298        Expr::AllArgs => "(all-args)".to_string(),
299        Expr::ArgCount => "(arg-count)".to_string(),
300        Expr::VarLength(name) => format!("(var-length {})", name),
301        Expr::VarWithDefault { name, default } => {
302            let default_parts: Vec<String> = default.iter().map(format_string_part).collect();
303            format!("(var-default {} ({}))", name, default_parts.join(" "))
304        }
305        Expr::Arithmetic(expr_str) => format!("(arithmetic \"{}\")", expr_str),
306        Expr::Command(cmd) => format_command(cmd),
307        Expr::LastExitCode => "(last-exit-code)".to_string(),
308        Expr::CurrentPid => "(current-pid)".to_string(),
309        Expr::GlobPattern(s) => format!("(glob \"{}\")", s),
310    }
311}
312
313/// Format a test expression as an S-expression.
314pub fn format_test_expr(test: &TestExpr) -> String {
315    match test {
316        TestExpr::FileTest { op, path } => {
317            let op_str = match op {
318                FileTestOp::Exists => "-e",
319                FileTestOp::IsFile => "-f",
320                FileTestOp::IsDir => "-d",
321                FileTestOp::Readable => "-r",
322                FileTestOp::Writable => "-w",
323                FileTestOp::Executable => "-x",
324            };
325            format!("(file {} {})", op_str, format_expr(path))
326        }
327        TestExpr::StringTest { op, value } => {
328            let op_str = match op {
329                StringTestOp::IsEmpty => "-z",
330                StringTestOp::IsNonEmpty => "-n",
331            };
332            format!("(string {} {})", op_str, format_expr(value))
333        }
334        TestExpr::Comparison { left, op, right } => {
335            let op_str = match op {
336                TestCmpOp::Eq => "==",
337                TestCmpOp::NotEq => "!=",
338                TestCmpOp::Match => "=~",
339                TestCmpOp::NotMatch => "!~",
340                TestCmpOp::Gt => ">",
341                TestCmpOp::Lt => "<",
342                TestCmpOp::GtEq => ">=",
343                TestCmpOp::LtEq => "<=",
344                TestCmpOp::NumEq => "-eq",
345                TestCmpOp::NumNotEq => "-ne",
346                TestCmpOp::NumGt => "-gt",
347                TestCmpOp::NumLt => "-lt",
348                TestCmpOp::NumGtEq => "-ge",
349                TestCmpOp::NumLtEq => "-le",
350            };
351            format!(
352                "(cmp {} {} {})",
353                op_str,
354                format_expr(left),
355                format_expr(right)
356            )
357        }
358        TestExpr::And { left, right } => {
359            format!("(and {} {})", format_test_expr(left), format_test_expr(right))
360        }
361        TestExpr::Or { left, right } => {
362            format!("(or {} {})", format_test_expr(left), format_test_expr(right))
363        }
364        TestExpr::Not { expr } => {
365            format!("(not {})", format_test_expr(expr))
366        }
367    }
368}
369
370/// Format a StringPart as an S-expression.
371fn format_string_part(part: &StringPart) -> String {
372    match part {
373        StringPart::Literal(s) => format!("\"{}\"", escape_for_display(s)),
374        StringPart::Var(path) => format!("(varref {})", format_varpath(path)),
375        StringPart::VarWithDefault { name, default } => {
376            let default_parts: Vec<String> = default.iter().map(format_string_part).collect();
377            format!("(vardefault {} ({}))", name, default_parts.join(" "))
378        }
379        StringPart::VarLength(name) => format!("(varlength {})", name),
380        StringPart::Positional(n) => format!("(positional {})", n),
381        StringPart::AllArgs => "(allargs)".to_string(),
382        StringPart::ArgCount => "(argcount)".to_string(),
383        StringPart::Arithmetic(expr) => format!("(arith \"{}\")", expr),
384        StringPart::CommandSubst(stmts) => format!("(cmdsubst {})", format_stmt_block(stmts)),
385        StringPart::LastExitCode => "(last-exit-code)".to_string(),
386        StringPart::CurrentPid => "(current-pid)".to_string(),
387    }
388}
389
390/// Escape control characters for display in test output.
391fn escape_for_display(s: &str) -> String {
392    s.replace('\n', "\\n")
393        .replace('\t', "\\t")
394        .replace('\r', "\\r")
395}
396
397/// Format a value as an S-expression.
398pub fn format_value(value: &Value) -> String {
399    match value {
400        Value::Null => "(null)".to_string(),
401        Value::Bool(b) => format!("(bool {})", b),
402        Value::Int(n) => format!("(int {})", n),
403        Value::Float(f) => format!("(float {})", f),
404        Value::String(s) => format!("(string \"{}\")", escape_for_display(s)),
405        Value::Json(json) => format!("(json {})", json),
406        Value::Blob(blob) => format!("(blob id={} size={} type={})", blob.id, blob.size, blob.content_type),
407    }
408}
409
410/// Format a variable path as an S-expression.
411pub fn format_varpath(path: &VarPath) -> String {
412    path.segments
413        .iter()
414        .map(|seg| match seg {
415            VarSegment::Field(name) => name.clone(),
416        })
417        .collect::<Vec<_>>()
418        .join(".")
419}
420
421#[cfg(test)]
422mod tests {
423    use super::*;
424
425    #[test]
426    fn format_simple_int() {
427        assert_eq!(format_value(&Value::Int(42)), "(int 42)");
428    }
429
430    #[test]
431    fn format_simple_string() {
432        assert_eq!(format_value(&Value::String("hello".to_string())), "(string \"hello\")");
433    }
434
435    #[test]
436    fn format_varpath_simple() {
437        let path = VarPath::simple("X");
438        assert_eq!(format_varpath(&path), "X");
439    }
440
441    #[test]
442    fn format_varpath_nested() {
443        let path = VarPath {
444            segments: vec![
445                VarSegment::Field("VAR".to_string()),
446                VarSegment::Field("field".to_string()),
447            ],
448        };
449        assert_eq!(format_varpath(&path), "VAR.field");
450    }
451}