<commit_before>﻿#region copyright
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OpUnaryMinus.cs" company="<NAME>">
// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>
// <summary>
//     Part of oberon0 - Oberon0Compiler/OpUnaryMinus.cs
// </summary>
// --------------------------------------------------------------------------------------------------------------------
#endregion

namespace Oberon0.Compiler.Expressions.Operations
{
    using JetBrains.Annotations;

    using Oberon0.Compiler.Definitions;
    using Oberon0.Compiler.Expressions.Constant;
    using Oberon0.Compiler.Expressions.Operations.Internal;
    using Oberon0.Compiler.Types;

    /// <summary>
    /// Handle "~".
    /// </summary>
    /// <seealso cref="IArithmeticOperation" />
    /// <remarks>This function is some kind of exception as - usually takes one parameter. The second is handled as a dummy</remarks>
    [ArithmeticOperation(OberonGrammarLexer.MINUS, BaseTypes.IntType, BaseTypes.AnyType, BaseTypes.IntType)]
    [ArithmeticOperation(OberonGrammarLexer.MINUS, BaseTypes.DecimalType, BaseTypes.AnyType, BaseTypes.DecimalType)]
    [UsedImplicitly]
    internal class OpUnaryMinus : BinaryOperation
    {
        protected override Expression BinaryOperate(
            BinaryExpression bin,
            Block block,
            IArithmeticOpMetadata operationParameters)
        {
            if (bin.LeftHandSide.IsConst)
            {
                switch (bin.LeftHandSide.TargetType.BaseTypes)
                {
                    case BaseTypes.IntType:
                        ConstantIntExpression leftInt = (ConstantIntExpression)bin.LeftHandSide;
                        leftInt.Value = -(int)leftInt.Value;
                        return leftInt;

                    case BaseTypes.DecimalType:
                        ConstantDoubleExpression leftDouble = (ConstantDoubleExpression)bin.LeftHandSide;
                        leftDouble.Value = -(decimal)leftDouble.Value;
                        return leftDouble;
                }
            }

            return bin; // expression remains the same
        }
    }
}<commit_msg>Throw exception on invalid unary type<commit_after>﻿#region copyright
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OpUnaryMinus.cs" company="<NAME>">
// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>
// <summary>
//     Part of oberon0 - Oberon0Compiler/OpUnaryMinus.cs
// </summary>
// --------------------------------------------------------------------------------------------------------------------
#endregion

namespace Oberon0.Compiler.Expressions.Operations
{
    using System;

    using JetBrains.Annotations;

    using Oberon0.Compiler.Definitions;
    using Oberon0.Compiler.Expressions.Constant;
    using Oberon0.Compiler.Expressions.Operations.Internal;
    using Oberon0.Compiler.Types;

    /// <summary>
    /// Handle "~".
    /// </summary>
    /// <seealso cref="IArithmeticOperation" />
    /// <remarks>This function is some kind of exception as - usually takes one parameter. The second is handled as a dummy</remarks>
    [ArithmeticOperation(OberonGrammarLexer.MINUS, BaseTypes.IntType, BaseTypes.AnyType, BaseTypes.IntType)]
    [ArithmeticOperation(OberonGrammarLexer.MINUS, BaseTypes.DecimalType, BaseTypes.AnyType, BaseTypes.DecimalType)]
    [UsedImplicitly]
    internal class OpUnaryMinus : BinaryOperation
    {
        protected override Expression BinaryOperate(
            BinaryExpression bin,
            Block block,
            IArithmeticOpMetadata operationParameters)
        {
            if (bin.LeftHandSide.IsConst)
            {
                switch (bin.LeftHandSide.TargetType.BaseTypes)
                {
                    case BaseTypes.IntType:
                        ConstantIntExpression leftInt = (ConstantIntExpression)bin.LeftHandSide;
                        leftInt.Value = -(int)leftInt.Value;
                        return leftInt;

                    case BaseTypes.DecimalType:
                        ConstantDoubleExpression leftDouble = (ConstantDoubleExpression)bin.LeftHandSide;
                        leftDouble.Value = -(decimal)leftDouble.Value;
                        return leftDouble;

                    default:
                        throw new ArgumentException($"Cannot perform unary minus on {bin.LeftHandSide.TargetType.BaseTypes}");
                }
            }

            return bin; // expression remains the same
        }
    }
}
{'fraction_non_alphanumeric': 0.17641046606704824, 'fraction_numerical': 0.0028618152085036794, 'mean_word_length': 1.63915857605178, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3622992', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1132, 'n_words': 260}