Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions csharp/NShovel/Shovel/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
using System.Linq;
using System.Security.Cryptography;
using Shovel.Exceptions;
using Shovel.Vm.Types;
using Shovel.Vm;

namespace Shovel
{
Expand Down Expand Up @@ -214,6 +216,14 @@ public static Value CheckStackTop (Vm.Vm vm)
return vm.CheckStackTop ();
}

public static IEnumerable<Value> GetUsedStack( Vm.Vm vm ) {
return vm.GetUsedStack();
}

public static VmEnvironment GetCurrentEnvironment( Vm.Vm vm ) {
return vm.GetCurrentEnvironment();
}

public static void WakeUpVm (Vm.Vm vm)
{
vm.WakeUp ();
Expand Down Expand Up @@ -253,7 +263,7 @@ public static Value GetStructInstanceValue(StructInstance str, string key, Value
{
for (int i = 0; i < str.Struct.Fields.Length; i++) {
if (str.Struct.Fields[i] == key) {
return str.Values[i];
return str.values[i];
}
}
return defaultValue;
Expand All @@ -263,7 +273,7 @@ public static bool SetStructInstanceValue(StructInstance str, string key, Value
{
for (int i = 0; i < str.Struct.Fields.Length; i++) {
if (str.Struct.Fields[i] == key) {
str.Values[i] = newValue;
str.values[i] = newValue;
return true;
}
}
Expand Down
8 changes: 3 additions & 5 deletions csharp/NShovel/Shovel/Callable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Callable
// The next two fields are only used if this is a ShovelScript closure.
internal int? ProgramCounter { get; set; }

internal Vm.Types.VmEnvironment Environment { get; set; }
public Vm.VmEnvironment Environment { get; internal set; }

internal Func<VmApi, Value[], int, int, Value> RequiredPrimitive { get; set; }

Expand Down Expand Up @@ -95,8 +95,6 @@ internal static Func<VmApi, Value[], int, int, Value> MakeHostCallable (
Func<VmApi, Value, Value, Value, Value> callable)
{
return (vmapi, args, start, length) => callable (vmapi, args [start], args [start + 1], args [start + 2]);
}

}
}
}

}
21 changes: 11 additions & 10 deletions csharp/NShovel/Shovel/Serialization/VmStateSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
using System.IO;
using Shovel.Vm.Types;
using System.Text;
using System.Linq;
using System.Linq;
using Shovel.Vm;

namespace Shovel.Serialization
{
Expand Down Expand Up @@ -433,8 +434,8 @@ VmEnvFrame RebuildEnvFrame (
var result = new VmEnvFrame ();
objects [index] = result;

result.VarNames = (string[])reader (composite.Elements [0]);
result.Values = (Value[])reader (composite.Elements [1]);
result.VarNamesInternal = (string[])reader (composite.Elements [0]);
result.ValuesInernal = (Value[])reader (composite.Elements [1]);
result.IntroducedAtProgramCounter = (int)(long)reader (composite.Elements [2]);

return result;
Expand All @@ -457,7 +458,7 @@ StructInstance RebuildStructInstance (
objects [index] = result;

result.Struct = (Struct)reader (composite.Elements [0]);
result.Values = (Value[])reader (composite.Elements [1]);
result.values = (Value[])reader (composite.Elements [1]);

return result;
}
Expand Down Expand Up @@ -670,8 +671,8 @@ int SerializeEnvFrame (VmEnvFrame frame, object obj)
Elements = new int[3]
};
var result = SerializeOneHashed (composite, obj);
composite.Elements [0] = Serialize (frame.VarNames);
composite.Elements [1] = Serialize (frame.Values);
composite.Elements [0] = Serialize (frame.VarNamesInternal);
composite.Elements [1] = Serialize (frame.ValuesInernal);
composite.Elements [2] = SerializeOne (frame.IntroducedAtProgramCounter);
return result;
}
Expand All @@ -695,7 +696,7 @@ int SerializeStructInstance (StructInstance structInstance, object obj)
};
var result = SerializeOneHashed (composite, obj);
composite.Elements [0] = Serialize (structInstance.Struct);
composite.Elements [1] = SerializeShovelValueArray (structInstance.Values);
composite.Elements [1] = SerializeShovelValueArray (structInstance.values);
return result;
}

