From 6516ba1931e449bd223f9e4891d2d7842b351084 Mon Sep 17 00:00:00 2001 From: Jonathan Worthington Date: Fri, 10 Sep 2010 22:54:17 +0200 Subject: [PATCH] Mysteriously forgot to commit Lexpad.cs at some point in the past. :/ --- dotnet/runtime/Runtime/Lexpad.cs | 83 ++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 dotnet/runtime/Runtime/Lexpad.cs diff --git a/dotnet/runtime/Runtime/Lexpad.cs b/dotnet/runtime/Runtime/Lexpad.cs new file mode 100644 index 0000000..b8003e7 --- /dev/null +++ b/dotnet/runtime/Runtime/Lexpad.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Rakudo.Metamodel; + +namespace Rakudo.Runtime +{ + /// + /// Represents a lexpad - either the static version or the dynamic + /// one. + /// + public struct Lexpad + { + /// + /// This is the slot mapping, allocating names to slots. All the + /// dynamic variants of a lexpad will share this with the static + /// lexpad. + /// + public Dictionary SlotMapping; + + /// + /// The storage assocaited with the lexpad. + /// + public RakudoObject[] Storage; + + /// + /// Creates a new static lexpad with the given names all + /// allocated slots, in the order they appear in the array. + /// + /// + public Lexpad(string[] SlotNames) + { + SlotMapping = new Dictionary(SlotNames.Length); + int Slot = 0; + foreach (var Name in SlotNames) + SlotMapping.Add(Name, Slot++); + Storage = new RakudoObject[SlotNames.Length]; + } + + /// + /// Looks up a lexical by name. + /// + /// + /// + public RakudoObject GetByName(string Name) + { + return Storage[SlotMapping[Name]]; + } + + /// + /// Sets a lexical by name. + /// + /// + /// + public RakudoObject SetByName(string Name, RakudoObject Value) + { + Storage[SlotMapping[Name]] = Value; + return Value; + } + + /// + /// Extends the lexpad with an extra slot. + /// + /// + public void Extend(string[] Names) + { + // Add new entry to the mapping. Note that we re-build + // it and make it unique to this lexpad now, even if it + // was shared before, but add the extra entry. + SlotMapping = new Dictionary(SlotMapping); + int NewSlot = Storage.Length; + foreach (var Name in Names) + SlotMapping.Add(Name, NewSlot++); + + // Reallocate enlarged storage. + var NewStorage = new RakudoObject[Storage.Length + Names.Length]; + for (int i = 0; i < Storage.Length; i++) + NewStorage[i] = Storage[i]; + Storage = NewStorage; + } + } +}