Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[java] add SignatureBinder
  • Loading branch information
mberends committed Sep 25, 2010
1 parent 12f5afe commit a301155
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 36 deletions.
90 changes: 54 additions & 36 deletions java/runtime/Makefile
Expand Up @@ -6,32 +6,32 @@
#
# Hints
# |
# SerializationContext
# | |
# +------------------------> RakudoObject |
# | | | | |
# | Parameter Lexpad | |
# | | | | |
# | Signature | Representation |
# | | | | | | |
# | +--+----------------> RakudoCodeRef | | |
# | | | | | | |
# 1 2 3 ExecutionDomain Context | | |
# | | | | | | | |
# | | +-------------- ThreadContext | | |
# | | | | | |
# | | P6capture | | | |
# | | | | | | |
# | | MultiDispatcher | | | |
# | | | | | | |
# | | +-------> Ops P6hash | | | |
# | | | | P6int | | | |
# | | 4 P6opaque IFindMethod | REPRRegistry
# | | | | | | |
# | | | CaptureHelper | | |
# | | | | | | |
# +--+--+------------------------ SharedTable |
# | |
# SerializationContext
# | |
# +------------------------> RakudoObject |
# | | | | |
# | Parameter Lexpad | |
# | | | | |
# | Signature | Representation |
# | | | | | | |
# | +--+----------------> RakudoCodeRef | | |
# | | | | | | |
# 1 2 3 ExecutionDomain Context | | |
# | | | | | | | |
# | | +--------------------- ThreadContext | | |
# | | | | | |
# | | P6capture | | | |
# | | | | | | | |
# | | MultiDispatcher SignatureBinder | | | |
# | | | | | | |
# | | +-------> Ops P6hash | | | |
# | | | | P6int | | | |
# | | 4 P6opaque IFindMethod | REPRRegistry
# | | | | | | |
# | | | CaptureHelper | | |
# | | | | | | |
# +--+--+------------------------- SharedTable |
# | |
# KnowHOWREPR CodeObjectUtility
# | |
# KnowHOWBootstrapper
Expand All @@ -55,6 +55,7 @@ ALL_BUILD_TARGETS = \
Rakudo/Metamodel/Representations/P6str.class \
Rakudo/Metamodel/Representations/P6opaque.class \
Rakudo/Runtime/CodeObjectUtility.class \
Rakudo/Runtime/SignatureBinder.class \
Rakudo/Metamodel/KnowHOW/KnowHOWREPR.class \
Rakudo/Metamodel/KnowHOW/KnowHOWBootstrapper.class \
Rakudo/Init.class
Expand All @@ -75,9 +76,16 @@ OTHER_DEPENDENT_TARGETS = \
Rakudo/Metamodel/IFindMethod.class \
Rakudo/Metamodel/SharedTable.class

all: $(ALL_BUILD_TARGETS)
INNER_CLASSES = \
Rakudo/Metamodel/Representations/RakudoCodeRef$$Instance.class

all: RakudoRuntime.jar

RakudoRuntime.jar: $(ALL_BUILD_TARGETS) $(OTHER_DEPENDENT_TARGETS) $(INNER_CLASSES)
jar cf RakudoRuntime.jar $(ALL_BUILD_TARGETS) $(OTHER_DEPENDENT_TARGETS) '$(INNER_CLASSES)'
# For troubleshooting, use `jar cvf RakudoRuntime.jar` instead.
# Use `jar tf RakudoRuntime.jar` to tell what was done, or `jar tvf` instead.

# compiles ok: (listed in dependency order)
Rakudo/Metamodel/Hints.class: Rakudo/Metamodel/Hints.java
javac Rakudo/Metamodel/Hints.java

Expand Down Expand Up @@ -126,13 +134,6 @@ Rakudo/Metamodel/RakudoObject.class: Rakudo/Metamodel/RakudoObject.java \
Rakudo/Metamodel/REPRRegistry.class: Rakudo/Metamodel/REPRRegistry.java
javac Rakudo/Metamodel/REPRRegistry.java

