kaish-kernel 0.7.0

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
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
616
617
618
619
620
621
622
623
624
//! test — POSIX conditional expressions.
//!
//! Evaluates conditional expressions and returns exit code 0 (true) or 1 (false).
//!
//! # Examples
//!
//! ```kaish
//! test -e file.txt              # File exists
//! test -f file.txt              # Is regular file
//! test -d mydir                 # Is directory
//! test -z ""                    # String is empty
//! test -n "hello"               # String is not empty
//! test "a" = "a"                # String equality
//! test "a" != "b"               # String inequality
//! test 5 -eq 5                  # Numeric equality
//! test 3 -lt 5                  # Numeric less than
//! test 5 -gt 3                  # Numeric greater than
//! test 5 -ge 5                  # Numeric greater than or equal
//! test 5 -le 5                  # Numeric less than or equal
//! test ! -e nofile              # Negation
//! [ -f file.txt ]               # Alternate syntax (requires closing ])
//! ```

use async_trait::async_trait;
use std::path::Path;

use crate::ast::Value;
use crate::interpreter::ExecResult;
use crate::tools::{ExecContext, ParamSchema, Tool, ToolArgs, ToolSchema};

/// Test tool: evaluates conditional expressions.
pub struct Test;

/// Bracket alias for test: `[`
pub struct Bracket;

#[async_trait]
impl Tool for Test {
    fn name(&self) -> &str {
        "test"
    }

    fn schema(&self) -> ToolSchema {
        ToolSchema::new("test", "Evaluate conditional expressions")
            .param(ParamSchema::optional(
                "expression",
                "string",
                Value::Null,
                "Conditional expression to evaluate",
            ))
            .example("File exists", "test -e file.txt")
            .example("String equality", "test \"$VAR\" = \"expected\"")
            .example("Numeric comparison", "test 5 -gt 3")
    }

    async fn execute(&self, args: ToolArgs, ctx: &mut ExecContext) -> ExecResult {
        evaluate_test(args, ctx, false).await
    }
}

#[async_trait]
impl Tool for Bracket {
    fn name(&self) -> &str {
        "["
    }

    fn schema(&self) -> ToolSchema {
        ToolSchema::new("[", "Evaluate conditional expressions (alternate syntax)")
            .param(ParamSchema::optional(
                "expression",
                "string",
                Value::Null,
                "Conditional expression to evaluate (must end with ])",
            ))
            .example("Check file type", "[ -f file.txt ]")
            .example("String test", "[ -n \"$VAR\" ]")
    }

    async fn execute(&self, args: ToolArgs, ctx: &mut ExecContext) -> ExecResult {
        evaluate_test(args, ctx, true).await
    }
}

/// Evaluate a test expression.
async fn evaluate_test(args: ToolArgs, ctx: &mut ExecContext, bracket_mode: bool) -> ExecResult {
    // Reconstruct the original token sequence from parsed ToolArgs.
    // The schema-aware parser splits "-r" into args.flags, but test needs
    // it as a positional operator. Prepend flags as "-{flag}" tokens.
    let mut tokens: Vec<String> = Vec::new();
    for flag in &args.flags {
        tokens.push(format!("-{flag}"));
    }
    for v in &args.positional {
        match v {
            Value::String(s) => tokens.push(s.clone()),
            Value::Int(i) => tokens.push(i.to_string()),
            Value::Float(f) => tokens.push(f.to_string()),
            Value::Bool(b) => tokens.push(if *b { "true" } else { "false" }.to_string()),
            Value::Null => {}
            Value::Json(json) => tokens.push(json.to_string()),
            Value::Blob(blob) => tokens.push(format!("[blob: {} {}]", blob.formatted_size(), blob.content_type)),
        }
    }

    // In bracket mode, verify and remove trailing ]
    if bracket_mode {
        if tokens.last().map(|s| s.as_str()) != Some("]") {
            return ExecResult::failure(2, "[: missing closing ]");
        }
        tokens.pop();
    }

    // Empty expression is false
    if tokens.is_empty() {
        return ExecResult::from_output(1, "", "");
    }

    // Parse and evaluate the expression
    let result = evaluate_expression(&tokens, ctx).await;

    match result {
        Ok(true) => ExecResult::success(""),
        Ok(false) => ExecResult::from_output(1, "", ""),
        Err(e) => ExecResult::failure(2, format!("test: {}", e)),
    }
}

