Skip to content

Commit

Permalink
implement basic set
Browse files Browse the repository at this point in the history
  • Loading branch information
voschezang committed Dec 24, 2023
1 parent 5490c5b commit 5a3d05e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/mash/shell/ast/set_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ def __init__(self, items, condition=None):
self.condition = condition

def run(self, prev_result='', shell: BaseShell = None, lazy=False):
items = [shell.run_commands(item) for item in self.items]
items = []
for item in self.items.values:
results = shell.run_commands(item)
for row in results:
items.append(row.splitlines())

if lazy:
return f'{{ {self.items} | {self.condition} }}'

return list(self.apply(items, shell))
result = list(self.apply(items, shell))
return ['\n'.join(c) for c in result]

def apply(self, items, shell: BaseShell = None):
"""Returns the outer product of a nested list.
"""
if self.condition is None:
yield from product(*items)
return
Expand Down
3 changes: 3 additions & 0 deletions test/shell/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,9 @@ def test_parse_set_with_filter():
assert isinstance(result, SetDefinition)
result = parse_line('{ users groups | x.id == y.id }')
assert isinstance(result, SetDefinition)
result = parse_line('x <- { users }')
assert isinstance(result, Assign)
assert isinstance(result.rhs, SetDefinition)

# TODO
if 0:
Expand Down
13 changes: 13 additions & 0 deletions test/shell/test_shell_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,16 @@ def test_variable_assignment_with_if_then():

run_command('a <- if "" then echo 10', shell=shell)
assert shell.env['a'] == ''

def test_set_definition():
shell = Shell()
shell.ignore_invalid_syntax = False

run_command('a <- range 3', shell=shell)
run_command('b <- range 3', shell=shell)
run_command('c <- { $a }', shell=shell)
assert shell.env['c'] == '0 1 2'

# TODO
run_command('c <- { a b }', shell=shell)
run_command('c <- { a b | a == b }', shell=shell)

0 comments on commit 5a3d05e

Please sign in to comment.