Expand All @@ -717,11 +718,11 @@ int SerializeShovelValue (Value sv, object obj)
case Value.Kinds.Hash:
return SerializeHash (sv.hashValue, obj);
case Value.Kinds.Callable:
return SerializeCallable (sv.CallableValue, obj);
return SerializeCallable (sv.callableValue, obj);
case Value.Kinds.ReturnAddress:
return SerializeReturnAddress (sv.ReturnAddressValue, obj);
return SerializeReturnAddress (sv.returnAddressValue, obj);
case Value.Kinds.NamedBlock:
return SerializeNamedBlock (sv.NamedBlockValue, obj);
return SerializeNamedBlock (sv.namedBlockValue, obj);
case Value.Kinds.Struct:
return SerializeStruct (sv.StructValue, obj);
case Value.Kinds.StructInstance:
Expand Down
5 changes: 4 additions & 1 deletion csharp/NShovel/Shovel/StructInstance.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;

namespace Shovel
{
public class StructInstance
{
internal Struct Struct;
internal Value[] Values;
internal Value[] values;

public IEnumerable<Value> Values { get { return values; } }
}
}

84 changes: 67 additions & 17 deletions csharp/NShovel/Shovel/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,67 @@ public Either<Dictionary<Value, Value>> Hash
}

[FieldOffset(8)]
internal Callable CallableValue;
internal Callable callableValue;

public Either<Callable> CallableValue {
get {
if ( Kind == Kinds.Callable ) {
return new Either<Callable>( this.callableValue );
}
else {
return new Either<Callable>( new ShovelException( "Value is not a callable.", null ) );
}
}
}

[FieldOffset(8)]
internal ReturnAddress ReturnAddressValue;
internal StructInstance structInstanceValue;

public Either<StructInstance> StructInstanceValue {
get {
if ( Kind == Kinds.StructInstance ) {
return new Either<StructInstance>( this.structInstanceValue );
}
else {
return new Either<StructInstance>( new ShovelException( "Value is not a struct instance.", null ) );
}
}
}

[FieldOffset( 8 )]
internal NamedBlock namedBlockValue;

public Either<NamedBlock> NamedBlockValue {
get {
if ( Kind == Kinds.NamedBlock ) {
return new Either<NamedBlock>( this.namedBlockValue );
}
else {
return new Either<NamedBlock>( new ShovelException( "Value is not a named block.", null ) );
}
}
}

[FieldOffset(8)]
internal NamedBlock NamedBlockValue;
internal ReturnAddress returnAddressValue;

public Either<ReturnAddress> ReturnAddressValue {
get {
if ( Kind == Kinds.ReturnAddress ) {
return new Either<ReturnAddress>( this.returnAddressValue );
}
else {
return new Either<ReturnAddress>( new ShovelException( "Value is not a return address.", null ) );
}
}
}

[FieldOffset(8)]
internal Struct StructValue;
internal Struct StructValue;

[FieldOffset(8)]
internal StructInstance structInstanceValue;
internal StructInstance structInstanceValue;