/// Evaluate a test expression from tokens.
///
/// Handles most common POSIX test expressions without complex nesting.
async fn evaluate_expression(tokens: &[String], ctx: &ExecContext) -> Result<bool, String> {
    if tokens.is_empty() {
        return Ok(false);
    }

    // Handle leading negation
    let (negate, tokens) = if tokens[0] == "!" {
        (true, &tokens[1..])
    } else {
        (false, tokens)
    };

    if tokens.is_empty() {
        // "test !" with nothing after - error
        return Err("argument expected".to_string());
    }

    // Single token: non-empty string is true
    let result = if tokens.len() == 1 {
        !tokens[0].is_empty()
    } else if tokens.len() == 2 {
        // Two tokens: unary operator
        evaluate_unary(&tokens[0], &tokens[1], ctx).await?
    } else if tokens.len() >= 3 {
        // Three or more tokens: handle binary + optional compound
        evaluate_with_compounds(tokens, ctx).await?
    } else {
        false
    };

    Ok(if negate { !result } else { result })
}

/// Evaluate expression with potential compound operators (-a, -o).
async fn evaluate_with_compounds(tokens: &[String], ctx: &ExecContext) -> Result<bool, String> {
    // Find all compound operators
    let mut parts: Vec<&[String]> = Vec::new();
    let mut operators: Vec<&str> = Vec::new();
    let mut start = 0;

    for (i, token) in tokens.iter().enumerate() {
        if token == "-a" || token == "-o" {
            if i > start {
                parts.push(&tokens[start..i]);
            }
            operators.push(token);
            start = i + 1;
        }
    }
    if start < tokens.len() {
        parts.push(&tokens[start..]);
    }

    // If no compound operators, just evaluate the simple expression
    if operators.is_empty() {
        return evaluate_simple(tokens, ctx).await;
    }

    // Evaluate left-to-right with compound operators
    // Note: -a has higher precedence than -o in POSIX, but we'll use simple left-to-right
    let mut result = evaluate_simple(parts[0], ctx).await?;

    for (i, op) in operators.iter().enumerate() {
        let right = evaluate_simple(parts[i + 1], ctx).await?;

        result = match *op {
            "-a" => result && right,
            "-o" => result || right,
            _ => return Err(format!("unexpected operator: {}", op)),
        };
    }

    Ok(result)
}

/// Evaluate a simple expression (unary or binary, no compounds).
async fn evaluate_simple(tokens: &[String], ctx: &ExecContext) -> Result<bool, String> {
    if tokens.is_empty() {
        return Ok(false);
    }

    // Handle negation - at most one level
    let (negate, tokens) = if tokens.first().map(|s| s.as_str()) == Some("!") {
        (true, &tokens[1..])
    } else {
        (false, tokens)
    };

    let result = match tokens.len() {
        0 => return Err("argument expected after !".to_string()),
        1 => !tokens[0].is_empty(),
        2 => evaluate_unary(&tokens[0], &tokens[1], ctx).await?,
        3 => evaluate_binary(&tokens[0], &tokens[1], &tokens[2], ctx).await?,
        _ => return Err(format!("too many arguments: {:?}", tokens)),
    };

    Ok(if negate { !result } else { result })
}

/// Evaluate unary operators.
async fn evaluate_unary(op: &str, arg: &str, ctx: &ExecContext) -> Result<bool, String> {
    match op {
        // String tests
        "-z" => Ok(arg.is_empty()),
        "-n" => Ok(!arg.is_empty()),

        // File tests
        "-e" => {
            let path = ctx.resolve_path(arg);
            Ok(ctx.backend.exists(Path::new(&path)).await)
        }
        "-f" => {
            let path = ctx.resolve_path(arg);
            match ctx.backend.stat(Path::new(&path)).await {
                Ok(info) => Ok(info.is_file()),
                Err(_) => Ok(false),
            }
        }
        "-d" => {
            let path = ctx.resolve_path(arg);
            match ctx.backend.stat(Path::new(&path)).await {
                Ok(info) => Ok(info.is_dir()),
                Err(_) => Ok(false),
            }
        }
        "-r" => {
            let path = ctx.resolve_path(arg);
            match ctx.backend.stat(Path::new(&path)).await {
                // MemoryFs has no permissions — assume readable if file exists
                Ok(info) => Ok(info.permissions.is_none_or(|p| p & 0o444 != 0)),
                Err(_) => Ok(false),
            }
        }
        "-w" => {
            let path = ctx.resolve_path(arg);
            match ctx.backend.stat(Path::new(&path)).await {
                Ok(info) => Ok(info.permissions.is_none_or(|p| p & 0o222 != 0)),
                Err(_) => Ok(false),
            }
        }
        "-x" => {
            let path = ctx.resolve_path(arg);
            match ctx.backend.stat(Path::new(&path)).await {
                // Not executable by default when permissions unknown
                Ok(info) => Ok(info.permissions.is_some_and(|p| p & 0o111 != 0)),
                Err(_) => Ok(false),
            }
        }
        "-s" => {
            // File exists and has size > 0
            let path = ctx.resolve_path(arg);
            match ctx.backend.stat(Path::new(&path)).await {
                Ok(info) => Ok(info.size > 0),
                Err(_) => Ok(false),
            }
        }
        "-L" | "-h" => {
            let path = ctx.resolve_path(arg);
            match ctx.backend.lstat(Path::new(&path)).await {
                Ok(info) => Ok(info.is_symlink()),
                Err(_) => Ok(false),
            }
        }

        _ => Err(format!("unknown unary operator: {}", op)),
    }
}

