Skip to content

Commit

Permalink
Revamped executables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Uralstech committed Aug 8, 2024
1 parent db98d08 commit 652b073
Show file tree
Hide file tree
Showing 20 changed files with 490 additions and 138 deletions.
1 change: 1 addition & 0 deletions src/Runtime/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using EzrSquared.Runtime.Types.Core.Numerics;
using EzrSquared.Runtime.Types.Core.Text;
using EzrSquared.Runtime.Types.CSharpWrappers.SourceWrappers;
using EzrSquared.Runtime.Types.Executables;
using System;
using System.Collections.Generic;
using System.Numerics;
Expand Down
6 changes: 3 additions & 3 deletions src/Runtime/Types/BaseTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public int HashTag
/// <summary>
/// Creates a new object with the specified parent context and position.
/// </summary>
/// <param name="creationContext">The context in which this object was created in.</param>
/// <param name="parentContext">The context in which this object was created.</param>
/// <param name="startPosition">The starting position of the object.</param>
/// <param name="endPosition">The ending position of the object.</param>
public EzrObject(Context creationContext, Position startPosition, Position endPosition)
public EzrObject(Context parentContext, Position startPosition, Position endPosition)
{
StartPosition = startPosition;
EndPosition = endPosition;

_executionContext = _creationContext = creationContext;
_executionContext = _creationContext = parentContext;
Context = new Context(TypeName, false, startPosition, _creationContext, _creationContext.StaticContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,25 @@ public class EzrSharpCompatibilityType : EzrSharpCompatibilityWrapper
/// <inheritdoc/>
public override string Tag { get; protected internal set; } = "ezrSquared.CSharpType";

[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicFields
| DynamicallyAccessedMemberTypes.PublicMethods
| DynamicallyAccessedMemberTypes.PublicProperties
| DynamicallyAccessedMemberTypes.PublicConstructors
| DynamicallyAccessedMemberTypes.NonPublicConstructors
)] public readonly Type SharpType;
public readonly Type SharpType;

Check warning on line 20 in src/Runtime/Types/CSharpWrappers/CompatWrappers/EzrSharpCompatibilityType.cs

View workflow job for this annotation

GitHub Actions / publish-docs

Missing XML comment for publicly visible type or member 'EzrSharpCompatibilityType.SharpType'

public readonly string SharpTypeName;

Check warning on line 22 in src/Runtime/Types/CSharpWrappers/CompatWrappers/EzrSharpCompatibilityType.cs

View workflow job for this annotation

GitHub Actions / publish-docs

Missing XML comment for publicly visible type or member 'EzrSharpCompatibilityType.SharpTypeName'

public EzrSharpCompatibilityType(

Check warning on line 24 in src/Runtime/Types/CSharpWrappers/CompatWrappers/EzrSharpCompatibilityType.cs

View workflow job for this annotation

GitHub Actions / publish-docs

Missing XML comment for publicly visible type or member 'EzrSharpCompatibilityType.EzrSharpCompatibilityType(string, Type, RuntimeResult, Context, Position, Position)'
string name,

[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicFields
| DynamicallyAccessedMemberTypes.PublicMethods
| DynamicallyAccessedMemberTypes.PublicProperties
| DynamicallyAccessedMemberTypes.PublicConstructors
| DynamicallyAccessedMemberTypes.NonPublicConstructors
)]Type type,
)] Type type,

