Ejemplo n.º 1
0
        public override void ExitBranch(HS_Gen1Parser.BranchContext context)
        {
            if (_debug)
            {
                _logger.Branch(context, CompilerContextAction.Exit);
            }

            // Generate the script name.
            var scriptContext = GetParentContext(context, HS_Gen1Parser.RULE_scriptDeclaration) as HS_Gen1Parser.ScriptDeclarationContext;
            if(scriptContext is null)
            {
                throw new CompilerException("The compiler failed to retrieve the name of a script, from which \"branch\" was called.", context);
            }
            string fromScript = scriptContext.scriptID().GetTextSanitized();

            var parameters = context.expression();
            var callParameter = parameters[1].call();
            if (callParameter is null)
            {
                throw new CompilerException("A branch call's second argument must be a script call.", context);
            }
            string toScript = callParameter.callID().GetTextSanitized();
            string generatedName = fromScript + "_to_" + toScript;
            var condition = _expressions[_branchBoolIndex].Clone();
            var scriptReference = _expressions[condition.Next.Index].Clone();

            // Modify the original script reference. The opcode points to the generated script.
            _expressions[condition.Next.Index].Opcode = _nextGenBranchIndex;

            // Add the generated script to the lookup.
            ScriptInfo info = new ScriptInfo(generatedName, "static", "void", _nextGenBranchIndex);
            AddScriptToLookup(info);
            _nextGenBranchIndex++;
            _generatedBranches.Add(new Branch(generatedName, context.GetCorrectTextPosition(_missingCarriageReturnPositions), condition, scriptReference));

            CloseDatum();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generate script and global lookups.
        /// </summary>
        /// <param name="context"></param>
        public override void EnterHsc(HS_Gen1Parser.HscContext context)
        {
            // Generate the globals lookup.
            if (context.globalDeclaration() != null)
            {
                _mapGlobalsLookup = context.globalDeclaration().Select((g, index) => new GlobalInfo(g, (ushort)index)).ToDictionary(g => g.Name);

            }

            // Generate the script lookup and determine the index, which the next generated branch script will have.
            var declarations = context.scriptDeclaration();
            if (declarations != null)
            {
                _nextGenBranchIndex = (ushort)declarations.Length;
                _scriptLookup = new Dictionary<string, List<ScriptInfo>>();

                for(ushort i = 0; i < context.scriptDeclaration().Length; i++)
                {
                    var info = new ScriptInfo(declarations[i], i);
                    AddScriptToLookup(info);
                }
            }

            // The declaration count is used to calculate the current progress.
            _declarationCount = context.scriptDeclaration().Length + context.globalDeclaration().Length;

            // Find all missing carriage returns. Somehow antlr removes them under certain circumstances and messes up the text position indeces.
            string text = context.Start.InputStream.GetText(new Interval(0, context.Start.InputStream.Size));
            string otherText = context.GetText();
            bool same = text == otherText;
            var missingCarriageReturns = Regex.Matches(text, @"(?<!\r)\n");
            foreach (Match match in missingCarriageReturns)
            {
                _missingCarriageReturnPositions.Add(match.Index);
            }
        }