Skip to content

Commit

Permalink
Added some new built-ins.
Browse files Browse the repository at this point in the history
- Also added wrapper attributes to all wrapped type members for naming consistency.
- Also made Context an IEnumerable.
  • Loading branch information
Uralstech committed Nov 10, 2024
1 parent 8d980e3 commit ef941d6
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 6 deletions.
15 changes: 14 additions & 1 deletion src/Runtime/Context.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using EzrSquared.Runtime.Types;
using EzrSquared.Util;
using System;
using System.Collections;
using System.Collections.Generic;

namespace EzrSquared.Runtime;

/// <summary>
/// Stores all user defined variables/constant references, called symbols.
/// </summary>
public class Context
public class Context : IEnumerable<KeyValuePair<string, Reference>>
{
/// <summary>
/// Represents the status of a <see cref="Get(Context?, string, out Reference, AccessMod, bool)"/> call.
Expand Down Expand Up @@ -473,6 +474,18 @@ public void Release()
LinkedContexts[i]?.Release();
}

/// <inheritdoc/>
public IEnumerator<KeyValuePair<string, Reference>> GetEnumerator()
{
return _symbols.GetEnumerator();
}

/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

/// <summary>Destructor.</summary>
~Context() => Release();
}
68 changes: 67 additions & 1 deletion src/Runtime/Types/CSharpWrappers/Builtins/EzrBuiltinFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using EzrSquared.Runtime.Types.Core;
using EzrSquared.Runtime.Collections;
using EzrSquared.Runtime.Types.Collections;
using EzrSquared.Runtime.Types.Core;
using EzrSquared.Runtime.Types.Core.Errors;
using EzrSquared.Runtime.Types.Core.Numerics;
using EzrSquared.Runtime.Types.Core.Text;
using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers;
using EzrSquared.Runtime.WrapperAttributes;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -377,4 +380,67 @@ public static void Copy(SharpMethodParameters arguments)

result.Success(ReferencePool.Get(copy, AccessMod.PrivateConstant));
}

/// <summary>
/// Wraps the given ezr² object so that the runtime has access to its raw C# object.
/// </summary>
/// <remarks>
/// ezr² parameters:
/// <list type="table">
/// <item>
/// <term>to_wrap</term>
/// <description>(<see cref="IEzrObject"/>) The object to wrap.</description>
/// </item>
/// </list>
///
/// ezr² return type:
/// <see cref="IEzrObject"/>
/// </remarks>
/// <param name="arguments">The method arguments.</param>
[SharpMethodWrapper("get_raw", RequiredParameters = ["to_wrap"])]
public static void GetRaw(SharpMethodParameters arguments)
{
RuntimeResult result = arguments.Result;
IEzrObject objectToWrap = arguments.ArgumentReferences["to_wrap"].Object;

IEzrObject wrapped = new EzrSharpCompatibilityObjectInstance(objectToWrap, objectToWrap.GetType(), arguments.ExecutionContext, arguments.StartPosition, arguments.EndPosition);
result.Success(ReferencePool.Get(wrapped, AccessMod.PrivateConstant));
}

