Handle Unary operator overloading for UserData
This commit is contained in:
@@ -48,5 +48,10 @@ namespace Upsilon.BaseTypes.UserData
|
||||
{
|
||||
return _typeInfo.BinaryOperator(Value, par1, op, par2);
|
||||
}
|
||||
|
||||
public (LuaType Type, bool Failed) UnaryOperator(LuaType par1, OperatorType op)
|
||||
{
|
||||
return _typeInfo.UnaryOperator(Value, par1, op);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,5 +82,15 @@ namespace Upsilon.BaseTypes.UserData
|
||||
|
||||
return (method.Invoke(value, new[] {par1.ToCSharpObject(), par2.ToCSharpObject()}).ToLuaType(), false);
|
||||
}
|
||||
public (LuaType Type, bool Failed) UnaryOperator(object value, LuaType par1, OperatorType op)
|
||||
{
|
||||
var method = OperatorHandler.GetUnaryOperator(op, par1.GetCSharpType());
|
||||
if (method == null)
|
||||
{
|
||||
return (new LuaNull(), true);
|
||||
}
|
||||
|
||||
return (method.Invoke(value, new[] {par1.ToCSharpObject()}).ToLuaType(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,8 @@ namespace Upsilon.Binder
|
||||
throw new Exception("Unknown unary operator token: " + operatorToken);
|
||||
}
|
||||
|
||||
if (inType == Type.Unknown)
|
||||
return _operators.FirstOrDefault(op => op.Kind == kind);
|
||||
return _operators.FirstOrDefault(op => op.Kind == kind && op.InType == inType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,9 +165,28 @@ namespace Upsilon.Evaluator
|
||||
case BoundUnaryOperator.OperatorKind.Identity:
|
||||
return operand;
|
||||
case BoundUnaryOperator.OperatorKind.Negation:
|
||||
return -((Number)operand);
|
||||
if (operand.Type == Type.Number)
|
||||
return -((Number)operand);
|
||||
else if (operand.Type == Type.UserData)
|
||||
{
|
||||
var ud = (UserData) operand;
|
||||
var (type, failed) = ud.UnaryOperator(operand, OperatorType.UnaryNegation);
|
||||
if (failed) goto default;
|
||||
return type;
|
||||
}
|
||||
goto default;
|
||||
case BoundUnaryOperator.OperatorKind.LogicalNegation:
|
||||
return !(LuaBoolean) operand;
|
||||
if (operand.Type == Type.Boolean)
|
||||
return !(LuaBoolean) operand;
|
||||
else if (operand.Type == Type.UserData)
|
||||
{
|
||||
var ud = (UserData) operand;
|
||||
var (type, failed) = ud.UnaryOperator(operand, OperatorType.LogicalNot);
|
||||
if (failed) goto default;
|
||||
return type;
|
||||
}
|
||||
goto default;
|
||||
|
||||
default:
|
||||
throw new Exception("Invalid Unary Operator: " + e.Operator.Kind);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user