RuntimeResult result, Context parentContext, Position startPosition, Position endPosition) : base(parentContext, startPosition, endPosition)
{
SharpType = type;
SharpTypeName = Utils.PascalToSnakeCase(SharpType.Name);
SharpTypeName = name;
Tag = $"{Tag}.{SharpTypeName}.{Utils.GetNextUniqueId()}";

if (SharpType.IsGenericType)
Expand All @@ -49,7 +44,7 @@ public EzrSharpCompatibilityType(
return;
}

MethodInfo[] publicStaticMethods = SharpType.GetMethods(BindingFlags.Static | BindingFlags.Public);
MethodInfo[] publicStaticMethods = type.GetMethods(BindingFlags.Static | BindingFlags.Public);
Dictionary<string, int> duplicateNames = new(publicStaticMethods.Length);
for (int i = 0; i < publicStaticMethods.Length; i++)
{
Expand All @@ -76,32 +71,45 @@ public EzrSharpCompatibilityType(
Context.Set(null, methodObjectName, ReferencePool.Get(methodObject, AccessMod.Constant));
}

PropertyInfo[] publicStaticProperties = SharpType.GetProperties(BindingFlags.Static | BindingFlags.Public);
PropertyInfo[] publicStaticProperties = type.GetProperties(BindingFlags.Static | BindingFlags.Public);
for (int i = 0; i < publicStaticProperties.Length; i++)
{
EzrSharpCompatibilityProperty property = new(publicStaticProperties[i], null, Context, StartPosition, EndPosition);
Context.Set(null, property.SharpPropertyName, ReferencePool.Get(property, AccessMod.Constant));
}

FieldInfo[] publicStaticFields = SharpType.GetFields(BindingFlags.Static | BindingFlags.Public);
FieldInfo[] publicStaticFields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
for (int i = 0; i < publicStaticFields.Length; i++)
{
EzrSharpCompatibilityField field = new(publicStaticFields[i], null, Context, StartPosition, EndPosition);
Context.Set(null, field.SharpFieldName, ReferencePool.Get(field, AccessMod.Constant));
}

ConstructorInfo[] publicConstructors = SharpType.GetConstructors();
ConstructorInfo[] publicConstructors = type.GetConstructors();
for (int i = 0; i < publicConstructors.Length; i++)
{
ConstructorInfo constructor = publicConstructors[i];
if (!constructor.IsPublic)
continue;

IEzrObject constructorObject = new EzrSharpCompatibilityConstructor(constructor, SharpType, Context, StartPosition, EndPosition);
IEzrObject constructorObject = new EzrSharpCompatibilityConstructor(constructor, type, Context, StartPosition, EndPosition);
Context.Set(null, $"make_{i}", ReferencePool.Get(constructorObject, AccessMod.Constant));
}
}

public EzrSharpCompatibilityType(

Check warning on line 100 in src/Runtime/Types/CSharpWrappers/CompatWrappers/EzrSharpCompatibilityType.cs

View workflow job for this annotation

GitHub Actions / publish-docs

Missing XML comment for publicly visible type or member 'EzrSharpCompatibilityType.EzrSharpCompatibilityType(Type, RuntimeResult, Context, Position, Position)'

[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicFields
| DynamicallyAccessedMemberTypes.PublicMethods
| DynamicallyAccessedMemberTypes.PublicProperties
| DynamicallyAccessedMemberTypes.PublicConstructors
| DynamicallyAccessedMemberTypes.NonPublicConstructors
)] Type type,

RuntimeResult result, Context parentContext, Position startPosition, Position endPosition)
: this(Utils.PascalToSnakeCase(type.Name), type, result, parentContext, startPosition, endPosition) { }

/// <inheritdoc/>
public override int ComputeHashCode(RuntimeResult result)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ public abstract class EzrSharpCompatibilityWrapper : EzrObject

public EzrSharpCompatibilityWrapper(Context parentContext, Position startPosition, Position endPosition) : base(parentContext, startPosition, endPosition) { }

/// <summary>
/// Checks if the given type is supported by the primitive compatibility wrappers.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns><see langword="true"/> if yes, <see langword="false"/> otherwise.</returns>
public static bool IsSupportedPrimitiveType(Type type)
{
TypeCode typeCode = Type.GetTypeCode(type);
return typeCode is TypeCode.Empty
or TypeCode.Int16
or TypeCode.Int32
or TypeCode.Int64
or TypeCode.UInt16
or TypeCode.UInt32
or TypeCode.UInt64
or TypeCode.Byte
or TypeCode.SByte
or TypeCode.Single
or TypeCode.Double
or TypeCode.Decimal
or TypeCode.Boolean
or TypeCode.Char
or TypeCode.String;
}