public Either<StructInstance> StructInstance
{
get
Expand Down Expand Up @@ -341,23 +391,23 @@ internal static Value Make (Callable c)
{
Value result = value;
result.Kind = Kinds.Callable;
result.CallableValue = c;
result.callableValue = c;
return result;
}

internal static Value Make (ReturnAddress ra)
{
Value result = value;
result.Kind = Kinds.ReturnAddress;
result.ReturnAddressValue = ra;
result.returnAddressValue = ra;
return result;
}

internal static Value Make (NamedBlock nb)
{
Value result = value;
result.Kind = Kinds.NamedBlock;
result.NamedBlockValue = nb;
result.namedBlockValue = nb;
return result;
}

Expand All @@ -383,15 +433,15 @@ public override bool Equals (object obj)
case Kinds.Hash:
return sv.Kind == Kinds.Hash && this.hashValue == sv.hashValue;
case Kinds.Callable:
return sv.Kind == Kinds.Callable && this.CallableValue == sv.CallableValue;
return sv.Kind == Kinds.Callable && Equals( this.callableValue, sv.callableValue );
case Kinds.ReturnAddress:
return sv.Kind == Kinds.ReturnAddress && this.ReturnAddressValue == sv.ReturnAddressValue;
return sv.Kind == Kinds.ReturnAddress && Equals( this.returnAddressValue, sv.returnAddressValue );
case Kinds.NamedBlock:
return sv.Kind == Kinds.NamedBlock && this.NamedBlockValue == sv.NamedBlockValue;
return sv.Kind == Kinds.NamedBlock && Equals( this.namedBlockValue, sv.namedBlockValue );
case Kinds.Struct:
return sv.Kind == Kinds.Struct && this.StructValue == sv.StructValue;
return sv.Kind == Kinds.Struct && Equals( this.StructValue, sv.StructValue );
case Kinds.StructInstance:
return sv.Kind == Kinds.StructInstance && this.structInstanceValue == sv.structInstanceValue;
return sv.Kind == Kinds.StructInstance && Equals( this.structInstanceValue, sv.structInstanceValue );
default:
Utils.Panic ();
throw new InvalidOperationException ();
Expand All @@ -416,11 +466,11 @@ public override int GetHashCode ()
case Kinds.Hash:
return this.hashValue.GetHashCode ();
case Kinds.Callable:
return this.CallableValue.GetHashCode ();
return this.callableValue.GetHashCode ();
case Kinds.ReturnAddress:
return this.ReturnAddressValue.GetHashCode ();
return this.returnAddressValue.GetHashCode ();
case Kinds.NamedBlock:
return this.NamedBlockValue.GetHashCode ();
return this.namedBlockValue.GetHashCode ();
case Kinds.Struct:
return this.StructValue.GetHashCode();
case Kinds.StructInstance:
Expand Down
26 changes: 13 additions & 13 deletions csharp/NShovel/Shovel/Vm/Prim0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ static Value HashToStruct (VmApi api, Value[] args, int start, int length)
var ztruct = args [start].StructValue;
var result = new StructInstance ();
result.Struct = ztruct;
result.Values = new Value[ztruct.Fields.Length];
result.values = new Value[ztruct.Fields.Length];
var hash = args [start + 1].hashValue;
var sizeIncrease = 1 + ztruct.Fields.Length;
api.CellsIncrementHerald (sizeIncrease);
for (int i = 0; i < ztruct.Fields.Length; i++) {
var svKey = Value.Make (ztruct.Fields [i]);
if (hash.ContainsKey (svKey)) {
result.Values [i] = hash [svKey];
result.values [i] = hash [svKey];
}
}
api.CellsIncrementer (sizeIncrease);
Expand All @@ -127,7 +127,7 @@ static Value StructToHash (VmApi api, Value[] args, int start, int length)
var sizeIncrease = 1 + 2 * ztruct.Fields.Length;
api.CellsIncrementHerald (sizeIncrease);
for (int i = 0; i < ztruct.Fields.Length; i++) {
result [Value.Make (ztruct.Fields [i])] = structInstance.Values [i];
result [Value.Make (ztruct.Fields [i])] = structInstance.values [i];
}
api.CellsIncrementer (sizeIncrease);
return Value.Make (result);
Expand All @@ -146,9 +146,9 @@ static Value InstantiateStruct (VmApi api, Value[] args, int start, int length)
api.CellsIncrementHerald (sizeIncrease);
var result = new StructInstance ();
result.Struct = ztruct;
result.Values = new Value[ztruct.Fields.Length];
result.values = new Value[ztruct.Fields.Length];
for (int i = 1; i < length; i++) {
result.Values [i - 1] = args [start + i];
result.values [i - 1] = args [start + i];
}
api.CellsIncrementer (sizeIncrease);
return Value.Make (result);
Expand Down Expand Up @@ -918,12 +918,12 @@ internal static bool HashOrStructGetDot (Vm vm, VmApi api, ref Value obj, ref Va
if (cache != null) {
var info = (Tuple<Struct, int>)cache;
if (info.Item1 == ztruct) {
obj = structInstance.Values [info.Item2];
obj = structInstance.values [info.Item2];
return true;
}
}
int location = FindLocationInStruct (api, ztruct, index.stringValue);
obj = structInstance.Values [location];
obj = structInstance.values [location];
vm.SetCurrentCache (Tuple.Create (ztruct, location));
} else if (obj.Kind == Value.Kinds.Hash) {
if (!obj.hashValue.ContainsKey (index)) {
Expand Down Expand Up @@ -951,12 +951,12 @@ internal static bool HashOrStructDotSet (
if (cache != null) {
var info = (Tuple<Struct, int>)cache;
if (info.Item1 == ztruct) {
structInstance.Values [info.Item2] = value;
structInstance.values [info.Item2] = value;
return true;
}
}
int location = FindLocationInStruct (api, ztruct, index.stringValue);
structInstance.Values [location] = value;
structInstance.values [location] = value;
vm.SetCurrentCache (Tuple.Create (ztruct, location));
} else if (obj.Kind == Value.Kinds.Hash) {
return HashSet(api, ref obj, ref index, ref value);
Expand Down Expand Up @@ -1062,12 +1062,12 @@ static void AdjustRealStartEnd (VmApi api, ref int realStart, ref int realEnd, i

static Value SetHandlers(VmApi api, Value arrayOrHash, Value getter, Value setter)
{
if (getter.Kind != Value.Kinds.Null && (getter.Kind != Value.Kinds.Callable || getter.CallableValue.Arity != 2))
if (getter.Kind != Value.Kinds.Null && (getter.Kind != Value.Kinds.Callable || getter.callableValue.Arity != 2))
{
api.RaiseShovelError("The second parameter (getter) should be a callable with 2 parameters.");
throw new InvalidOperationException();
}
if (setter.Kind != Value.Kinds.Null && (setter.Kind != Value.Kinds.Callable || setter.CallableValue.Arity != 3))
if (setter.Kind != Value.Kinds.Null && (setter.Kind != Value.Kinds.Callable || setter.callableValue.Arity != 3))
{
api.RaiseShovelError("The third parameter (setter) should be a callable with 3 parameters.");
throw new InvalidOperationException();
Expand Down Expand Up @@ -1394,8 +1394,8 @@ private static string ShovelStringRepresentationImpl (
var structInstance = obj.structInstanceValue;
var ztruct = structInstance.Struct;
pieces.Add (StructAsString (ztruct));
for (var i = 0; i < structInstance.Values.Length; i++) {
pieces.Add (ShovelStringRepresentationImpl (api, structInstance.Values [i], visited));
for (var i = 0; i < structInstance.values.Length; i++) {
pieces.Add (ShovelStringRepresentationImpl (api, structInstance.values [i], visited));
}
sb.Append (String.Join (", ", pieces));
sb.Append (")");
Expand Down
13 changes: 6 additions & 7 deletions csharp/NShovel/Shovel/Vm/Types/NamedBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@

namespace Shovel.Vm.Types
{
internal class NamedBlock
{
internal string Name { get; set; }
public class NamedBlock
{
internal string Name { get; set; }

internal int BlockEnd { get; set; }

internal VmEnvironment Environment { get; set; }
}
internal int BlockEnd { get; set; }

public VmEnvironment Environment { get; internal set; }
}
}

Loading