public override void EnterBranch(HS_Gen1Parser.BranchContext context) { if (_debug) { _logger.Branch(context, CompilerContextAction.Enter); } LinkDatum(); _ = _expectedTypes.PopType(); // Just ignore the type for now. // Branch always has two parameters. if (context.expression().Count() != 2) { throw new CompilerException($"\"Branch\" accepts two arguments. The compiler found {context.expression().Count() }.", context); } _expectedTypes.PushTypes("boolean", TypeHelper.ScriptReference); FunctionInfo info = _opcodes.GetFunctionInfo("branch").First(); CreateFunctionCall(info, _opcodes.GetTypeInfo("void").Opcode, context.GetCorrectTextPosition(_missingCarriageReturnPositions), GetLineNumber(context)); _branchBoolIndex = _currentIndex.Index; }
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(); }