@@ -51,8 +51,9 @@ public void GenerateCode(SourceProductionContext context)
5151
5252 if ( attributeData . AttributeClass is not { TypeArguments . Length : > 1 } attributeClass ) continue ;
5353
54- var otherType = attributeClass . TypeArguments [ 0 ] . GetName ( ) ;
55- var resultType = attributeClass . TypeArguments [ 1 ] . GetName ( ) ;
54+ var leftType = attributeClass . TypeArguments [ 0 ] . GetName ( ) ;
55+ var rightType = attributeClass . TypeArguments [ 1 ] . GetName ( ) ;
56+ var resultType = attributeClass . TypeArguments [ 2 ] . GetName ( ) ;
5657
5758 operators . Add (
5859 OperatorDeclaration ( IdentifierName ( resultType . FullName ) , Token ( value switch
@@ -70,8 +71,8 @@ public void GenerateCode(SourceProductionContext context)
7071 ] )
7172 . WithParameterList ( ParameterList (
7273 [
73- Parameter ( Identifier ( "left" ) ) . WithType ( IdentifierName ( quantity . Name ) ) ,
74- Parameter ( Identifier ( "right" ) ) . WithType ( IdentifierName ( otherType . FullName ) ) ,
74+ Parameter ( Identifier ( "left" ) ) . WithType ( IdentifierName ( leftType . Name ) ) ,
75+ Parameter ( Identifier ( "right" ) ) . WithType ( IdentifierName ( rightType . FullName ) ) ,
7576 ] ) )
7677 . WithExpressionBody ( ArrowExpressionClause ( CastExpression ( IdentifierName ( resultType . FullName ) ,
7778 ParenthesizedExpression ( BinaryExpression ( value switch
@@ -82,9 +83,11 @@ public void GenerateCode(SourceProductionContext context)
8283 3 => SyntaxKind . DivideExpression ,
8384 _ => SyntaxKind . None ,
8485 } ,
85- MemberAccessExpression ( SyntaxKind . SimpleMemberAccessExpression ,
86- IdentifierName ( "left" ) , IdentifierName ( "Value" ) ) ,
87- otherType . FullName . Contains ( "TedToolkit.Quantities" )
86+ leftType . FullName . Contains ( "TedToolkit.Quantities" )
87+ ? MemberAccessExpression ( SyntaxKind . SimpleMemberAccessExpression ,
88+ IdentifierName ( "left" ) , IdentifierName ( "Value" ) )
89+ : IdentifierName ( "left" ) ,
90+ rightType . FullName . Contains ( "TedToolkit.Quantities" )
8891 ? MemberAccessExpression ( SyntaxKind . SimpleMemberAccessExpression ,
8992 IdentifierName ( "right" ) , IdentifierName ( "Value" ) )
9093 : IdentifierName ( "right" ) ) ) ) ) )
@@ -200,18 +203,18 @@ public void GenerateCode(SourceProductionContext context)
200203 . WithModifiers ( TokenList ( Token ( SyntaxKind . PublicKeyword ) , Token ( SyntaxKind . StaticKeyword ) ) )
201204 . WithAttributeLists ( [ GeneratedCodeAttribute ( typeof ( QuantityStructGenerator ) ) ] )
202205 . WithExpressionBody ( ArrowExpressionClause ( CastExpression (
203- IdentifierName ( quantity . Name ) ,
204- LiteralExpression ( SyntaxKind . NumericLiteralExpression , Literal ( 0 ) ) ) ) )
206+ IdentifierName ( quantity . Name ) ,
207+ LiteralExpression ( SyntaxKind . NumericLiteralExpression , Literal ( 0 ) ) ) ) )
205208 . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ,
206-
209+
207210 PropertyDeclaration ( IdentifierName ( quantity . Name ) , Identifier ( "One" ) )
208211 . WithModifiers ( TokenList ( Token ( SyntaxKind . PublicKeyword ) , Token ( SyntaxKind . StaticKeyword ) ) )
209212 . WithAttributeLists ( [ GeneratedCodeAttribute ( typeof ( QuantityStructGenerator ) ) ] )
210213 . WithExpressionBody ( ArrowExpressionClause ( CastExpression (
211214 IdentifierName ( quantity . Name ) ,
212215 LiteralExpression ( SyntaxKind . NumericLiteralExpression , Literal ( 1 ) ) ) ) )
213216 . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ,
214-
217+
215218 FieldDeclaration (
216219 VariableDeclaration ( IdentifierName ( typeName . FullName ) )
217220 . WithVariables (
@@ -587,6 +590,19 @@ public void GenerateCode(SourceProductionContext context)
587590
588591 #region Operators
589592
593+ OperatorDeclaration ( IdentifierName ( quantity . Name ) , Token ( SyntaxKind . MinusToken ) )
594+ . WithModifiers ( TokenList ( Token ( SyntaxKind . PublicKeyword ) , Token ( SyntaxKind . StaticKeyword ) ) )
595+ . WithAttributeLists ( [ GeneratedCodeAttribute ( typeof ( QuantityStructGenerator ) ) ] )
596+ . WithParameterList ( ParameterList (
597+ [
598+ Parameter ( Identifier ( "value" ) ) . WithType ( IdentifierName ( quantity . Name ) )
599+ ] ) )
600+ . WithExpressionBody ( ArrowExpressionClause ( CastExpression ( IdentifierName ( quantity . Name ) ,
601+ ParenthesizedExpression ( PrefixUnaryExpression ( SyntaxKind . UnaryMinusExpression ,
602+ MemberAccessExpression ( SyntaxKind . SimpleMemberAccessExpression ,
603+ IdentifierName ( "value" ) , IdentifierName ( "Value" ) ) ) ) ) ) )
604+ . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ,
605+
590606 OperatorDeclaration ( IdentifierName ( quantity . Name ) , Token ( SyntaxKind . PlusToken ) )
591607 . WithModifiers ( TokenList ( Token ( SyntaxKind . PublicKeyword ) , Token ( SyntaxKind . StaticKeyword ) ) )
592608 . WithAttributeLists ( [ GeneratedCodeAttribute ( typeof ( QuantityStructGenerator ) ) ] )
@@ -606,7 +622,7 @@ public void GenerateCode(SourceProductionContext context)
606622 IdentifierName ( "right" ) ,
607623 IdentifierName ( "Value" ) ) ) ) ) ) )
608624 . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ,
609-
625+
610626 OperatorDeclaration ( IdentifierName ( quantity . Name ) , Token ( SyntaxKind . PercentToken ) )
611627 . WithModifiers ( TokenList ( Token ( SyntaxKind . PublicKeyword ) , Token ( SyntaxKind . StaticKeyword ) ) )
612628 . WithAttributeLists ( [ GeneratedCodeAttribute ( typeof ( QuantityStructGenerator ) ) ] )
0 commit comments