Skip to content

Commit

Permalink
Changed types to be reflected as objects
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed May 21, 2021
1 parent f0fde2b commit 9095cc7
Show file tree
Hide file tree
Showing 19 changed files with 280 additions and 95 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Changed the range operator from `:` to `..` (#76)
- Changed library target to .NET Standard 2 (#68)
- Changed application to run on .NET Core 3.1 (#68)
- Changed types to be reflected as objects (#60)

# 1.6.1

Expand Down
14 changes: 7 additions & 7 deletions src/Mages.Core.Tests/FunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,49 +326,49 @@ public void ListWithOneEntryAddNewEntryWithIndexSetAccessor()
[Test]
public void TypeOfNothingIsUndefined()
{
var result = "type(null)".Eval();
var result = "type(null).name".Eval();
Assert.AreEqual("Undefined", result);
}

[Test]
public void TypeOfMatrixIsMatrix()
{
var result = "type([])".Eval();
var result = "type([]).name".Eval();
Assert.AreEqual("Matrix", result);
}

[Test]
public void TypeOfDictionaryIsObject()
{
var result = "type(new {})".Eval();
var result = "type(new {}).name".Eval();
Assert.AreEqual("Object", result);
}

[Test]
public void TypeOfStringIsString()
{
var result = "type(\"\")".Eval();
var result = "type(\"\").name".Eval();
Assert.AreEqual("String", result);
}

[Test]
public void TypeOfBooleanIsBoolean()
{
var result = "type(true)".Eval();
var result = "type(true).name".Eval();
Assert.AreEqual("Boolean", result);
}

[Test]
public void TypeOfDoubleIsNumber()
{
var result = "type(2.3)".Eval();
var result = "type(2.3).name".Eval();
Assert.AreEqual("Number", result);
}

[Test]
public void TypeOfDelegateIsFunction()
{
var result = "type(() => {})".Eval();
var result = "type(() => {}).name".Eval();
Assert.AreEqual("Function", result);
}

Expand Down
33 changes: 24 additions & 9 deletions src/Mages.Core.Tests/OperationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -795,21 +795,28 @@ public void SomethingAintNothingIsFalse()
[Test]
public void TypeOperatorOnNullYieldsUndefined()
{
var result = "&null".Eval();
var result = "(&null).name".Eval();
Assert.AreEqual("Undefined", result);
}

[Test]
public void TypeOperatorOnResultOfTypeOperatorYieldsString()
public void TypeOperatorOnResultOfTypeOperatorYieldsObject()
{
var result = "& &null".Eval();
var result = "(& &null).name".Eval();
Assert.AreEqual("Object", result);
}

[Test]
public void TypeOperatorOnResultOfTypeOperatorNameYieldsString()
{
var result = "(& (&null).name).name".Eval();
Assert.AreEqual("String", result);
}

[Test]
public void TypeOperatorOnMatrixYieldsMatrix()
{
var result = "&[1, 2, 3]".Eval();
var result = "(&[1, 2, 3]).name".Eval();
Assert.AreEqual("Matrix", result);
}

Expand Down Expand Up @@ -839,35 +846,43 @@ public void PipeOperatorAfterMinusOnCurriedMultiplyYieldsResult()
[Test]
public void PipeOperatorIsLowerPrecendenceThanEquals()
{
var result = "3 == 4 | type".Eval();
var result = "(3 == 4 | type).name".Eval();
Assert.IsInstanceOf<String>(result);
Assert.AreEqual("Boolean", result);
}

[Test]
public void PipeOperatorIsLowerPrecendenceThanOr()
{
var result = "1 || 0 | type".Eval();
var result = "(1 || 0 | type).name".Eval();
Assert.IsInstanceOf<String>(result);
Assert.AreEqual("Boolean", result);
}

[Test]
public void PipeOperatorOnTypeYieldsResult()
{
var result = "2 | type".Eval();
var result = "(2 | type).name".Eval();
Assert.IsInstanceOf<String>(result);
Assert.AreEqual("Number", result);
}

[Test]
public void PipeOperatorOnTypeOfTypeYieldsString()
public void PipeOperatorOnTypeOfTypeNameYieldsString()
{
var result = "2 | type | type".Eval();
var result = "((2 | type).name | type).name".Eval();
Assert.IsInstanceOf<String>(result);
Assert.AreEqual("String", result);
}

[Test]
public void PipeOperatorOnTypeOfTypeYieldsObject()
{
var result = "(2 | type | type).name".Eval();
Assert.IsInstanceOf<String>(result);
Assert.AreEqual("Object", result);
}

[Test]
public void InvalidAssignmentAddWithNumberYieldsError()
{
Expand Down
21 changes: 11 additions & 10 deletions src/Mages.Core/Runtime/Converters/ConverterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Mages.Core.Runtime.Converters
{
using Mages.Core.Runtime.Types;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand All @@ -16,17 +17,17 @@ public static class ConverterExtensions
/// </summary>
/// <param name="value">The value to get the type of.</param>
/// <returns>The MAGES type string.</returns>
public static String ToType(this Object value) => value switch
public static IDictionary<String, Object> ToType(this Object value) => value switch
{
Double _ => "Number",
Complex _ => "Complex",
String _ => "String",
Boolean _ => "Boolean",
Double[,] _ => "Matrix",
Complex[,] _ => "CMatrix",
Function _ => "Function",
IDictionary<String, Object> _ => "Object",
_ => "Undefined",
Double _ => MagesNumber.Type,
Complex _ => MagesComplex.Type,
String _ => MagesString.Type,
Boolean _ => MagesBoolean.Type,
Double[,] _ => MagesMatrix.Type,
Complex[,] _ => MagesCMatrix.Type,
Function _ => MagesFunction.Type,
IDictionary<String, Object> _ => MagesObject.Type,
_ => MagesUndefined.Type,
};

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Mages.Core/Runtime/Converters/TypeCategories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
{
using System;
using System.Collections.Generic;
using System.Numerics;

static class TypeCategories
{
public static readonly Dictionary<Type, List<Type>> Mapping = new Dictionary<Type, List<Type>>
{
{ typeof(Double), new List<Type> { typeof(Double), typeof(Single), typeof(Decimal), typeof(Byte), typeof(UInt16), typeof(UInt32), typeof(UInt64), typeof(Int16), typeof(Int32), typeof(Int64) } },
{ typeof(Complex), new List<Type> { typeof(Complex) } },
{ typeof(Boolean), new List<Type> { typeof(Boolean) } },
{ typeof(String), new List<Type> { typeof(String), typeof(Char) } },
{ typeof(Double[,]), new List<Type> { typeof(Double[,]), typeof(Double[]), typeof(List<Double>) } },
{ typeof(Complex[,]), new List<Type> { typeof(Complex[,]), typeof(Complex[]), typeof(List<Complex>) } },
{ typeof(Function), new List<Type> { typeof(Function), typeof(Delegate) } },
{ typeof(IDictionary<String, Object>), new List<Type> { typeof(IDictionary<String, Object>), typeof(Object) } }
};
Expand Down
2 changes: 1 addition & 1 deletion src/Mages.Core/Runtime/Functions/StandardFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ public static class StandardFunctions
public static readonly Function Is = new Function(args =>
{
return Curry.MinTwo(Is, args) ??
If.Is<String>(args, type => type == args[1].ToType()) ??
If.Is<String>(args, type => type == args[1].ToType()["name"].ToString()) ??
If.Is<IDictionary<String, Object>>(args, type => type.Satisfies(args[1]));
});

Expand Down
6 changes: 2 additions & 4 deletions src/Mages.Core/Runtime/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,12 @@ public static Boolean Satisfies(this IDictionary<String, Object> constraints, Ob
{
foreach (var constraint in constraints)
{
var val = default(Object);

if (obj.TryGetValue(constraint.Key, out val))
if (obj.TryGetValue(constraint.Key, out var val))
{
var simple = constraint.Value as String;
var extended = constraint.Value as IDictionary<String, Object>;

if ((simple == null || val.ToType() == simple) &&
if ((simple == null || val.ToType()["name"].ToString() == simple) &&
(extended == null || extended.Satisfies(val)))
{
continue;
Expand Down
15 changes: 3 additions & 12 deletions src/Mages.Core/Runtime/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@

static class Matrix
{
public static Int32 GetRows<T>(this T[,] matrix)
{
return matrix.GetLength(0);
}
public static Int32 GetRows<T>(this T[,] matrix) => matrix.GetLength(0);

public static Int32 GetColumns<T>(this T[,] matrix)
{
return matrix.GetLength(1);
}
public static Int32 GetColumns<T>(this T[,] matrix) => matrix.GetLength(1);

public static Int32 GetCount<T>(this T[,] matrix)
{
return matrix.GetLength(0) * matrix.GetLength(1);
}
public static Int32 GetCount<T>(this T[,] matrix) => matrix.GetLength(0) * matrix.GetLength(1);

public static Object GetKeys<T>(this T[,] matrix)
{
Expand Down
22 changes: 22 additions & 0 deletions src/Mages.Core/Runtime/Types/MagesBoolean.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Mages.Core.Runtime.Types
{
using Mages.Core.Runtime.Converters;
using Mages.Core.Runtime.Functions;
using System;
using System.Collections.Generic;

static class MagesBoolean
{
private static readonly Function Create = new Function(args =>
{
return Curry.MinOne(Create, args) ??
args[0].ToBoolean();
});

public static readonly IDictionary<String, Object> Type = new Dictionary<String, Object>
{
{ "name", "Boolean" },
{ "create", Create },
};
}
}
22 changes: 22 additions & 0 deletions src/Mages.Core/Runtime/Types/MagesCMatrix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Mages.Core.Runtime.Types
{
using Mages.Core.Runtime.Converters;
using Mages.Core.Runtime.Functions;
using System;
using System.Collections.Generic;

static class MagesCMatrix
{
private static readonly Function Create = new Function(args =>
{
return Curry.MinOne(Create, args) ??
args[0].ToComplex().ToMatrix();
});

public static readonly IDictionary<String, Object> Type = new Dictionary<String, Object>
{
{ "name", "CMatrix" },
{ "create", Create },
};
}
}
22 changes: 22 additions & 0 deletions src/Mages.Core/Runtime/Types/MagesComplex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Mages.Core.Runtime.Types
{
using Mages.Core.Runtime.Converters;
using Mages.Core.Runtime.Functions;
using System;
using System.Collections.Generic;

static class MagesComplex
{
private static readonly Function Create = new Function(args =>
{
return Curry.MinOne(Create, args) ??
args[0].ToComplex();
});

public static readonly IDictionary<String, Object> Type = new Dictionary<String, Object>
{
{ "name", "Complex" },
{ "create", Create },
};
}
}
21 changes: 21 additions & 0 deletions src/Mages.Core/Runtime/Types/MagesFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Mages.Core.Runtime.Types
{
using Mages.Core.Runtime.Functions;
using System;
using System.Collections.Generic;

static class MagesFunction
{
private static readonly Function Create = new Function(args =>
{
return Curry.MinOne(Create, args) ??
(args[0] as Function);
});

public static readonly IDictionary<String, Object> Type = new Dictionary<String, Object>
{
{ "name", "Function" },
{ "create", Create },
};
}
}
22 changes: 22 additions & 0 deletions src/Mages.Core/Runtime/Types/MagesMatrix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Mages.Core.Runtime.Types
{
using Mages.Core.Runtime.Converters;
using Mages.Core.Runtime.Functions;
using System;
using System.Collections.Generic;

static class MagesMatrix
{
private static readonly Function Create = new Function(args =>
{
return Curry.MinOne(Create, args) ??
args[0].ToNumber().ToMatrix();
});

public static readonly IDictionary<String, Object> Type = new Dictionary<String, Object>
{
{ "name", "Matrix" },
{ "create", Create },
};
}
}
22 changes: 22 additions & 0 deletions src/Mages.Core/Runtime/Types/MagesNumber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Mages.Core.Runtime.Types
{
using Mages.Core.Runtime.Converters;
using Mages.Core.Runtime.Functions;
using System;
using System.Collections.Generic;

static class MagesNumber
{
private static readonly Function Create = new Function(args =>
{
return Curry.MinOne(Create, args) ??
args[0].ToNumber();
});

public static readonly IDictionary<String, Object> Type = new Dictionary<String, Object>
{
{ "name", "Number" },
{ "create", Create },
};
}
}
Loading

0 comments on commit 9095cc7

Please sign in to comment.