Skip to content

Commit

Permalink
Add deletion of list index item
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Nov 27, 2024
1 parent 1b1fad0 commit 768c2bf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
48 changes: 47 additions & 1 deletion mshell/Evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,53 @@ MainLoop:
}

stack.Push(newList)
} else if t.Lexeme == "~" || strings.HasPrefix(t.Lexeme, "~/") {
} else if t.Lexeme == "del" {
obj1, err := stack.Pop()
if err != nil {
return FailWithMessage(fmt.Sprintf("%d:%d: Cannot do 'del' operation on an empty stack.\n", t.Line, t.Column))
}

obj2, err := stack.Pop()
if err != nil {
return FailWithMessage(fmt.Sprintf("%d:%d: Cannot do 'del' operation on a stack with only one item.\n", t.Line, t.Column))
}

switch obj1.(type) {
case *MShellInt:
switch obj2.(type) {
case *MShellList:
index := obj1.(*MShellInt).Value
if index < 0 {
index = len(obj2.(*MShellList).Items) + index
}

if index < 0 || index >= len(obj2.(*MShellList).Items) {
return FailWithMessage(fmt.Sprintf("%d:%d: Index out of range for 'del'.\n", t.Line, t.Column))
}
obj2.(*MShellList).Items = append(obj2.(*MShellList).Items[:index], obj2.(*MShellList).Items[index+1:]...)
stack.Push(obj2)
default:
return FailWithMessage(fmt.Sprintf("%d:%d: Cannot delete from a %s.\n", t.Line, t.Column, obj2.TypeName()))
}
case *MShellList:
switch obj2.(type) {
case *MShellInt:
index := obj2.(*MShellInt).Value
if index < 0 {
index = len(obj1.(*MShellList).Items) + index
}
if index < 0 || index >= len(obj1.(*MShellList).Items) {
return FailWithMessage(fmt.Sprintf("%d:%d: Index out of range for 'del'.\n", t.Line, t.Column))
}
obj1.(*MShellList).Items = append(obj1.(*MShellList).Items[:index], obj1.(*MShellList).Items[index+1:]...)
stack.Push(obj1)
default:
return FailWithMessage(fmt.Sprintf("%d:%d: Cannot delete from a %s.\n", t.Line, t.Column, obj2.TypeName()))
}
default:
return FailWithMessage(fmt.Sprintf("%d:%d: Cannot delete from a %s.\n", t.Line, t.Column, obj1.TypeName()))
}
} else if t.Lexeme == "~" || strings.HasPrefix(t.Lexeme, "~/") {
// Only do tilde expansion
homeDir, err := os.UserHomeDir()
if err != nil {
Expand Down
Binary file modified mshell/mshell
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/indexing.msh
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ echo
"12345" -3: wl # 345
"12345" :-4 wl # 1
"12345" -4:-2 wl # 23

def cjoin ", " join wl end

# Test deletion at index
["a" "b" "c" "d"] 2 del cjoin
1 ["a" "b" "c" "d"] del cjoin
["a" "b" "c" "d"] -1 del cjoin
-1 ["a" "b" "c" "d"] del cjoin
4 changes: 4 additions & 0 deletions tests/indexing.msh.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ ul
345
1
23
a, b, d
a, c, d
a, b, c
a, b, c

0 comments on commit 768c2bf

Please sign in to comment.