Skip to content

Commit

Permalink
Merge pull request #11 from Walperr/issue-5
Browse files Browse the repository at this point in the history
Added functions for arrays
  • Loading branch information
Walperr authored Nov 30, 2023
2 parents 7794445 + 8017eb6 commit 4be232b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
3 changes: 3 additions & 0 deletions LanguageInterpreter/Execution/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Globalization;
using System.Runtime.InteropServices.JavaScript;
using LanguageInterpreter.Common;
using LanguageParser.Common;
using LanguageParser.Expressions;
Expand Down Expand Up @@ -333,6 +334,8 @@ public Result<SyntaxException, object> Evaluate(CancellationToken token)
return sArrayFunction.Invoke(args!);
case Function<bool[]> bArrayFunction:
return bArrayFunction.Invoke(args!);
case Function<Array> aFunction:
return aFunction.Invoke(args!);
default:
_errors.Add(new InterpreterException($"Unknown return type of function {function.Name}",
expression.Range));
Expand Down
2 changes: 1 addition & 1 deletion LanguageInterpreter/Execution/TypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private Result<SyntaxException, Type> Resolve(ExpressionBase expression, Cancell
return null;
}

if (variable.Type != rightType)
if (variable.Type != rightType && !variable.Type.IsAssignableTo(rightType))
{
_errors.Add(new ExpectedOtherTypeException(expression.Right, rightType, variable.Type));
return null;
Expand Down
85 changes: 85 additions & 0 deletions SimpleExecutor/Libraries/ArrayLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using LanguageParser.Common;

namespace SimpleExecutor.Libraries;

public static class ArrayLibrary
{
public static IEnumerable<FunctionBase> GetFunctions()
{
yield return FunctionBase.Create("length", args => (double)((Array)args[0]).Length, typeof(Array));

yield return FunctionBase.Create<Array>("repeatElement", args =>
{
var n = (int)(double)args[1];

return args[0] switch
{
double a => Enumerable.Repeat(a, n).ToArray(),
string a => Enumerable.Repeat(a, n).ToArray(),
bool a => Enumerable.Repeat(a, n).ToArray(),
_ => throw new Exception("Unknown type")
};
}, typeof(object), typeof(double));

yield return FunctionBase.Create("range", args =>
{
var n = (int)(double)args[1];

var result = new double[n];

for (var i = 0; i < n; i++)
result[i] = (double)args[0] + i;

return result;
}, typeof(double), typeof(double));

yield return FunctionBase.Create("range", args =>
{
var step = (double)args[1];
var n = (int)(double)args[2];

var result = new double[n];

for (var i = 0; i < n; i++)
result[i] = (double)args[0] + i * step;

return result;
}, typeof(double), typeof(double), typeof(double));

yield return FunctionBase.Create("sum", args => ((double[])args[0]).Sum(), typeof(double[]));

yield return FunctionBase.Create("min", args => ((double[])args[0]).Min(), typeof(double[]));

yield return FunctionBase.Create("max", args => ((double[])args[0]).Max(), typeof(double[]));

yield return FunctionBase.Create("concat", args => ((double[])args[0]).Concat((double[])args[1]).ToArray(),
typeof(double[]), typeof(double[]));

yield return FunctionBase.Create("concat", args => ((string[])args[0]).Concat((string[])args[1]).ToArray(),
typeof(string[]), typeof(string[]));

yield return FunctionBase.Create("concat", args => ((bool[])args[0]).Concat((bool[])args[1]).ToArray(),
typeof(bool[]), typeof(bool[]));

yield return FunctionBase.Create("sort", args => ((double[])args[0]).OrderBy(d => d).ToArray(),
typeof(double[]));

yield return FunctionBase.Create("sortDescending",
args => ((double[])args[0]).OrderByDescending(d => d).ToArray(), typeof(double[]));

yield return FunctionBase.Create("sort", args => ((string[])args[0]).OrderBy(d => d).ToArray(),
typeof(string[]));

yield return FunctionBase.Create("sortDescending",
args => ((string[])args[0]).OrderByDescending(d => d).ToArray(), typeof(string[]));

yield return FunctionBase.Create("reverse", args => ((double[])args[0]).Reverse().ToArray(), typeof(double[]));

yield return FunctionBase.Create("reverse", args => ((string[])args[0]).Reverse().ToArray(), typeof(string[]));

yield return FunctionBase.Create("reverse", args => ((bool[])args[0]).Reverse().ToArray(), typeof(bool[]));
}
}
1 change: 1 addition & 0 deletions SimpleExecutor/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public MainViewModel()
.WithPredefinedFunction(widthFunction)
.WithPredefinedFunction(heightFunction)
.WithPredefinedFunction(timeFunction)
.WithPredefinedFunctions(ArrayLibrary.GetFunctions())
.WithPredefinedFunctions(MathLibrary.GetMathFunctions())
.Build();
}
Expand Down

0 comments on commit 4be232b

Please sign in to comment.