Kaynağa Gözat

Added 'And' and 'Or' to Where expressions.

Mark Sibly 7 yıl önce
ebeveyn
işleme
09171e9522
2 değiştirilmiş dosya ile 52 ekleme ve 11 silme
  1. 29 7
      src/mx2cc/expr.monkey2
  2. 23 4
      src/mx2cc/test.monkey2

+ 29 - 7
src/mx2cc/expr.monkey2

@@ -732,6 +732,20 @@ Class UnaryopExpr Extends Expr
 		Return EvalUnaryop( type,op,value.UpCast( type ) )
 		Return EvalUnaryop( type,op,value.UpCast( type ) )
 	End
 	End
 	
 	
+	Method OnSemantWhere:Bool( scope:Scope ) Override
+	
+		Select op
+		Case "not"
+			Local expr:=Self.expr.SemantWhere( scope )
+			Return Not expr
+		Default
+			Throw New SemantEx( "Unary operator '"+op+"' cannot be used in where expressions" )
+		End
+		
+		Return False
+	End
+	
+	
 	Method ToString:String() Override
 	Method ToString:String() Override
 		Return op.Capitalize()+expr.ToString()
 		Return op.Capitalize()+expr.ToString()
 	End
 	End
@@ -795,14 +809,22 @@ Class BinaryopExpr Extends Expr
 	
 	
 	Method OnSemantWhere:Bool( scope:Scope ) Override
 	Method OnSemantWhere:Bool( scope:Scope ) Override
 	
 	
-		If op<>"=" And op<>"<>" Throw New SemantEx( "Types can only be compared for equality" )
-		
-		Local lhs:=Self.lhs.SemantType( scope )
-		Local rhs:=Self.rhs.SemantType( scope )
-		
-		If op="=" Return lhs.Equals( rhs )
+		Select op
+		Case "=","<>"
+			Local lhs:=Self.lhs.SemantType( scope )
+			Local rhs:=Self.rhs.SemantType( scope )
+			If op="=" Return lhs.Equals( rhs )
+			Return Not lhs.Equals( rhs )
+		Case "and","or"
+			Local lhs:=Self.lhs.SemantWhere( scope )
+			Local rhs:=Self.rhs.SemantWhere( scope )
+			If op="and" Return lhs And rhs
+			Return lhs Or rhs
+		Default
+			Throw New SemantEx( "Binary operator '"+op+"' cannot be used in where expressions" )
+		End
 		
 		
-		Return Not lhs.Equals( rhs )
+		Return False
 	End
 	End
 	
 	
 	Method ToString:String() Override
 	Method ToString:String() Override

+ 23 - 4
src/mx2cc/test.monkey2

@@ -1,9 +1,28 @@
 
 
+Interface I
+End
+
+Interface J
+End
+
+Class A
+End
+
+Class B
+End
+
+Class C Implements I
+End
+
+Class D Implements J
+End
+
+Class E<X,Y> Where X Implements I Or Y Implements J
+End
+
 Function Main()
 Function Main()
 	
 	
-	Local t:=True
-		
-	Print "Result="+t
-
+	Local test:=New E<A,D>
+	
 End
 End