コード例 #1
0
        public void Construct(CommonTree syntaxClass)
        {
            Name = syntaxClass.Children[0].Text;
            var syntaxModifiers = syntaxClass.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");
                    }
                });
            }
            var classblock = syntaxClass.GetChild(2).CastTo <CommonTree>();

            if (classblock.ChildCount == 0)
            {
                return;
            }
            classblock.Children
            .Cast <CommonTree>()
            .ForEach(child =>
            {
                if (child.Type == MathLangParser.STATIC_DECLARATION)
                {
                    //List<VariableDeclaration> variableList
                    //    =  TreeHelper.RunMultiDeclaration(this, Scope, child.GetChild(0).CastTo<CommonTree>());
                    //VarDeclarationNodes.AddRange(variableList);
                    child.Children.Cast <CommonTree>()
                    .Select(syntaxVariableDeclaration =>
                    {
                        var variableDeclaration = TreeHelper
                                                  .GetStatements(this, Scope, syntaxVariableDeclaration).First()
                                                  .CastTo <VariableDeclaration>();
                        variableDeclaration.Construct(syntaxVariableDeclaration);
                        return(variableDeclaration);
                    })
                    .ForEach(declaration => VarDeclarationNodes.Add(declaration));
                }
                else if (child.Type == MathLangParser.FUNCDECLARATION)
                {
                    FunctionDeclaration function = new FunctionDeclaration(this, Scope);
                    FunctionDeclarationNodes.Add(function);
//                        Scope.AddFunction(function);
                    function.Construct(child);
                }
            });
        }
コード例 #2
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>());
            }
        }