/// Evaluate binary operators.
async fn evaluate_binary(
    left: &str,
    op: &str,
    right: &str,
    _ctx: &ExecContext,
) -> Result<bool, String> {
    match op {
        // String comparison
        "=" | "==" => Ok(left == right),
        "!=" => Ok(left != right),

        // Numeric comparison
        "-eq" => {
            let l = parse_int(left)?;
            let r = parse_int(right)?;
            Ok(l == r)
        }
        "-ne" => {
            let l = parse_int(left)?;
            let r = parse_int(right)?;
            Ok(l != r)
        }
        "-lt" => {
            let l = parse_int(left)?;
            let r = parse_int(right)?;
            Ok(l < r)
        }
        "-le" => {
            let l = parse_int(left)?;
            let r = parse_int(right)?;
            Ok(l <= r)
        }
        "-gt" => {
            let l = parse_int(left)?;
            let r = parse_int(right)?;
            Ok(l > r)
        }
        "-ge" => {
            let l = parse_int(left)?;
            let r = parse_int(right)?;
            Ok(l >= r)
        }

        // String ordering (lexicographic)
        "<" => Ok(left < right),
        ">" => Ok(left > right),

        _ => Err(format!("unknown binary operator: {}", op)),
    }
}

/// Parse an integer from a string.
fn parse_int(s: &str) -> Result<i64, String> {
    s.parse::<i64>()
        .map_err(|_| format!("invalid integer: {}", s))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vfs::{Filesystem, MemoryFs, VfsRouter};
    use std::sync::Arc;

    async fn make_test_ctx() -> ExecContext {
        let mut vfs = VfsRouter::new();
        let mem = MemoryFs::new();

        // Create test files and directories
        mem.write(Path::new("file.txt"), b"content").await.unwrap();
        mem.write(Path::new("empty.txt"), b"").await.unwrap();
        mem.mkdir(Path::new("mydir")).await.unwrap();

        vfs.mount("/", mem);
        ExecContext::new(Arc::new(vfs))
    }

    #[tokio::test]
    async fn test_file_exists() {
        let mut ctx = make_test_ctx().await;
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("-e".into()));
        args.positional.push(Value::String("file.txt".into()));

        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "file.txt should exist");
    }

    #[tokio::test]
    async fn test_file_not_exists() {
        let mut ctx = make_test_ctx().await;
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("-e".into()));
        args.positional.push(Value::String("nonexistent.txt".into()));

        let result = Test.execute(args, &mut ctx).await;
        assert_eq!(result.code, 1, "nonexistent.txt should not exist");
    }

    #[tokio::test]
    async fn test_is_file() {
        let mut ctx = make_test_ctx().await;
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("-f".into()));
        args.positional.push(Value::String("file.txt".into()));

        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "file.txt should be a file");
    }

    #[tokio::test]
    async fn test_is_dir() {
        let mut ctx = make_test_ctx().await;
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("-d".into()));
        args.positional.push(Value::String("mydir".into()));

        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "mydir should be a directory");
    }

    #[tokio::test]
    async fn test_file_has_size() {
        let mut ctx = make_test_ctx().await;

        // file.txt has content
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("-s".into()));
        args.positional.push(Value::String("file.txt".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "file.txt should have size > 0");

        // empty.txt has no content
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("-s".into()));
        args.positional.push(Value::String("empty.txt".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert_eq!(result.code, 1, "empty.txt should have size 0");
    }

    #[tokio::test]
    async fn test_string_empty() {
        let mut ctx = make_test_ctx().await;

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("-z".into()));
        args.positional.push(Value::String("".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "empty string should be zero-length");

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("-z".into()));
        args.positional.push(Value::String("hello".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert_eq!(result.code, 1, "non-empty string should not be zero-length");
    }

    #[tokio::test]
    async fn test_string_not_empty() {
        let mut ctx = make_test_ctx().await;

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("-n".into()));
        args.positional.push(Value::String("hello".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "non-empty string should be non-zero-length");
    }

    #[tokio::test]
    async fn test_string_equality() {
        let mut ctx = make_test_ctx().await;

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("hello".into()));
        args.positional.push(Value::String("=".into()));
        args.positional.push(Value::String("hello".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "strings should be equal");

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("hello".into()));
        args.positional.push(Value::String("=".into()));
        args.positional.push(Value::String("world".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert_eq!(result.code, 1, "strings should not be equal");
    }

    #[tokio::test]
    async fn test_string_inequality() {
        let mut ctx = make_test_ctx().await;

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("a".into()));
        args.positional.push(Value::String("!=".into()));
        args.positional.push(Value::String("b".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "a != b should be true");
    }

    #[tokio::test]
    async fn test_numeric_equality() {
        let mut ctx = make_test_ctx().await;

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("5".into()));
        args.positional.push(Value::String("-eq".into()));
        args.positional.push(Value::String("5".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "5 -eq 5 should be true");
    }

    #[tokio::test]
    async fn test_numeric_less_than() {
        let mut ctx = make_test_ctx().await;

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("3".into()));
        args.positional.push(Value::String("-lt".into()));
        args.positional.push(Value::String("5".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "3 -lt 5 should be true");

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("5".into()));
        args.positional.push(Value::String("-lt".into()));
        args.positional.push(Value::String("3".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert_eq!(result.code, 1, "5 -lt 3 should be false");
    }

    #[tokio::test]
    async fn test_numeric_greater_than() {
        let mut ctx = make_test_ctx().await;

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("5".into()));
        args.positional.push(Value::String("-gt".into()));
        args.positional.push(Value::String("3".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "5 -gt 3 should be true");
    }

    #[tokio::test]
    async fn test_negation() {
        let mut ctx = make_test_ctx().await;

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("!".into()));
        args.positional.push(Value::String("-e".into()));
        args.positional.push(Value::String("nonexistent.txt".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "! -e nonexistent.txt should be true");
    }

    #[tokio::test]
    async fn test_bracket_syntax() {
        let mut ctx = make_test_ctx().await;

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("-e".into()));
        args.positional.push(Value::String("file.txt".into()));
        args.positional.push(Value::String("]".into()));
        let result = Bracket.execute(args, &mut ctx).await;
        assert!(result.ok(), "[ -e file.txt ] should be true");
    }

    #[tokio::test]
    async fn test_bracket_missing_close() {
        let mut ctx = make_test_ctx().await;

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("-e".into()));
        args.positional.push(Value::String("file.txt".into()));
        // Missing ]
        let result = Bracket.execute(args, &mut ctx).await;
        assert_eq!(result.code, 2, "should fail without closing ]");
    }

    #[tokio::test]
    async fn test_empty_expression() {
        let mut ctx = make_test_ctx().await;

        let args = ToolArgs::new();
        let result = Test.execute(args, &mut ctx).await;
        assert_eq!(result.code, 1, "empty expression should be false");
    }

    #[tokio::test]
    async fn test_single_string_true() {
        let mut ctx = make_test_ctx().await;

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("hello".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "non-empty single string should be true");
    }

    #[tokio::test]
    async fn test_numeric_comparisons() {
        let mut ctx = make_test_ctx().await;

        // -le (less than or equal)
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("5".into()));
        args.positional.push(Value::String("-le".into()));
        args.positional.push(Value::String("5".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "5 -le 5 should be true");

        // -ge (greater than or equal)
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("5".into()));
        args.positional.push(Value::String("-ge".into()));
        args.positional.push(Value::String("5".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "5 -ge 5 should be true");

        // -ne (not equal)
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("3".into()));
        args.positional.push(Value::String("-ne".into()));
        args.positional.push(Value::String("5".into()));
        let result = Test.execute(args, &mut ctx).await;
        assert!(result.ok(), "3 -ne 5 should be true");
    }
}