/// <summary>
/// Returns an <see cref="EzrDictionary"/> of the references (as &lt;name, object&gt;) contained in the <see cref="Context"/> of the given object.
/// </summary>
/// <remarks>
/// ezr² parameters:
/// <list type="table">
/// <item>
/// <term>to_get</term>
/// <description>(<see cref="IEzrObject"/>) The object to get the context of.</description>
/// </item>
/// </list>
///
/// ezr² return type:
/// <see cref="EzrDictionary"/>
/// </remarks>
/// <param name="arguments">The method arguments.</param>
[SharpMethodWrapper("get_context", RequiredParameters = ["to_get"])]
public static void GetContext(SharpMethodParameters arguments)
{
RuntimeResult result = arguments.Result;
IEzrObject objectToWrap = arguments.ArgumentReferences["to_get"].Object;

RuntimeEzrObjectDictionary context = new();
foreach (KeyValuePair<string, Reference> pair in objectToWrap.Context)
{
if (pair.Value.AccessibilityModifiers.HasFlag(AccessMod.Private))
continue;

context.Update(new EzrString(pair.Key, arguments.ExecutionContext, arguments.StartPosition, arguments.EndPosition), pair.Value.Object, result);
if (result.ShouldReturn)
return;
}

IEzrObject dictionary = new EzrDictionary(context, arguments.ExecutionContext, arguments.StartPosition, arguments.EndPosition);
result.Success(ReferencePool.Get(dictionary, AccessMod.PrivateConstant));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static void AddBuiltinFunctions(Context context)
EzrSharpSourceFunctionWrapper typeNameOf = new(EzrBuiltinFunctions.TypeNameOf, context, Position.None, Position.None);
EzrSharpSourceFunctionWrapper typeHashOf = new(EzrBuiltinFunctions.TypeHashOf, context, Position.None, Position.None);
EzrSharpSourceFunctionWrapper copy = new(EzrBuiltinFunctions.Copy, context, Position.None, Position.None);
EzrSharpSourceFunctionWrapper getRaw = new(EzrBuiltinFunctions.GetRaw, context, Position.None, Position.None);
EzrSharpSourceFunctionWrapper getContext = new(EzrBuiltinFunctions.GetContext, context, Position.None, Position.None);

context.Set(null, throwError.SharpFunctionName, ReferencePool.Get(throwError, AccessMod.Constant));
context.Set(null, assert.SharpFunctionName, ReferencePool.Get(assert, AccessMod.Constant));
Expand All @@ -30,6 +32,8 @@ public static void AddBuiltinFunctions(Context context)
context.Set(null, typeNameOf.SharpFunctionName, ReferencePool.Get(typeNameOf, AccessMod.Constant));
context.Set(null, typeHashOf.SharpFunctionName, ReferencePool.Get(typeHashOf, AccessMod.Constant));
context.Set(null, copy.SharpFunctionName, ReferencePool.Get(copy, AccessMod.Constant));
context.Set(null, getRaw.SharpFunctionName, ReferencePool.Get(getRaw, AccessMod.Constant));
context.Set(null, getContext.SharpFunctionName, ReferencePool.Get(getContext, AccessMod.Constant));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public override void Execute(Reference[] arguments, Interpreter interpreter, Run
break;

case { Length: 1 }:
object? argumentAsPrimitive = EzrObjectToCSharp(arguments[0].Object, SharpMember.FieldType, result);
object? convertedArgument = EzrObjectToCSharp(arguments[0].Object, SharpMember.FieldType, result);
if (result.ShouldReturn)
break;

try
{
SharpMember.SetValue(Instance, argumentAsPrimitive);
SharpMember.SetValue(Instance, convertedArgument);
result.Success(NewNothingConstant());
}
catch (Exception error)
Expand Down
2 changes: 2 additions & 0 deletions src/Runtime/Types/Collections/EzrArray.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using EzrSquared.Runtime.Types.Core.Errors;
using EzrSquared.Runtime.Types.Core.Numerics;
using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers;
using EzrSquared.Runtime.WrapperAttributes;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -26,6 +27,7 @@ public class EzrArray : EzrObject, IEzrIndexedCollection
public readonly IEzrObject[] Value;

/// <inheritdoc/>
[SharpAutoWrapper("length")]
public int Count { get; }

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Runtime/Types/Collections/EzrDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class EzrDictionary : EzrObject, IEzrMutableObject, IEzrDictionary
public readonly RuntimeEzrObjectDictionary Value;

/// <inheritdoc/>
[SharpAutoWrapper("length")]
public int Count => Value.Count;

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/Types/Collections/EzrList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class EzrList : EzrObject, IEzrMutableObject, IEzrIndexedCollection
public readonly RuntimeEzrObjectList Value;

/// <inheritdoc/>
[SharpAutoWrapper(isReadOnly: true)]
[SharpAutoWrapper("length", isReadOnly: true)]
public int Count => Value.Count;

/// <param name="elements">The base value.</param>
Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/Types/Core/Text/EzrCharacterList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class EzrCharacterList : EzrObject, IEzrMutableObject, IEzrString, IEzrIn
public string StringValue => Value.ToString();

/// <inheritdoc/>
[SharpAutoWrapper(isReadOnly: true)]
[SharpAutoWrapper("length", isReadOnly: true)]
public int Count => Value.Length;

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Runtime/Types/Core/Text/EzrString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using EzrSquared.Runtime.Types.Core.Errors;
using EzrSquared.Runtime.Types.Core.Numerics;
using EzrSquared.Runtime.Types.CSharpWrappers.CompatWrappers.ObjectMembers;
using EzrSquared.Runtime.WrapperAttributes;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -31,6 +32,7 @@ public class EzrString : EzrObject, IEzrString, IEzrIndexedCollection
public string StringValue => Value;

/// <inheritdoc/>
[SharpAutoWrapper("length")]
public int Count { get; }

/// <summary>
Expand Down

0 comments on commit ef941d6

Please sign in to comment.