Skip to content

Commit

Permalink
Mysteriously forgot to commit Lexpad.cs at some point in the past. :/
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Sep 10, 2010
1 parent f8cf789 commit 6516ba1
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions 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
{
/// <summary>
/// Represents a lexpad - either the static version or the dynamic
/// one.
/// </summary>
public struct Lexpad
{
/// <summary>
/// This is the slot mapping, allocating names to slots. All the
/// dynamic variants of a lexpad will share this with the static
/// lexpad.
/// </summary>
public Dictionary<string, int> SlotMapping;

/// <summary>
/// The storage assocaited with the lexpad.
/// </summary>
public RakudoObject[] Storage;

/// <summary>
/// Creates a new static lexpad with the given names all
/// allocated slots, in the order they appear in the array.
/// </summary>
/// <param name="SlotNames"></param>
public Lexpad(string[] SlotNames)
{
SlotMapping = new Dictionary<string, int>(SlotNames.Length);
int Slot = 0;
foreach (var Name in SlotNames)
SlotMapping.Add(Name, Slot++);
Storage = new RakudoObject[SlotNames.Length];
}

/// <summary>
/// Looks up a lexical by name.
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
public RakudoObject GetByName(string Name)
{
return Storage[SlotMapping[Name]];
}

/// <summary>
/// Sets a lexical by name.
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
public RakudoObject SetByName(string Name, RakudoObject Value)
{
Storage[SlotMapping[Name]] = Value;
return Value;
}

/// <summary>
/// Extends the lexpad with an extra slot.
/// </summary>
/// <param name="Name"></param>
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<string, int>(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;
}
}
}

0 comments on commit 6516ba1

Please sign in to comment.