Skip to content

Commit

Permalink
[java] fully update Lexpad and some other classes that use it
Browse files Browse the repository at this point in the history
  • Loading branch information
mberends committed Sep 12, 2010
1 parent 1db95fb commit 12b7d68
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 112 deletions.
20 changes: 12 additions & 8 deletions java/README.txt
Expand Up @@ -31,10 +31,13 @@ versatile, because the value part must be a reference type, it cannot be
a value type. Therefore C# Dictionary<string, int> becomes Java
HashMap<String, Integer> which it less convenient to use.

C# internal access modifier becomes Java protected or public.
C# foreach ( <type> name in <Iterable> ) becomes Java
for ( <type> name : <Iterable> ).

C# Func<typelist> becomes a Java anonymous class that implements an
interface.
C# Func<typelist> (see Lambdas etc below) becomes a Java anonymous class
that implements an interface.

C# internal access modifier becomes Java protected or public.

C# InvalidOperationException becomes Java UnsupportedOperationException.

Expand Down Expand Up @@ -75,16 +78,17 @@ Lambdas and References to Functions
C# has some language features that Java currently lacks, to safely
provide what C and C++ call pointers to functions.

The C# 'Func' generic type is a parameterized type. Variables declared
as 'Func<paramtype [,...], rettype>' store anonymous functions that take
certain parameters and return a certain result. Call the function using
the Invoke(...) method on the Func variable.
The C# 'Func' generic type is a parameterized type. A variable declared
as 'Func<paramtype [,...], rettype>' is a kind of delegate that
encapsulates an anonymous function that takes specified parameters and
returns a specified result. Call the function using the Invoke(...)
method on the Func variable.

The C# '=>' operator (also called Lambda) creates a reference to a block
of code. Store that reference in a Func variable.

The C# 'delegate' type contains a collection of function pointers. When
the delegate is called, each function that is pointed to gets called in
the delegate is invoked, each function that is pointed to gets called in
an unspecified order. Useful for multicast notification, event handlers
and publish and subscribe architectures.

Expand Down
85 changes: 54 additions & 31 deletions java/runtime/Makefile
Expand Up @@ -4,31 +4,33 @@
# (partial diagram - less significant (eg implicit transitive) dependencies
# omitted for simplicity)
#
# Hints
# |
# SerializationContext ExecutionDomain
# | |
# +------------> RakudoObject |
# | | | |
# | Parameter | |
# | | | |
# | Signature Representation |
# | | | | | |
# | +--+---> RakudoCodeRef | | |
# | | | | | | |
# 1 2 3 Context | | |
# | | | | | | |
# | | +---- ThreadContext | | |
# | | | | | |
# | | IFindMethod | REPRRegistry
# | | | | |
# +--+-------------- SharedTable | P6opaque
# | | P6int etc
# KnowHOWREPR | |
# | | |
# KnowHOWBootstrapper |Lexpad
# | ||
# Init
# Hints
# |
# SerializationContext
# | |
# +------------------------> RakudoObject |
# | | | | |
# | Parameter Lexpad | |
# | | | | |
# | Signature | Representation |
# | | | | | | |
# | +--+----------------> RakudoCodeRef | | |
# | | | | | | |
# 1 2 3 ExecutionDomain Context | | |
# | | | | | | | |
# | | +-------------- ThreadContext | | |
# | | | | | |
# | | P6capture IFindMethod | REPRRegistry
# | | | | | |
# | | CaptureHelper | | |
# | | | | | |
# +--+--------------------------- SharedTable | P6opaque
# | | P6int etc
# KnowHOWREPR | |
# | | |
# KnowHOWBootstrapper |
# | |
# Init
#

ALL_BUILD_TARGETS = \
Expand All @@ -37,9 +39,11 @@ ALL_BUILD_TARGETS = \
Rakudo/Runtime/ExecutionDomain.class \
Rakudo/Metamodel/RakudoObject.class \
Rakudo/Metamodel/REPRRegistry.class \
Rakudo/Metamodel/Representations/P6capture.class \
Rakudo/Runtime/CodeObjectUtility.class \
Rakudo/Metamodel/KnowHOW/KnowHOWREPR.class \
Rakudo/Metamodel/KnowHOW/KnowHOWBootstrapper.class \
Rakudo/Metamodel/Representations/P6capture.class
Rakudo/Init.class

OTHER_DEPENDENT_TARGETS = \
Rakudo/Runtime/Lexpad.class \
Expand Down Expand Up @@ -93,13 +97,21 @@ 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