protected internal object? EzrObjectToPrimitive(IEzrObject value, TypeCode typeCode, RuntimeResult result)
{
switch (typeCode)
Expand Down Expand Up @@ -130,6 +155,12 @@ public EzrSharpCompatibilityWrapper(Context parentContext, Position startPositio
if (value is EzrFloat doubleValue)
return doubleValue.Value;

result.Failure(new EzrUnexpectedTypeError($"Expected float, but got object of type \"{value.TypeName}\"!", Context, value.StartPosition, value.EndPosition));
break;
case TypeCode.Decimal:
if (value is EzrFloat decimalValue)
return (decimal)decimalValue.Value;

result.Failure(new EzrUnexpectedTypeError($"Expected float, but got object of type \"{value.TypeName}\"!", Context, value.StartPosition, value.EndPosition));
break;
case TypeCode.Boolean:
Expand All @@ -149,8 +180,10 @@ public EzrSharpCompatibilityWrapper(Context parentContext, Position startPositio
return stringValue.Value;
else if (value is EzrCharacterList characterListValue)
return characterListValue.StringValue;
else if (value is EzrCharacter stringCharacterValue)
return stringCharacterValue.Value.ToString();

result.Failure(new EzrUnexpectedTypeError($"Expected string or character list, but got object of type \"{value.TypeName}\"!", Context, value.StartPosition, value.EndPosition));
result.Failure(new EzrUnexpectedTypeError($"Expected string, character or character list, but got object of type \"{value.TypeName}\"!", Context, value.StartPosition, value.EndPosition));
break;
case TypeCode.Empty:
if (value is EzrNothing)
Expand All @@ -159,7 +192,7 @@ public EzrSharpCompatibilityWrapper(Context parentContext, Position startPositio
result.Failure(new EzrUnexpectedTypeError($"Expected type nothing, but got object of type \"{value.TypeName}\"!", Context, value.StartPosition, value.EndPosition));
break;
default:
result.Failure(new EzrUnsupportedWrappingError($"Object of type \"{value.TypeName}\" cannot be converted from to CSharp type!", Context, value.StartPosition, value.EndPosition));
result.Failure(new EzrUnsupportedWrappingError($"Object of type \"{value.TypeName}\" cannot be converted to CSharp type \"{typeCode}\"!", Context, value.StartPosition, value.EndPosition));
break;
}

Expand Down Expand Up @@ -200,10 +233,7 @@ protected internal void PrimitiveToEzrObject(object? value, TypeCode typeCode, R
case TypeCode.SByte:
result.Success(NewIntegerConstant((sbyte)value));
break;
case TypeCode.Single:
result.Success(NewFloatConstant((float)value));
break;
case TypeCode.Double:
case TypeCode.Single or TypeCode.Double or TypeCode.Decimal:
result.Success(NewFloatConstant((double)value));
break;
case TypeCode.Boolean:
Expand All @@ -216,7 +246,7 @@ protected internal void PrimitiveToEzrObject(object? value, TypeCode typeCode, R
result.Success(NewStringConstant((string)value));
break;
default:
result.Failure(new EzrUnsupportedWrappingError($"CSharp type \"{value.GetType().Name}\" cannot be converted to EzrSquared type!", Context, StartPosition, EndPosition));
result.Failure(new EzrUnsupportedWrappingError($"CSharp type \"{value.GetType().Name}\" cannot be converted to an ezr² type!", Context, StartPosition, EndPosition));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class EzrSharpCompatibilityConstructor : EzrSharpCompatibilityExecutable
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)]
public readonly Type ConstructingType;
public readonly string ConstructingTypeName;
public readonly ConstructorInfo SharpConstructor;

public EzrSharpCompatibilityConstructor(ConstructorInfo sharpConstructor,

Expand All @@ -21,7 +20,6 @@ public EzrSharpCompatibilityConstructor(ConstructorInfo sharpConstructor,

Context context, Position startPosition, Position endPosition) : base(sharpConstructor, null, context, startPosition, endPosition)
{
SharpConstructor = sharpConstructor;
ConstructingType = constructType;
ConstructingTypeName = Utils.PascalToSnakeCase(ConstructingType.Name);
}
Expand All @@ -38,7 +36,7 @@ public override void Execute(Reference[] arguments, Interpreter interpreter, Run

try
{
object? output = SharpConstructor.Invoke(mappedArguments);
object? output = ((ConstructorInfo)Executable).Invoke(mappedArguments);

if (output is null)
result.Success(NewNothingConstant());
Expand All @@ -58,18 +56,6 @@ public override void Execute(Reference[] arguments, Interpreter interpreter, Run
}
}

/// <inheritdoc/>
public override bool StrictEquals(IEzrObject other, RuntimeResult result)
{
return (other as EzrSharpCompatibilityConstructor)?.SharpConstructor == SharpConstructor && other.HashTag == HashTag;
}

/// <inheritdoc/>
public override int ComputeHashCode(RuntimeResult result)
{
return HashCode.Combine(HashTag, SharpConstructor);
}

/// <inheritdoc/>
public override string ToString(RuntimeResult result)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ public abstract class EzrSharpCompatibilityExecutable : EzrSharpCompatibilityWra

public readonly ParameterInfo[] Parameters;
public readonly string[] ParameterNames;
public readonly MethodBase Executable;
public readonly object? Instance;
public readonly string SharpRuntimeExecutableName;

public EzrSharpCompatibilityExecutable(MethodBase sharpMember, object? instance, Context context, Position startPosition, Position endPosition) : base(context, startPosition, endPosition)
public EzrSharpCompatibilityExecutable(string name, MethodBase sharpMember, object? instance, Context context, Position startPosition, Position endPosition) : base(context, startPosition, endPosition)
{
SharpRuntimeExecutableName = Utils.PascalToSnakeCase(sharpMember.Name);
SharpRuntimeExecutableName = name;
Tag = $"{Tag}.{SharpRuntimeExecutableName}.{Utils.GetNextUniqueId()}";

Parameters = sharpMember.GetParameters();

Executable = sharpMember;
Parameters = Executable.GetParameters();
ParameterNames = Array.ConvertAll(Parameters, p => Utils.PascalToSnakeCase(p.Name ?? string.Empty));
Instance = instance;
}

public EzrSharpCompatibilityExecutable(MethodBase sharpMember, object? instance, Context context, Position startPosition, Position endPosition)
: this(Utils.PascalToSnakeCase(sharpMember.Name), sharpMember, instance, context, startPosition, endPosition) { }

protected internal Dictionary<string, IEzrObject> ArgumentsArrayToDictionary(Reference[] arguments, RuntimeResult result)
{
Dictionary<string, IEzrObject> formattedArguments = new(arguments.Length);
Expand Down Expand Up @@ -123,4 +128,19 @@ protected internal Dictionary<string, IEzrObject> ArgumentsArrayToDictionary(Ref

return formattedArguments;
}

/// <inheritdoc/>
public override int ComputeHashCode(RuntimeResult result)
{
return HashCode.Combine(HashTag, Executable);
}

/// <inheritdoc/>
public override bool StrictEquals(IEzrObject other, RuntimeResult result)
{
return other is EzrSharpCompatibilityExecutable executable
&& executable.Executable == Executable
&& executable.Instance?.GetHashCode() == Instance?.GetHashCode()
&& other.HashTag == HashTag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ public class EzrSharpCompatibilityFunction : EzrSharpCompatibilityExecutable
/// <inheritdoc/>
public override string Tag { get; protected internal set; } = "ezrSquared.CSharpFunction";

public readonly MethodInfo SharpFunction;
public EzrSharpCompatibilityFunction(string name, MethodInfo sharpFunction, object? instance, Context context, Position startPosition, Position endPosition)
: base(name, sharpFunction, instance, context, startPosition, endPosition) { }

public EzrSharpCompatibilityFunction(MethodInfo sharpFunction, object? instance, Context context, Position startPosition, Position endPosition) : base(sharpFunction, instance, context, startPosition, endPosition)
{
SharpFunction = sharpFunction;
}
public EzrSharpCompatibilityFunction(MethodInfo sharpFunction, object? instance, Context context, Position startPosition, Position endPosition)
: base(sharpFunction, instance, context, startPosition, endPosition) { }

public override void Execute(Reference[] arguments, Interpreter interpreter, RuntimeResult result)
{
Expand All @@ -32,7 +31,7 @@ public override void Execute(Reference[] arguments, Interpreter interpreter, Run

try
{
object? output = SharpFunction.Invoke(Instance, mappedArguments);
object? output = Executable.Invoke(Instance, mappedArguments);

if (output is null)
result.Success(NewNothingConstant());
Expand All @@ -46,21 +45,6 @@ public override void Execute(Reference[] arguments, Interpreter interpreter, Run
}
}

/// <inheritdoc/>
public override int ComputeHashCode(RuntimeResult result)
{
return HashCode.Combine(HashTag, SharpFunction);
}

/// <inheritdoc/>
public override bool StrictEquals(IEzrObject other, RuntimeResult result)
{
return other is EzrSharpCompatibilityFunction function
&& function.SharpFunction == SharpFunction
&& function.Instance?.GetHashCode() == Instance?.GetHashCode()
&& other.HashTag == HashTag;
}

/// <inheritdoc/>
public override string ToString(RuntimeResult result)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ public class EzrSharpCompatibilityField : EzrSharpCompatibilityWrapper
public readonly object? Instance;
public readonly string SharpFieldName;

public EzrSharpCompatibilityField(FieldInfo sharpField, object? instance, Context parentContext, Position startPosition, Position endPosition) : base(parentContext, startPosition, endPosition)
public EzrSharpCompatibilityField(string name, FieldInfo sharpField, object? instance, Context parentContext, Position startPosition, Position endPosition) : base(parentContext, startPosition, endPosition)
{
SharpField = sharpField;
Instance = instance;
SharpFieldName = Utils.PascalToSnakeCase(SharpField.Name);
SharpFieldName = name;
Tag = $"{Tag}.{SharpFieldName}.{Utils.GetNextUniqueId()}";
}

public EzrSharpCompatibilityField(FieldInfo sharpField, object? instance, Context parentContext, Position startPosition, Position endPosition)
: this(Utils.PascalToSnakeCase(sharpField.Name), sharpField, instance, parentContext, startPosition, endPosition) { }

public override void Execute(Reference[] arguments, Interpreter interpreter, RuntimeResult result)
{
if (arguments.Length > 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ public class EzrSharpCompatibilityProperty : EzrSharpCompatibilityWrapper
public readonly object? Instance;
public readonly string SharpPropertyName;

public EzrSharpCompatibilityProperty(PropertyInfo sharpProperty, object? instance, Context parentContext, Position startPosition, Position endPosition) : base(parentContext, startPosition, endPosition)
public EzrSharpCompatibilityProperty(string name, PropertyInfo sharpProperty, object? instance, Context parentContext, Position startPosition, Position endPosition) : base(parentContext, startPosition, endPosition)
{
SharpProperty = sharpProperty;
Instance = instance;
SharpPropertyName = Utils.PascalToSnakeCase(SharpProperty.Name);
SharpPropertyName = name;
Tag = $"{Tag}.{SharpPropertyName}.{Utils.GetNextUniqueId()}";
}

public EzrSharpCompatibilityProperty(PropertyInfo sharpProperty, object? instance, Context parentContext, Position startPosition, Position endPosition)
: this(Utils.PascalToSnakeCase(sharpProperty.Name), sharpProperty, instance, parentContext, startPosition, endPosition) { }

public override void Execute(Reference[] arguments, Interpreter interpreter, RuntimeResult result)
{
if (arguments.Length > 1)
Expand Down
Loading

0 comments on commit 652b073

Please sign in to comment.