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();
}