示例#1
0
        void Translate()
        {
            // Get the reserved variables and IDs
            // foreach (ScriptFile script in Importer.ScriptFiles)
            // {
            //     if (script.Context.reserved_global()?.reserved_list() != null)
            //     {
            //         foreach (var name in script.Context.reserved_global().reserved_list().PART()) VarCollection.Reserve(name.GetText(), true);
            //         foreach (var id in script.Context.reserved_global().reserved_list().NUMBER()) VarCollection.Reserve(int.Parse(id.GetText()), true, null, null);
            //     }
            //     if (script.Context.reserved_player()?.reserved_list() != null)
            //     {
            //         foreach (var name in script.Context.reserved_player().reserved_list().PART()) VarCollection.Reserve(name.GetText(), false);
            //         foreach (var id in script.Context.reserved_player().reserved_list().NUMBER()) VarCollection.Reserve(int.Parse(id.GetText()), false, null, null);
            //     }
            // }

            // Get the enums
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                foreach (var enumContext in script.Context.Enums)
                {
                    var newEnum = new DefinedEnum(new ParseInfo(script, this), enumContext);
                    Types.AllTypes.Add(newEnum);
                    Types.DefinedTypes.Add(newEnum);
                    Types.CalledTypes.Add(newEnum);
                }
            }

            // Get the types
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                foreach (var typeContext in script.Context.Classes)
                {
                    var newType = new DefinedType(new ParseInfo(script, this), GlobalScope, typeContext);
                    Types.AllTypes.Add(newType);
                    Types.DefinedTypes.Add(newType);
                    Types.CalledTypes.Add(newType);
                }
            }

            // Get the declarations
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                ParseInfo parseInfo = new ParseInfo(script, this);

                // Get the functions.
                foreach (var declaration in script.Context.Declarations)
                {
                    // Function
                    if (declaration is FunctionContext function)
                    {
                        new DefinedMethod(parseInfo, RulesetScope, RulesetScope, function, null);
                    }
                    // Macro function
                    else if (declaration is MacroFunctionContext macroFunction)
                    {
                        parseInfo.GetMacro(RulesetScope, RulesetScope, macroFunction);
                    }
                    // Macro var
                    else if (declaration is MacroVarDeclaration macroVar)
                    {
                        parseInfo.GetMacro(RulesetScope, RulesetScope, macroVar);
                    }
                    // Variables
                    else if (declaration is VariableDeclaration variable)
                    {
                        Var newVar = new RuleLevelVariable(RulesetScope, new DefineContextHandler(new ParseInfo(script, this), variable));
                        rulesetVariables.Add(newVar);

                        // Add the variable to the player variables scope if it is a player variable.
                        if (newVar.VariableType == VariableType.Player)
                        {
                            PlayerVariableScope.CopyVariable(newVar);
                        }
                    }
                }
            }

            foreach (var applyType in Types.AllTypes)
            {
                if (applyType is ClassType classType)
                {
                    classType.ResolveElements();
                }
            }
            foreach (var apply in _applyBlocks)
            {
                apply.SetupParameters();
            }
            foreach (var apply in _applyBlocks)
            {
                apply.SetupBlock();
            }
            foreach (var callInfo in _recursionCheck)
            {
                callInfo.CheckRecursion();
            }

            // Get hooks
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                foreach (var hookContext in script.Context.Hooks)
                {
                    HookVar.GetHook(new ParseInfo(script, this), RulesetScope, hookContext);
                }
            }

            // Get the rules
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                foreach (var ruleContext in script.Context.Rules)
                {
                    rules.Add(new RuleAction(new ParseInfo(script, this), RulesetScope, ruleContext));
                }
            }
        }
        void Translate()
        {
            // Get the reserved variables and IDs
            foreach (ScriptFile script in ScriptFiles)
            {
                if (script.Context.reserved_global()?.reserved_list() != null)
                {
                    foreach (var name in script.Context.reserved_global().reserved_list().PART())
                    {
                        VarCollection.Reserve(name.GetText(), true);
                    }
                    foreach (var id in script.Context.reserved_global().reserved_list().NUMBER())
                    {
                        VarCollection.Reserve(int.Parse(id.GetText()), true, null, null);
                    }
                }
                if (script.Context.reserved_player()?.reserved_list() != null)
                {
                    foreach (var name in script.Context.reserved_player().reserved_list().PART())
                    {
                        VarCollection.Reserve(name.GetText(), false);
                    }
                    foreach (var id in script.Context.reserved_player().reserved_list().NUMBER())
                    {
                        VarCollection.Reserve(int.Parse(id.GetText()), false, null, null);
                    }
                }
            }

            // Get the enums
            foreach (ScriptFile script in ScriptFiles)
            {
                foreach (var enumContext in script.Context.enum_define())
                {
                    var newEnum = new DefinedEnum(new ParseInfo(script, this), enumContext);
                    types.Add(newEnum);
                    definedTypes.Add(newEnum);
                }
            }

            // Get the types
            foreach (ScriptFile script in ScriptFiles)
            {
                foreach (var typeContext in script.Context.type_define())
                {
                    var newType = new DefinedType(new ParseInfo(script, this), GlobalScope, typeContext);
                    types.Add(newType);
                    definedTypes.Add(newType);
                }
            }

            // Get the methods and macros
            foreach (ScriptFile script in ScriptFiles)
            {
                // Get the methods.
                foreach (var methodContext in script.Context.define_method())
                {
                    var newMethod = new DefinedMethod(new ParseInfo(script, this), RulesetScope, RulesetScope, methodContext, null);
                }

                // Get the macros.
                foreach (var macroContext in script.Context.define_macro())
                {
                    GetMacro(new ParseInfo(script, this), RulesetScope, RulesetScope, macroContext);
                }
            }

            // Get the defined variables.
            foreach (ScriptFile script in ScriptFiles)
            {
                foreach (var varContext in script.Context.define())
                {
                    Var newVar = new RuleLevelVariable(RulesetScope, new DefineContextHandler(new ParseInfo(script, this), varContext));
                    rulesetVariables.Add(newVar);

                    // Add the variable to the player variables scope if it is a player variable.
                    if (newVar.VariableType == VariableType.Player)
                    {
                        PlayerVariableScope.CopyVariable(newVar);
                    }
                }
            }

            foreach (var applyType in types)
            {
                if (applyType is ClassType classType)
                {
                    classType.ResolveElements();
                }
            }
            foreach (var apply in applyBlocks)
            {
                apply.SetupParameters();
            }
            foreach (var apply in applyBlocks)
            {
                apply.SetupBlock();
            }
            foreach (var apply in applyBlocks)
            {
                apply.CallInfo?.CheckRecursion();
            }

            // Get the rules
            foreach (ScriptFile script in ScriptFiles)
            {
                foreach (var ruleContext in script.Context.ow_rule())
                {
                    rules.Add(new RuleAction(new ParseInfo(script, this), RulesetScope, ruleContext));
                }
            }
        }
        void Translate()
        {
            AddComponent <RecursionCheckComponent>();

            // Get the enums
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                foreach (var enumContext in script.Context.Enums)
                {
                    var newEnum = new GenericCodeTypeInitializer(new DefinedEnum(new ParseInfo(script, this), enumContext));
                    RulesetScope.AddType(newEnum);
                    Types.AllTypes.Add(newEnum);
                    Types.DefinedTypes.Add(newEnum);
                }
            }

            // Get the types
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                foreach (var typeContext in script.Context.Classes)
                {
                    var newType = IDefinedTypeInitializer.GetInitializer(new ParseInfo(script, this), RulesetScope, typeContext);
                    RulesetScope.AddType(newType);
                    Types.AllTypes.Add(newType);
                    Types.DefinedTypes.Add(newType);
                }
            }

            // Get the variable reservations
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                foreach (Token reservation in script.Context.GlobalvarReservations)
                {
                    string text = reservation.GetText().RemoveQuotes();

                    if (Int32.TryParse(text, out int id))
                    {
                        VarCollection.Reserve(id, true, script.Diagnostics, reservation.Range);
                    }
                    else
                    {
                        VarCollection.Reserve(text, true);
                    }
                }
                foreach (Token reservation in script.Context.PlayervarReservations)
                {
                    string text = reservation.GetText().RemoveQuotes();

                    if (Int32.TryParse(text, out int id))
                    {
                        VarCollection.Reserve(id, false, script.Diagnostics, reservation.Range);
                    }
                    else
                    {
                        VarCollection.Reserve(text, false);
                    }
                }
            }
            // Get variable declarations
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                foreach (var declaration in script.Context.Declarations)
                {
                    if (declaration is VariableDeclaration variable)
                    {
                        Var var = new RuleLevelVariable(RulesetScope, new DefineContextHandler(new ParseInfo(script, this), variable)).GetVar();

                        if (var.StoreType != StoreType.None)
                        {
                            rulesetVariables.Add(var);

                            // Add the variable to the player variables scope if it is a player variable.
                            if (var.VariableType == VariableType.Player)
                            {
                                PlayerVariableScope.CopyVariable(var.GetDefaultInstance(null));
                            }
                        }
                    }
                }
            }

            ElementList.AddWorkshopFunctionsToScope(GlobalScope, Types); // Add workshop methods to global scope.
            GlobalFunctions.GlobalFunctions.Add(this, GlobalScope);      // Add built-in methods.

            // Get the function declarations
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                ParseInfo parseInfo = new ParseInfo(script, this);
                foreach (var declaration in script.Context.Declarations)
                {
                    if (declaration is FunctionContext function)
                    {
                        DefinedMethodProvider.GetDefinedMethod(parseInfo, this, function, null);
                    }
                }
            }

            StagedInitiation.Start();

            // Get hooks
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                foreach (var hookContext in script.Context.Hooks)
                {
                    HookVar.GetHook(new ParseInfo(script, this), RulesetScope, hookContext);
                }
            }

            // Get the rules
            foreach (ScriptFile script in Importer.ScriptFiles)
            {
                foreach (var ruleContext in script.Context.Rules)
                {
                    rules.Add(new RuleAction(new ParseInfo(script, this), RulesetScope, ruleContext));
                }
            }

            GetComponent <SymbolLinkComponent>().Collect();
        }