public static void Run(Nodes.Program program, FunctionIndexingStrategy indexingStrategy)
 {
     AddLibraryClasses(program);
     program.PreProcess();
     program.Process();
     program.SetVariableIndexes(indexingStrategy);
 }
        public static void AddLibraryClasses(Nodes.Program program)
        {
            program.ClassNodes.Add(new AttributeBuilder(program)
                                   .WithName("JavaRef")
                                   .WithParameter(ReturnType.String, "ReferencePath", 0)
                                   .Build());

            program.ClassNodes.Add(new AttributeBuilder(program)
                                   .WithName("DotNetRef")
                                   .WithParameter(ReturnType.String, "ReferencePath", 0)
                                   .Build());
        }
Exemple #3
0
        private static void CheckMainClass(this Nodes.Program program)
        {
            var mainClass = program.Scope.LocalClassSearch(MainClassName);

            if (mainClass == null)
            {
                throw new ScopeException("Program must contain a class\"Main\"");
            }
            var mainFunc = mainClass.Scope.LocalFunctionSearch(MainFuncName);

            if (mainFunc == null)
            {
                throw new ScopeException("Program must contain a \"Main\" function inside of Main class");
            }
        }
Exemple #4
0
        public static void PreProcess(this Nodes.Program program)
        {
            var scope = program.Scope;
            FunctionDeclaration mainFunction = null;

            program.ClassNodes.ForEach(classDeclaration =>
            {
                classDeclaration.CheckName(scope);
                scope.AddClass(classDeclaration);
                classDeclaration.PreProcess();

                //FunctionDeclaration func = classDeclaration.Scope.LocalFunctionSearch(MainClassName);
                //if(func != null && classDeclaration.IsStatic)
                //    throw new ScopeException("Main function must be declared inside of a non static class");
                //if(func != null && mainFunction != null)
                //    throw new ScopeException("Only 1 Main function can be present");
                //mainFunction = func ?? mainFunction;
            });
            CheckMainClass(program);
            //if(mainFunction == null)
            //    throw new ScopeException("Program must contain a Main function which is an entry point");
        }
 public AttributeBuilder(Nodes.Program program)
 {
     Attribute = new AttributeDeclaration(program, program.Scope);
 }
Exemple #6
0
        public static void SetVariableIndexes(this Nodes.Program program,
                                              FunctionIndexingStrategy functionIndexingStrategy)
        {
            program.ClassNodes.ForEach(classDeclaration =>
            {
                classDeclaration.FunctionDeclarationNodes.ForEach(functionDeclaration =>
                {
                    if (functionIndexingStrategy == FunctionIndexingStrategy.United)
                    {
                        int varIndex = functionDeclaration.IsStatic ? 0 : 1;
                        SetFunctionArgsIndexes(functionDeclaration, ref varIndex);
                        if (functionDeclaration.StatementBlock != null)
                        {
                            SetBlockVarsIndexes(functionDeclaration.StatementBlock, ref varIndex);
                        }
                    }
                    else if (functionIndexingStrategy == FunctionIndexingStrategy.Splitted)
                    {
                        int argumenIndex = functionDeclaration.IsStatic ? 0 : 1;
                        int localIndex   = 0;
                        SetFunctionArgsIndexes(functionDeclaration, ref argumenIndex);
                        if (functionDeclaration.StatementBlock != null)
                        {
                            SetBlockVarsIndexes(functionDeclaration.StatementBlock, ref localIndex);
                        }
                    }
                });
            });

            void SetFunctionArgsIndexes(FunctionDeclaration functionDeclaration, ref int index)
            {
                foreach (var parameter in functionDeclaration.ParameterNodes)
                {
                    parameter.Index = index++;
                }
                //SetBlockVarsIndexes(functionDeclaration.StatementBlock, ref index);
            }

            void SetDeclarationIndex(VariableDeclaration declaration, ref int varIndex)
            {
                declaration.Index = varIndex++;
            }

            void SetBlockVarsIndexes(BlockStatement blockStatement, ref int varIndex)
            {
                foreach (var statement in blockStatement.Statements)
                {
                    SetStatementIndexes(statement, ref varIndex);
                }
            }

            void SetStatementIndexes(IStatement statement, ref int varIndex)
            {
                switch (statement)
                {
                case VariableDeclaration declaration:
                {
                    SetDeclarationIndex(declaration, ref varIndex);
                    break;
                }

                case BlockStatement blockStatement:
                    SetBlockVarsIndexes(blockStatement, ref varIndex);
                    break;

                case IfStatement ifStatement:
                {
                    SetStatementIndexes(ifStatement.TrueCaseBlockStatement, ref varIndex);
                    SetStatementIndexes(ifStatement.FasleCaseBlockStatement, ref varIndex);
                    break;
                }

                case WhileStatement whileStatement:
                {
                    SetStatementIndexes(whileStatement.BlockOrSingleStatement, ref varIndex);
                    break;
                }

                case ForStatement forStatement:
                {
                    SetStatementIndexes(forStatement.InitializationStatement, ref varIndex);
                    SetStatementIndexes(forStatement.BlockOrSingleStatement, ref varIndex);
                    SetStatementIndexes(forStatement.IterationStatement, ref varIndex);
                    break;
                }
                }
            }
        }
Exemple #7
0
 public static void Process(this Nodes.Program program)
 {
     program.ClassNodes.ForEach(Process);
 }