Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[dotnet] Simple implementation of flattening in the signature binder.
  • Loading branch information
jnthn committed Nov 21, 2010
1 parent d73ce45 commit f3c12fd
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions dotnet/runtime/Runtime/Signatures/SignatureBinder.cs
Expand Up @@ -45,6 +45,10 @@ public static void Bind(ThreadContext TC, Context C, RakudoObject Capture)
var Nameds = NativeCapture.Nameds ?? EmptyNamed;
Dictionary<string, bool> SeenNames = null;

// See if we have to do any flattening.
if (NativeCapture.FlattenSpec != null)
Flatten(NativeCapture.FlattenSpec, ref Positionals, ref Nameds);

// If we have no signature, that's same as an empty signature.
var Sig = C.StaticCodeObject.Sig;
if (Sig == null)
Expand Down Expand Up @@ -178,5 +182,63 @@ private static int NumRequiredPositionals(Signature Sig)
Num++;
return Num;
}

/// <summary>
/// Flattens arguments into the positionals list or the nameds. This
/// is pretty straightforward way, could be optimized in various ways,
/// such as special case where we only have one arg which is the whole
/// parameter set.
/// </summary>
/// <param name="FlattenSpec"></param>
/// <param name="Positionals"></param>
/// <param name="Naneds"></param>
private static void Flatten(int[] FlattenSpec, ref RakudoObject[] Positionals, ref Dictionary<string, RakudoObject> Naneds)
{
// We'll build new positional and nameds.
var NewPositionals = new List<RakudoObject>();
var NewNameds = new Dictionary<string, RakudoObject>(Naneds);

// Go through positional arguments and look for things to flatten.
for (int i = 0; i < Positionals.Length; i++)
{
if (FlattenSpec[i] == CaptureHelper.FLATTEN_NONE)
{
NewPositionals.Add(Positionals[i]);
}
else if (FlattenSpec[i] == CaptureHelper.FLATTEN_POS)
{
// XXX For now rely on it being a P6list but in the future we
// should handle other cases.
var Flattenee = Positionals[i] as P6list.Instance;
if (Flattenee != null)
{
NewPositionals.AddRange(Flattenee.Storage);
}
else
{
throw new InvalidOperationException("Currently can only flatten a P6list");
}
}
else if (FlattenSpec[i] == CaptureHelper.FLATTEN_NAMED)
{
// XXX For now rely on it being a P6mapping but in the future we
// should handle other cases.
var Flattenee = Positionals[i] as P6mapping.Instance;
if (Flattenee != null)
{
foreach (var Pair in Flattenee.Storage)
NewNameds.Add(Pair.Key, Pair.Value);
}
else
{
throw new InvalidOperationException("Currently can only flatten a P6mapping");
}
}
}

// Put updated positionals and nameds in place.
Positionals = NewPositionals.ToArray();
Naneds = NewNameds;
}
}
}

0 comments on commit f3c12fd

Please sign in to comment.