Rakudo/Runtime/CodeObjectUtility.class: Rakudo/Runtime/CodeObjectUtility.java \
Rakudo/Metamodel/RakudoObject.class \
Rakudo/Metamodel/REPRRegistry.class \
Rakudo/Metamodel/Representation.class \
Rakudo/Metamodel/Representations/RakudoCodeRef.class
javac Rakudo/Runtime/CodeObjectUtility.java

Rakudo/Metamodel/KnowHOW/KnowHOWREPR.class: Rakudo/Metamodel/KnowHOW/KnowHOWREPR.java \
Rakudo/Metamodel/Hints.class
javac Rakudo/Metamodel/KnowHOW/KnowHOWREPR.java
Expand Down Expand Up @@ -203,6 +204,19 @@ Rakudo/Metamodel/Representations/P6opaque.class: Rakudo/Metamodel/Representation
Rakudo/Serialization/SerializationContext.class
javac Rakudo/Metamodel/Representations/P6opaque.java

Rakudo/Runtime/CodeObjectUtility.class: Rakudo/Runtime/CodeObjectUtility.java \
Rakudo/Metamodel/RakudoObject.class \
Rakudo/Metamodel/REPRRegistry.class \
Rakudo/Metamodel/Representation.class \
Rakudo/Metamodel/Representations/RakudoCodeRef.class
javac Rakudo/Runtime/CodeObjectUtility.java

Rakudo/Runtime/SignatureBinder.class: Rakudo/Runtime/SignatureBinder.java \
Rakudo/Metamodel/RakudoObject.class \
Rakudo/Metamodel/Representations/P6capture.class \
Rakudo/Runtime/Context.class
javac Rakudo/Runtime/SignatureBinder.java

Rakudo/Init.class: Rakudo/Init.java \
Rakudo/Metamodel/KnowHOW/KnowHOWBootstrapper.class \
Rakudo/Metamodel/KnowHOW/KnowHOWREPR.class \
Expand Down Expand Up @@ -331,6 +345,9 @@ Rakudo/Runtime/Parameter.java: ../../dotnet/runtime/Runtime/Signatures/Parameter
Rakudo/Runtime/Signature.java: ../../dotnet/runtime/Runtime/Signatures/Signature.cs
@echo "todo: Rakudo/Runtime/Signature.java is older than ../../dotnet/runtime/Metamodel/Signatures/Signature.cs"

Rakudo/Runtime/SignatureBinder.java: ../../dotnet/runtime/Runtime/Signatures/SignatureBinder.cs
@echo "todo: $@ is older than $<"

Rakudo/Runtime/ThreadContext.java: ../../dotnet/runtime/Runtime/ThreadContext.cs
@echo "todo: Rakudo/Runtime/ThreadContext.java is older than ../../dotnet/runtime/Runtime/ThreadContext.cs"

Expand All @@ -342,7 +359,8 @@ Rakudo/Serialization/SerializationContext.java: ../../dotnet/runtime/Serializati
todolist: $(TRANSLATED_SOURCE_FILES)

clean:
perl -MExtUtils::Command -e rm_f $(ALL_BUILD_TARGETS) $(OTHER_DEPENDENT_TARGETS)
perl -MExtUtils::Command -e rm_f RakudoRuntime.jar \
$(ALL_BUILD_TARGETS) $(OTHER_DEPENDENT_TARGETS)

help:
@echo "In 6model/java/runtime you can make the following:"
Expand Down
167 changes: 167 additions & 0 deletions java/runtime/Rakudo/Runtime/SignatureBinder.java
@@ -0,0 +1,167 @@
package Rakudo.Runtime;

import java.util.HashMap;
import Rakudo.Metamodel.RakudoObject;
import Rakudo.Metamodel.Representations.P6capture;
import Rakudo.Runtime.Context;