Rakudo/Metamodel/KnowHOW/KnowHOWBootstrapper.class: Rakudo/Metamodel/KnowHOW/KnowHOWBootstrapper.java \
Rakudo/Metamodel/REPRRegistry.class \
Rakudo/Metamodel/SharedTable.class
Rakudo/Metamodel/SharedTable.class \
Rakudo/Runtime/CodeObjectUtility.class
javac Rakudo/Metamodel/KnowHOW/KnowHOWBootstrapper.java

Rakudo/Metamodel/Representations/P6opaque.class: Rakudo/Metamodel/Representations/P6opaque.java
Expand All @@ -114,7 +126,7 @@ Rakudo/Metamodel/Representations/P6capture.class: Rakudo/Metamodel/Representatio
javac Rakudo/Metamodel/Representations/P6capture.java

Rakudo/Init.class: Rakudo/Init.java \
Rakudo/Metamodel/Representations/P6opaque.class
Rakudo/Metamodel/Representations/P6capture.class
javac Rakudo/Init.java

# System to report which java files are older than their dotnet
Expand All @@ -133,7 +145,9 @@ TRANSLATED_SOURCE_FILES = \
Rakudo/Metamodel/Representations/RakudoCodeRef.java \
Rakudo/Runtime/Context.java \
Rakudo/Runtime/ThreadContext.java \
Rakudo/Metamodel/SharedTable.java
Rakudo/Metamodel/SharedTable.java \
Rakudo/Runtime/CodeObjectUtility.java \
Rakudo/Init.java

# Java source code files "depend" on files in the dotnet/* directories,
# in the sense that they were manually translated.
Expand All @@ -156,8 +170,8 @@ Rakudo/Metamodel/RakudoObject.java: ../../dotnet/runtime/Metamodel/RakudoObject.
Rakudo/Metamodel/Representation.java: ../../dotnet/runtime/Metamodel/Representation.cs
@echo "todo: Rakudo/Metamodel/Representation.java is older than ../../dotnet/runtime/Metamodel/Representation.cs"

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

Rakudo/Runtime/Parameter.java: ../../dotnet/runtime/Runtime/Signatures/Parameter.cs
@echo "todo: Rakudo/Runtime/Parameter.java is older than ../../dotnet/runtime/Runtime/Signatures/Parameter.cs"
Expand All @@ -177,6 +191,15 @@ Rakudo/Runtime/ThreadContext.java: ../../dotnet/runtime/Runtime/ThreadContext.cs
Rakudo/Metamodel/SharedTable.java: ../../dotnet/runtime/Metamodel/SharedTable.cs
@echo "todo: Rakudo/Metamodel/SharedTable.java is older than ../../dotnet/runtime/Metamodel/SharedTable.cs"

Rakudo/Metamodel/Representations/P6capture.java: ../../dotnet/runtime/Metamodel/Representations/P6capture.cs
@echo "todo: Rakudo/Metamodel/Representations/P6capture.java is older than ../../dotnet/runtime/Metamodel/Representations/P6capture.cs"

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

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

# To show java files that are older than their dotnet equivalents, run:
# make todolist
todolist: $(TRANSLATED_SOURCE_FILES)
Expand Down
31 changes: 17 additions & 14 deletions java/runtime/Rakudo/Init.java
Expand Up @@ -4,8 +4,10 @@
import Rakudo.Metamodel.RakudoObject;
import Rakudo.Runtime.Lexpad;
import Rakudo.Metamodel.Representations.RakudoCodeRef;
import Rakudo.Metamodel.Representations.P6capture;
import Rakudo.Runtime.Context;
import Rakudo.Runtime.ThreadContext;
import Rakudo.Runtime.CaptureHelper;
import Rakudo.Metamodel.REPRRegistry;
import Rakudo.Metamodel.KnowHOW.KnowHOWREPR;
import Rakudo.Metamodel.KnowHOW.KnowHOWBootstrapper;
Expand Down Expand Up @@ -43,7 +45,7 @@ public static ThreadContext Initialize(String SettingName)
}

// Cache native capture and LLCode type object.
// TODO CaptureHelper.CaptureTypeObject = SettingContext.LexPad.GetByName("capture");
CaptureHelper.CaptureTypeObject = settingContext.LexPad.GetByName("capture");
// TODO CodeObjectUtility.LLCodeTypeObject = (RakudoCodeRef.Instance)SettingContext.LexPad.GetByName("LLCode");

