<commit_before>﻿using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq.Expressions;
using LinqToTTreeInterfacesLib;
using LINQToTTreeLib.Expressions;
using LINQToTTreeLib.Statements;
using LINQToTTreeLib.Variables;
using Remotion.Linq;
using Remotion.Linq.Clauses;
using Remotion.Linq.Clauses.ResultOperators;

namespace LINQToTTreeLib.ResultOperators
{
    /// <summary>
    /// Implement the Sum operator in LINQ.
    /// </summary>
    [Export(typeof(IQVScalarResultOperator))]
    public class ROSum : IQVScalarResultOperator
    {
        /// <summary>
        /// Deal with just the Sum operator
        /// </summary>
        /// <param name="resultOperatorType"></param>
        /// <returns></returns>
        public bool CanHandle(Type resultOperatorType)
        {
            return resultOperatorType == typeof(SumResultOperator);
        }

        public IVariable ProcessResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel,
            IGeneratedQueryCode gc, ICodeContext cc,
            CompositionContainer container)
        {
            if (resultOperator == null)
                throw new ArgumentNullException("resultOperator");
            if (cc == null)
                throw new ArgumentNullException("CodeContext can't be null");

            var sumOperator = resultOperator as SumResultOperator;

            //
            // We only know how to sum basic types
            //

            var sumType = cc.LoopVariable.Type;
            if (sumType != typeof(int)
                && sumType != typeof(short)
                && sumType != typeof(double)
                && sumType != typeof(float))
            {
                throw new InvalidOperationException(string.Format("Do not know how to generate C++ to sum type {0}.", sumType.Name));
            }

            var accumulator = new Variables.VarSimple(sumType);
            accumulator.InitialValue = new ValSimple("0", sumType);
            accumulator.Declare = true;

            //
            // Now, in the loop we are currently in, we do the "add".
            //

            var stack = cc.Add(accumulator.VariableName, accumulator.AsExpression());
            var p = Expression.Parameter(sumType, accumulator.VariableName);
            var add = Expression.Add(p, cc.LoopVariable);

            var addResolved = ExpressionToCPP.GetExpression(add, gc, cc, container);
            gc.Add(new StatementAggregate(accumulator, addResolved));

            stack.Pop();

            return accumulator;
        }<commit_msg>Fix up recursive parameter definition. :-)<commit_after>﻿using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq.Expressions;
using LinqToTTreeInterfacesLib;
using LINQToTTreeLib.Expressions;
using LINQToTTreeLib.Statements;
using LINQToTTreeLib.Variables;
using Remotion.Linq;
using Remotion.Linq.Clauses;
using Remotion.Linq.Clauses.ResultOperators;

namespace LINQToTTreeLib.ResultOperators
{
    /// <summary>
    /// Implement the Sum operator in LINQ.
    /// </summary>
    [Export(typeof(IQVScalarResultOperator))]
    public class ROSum : IQVScalarResultOperator
    {
        /// <summary>
        /// Deal with just the Sum operator
        /// </summary>
        /// <param name="resultOperatorType"></param>
        /// <returns></returns>
        public bool CanHandle(Type resultOperatorType)
        {
            return resultOperatorType == typeof(SumResultOperator);
        }

        public IVariable ProcessResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel,
            IGeneratedQueryCode gc, ICodeContext cc,
            CompositionContainer container)
        {
            if (resultOperator == null)
                throw new ArgumentNullException("resultOperator");
            if (cc == null)
                throw new ArgumentNullException("CodeContext can't be null");

            var sumOperator = resultOperator as SumResultOperator;

            //
            // We only know how to sum basic types
            //

            var sumType = cc.LoopVariable.Type;
            if (sumType != typeof(int)
                && sumType != typeof(short)
                && sumType != typeof(double)
                && sumType != typeof(float))
            {
                throw new InvalidOperationException(string.Format("Do not know how to generate C++ to sum type {0}.", sumType.Name));
            }

            var accumulator = new Variables.VarSimple(sumType);
            accumulator.InitialValue = new ValSimple("0", sumType);
            accumulator.Declare = true;

            //
            // Now, in the loop we are currently in, we do the "add".
            //

            var p = Expression.Parameter(sumType, accumulator.VariableName);
            var add = Expression.Add(p, cc.LoopVariable);

            var addResolved = ExpressionToCPP.GetExpression(add, gc, cc, container);
            gc.Add(new StatementAggregate(accumulator, addResolved));

            return accumulator;
        }
{'fraction_non_alphanumeric': 0.09488625315963445, 'fraction_numerical': 0.0007777561734396267, 'mean_word_length': 2.2049844236760126, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '4258233', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1288, 'n_words': 334}