From eae223f3921747ec858ffa3c9235199f6088de77 Mon Sep 17 00:00:00 2001 From: Jonathan Worthington Date: Sun, 22 Aug 2010 22:00:26 +0200 Subject: [PATCH] Add a first cut of a P6list representation and a list sub that makes one (hardcoded rather than in the setting for now). Stub in NQPList in the setting; expect we'll base NQPArray on this representation too. --- common/NQP/NQPSetting.pm | 6 + dotnet/runtime/Init.cs | 21 ++- .../Metamodel/Representations/P6list.cs | 121 ++++++++++++++++++ dotnet/runtime/Rakudo.Net.csproj | 1 + 4 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 dotnet/runtime/Metamodel/Representations/P6list.cs diff --git a/common/NQP/NQPSetting.pm b/common/NQP/NQPSetting.pm index 802b0de..1f24dde 100644 --- a/common/NQP/NQPSetting.pm +++ b/common/NQP/NQPSetting.pm @@ -47,6 +47,12 @@ knowhow NQPNum is repr('P6num') { } } +knowhow NQPList is repr('P6list') { +} + +knowhow NQPArray is repr('P6list') { +} + ## XXX All of these should become multi when we can do that. sub &infix:<==>($x, $y) { diff --git a/dotnet/runtime/Init.cs b/dotnet/runtime/Init.cs index d08b2da..78491b7 100644 --- a/dotnet/runtime/Init.cs +++ b/dotnet/runtime/Init.cs @@ -73,6 +73,7 @@ private static void RegisterRepresentations() REPRRegistry.register_REPR("P6str", new P6str()); REPRRegistry.register_REPR("P6capture", new P6capture()); REPRRegistry.register_REPR("RakudoCodeRef", new RakudoCodeRef()); + REPRRegistry.register_REPR("P6list", new P6list()); REPRS_Registered = true; } } @@ -89,6 +90,11 @@ private static Context BootstrapSetting(RakudoObject KnowHOW) SettingContext.LexPad = new Dictionary() { { "KnowHOW", KnowHOW }, + { "capture", REPRRegistry.get_REPR_by_name("P6capture").type_object_for(null) }, + { "NQPInt", REPRRegistry.get_REPR_by_name("P6int").type_object_for(null) }, + { "NQPNum", REPRRegistry.get_REPR_by_name("P6num").type_object_for(null) }, + { "NQPStr", REPRRegistry.get_REPR_by_name("P6str").type_object_for(null) }, + { "LLCode", REPRRegistry.get_REPR_by_name("RakudoCodeRef").type_object_for(KnowHOW.STable.REPR.instance_of(KnowHOW)) }, { "print", CodeObjectUtility.WrapNativeMethod((TC, self, C) => { var Value = CaptureHelper.GetPositional(C, 0); @@ -107,11 +113,16 @@ private static Context BootstrapSetting(RakudoObject KnowHOW) return CaptureHelper.Nil(); }) }, - { "capture", REPRRegistry.get_REPR_by_name("P6capture").type_object_for(null) }, - { "NQPInt", REPRRegistry.get_REPR_by_name("P6int").type_object_for(null) }, - { "NQPNum", REPRRegistry.get_REPR_by_name("P6num").type_object_for(null) }, - { "NQPStr", REPRRegistry.get_REPR_by_name("P6str").type_object_for(null) }, - { "LLCode", REPRRegistry.get_REPR_by_name("RakudoCodeRef").type_object_for(KnowHOW.STable.REPR.instance_of(KnowHOW)) } + { "list", CodeObjectUtility.WrapNativeMethod((TC, self, C) => + { + var NQPList = Ops.get_lex(TC, "NQPList"); + var List = NQPList.STable.REPR.instance_of(NQPList) as P6list.Instance; + var NativeCapture = C as P6capture.Instance; + foreach (var Obj in NativeCapture.Positionals) + List.Storage.Add(Obj); + return List; + }) + } }; return SettingContext; } diff --git a/dotnet/runtime/Metamodel/Representations/P6list.cs b/dotnet/runtime/Metamodel/Representations/P6list.cs new file mode 100644 index 0000000..78380db --- /dev/null +++ b/dotnet/runtime/Metamodel/Representations/P6list.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rakudo.Metamodel.Representations +{ + /// + /// This is a very first cut at a list representation. Essentially, + /// it just knows how to store a list of objects at the moment. At + /// some point we need to define the way that it will handle compact + /// arrays. + /// + public class P6list : Representation + { + internal class Instance : RakudoObject + { + /// + /// Just use a .Net List at the moment, but an array would + /// be more efficient in the long run (though give us more + /// stuff to implement ourselves). + /// + public List Storage; + public Instance(SharedTable STable) + { + this.STable = STable; + } + } + + /// + /// Create a new type object. + /// + /// + /// + public override RakudoObject type_object_for(RakudoObject MetaPackage) + { + var STable = new SharedTable(); + STable.HOW = MetaPackage; + STable.REPR = this; + STable.WHAT = new Instance(STable); + return STable.WHAT; + } + + /// + /// Creates an instance of the type with the given type object. + /// + /// + /// + public override RakudoObject instance_of(RakudoObject WHAT) + { + var Object = new Instance(WHAT.STable); + Object.Storage = new List(); + return Object; + } + + /// + /// Determines if the representation is defined or not. + /// + /// + /// + public override bool defined(RakudoObject Obj) + { + return ((Instance)Obj).Storage != null; + } + + public override RakudoObject get_attribute(RakudoObject Object, RakudoObject ClassHandle, string Name) + { + throw new NotImplementedException(); + } + + public override RakudoObject get_attribute_with_hint(RakudoObject Object, RakudoObject ClassHandle, string Name, int Hint) + { + throw new NotImplementedException(); + } + + public override void bind_attribute(RakudoObject Object, RakudoObject ClassHandle, string Name, RakudoObject Value) + { + throw new NotImplementedException(); + } + + public override void bind_attribute_with_hint(RakudoObject Object, RakudoObject ClassHandle, string Name, int Hint, RakudoObject Value) + { + throw new NotImplementedException(); + } + + public override int hint_for(RakudoObject ClassHandle, string Name) + { + throw new NotImplementedException(); + } + + public override void set_int(RakudoObject Object, int Value) + { + throw new NotImplementedException(); + } + + public override int get_int(RakudoObject Object) + { + throw new NotImplementedException(); + } + + public override void set_num(RakudoObject Object, double Value) + { + throw new NotImplementedException(); + } + + public override double get_num(RakudoObject Object) + { + throw new NotImplementedException(); + } + + public override void set_str(RakudoObject Object, string Value) + { + throw new NotImplementedException(); + } + + public override string get_str(RakudoObject Object) + { + throw new NotImplementedException(); + } + } +} diff --git a/dotnet/runtime/Rakudo.Net.csproj b/dotnet/runtime/Rakudo.Net.csproj index 848a99c..8ecd355 100644 --- a/dotnet/runtime/Rakudo.Net.csproj +++ b/dotnet/runtime/Rakudo.Net.csproj @@ -56,6 +56,7 @@ +