From 202da8708dc1ccdec8a9c0dfb6f3dee99a573756 Mon Sep 17 00:00:00 2001 From: baltdev Date: Sun, 3 Nov 2024 19:33:29 -0600 Subject: [PATCH] Various changes --- src/cogs/macros.py | 5 +++++ src/types.py | 23 ++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/cogs/macros.py b/src/cogs/macros.py index 9076afc26..bcb730527 100644 --- a/src/cogs/macros.py +++ b/src/cogs/macros.py @@ -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.""" diff --git a/src/types.py b/src/types.py index 59680e254..5be1a3abe 100644 --- a/src/types.py +++ b/src/types.py @@ -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)