Skip to content

Commit

Permalink
Various changes
Browse files Browse the repository at this point in the history
  • Loading branch information
balt-dev committed Nov 4, 2024
1 parent d78f18a commit 202da87
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/cogs/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ async def hash_(vars: VariableRegistry, seed: Any):

# Strings

@builtin("join")
async def join_(vars: VariableRegistry, delimiter: str, l: Any):
"""Joins its input by a given delimiter."""
return str(delimiter).join(l)

@builtin("slice")
async def slice_(vars: VariableRegistry, slicable: Any, start: int, end: int, step: int):
"""Slices a list or string given a start, end, and step."""
Expand Down
23 changes: 20 additions & 3 deletions src/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,35 @@ def __init__(self, name, description, source, inputs, variable_args, author, for
self.author = author
self.forest = forest

async def simplify_value(self, ctx: MacroCog, vars: VariableRegistry, val: Any, l: list):
if isinstance(val, MacroTree):
val = await ctx.evaluate_tree(val, vars)
if isinstance(val, list):
new_val = []
for v in val:
await self.simplify_value(ctx, vars, v, new_val)
l.append(new_val)
elif isinstance(val, Unpack):
args = []
await self.simplify_value(ctx, vars, val.inner, args)
l.extend(args[0])
else:
l.append(val)

async def eval(self, ctx: MacroCog, vars: VariableRegistry, args: list[Any]):
if self.variable_args:
if len(args) >= len(self.inputs):
variable = args[len(self.inputs) - 1:]

args[len(self.inputs) - 1] = variable
if self.captured_scope is not None:
self.captured_scope.parent = vars
vars = self.captured_scope
unpacked_args = []
for arg in args:
await self.simplify_value(ctx, vars, arg, unpacked_args)
scope = vars.branch()
for name, val in zip(self.inputs, args):
if isinstance(val, MacroTree):
val = await ctx.evaluate_tree(val, vars)
for name, val in zip(self.inputs, unpacked_args):
scope.variables[name] = val
return await ctx.evaluate_forest(self.forest, vars = scope)

Expand Down

0 comments on commit 202da87

Please sign in to comment.