/// <summary>
/// Simple signature binder implementation.
/// </summary>
public class SignatureBinder // static class in the C# version
{
/// <summary>
/// Singleton empty positionals array.
/// </summary>
private static RakudoObject[] EmptyPos = new RakudoObject[0];

/// <summary>
/// Single empty nameds hash.
/// </summary>
private static HashMap<String, RakudoObject> EmptyNamed = new HashMap<String, RakudoObject>();

/// <summary>
/// Binds the capture against the given signature and stores the
/// bound values into variables in the lexpad.
///
/// XXX No type-checking is available just yet. :-(
///
/// XXX No proper handling of optionals and defaults yet.
///
/// XXX No support for nameds mapping to positionals yet either.
///
/// (In other words, this kinda sucks...)
/// </summary>
/// <param name="C"></param>
/// <param name="Capture"></param>
public static void Bind(Context C, RakudoObject Capture)
{
// Make sure the object is really a low level capture (don't handle
// otherwise yet) and grab the pieces.
P6capture.Instance NativeCapture = (P6capture.Instance)Capture;
if (NativeCapture == null)
throw new UnsupportedOperationException("Can only deal with native captures at the moment");
RakudoObject[] Positionals = NativeCapture.Positionals != null ? NativeCapture.Positionals : EmptyPos;
HashMap<String,RakudoObject> Nameds = NativeCapture.Nameds != null ? NativeCapture.Nameds : EmptyNamed;

// If we have no signature, that's same as an empty signature.
Signature Sig = C.StaticCodeObject.Sig;
if (Sig == null)
return;

// Current positional.
int CurPositional = 0;

// Iterate over the parameters.
Parameter[] Params = Sig.Parameters;
int NumParams = Params.length;
for (int i = 0; i < NumParams; i++)
{
Parameter Param = Params[i];

// Positional required?
if (Param.Flags == Parameter.POS_FLAG)
{
if (CurPositional < Positionals.length)
{
// We have an argument, just bind it.
C.LexPad.Storage[Param.VariableLexpadPosition] = Positionals[CurPositional];
}
else
{
throw new UnsupportedOperationException("Not enough positional parameters; got " +
Integer.toString(CurPositional) + " but needed " +
Integer.toString(NumRequiredPositionals(C.StaticCodeObject.Sig)));
}

// Increment positional counter.
CurPositional++;
}

// Positonal optional?
else if (Param.Flags == Parameter.OPTIONAL_FLAG)
{
if (CurPositional < Positionals.length)
{
// We have an argument, just bind it.
C.LexPad.Storage[Param.VariableLexpadPosition] = Positionals[CurPositional];
}
else
{
// XXX Default value, vivification.
}

// Increment positional counter.
CurPositional++;
}

// Named slurpy?
else if ((Param.Flags & Parameter.NAMED_SLURPY_FLAG) != 0)
{
throw new UnsupportedOperationException("Named slurpy parameters are not yet implemented.");
}

// Named positional?
else if ((Param.Flags & Parameter.POS_SLURPY_FLAG) != 0)
{
throw new UnsupportedOperationException("Positional slurpy parameters are not yet implemented.");
}

// Named?
else if (Param.Name != null)
{
// Yes, try and get argument.
if (Nameds.containsKey(Param.Name))
{
// We have an argument, just bind it.
RakudoObject Value = Nameds.get(Param.Name);
C.LexPad.Storage[Param.VariableLexpadPosition] = Value;
}
else
{
// Optional?
if ((Param.Flags & Parameter.OPTIONAL_FLAG) == 0)
{
throw new UnsupportedOperationException("Required named parameter " + Param.Name + " missing");
}
else
{
// XXX Default value, vivification.
}
}
}

// Otherwise, WTF?
else
{

}
}

// Ensure we had enough positionals.
int PossiesInCapture = Positionals.length;
if (CurPositional != PossiesInCapture)
throw new UnsupportedOperationException("Too many positional arguments passed; expected " +
Integer.toString(NumRequiredPositionals(C.StaticCodeObject.Sig)) +
" but got " + Integer.toString(PossiesInCapture));

// XXX TODO; Ensure we don't have leftover nameds.
}

/// <summary>
/// The number of positionals we require.
/// </summary>
/// <param name="Sig"></param>
/// <returns></returns>
private static int NumRequiredPositionals(Signature Sig)
{
int Num = 0;
for (Parameter Param : Sig.Parameters)
if (Param.Flags != 0 || Param.Name != null)
break;
else
Num++;
return Num;
}
}

0 comments on commit a301155

Please sign in to comment.