fixed bash-for-windows

This commit is contained in:
Cametendo
2026-05-31 21:49:13 +02:00
parent 7b3a101946
commit 9037606447
8 changed files with 437 additions and 123 deletions

View File

@@ -21,17 +21,40 @@ func (s *Shell) executeIf(block string) error {
phase := "if_cond"
var curCond []string
var curBody []string
depth := 0 // nesting depth of if/for/while/until/case inside the body
addToBody := func(s string) {
switch phase {
case "body":
curBody = append(curBody, s)
case "else":
elseBody = append(elseBody, s)
}
}
for _, stmt := range stmts {
w := firstWord(stmt)
rest := afterWord(stmt)
// When depth > 0, we're inside a nested block; all keywords are body content.
if depth > 0 {
switch w {
case "if", "for", "while", "until", "case":
depth++
case "fi", "done", "esac":
depth--
}
addToBody(stmt)
continue
}
// depth == 0: structural keywords for the outer if
switch {
case w == "if" && phase == "if_cond":
if rest != "" {
curCond = append(curCond, rest)
}
case w == "then":
case w == "then" && (phase == "if_cond" || phase == "elif_cond"):
if rest != "" {
curBody = append(curBody, rest)
}
@@ -67,8 +90,17 @@ func (s *Shell) executeIf(block string) error {
curCond = append(curCond, stmt)
case "body":
curBody = append(curBody, stmt)
// Track depth for nested blocks starting in body
switch w {
case "if", "for", "while", "until", "case":
depth++
}
case "else":
elseBody = append(elseBody, stmt)
switch w {
case "if", "for", "while", "until", "case":
depth++
}
}
}
}