Skip to content

Commit

Permalink
Add lines
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Nov 11, 2024
1 parent 3dd1360 commit 94b7d11
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/mshell.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@

`x`: Interpret/execute quotation

### String Functions

`str`: Convert to string
`findReplace`: Find and replace in string. `findReplace (string string, string find, string replace -- string)`
`lines`: Split string into list of string lines


## Process Substitution
Expand Down
19 changes: 19 additions & 0 deletions mshell-go/Evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,25 @@ func (state *EvalState) Evaluate(objects []MShellParseItem, stack *MShellStack,
return FailWithMessage(fmt.Sprintf("%d:%d: Cannot find-replace a %s.\n", t.Line, t.Column, obj1.TypeName()))
}

} else if t.Lexeme == "lines" {
obj, err := stack.Pop()
if err != nil {
return FailWithMessage(fmt.Sprintf("%d:%d: Cannot evaluate 'lines' on an empty stack.\n", t.Line, t.Column))
}

s1, ok := obj.(*MShellString)
if !ok {
return FailWithMessage(fmt.Sprintf("%d:%d: Cannot evaluate 'lines' on a %s.\n", t.Line, t.Column, obj.TypeName()))
}

// TODO: Maybe reuse a scanner?
scanner := bufio.NewScanner(strings.NewReader(s1.Content))
newList := &MShellList { Items: []MShellObject{}, StandardInputFile: "", StandardOutputFile: "", StdoutBehavior: STDOUT_NONE }
for scanner.Scan() {
newList.Items = append(newList.Items, &MShellString { scanner.Text() })
}

stack.Push(newList)
} else if t.Lexeme == "~" || strings.HasPrefix(t.Lexeme, "~/") {
// Only do tilde expansion
homeDir, err := os.UserHomeDir()
Expand Down

0 comments on commit 94b7d11

Please sign in to comment.