// Create an execution domain and a thread context for it.
Expand All @@ -67,14 +69,14 @@ private static void RegisterRepresentations()
if (!REPRS_Registered)
{
REPRRegistry.register_REPR("KnowHOWREPR", new KnowHOWREPR());
REPRRegistry.register_REPR("P6opaque", new P6opaque());
REPRRegistry.register_REPR("P6hash", new P6hash());
REPRRegistry.register_REPR("P6int", new P6int());
REPRRegistry.register_REPR("P6num", new P6num());
REPRRegistry.register_REPR("P6str", new P6str());
// TODO REPRRegistry.register_REPR("P6opaque", new P6opaque());
// TODO REPRRegistry.register_REPR("P6hash", new P6hash());
// TODO REPRRegistry.register_REPR("P6int", new P6int());
// TODO REPRRegistry.register_REPR("P6num", new P6num());
// TODO REPRRegistry.register_REPR("P6str", new P6str());
REPRRegistry.register_REPR("P6capture", new P6capture());
REPRRegistry.register_REPR("RakudoCodeRef", new RakudoCodeRef());
REPRRegistry.register_REPR("P6list", new P6list());
// TODO REPRRegistry.register_REPR("P6list", new P6list());
REPRS_Registered = true;
}
}
Expand All @@ -97,7 +99,7 @@ private static Context BootstrapSetting(RakudoObject KnowHOW)
REPRRegistry.get_REPR_by_name("P6int").type_object_for(null),
REPRRegistry.get_REPR_by_name("P6num").type_object_for(null),
REPRRegistry.get_REPR_by_name("P6str").type_object_for(null),
REPRRegistry.get_REPR_by_name("RakudoCodeRef").type_object_for(KnowHOW.STable.REPR.instance_of(KnowHOW)),
REPRRegistry.get_REPR_by_name("RakudoCodeRef").type_object_for(KnowHOW.getSTable().REPR.instance_of(KnowHOW)),
/* TODO
CodeObjectUtility.WrapNativeMethod((TC, self, C) =>
{
Expand All @@ -122,17 +124,18 @@ private static Context BootstrapSetting(RakudoObject KnowHOW)
public static Context LoadSetting(String Name, RakudoObject KnowHOW)
{
// Load the assembly.
var settingAssembly = AppDomain.CurrentDomain.Load(Name);
// TODO var settingAssembly = AppDomain.CurrentDomain.Load(Name);

// Find the setting type and its LoadSetting method.
var Class = settingAssembly.GetType("NQPSetting");
var Method = Class.GetMethod("LoadSetting", BindingFlags.NonPublic | BindingFlags.Static);
// TODO var Class = settingAssembly.GetType("NQPSetting");
// TODO var Method = Class.GetMethod("LoadSetting", BindingFlags.NonPublic | BindingFlags.Static);

// Run it to get the context we want.
Context settingContext = (Context)Method.Invoke(null, new object[] { });
// TODO Context settingContext = (Context)Method.Invoke(null, new object[] { });
Context settingContext = null; // TODO remove

// Fudge a few more things in.
// XXX Should be able to toss all of thse but KnowHOW.
// XXX Should be able to toss all of these but KnowHOW.
settingContext.LexPad.Extend(new String[]
{ "KnowHOW", "print", "say", "capture", "LLCode" });
settingContext.LexPad.SetByName("KnowHOW", KnowHOW);
Expand All @@ -159,7 +162,7 @@ public static Context LoadSetting(String Name, RakudoObject KnowHOW)
}));
*/
settingContext.LexPad.SetByName("capture", REPRRegistry.get_REPR_by_name("P6capture").type_object_for(null));
settingContext.LexPad.SetByName("LLCode", REPRRegistry.get_REPR_by_name("RakudoCodeRef").type_object_for(KnowHOW.STable.REPR.instance_of(KnowHOW)));
settingContext.LexPad.SetByName("LLCode", REPRRegistry.get_REPR_by_name("RakudoCodeRef").type_object_for(KnowHOW.getSTable().REPR.instance_of(KnowHOW)));

return settingContext;
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import Rakudo.Metamodel.RakudoObject;
import Rakudo.Metamodel.Representation;
import Rakudo.Metamodel.KnowHOW.KnowHOWREPR;
import Rakudo.Runtime.CodeObjectUtility;

