blitz-research пре 9 година
родитељ
комит
898e0058c2
3 измењених фајлова са 66 додато и 5 уклоњено
  1. 9 2
      src/mx2new/expr.monkey2
  2. 4 3
      src/mx2new/func.monkey2
  3. 53 0
      tests/monkey/operators.monkey2

+ 9 - 2
src/mx2new/expr.monkey2

@@ -758,13 +758,20 @@ Class BinaryopExpr Extends Expr
 			
 		Case "=","<>","<",">","<=",">="
 		
-			'Local node:=lhs.FindValue( "<=>" )
+			Local node:=lhs.FindValue( "<=>" )
 			If node
 			
 				Local args:=New Value[1]
 				args[0]=rhs
 				lhs=node.Invoke( args )
-				rhs=New LiteralValue( lhs.type,"" )
+
+				lhsType=lhs.type
+				rhsType=lhsType
+				
+				Local ptype:=Cast<PrimType>( lhsType )
+				Assert( ptype And ptype.IsNumeric )
+				
+				rhs=New LiteralValue( rhsType,"" )
 				type=Type.BoolType
 				
 			Else If plhs=Type.BoolType Or prhs=Type.BoolType

+ 4 - 3
src/mx2new/func.monkey2

@@ -175,11 +175,12 @@ Class FuncValue Extends Value
 				Case "=","<>","<",">","<=",">="
 					If ftype.retType<>Type.BoolType Throw New SemantEx( "Comparison operator '"+op+"' must return Bool" )
 					If ftype.argTypes.Length<>1 Throw New SemantEx( "Comparison operator '"+op+"' must have 1 parameter" )
-					If Not ftype.argTypes[0].Equals( ctype ) Throw New SemantEx( "Comparison operator '"+op+"' parameter must be of type '"+ctype.ToString()+"'" )
+'					If Not ftype.argTypes[0].Equals( ctype ) Throw New SemantEx( "Comparison operator '"+op+"' parameter must be of type '"+ctype.ToString()+"'" )
 				Case "<=>"
-					If ftype.retType<>Type.IntType Throw New SemantEx( "Comparison operator '"+op+"' must return Int" )
+					Local ptype:=Cast<PrimType>( ftype.retType )
+					If Not ptype Or Not ptype.IsNumeric Throw New SemantEx( "Comparison operator '<=>' must return a numeric type" )
 					If ftype.argTypes.Length<>1 Throw New SemantEx( "Comparison operator '"+op+"' must have 1 parameter" )
-					If Not ftype.argTypes[0].Equals( ctype ) Throw New SemantEx( "Comparison operator '"+op+"' parameter must be of type '"+ctype.ToString()+"'" )
+'					If Not ftype.argTypes[0].Equals( ctype ) Throw New SemantEx( "Comparison operator '"+op+"' parameter must be of type '"+ctype.ToString()+"'" )
 				End
 			Endif
 			

+ 53 - 0
tests/monkey/operators.monkey2

@@ -0,0 +1,53 @@
+'Operator Overloading Example 2
+'Reference(s):
+'http://monkey2.monkey-x.com/forums/topic/operator-overloading/
+'https://en.wikipedia.org/wiki/Operator_overloading
+
+#Import "<std.monkey2>"
+
+Using std
+
+Function Main()
+
+	Local objA:=New MyClass("Gold", 100)
+	Local objB:=New MyClass("Silver", 90)
+	
+	Print ""
+	
+	If (objA > objB) Then Print objA.Label + " > " + objB.Label
+	If (objA < objB) Then Print objA.Label + " < " + objB.Label
+
+	If (objA = objB) Then Print objA.Label + " = " + objB.Label
+	If (objA <> objB) Then Print objA.Label + " <> " + objB.Label
+	
+	Print ""
+	
+End
+
+
+Class MyClass
+	
+	Private
+	
+		Field _label:String
+		Field _value:UByte
+	
+	Public
+		
+		Method New(label:String, value:UByte)
+			Label = label
+			_value = value
+		End
+		
+		Property Label:String()
+			Return _label
+		Setter(label:String)
+			_label = label
+		End
+
+		'This will give you >, <, >=, <=, = and <> for free!		
+		Operator<=>:Int( o2:MyClass )
+			Return Self._value<=>o2._value
+		End
+
+End