<commit_before>using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Text;

namespace IronAHK.Scripting
{
    partial class Parser
    {
        const string args = "args";

        public void ParseFunction(CodeLine line)
        {
            CloseTopLabelBlock();
            string code = line.Code;
            int i;
            var buf = new StringBuilder();

            #region Name

            string name;

            for (i = 0; i < code.Length; i++)
            {
                char sym = code[i];

                if (IsIdentifier(sym))
                    buf.Append(sym);
                else if (sym == ParenOpen)
                {
                    i++;
                    break;
                }
                else
                    throw new ParseException(ExUnexpected);
            }

            if (buf.Length == 0)
                throw new ParseException(ExUnexpected);

            name = buf.ToString();
            buf.Length = 0;

            if (IsLocalMethodReference(name))
                throw new ParseException("Duplicate function");

            #endregion

            #region Parameters

            CodeBlock.BlockType blockType = CodeBlock.BlockType.Expect;

            bool str = false;
            bool stop = false;

            for (; i < code.Length; i++)
            {
                char sym = code[i];

                switch (sym)
                {
                    case StringBound:
                        str = !str;
                        goto default;

                    case ParenClose:
                        if (str)
                            goto default;
                        else
                            stop = true;
                        break;

                    default:
                        buf.Append(sym);
                        break;
                }

                if (stop)
                    break;
            }

            if (!stop)
                throw new ParseException("Expected closing parenthesis");

            string param = buf.ToString();
            buf.Length = 0;
            i++;

            #region Opening brace

            for (; i < code.Length; i++)
            {
                char sym = code[i];

                if (sym == BlockOpen)
                {
                    blockType = CodeBlock.BlockType.Within;
                    break;
                }
                else if (IsCommentAt(code, i))
                    break;
                else if (!IsSpace(sym))
                    throw new ParseException(ExUnexpected);
            }

            #endregion

            #endregion

            #region Block

            var method = LocalMethod(name);
            method.Attributes = MemberAttributes.Static | MemberAttributes.AccessMask;

            var block = new CodeBlock(line, method.Name, method.Statements, CodeBlock.BlockKind.Function, blocks.Count == 0 ? null : blocks.Peek());
            block.Type = blockType;
            CloseTopSingleBlock();
            blocks.Push(block);

            var fix = ParseFunctionParameters(param);
            if (fix != null)
                method.Statements.Add(fix);

            #endregion

            methods.Add(method.Name, method);
        }

        #region Parameters

        CodeStatement ParseFunctionParameters(string code)<commit_msg>Automatically create same name delegates for functions in local scope.<commit_after>using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Text;

namespace IronAHK.Scripting
{
    partial class Parser
    {
        const string args = "args";

        public void ParseFunction(CodeLine line)
        {
            CloseTopLabelBlock();
            string code = line.Code;
            int i;
            var buf = new StringBuilder();

            #region Name

            string name;

            for (i = 0; i < code.Length; i++)
            {
                char sym = code[i];

                if (IsIdentifier(sym))
                    buf.Append(sym);
                else if (sym == ParenOpen)
                {
                    i++;
                    break;
                }
                else
                    throw new ParseException(ExUnexpected);
            }

            if (buf.Length == 0)
                throw new ParseException(ExUnexpected);

            name = buf.ToString();
            buf.Length = 0;

            if (IsLocalMethodReference(name))
                throw new ParseException("Duplicate function");

            #endregion

            #region Parameters

            CodeBlock.BlockType blockType = CodeBlock.BlockType.Expect;

            bool str = false;
            bool stop = false;

            for (; i < code.Length; i++)
            {
                char sym = code[i];

                switch (sym)
                {
                    case StringBound:
                        str = !str;
                        goto default;

                    case ParenClose:
                        if (str)
                            goto default;
                        else
                            stop = true;
                        break;

                    default:
                        buf.Append(sym);
                        break;
                }

                if (stop)
                    break;
            }

            if (!stop)
                throw new ParseException("Expected closing parenthesis");

            string param = buf.ToString();
            buf.Length = 0;
            i++;

            #region Opening brace

            for (; i < code.Length; i++)
            {
                char sym = code[i];

                if (sym == BlockOpen)
                {
                    blockType = CodeBlock.BlockType.Within;
                    break;
                }
                else if (IsCommentAt(code, i))
                    break;
                else if (!IsSpace(sym))
                    throw new ParseException(ExUnexpected);
            }

            #endregion

            #endregion

            #region Block

            var method = LocalMethod(name);
            method.Attributes = MemberAttributes.Static | MemberAttributes.AccessMask;

            var block = new CodeBlock(line, method.Name, method.Statements, CodeBlock.BlockKind.Function, blocks.Count == 0 ? null : blocks.Peek());
            block.Type = blockType;
            CloseTopSingleBlock();
            blocks.Push(block);

            var fix = ParseFunctionParameters(param);
            if (fix != null)
                method.Statements.Add(fix);

            #endregion

            methods.Add(method.Name, method);

            var type = typeof(Rusty.Core.GenericFunction);
            var typeref = new CodeTypeReference();
            typeref.UserData.Add(RawData, type);
            var del = new CodeDelegateCreateExpression(typeref, new CodeTypeReferenceExpression(className), method.Name);
            var obj = VarAssign(VarRef(mainScope + ScopeVar + method.Name), del);
            prepend.Add(new CodeExpressionStatement(obj));
        }

        #region Parameters

        CodeStatement ParseFunctionParameters(string code)
{'fraction_non_alphanumeric': 0.08046293744833287, 'fraction_numerical': 0.0013777900248002205, 'mean_word_length': 1.0913281475079228, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '2349951', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1575, 'n_words': 424}