Skip to content

Commit

Permalink
Revert "Proposed enhancement for issue matheval#7"
Browse files Browse the repository at this point in the history
This reverts commit e918925.
  • Loading branch information
MSpekkio committed Nov 10, 2022
1 parent e918925 commit ec6ea01
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 61 deletions.
28 changes: 0 additions & 28 deletions Math expression eval/UnitTest/TestAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,33 +1188,5 @@ public void Or_Operator_Test()
.Bind("b", 1);
Assert.AreEqual(true, expr4.Eval<bool>());
}

[TestMethod]
public void Custom_Function_Test()
{
//register new custom function
Parser.RegisterFunction("CUSTOM", typeof(customFunction));

//call function
var expr = new Expression("CUSTOM('1','a','#')");
Assert.AreEqual("1-a-#", expr.Eval<string>());
}

public class customFunction : org.matheval.Functions.IFunction
{
public List<org.matheval.Functions.FunctionDef> GetInfo()
{
return new List<org.matheval.Functions.FunctionDef> { new org.matheval.Functions.FunctionDef("custom", new Type[] { typeof(string), typeof(string), typeof(string) }, typeof(string), 3) };
}

public object Execute(Dictionary<string, object> args, ExpressionContext dc)
{
string a1 = (string)args["1"];
string a2 = (string)args["2"];
string a3 = (string)args["3"];

return string.Join("-", a1, a2, a3);
}
}
}
}
43 changes: 10 additions & 33 deletions Math expression eval/org.matheval/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,11 @@ namespace org.matheval
{
public class Parser
{
private static Dictionary<string, Type> _registeredFunctions;

/// <summary>
/// Registers a new custom function to all new Parsers
/// @param name function name as used in the expression
/// @param functionType type that implements the IFunction interface and will be used to handle the custom function call
/// </summary>
/// <param name="op">op</param>
public static void RegisterFunction(string name, Type functionType)
{
if (_registeredFunctions == null)
{
_registeredFunctions = new Dictionary<string, Type>();
}
//sanity check the provided type
//Additional checks could be done here to if needed.
if (functionType.IsAbstract || functionType.IsGenericType || !typeof(IFunction).IsAssignableFrom(functionType))
throw new ArgumentException(functionType.Name + " is not a concrete, non-generic type that implements the org.matheval.Functions.IFunction interface");

_registeredFunctions[name.ToLowerInvariant()] = functionType;
}

/// <summary>
/// Create object Lexer
/// </summary>
private Lexer Lexer;

/// <summary>
/// Dc
/// </summary>
Expand Down Expand Up @@ -366,8 +344,7 @@ private Implements.Node ParseIdentifier()
IFunction funcExecuter;
try
{
if (_registeredFunctions == null || !_registeredFunctions.TryGetValue(identifierStr.ToLowerInvariant(), out Type t))
t = Type.GetType("org.matheval.Functions." + identifierStr.ToLowerInvariant() + "Function", true);
Type t = Type.GetType("org.matheval.Functions." + identifierStr.ToLowerInvariant() + "Function", true);
Object obj = (Activator.CreateInstance(t));

if (obj == null)
Expand All @@ -380,7 +357,7 @@ private Implements.Node ParseIdentifier()
{
throw new Exception(string.Format(Afe_Common.MSG_METH_NOTFOUND, new string[] { identifierStr.ToUpperInvariant() }));
}

List<FunctionDef> functionInfos = funcExecuter.GetInfo();
foreach (FunctionDef functionInfo in functionInfos)
{
Expand Down Expand Up @@ -497,9 +474,9 @@ private Implements.Node ParseBo(int inputPrec, Implements.Node lhs)
while (true)
{
IOperator iopNext = this.GetOperator();
if (iopNext == null ||
if (iopNext == null ||
!(iopCurr.GetPrec() < iopNext.GetPrec() ||
(iopCurr.GetPrec() == iopNext.GetPrec() &&
(iopCurr.GetPrec() == iopNext.GetPrec() &&
iopNext.GetAss() == Assoc.RIGHT)))
{
break;
Expand All @@ -514,8 +491,8 @@ private Implements.Node ParseBo(int inputPrec, Implements.Node lhs)
{
/*if (!lhs.ReturnType.Equals(typeof(object)) && !rhs.ReturnType.Equals(typeof(object)))
{*/
throw new Exception(string.Format(Afe_Common.MSG_WRONG_OP_PARAM,
new String[] { iopCurr.GetOp(), lhs.ReturnType.ToString(), rhs.ReturnType.ToString() }));
throw new Exception(string.Format(Afe_Common.MSG_WRONG_OP_PARAM,
new String[] { iopCurr.GetOp(), lhs.ReturnType.ToString(), rhs.ReturnType.ToString() }));
/*}
else if (!lhs.ReturnType.Equals(typeof(object)))
{
Expand Down Expand Up @@ -623,8 +600,8 @@ private Implements.Node ParseIfElse()
throw new Exception(string.Format(Afe_Common.MSG_IFELSE_WRONG_CONDITION,
new string[] { condition.ReturnType.ToString() }));
}
// else if (!ifTrueNode.ReturnType.Equals(typeof(VariableNode)) &&
// !ifFalseNode.ReturnType.Equals(typeof(VariableNode)) &&
// else if (!ifTrueNode.ReturnType.Equals(typeof(VariableNode)) &&
// !ifFalseNode.ReturnType.Equals(typeof(VariableNode)) &&
else if (!ifTrueNode.ReturnType.Equals(typeof(object)) &&
!ifFalseNode.ReturnType.Equals(typeof(object)) &&
!ifTrueNode.ReturnType.Equals(ifFalseNode.ReturnType))
Expand Down Expand Up @@ -700,7 +677,7 @@ private Implements.Node ParseSwitchCase()
{
//if (!temp.Equals(typeof(VariableNode)) && !temp.Equals(varResultExprs[i].ReturnType))
if (!temp.Equals(typeof(object)) && !temp.Equals(varResultExprs[i].ReturnType))
{
{
throw new Exception(string.Format(Afe_Common.MSG_CONDITIONAL_WRONG_PARAMS,
new string[] { Afe_Common.Const_SWITCH, temp.ToString(), varResultExprs[i].ReturnType.ToString() }));
}
Expand Down

0 comments on commit ec6ea01

Please sign in to comment.