/// <summary>
/// Contains the logic that bootstraps KnowHOW, the foundation
Expand Down Expand Up @@ -36,7 +37,7 @@ public static RakudoObject Bootstrap()
// We'll set up a dictionary of our various methods to go into
// KnowHOW's HOW, since we'll want to work with them a bit.
HashMap KnowHOWMeths = new HashMap<String, RakudoObject>();
/*
/* TODO
KnowHOWMeths.Add("new_type", CodeObjectUtility.WrapNativeMethod((TC, Ignored, Cap) =>
{
// We first create a new HOW instance.
Expand Down Expand Up @@ -100,8 +101,8 @@ public static RakudoObject Bootstrap()
// means .HOW.HOW.HOW.HOW etc will always return that, which
// closes the model up.
KnowHOWREPR.KnowHOWInstance KnowHOWHOW = (KnowHOWREPR.KnowHOWInstance)REPR.instance_of(KnowHOW);
// var KnowHOWHOW = (KnowHOWREPR.KnowHOWInstance)REPR.instance_of(KnowHOW);
// for (Iterator iter = KnowHOWMeths.entrySet.Iterator(); iter.hasNext(); )
// TODO var KnowHOWHOW = (KnowHOWREPR.KnowHOWInstance)REPR.instance_of(KnowHOW);
// TODO for (Iterator iter = KnowHOWMeths.entrySet.Iterator(); iter.hasNext(); )
// foreach (var Method in KnowHOWMeths)
{
// RakudoMethod meth = (RakudoMethod) iter.next();
Expand Down
Expand Up @@ -15,7 +15,7 @@ public final class P6capture implements Representation
/// <summary>
/// This is how a Capture looks.
/// </summary>
protected final class Instance implements RakudoObject
public final class Instance implements RakudoObject
{
public RakudoObject[] Positionals;
public HashMap<String, RakudoObject> Nameds;
Expand Down
40 changes: 14 additions & 26 deletions java/runtime/Rakudo/Metamodel/SharedTable.java
Expand Up @@ -4,6 +4,7 @@
import Rakudo.Metamodel.Representation;
import Rakudo.Metamodel.Representations.RakudoCodeRef;
import Rakudo.Runtime.ThreadContext;
import Rakudo.Runtime.CaptureHelper;
import Rakudo.Serialization.SerializationContext;

/// <summary>
Expand All @@ -19,7 +20,7 @@ public class SharedTable
/// </summary>
public IFindMethod FindMethod =
new IFindMethod() { // anonymous class instead of lambda-expression
public RakudoObject FindMethod(ThreadContext tc, RakudoObject obj, String s, int hint)
public RakudoObject FindMethod(ThreadContext tc, RakudoObject obj, String name, int hint)
{
// See if we can find it by hint.
if (hint != Hints.NO_HINT && obj.getSTable().VTable != null && hint < obj.getSTable().VTable.length)
Expand All @@ -29,37 +30,24 @@ public RakudoObject FindMethod(ThreadContext tc, RakudoObject obj, String s, int
}
else
{
// // Find the find_method method.
// RakudoObject HOW = obj.getSTable().HOW;
// RakudoObject meth = HOW.getSTable().FindMethod.FindMethod(tc, HOW, "find_method", Hints.NO_HINT);
//
// // Call it.
// RakudoObject cap = CaptureHelper.FormWith(new RakudoObject[] { HOW, Ops.box_str(tc, Name, tc.DefaultStrBoxType) });
// return meth.getSTable().Invoke.Invoke(tc, meth, cap);
return null; // TODO
}
}
};
// public Func<ThreadContext, RakudoObject, string, int, RakudoObject> FindMethod =
// (TC, Obj, Name, Hint) =>
// {
// // See if we can find it by hint.
// if (Hint != Hints.NO_HINT && Obj.STable.VTable != null && Hint < Obj.STable.VTable.Length)
// {
// // Yes, just grab it from the v-table.
// return Obj.STable.VTable[Hint];
// }
// else
// {
// // Find the find_method method.
// Find the find_method method.
RakudoObject HOW = obj.getSTable().HOW;
RakudoObject meth = HOW.getSTable().FindMethod.FindMethod(tc, HOW, "find_method", Hints.NO_HINT);

// Call it.
// TODO RakudoObject cap = CaptureHelper.FormWith(new RakudoObject[] { HOW, Ops.box_str(tc, name, tc.DefaultStrBoxType) });
// TODO return meth.getSTable().Invoke.Invoke(tc, meth, cap);
return null; // TODO remove
// // Find the find_method method. // the C# version
// var HOW = Obj.STable.HOW;
// var Meth = HOW.STable.FindMethod(TC, HOW, "find_method", Hints.NO_HINT);
//
// // Call it.
// var Cap = CaptureHelper.FormWith(new RakudoObject[] { HOW, Ops.box_str(TC, Name, TC.DefaultStrBoxType) });
// return Meth.STable.Invoke(TC, Meth, Cap);
// }
// };
}
}
};

/// <summary>
/// The default invoke looks up a postcircumfix:<( )> and runs that.
Expand Down

0 comments on commit 12b7d68

Please sign in to comment.