Пример #1
0
        public void Construct(CommonTree syntaxFunctionDeclaration)
        {
            //Name
            Name = syntaxFunctionDeclaration.GetChild(0).Text;
            //Modifiers
            var syntaxModifiers = syntaxFunctionDeclaration.GetChild(1).CastTo <CommonTree>();

            if (syntaxModifiers.ChildCount > 0)
            {
                syntaxModifiers.Children.ForEach(mod =>
                {
                    if (mod.Text == Modifiers.Extern)
                    {
                        ModifiersList.Add(Modifier.Extern);
                    }
                    else if (mod.Text == Modifiers.Static)
                    {
                        ModifiersList.Add(Modifier.Static);
                    }
                    else
                    {
                        throw new Exception($"Modifier {mod.Text} is not defined");
                    }
                });
            }
            //Attributes
            var syntaxAttributeUsages = syntaxFunctionDeclaration.GetChild(2).CastTo <CommonTree>();

            if (syntaxAttributeUsages.ChildCount > 0)
            {
                syntaxAttributeUsages.Children.Cast <CommonTree>().ForEach(syntaxAttributeUsage =>
                {
                    AttributeUsage attributeUsage = new AttributeUsage(this, Scope);
                    attributeUsage.Construct(syntaxAttributeUsage);
                    AttributeUsages.Add(attributeUsage);
                });
            }
            //ReturnType
            string strType          = string.Empty;
            var    syntaxReturnType = syntaxFunctionDeclaration.GetChild(3).CastTo <CommonTree>();

            syntaxReturnType.Children.ForEach(node => strType += node.Text);
            //ReturnType = TreeHelper.GetReturnType(syntaxFunctionDeclaration.GetChild(1).GetChild(0).Text);
            ReturnType = TreeHelper.GetReturnType(strType);
            //Parameters
            var syntaxParametersNode = syntaxFunctionDeclaration.GetChild(4).CastTo <CommonTree>();

            if (syntaxParametersNode.ChildCount > 0)
            {
                syntaxParametersNode.Children.Cast <CommonTree>()
                .ForEach(syntaxParameter =>
                {
                    FunctionVariableDeclarationParameter functionVariableDeclarationParameter =
                        new FunctionVariableDeclarationParameter(this, Scope);
                    ParameterNodes.Add(functionVariableDeclarationParameter);
//                        Scope.AddVariable(functionDeclarationParameter);
                    functionVariableDeclarationParameter.Construct(syntaxParameter);
                });
            }

            //Statements
            var syntaxFunctionBody = syntaxFunctionDeclaration.GetChild(5).CastTo <CommonTree>();

            //StatemenBlock = TreeHelper.GetStatements(this, Scope, syntaxStatementBlock)
            //    .First().CastTo<BlockStatement>();
            if (syntaxFunctionBody.ChildCount > 0)
            {
                StatementBlock = new BlockStatement(this, Scope, false);
                StatementBlock.Construct(syntaxFunctionBody.GetChild(0).CastTo <CommonTree>());
            }
        }