public override object VisitStatement(DeltinScriptParser.StatementContext context) { if (context.GetChild(0) is DeltinScriptParser.MethodContext && context.ChildCount == 1) { _diagnostics.Add(new Diagnostic("Expected ';'", Range.GetRange(context)) { severity = Diagnostic.Error }); } return(base.VisitStatement(context)); }
public override Node VisitStatement(DeltinScriptParser.StatementContext context) { Node node = null; if (node == null) { return(Visit(context.GetChild(0))); } else { return(node); } }
public override object VisitStatement(DeltinScriptParser.StatementContext context) { switch (context.GetChild(0)) { case DeltinScriptParser.MethodContext _: case DeltinScriptParser.DefineContext _: case DeltinScriptParser.VarsetContext _: case DeltinScriptParser.ExprContext _: if (context.ChildCount == 1) { _diagnostics.Error("Expected ';'", new Location(_file, Range.GetRange(context).end.ToRange())); } break; } return(base.VisitStatement(context)); }
void ParseStatement(DeltinScriptParser.StatementContext statementContext) { #region Method if (statementContext.GetChild(0) is DeltinScriptParser.MethodContext) { Actions.Add(ParseMethod(statementContext.GetChild(0) as DeltinScriptParser.MethodContext, false)); return; } #endregion #region Variable set if (statementContext.STATEMENT_OPERATION() != null) { DefinedVar variable; Element target; Element index = null; string operation = statementContext.STATEMENT_OPERATION().GetText(); Element value; value = ParseExpression(statementContext.expr(1) as DeltinScriptParser.ExprContext); /* Format if the variable has an expression beforehand (sets the target player) * expr(0) .ChildCount * v v * Statement ( v v ) | Operation | Set to variable * Variable to set ( v ) * ^ expr | . | expr * ^ ^ * expr(0) .GetChild(1) == '.' */ if (statementContext.expr(0).ChildCount == 3 && statementContext.expr(0).GetChild(1).GetText() == ".") { /* Get Variable: .expr(0) .expr(1) * v v .expr(1) (if the value to be set is an array) * Statement ( v v v ) | Operation | Set to variable * Variable to set ( v v ) * ^ expr | . | expr | [] * ^ ^ * Get Target: .expr(0) .expr(0) */ variable = DefinedVar.GetVar(statementContext.expr(0).expr(1).GetChild(0).GetText(), statementContext.expr(0).expr(1).start); target = ParseExpression(statementContext.expr(0).expr(0)); // Get the index if the variable has [] var indexExpression = statementContext.expr(0).expr(1).expr(1); if (indexExpression != null) { index = ParseExpression(indexExpression); } } else { /* .expr(0) .expr(1) * v v * Statement ( v v ) | Operation | Set to variable * Variable to set (expr) | [] */ variable = DefinedVar.GetVar(statementContext.expr(0).GetChild(0).GetText(), statementContext.expr(0).start); target = new V_EventPlayer(); // Get the index if the variable has [] var indexExpression = statementContext.expr(0).expr(1); if (indexExpression != null) { index = ParseExpression(indexExpression); } } switch (operation) { case "+=": value = Element.Part <V_Add>(variable.GetVariable(target, index), value); break; case "-=": value = Element.Part <V_Subtract>(variable.GetVariable(target, index), value); break; case "*=": value = Element.Part <V_Multiply>(variable.GetVariable(target, index), value); break; case "/=": value = Element.Part <V_Divide>(variable.GetVariable(target, index), value); break; case "^=": value = Element.Part <V_RaiseToPower>(variable.GetVariable(target, index), value); break; case "%=": value = Element.Part <V_Modulo>(variable.GetVariable(target, index), value); break; } Actions.Add(variable.SetVariable(value, target, index)); return; } #endregion #region for if (statementContext.GetChild(0) is DeltinScriptParser.ForContext) { /* * CreateInitialSkip = true; * * if (SkipCountIndex == -1) * SkipCountIndex = Assign(); */ // The action the for loop starts on. // +1 for the counter reset. int forActionStartIndex = Actions.Count() + 1; // The target array in the for statement. Element forArrayElement = ParseExpression(statementContext.@for().expr()); // Use skipIndex with Get/SetIVarAtIndex to get the bool to determine if the loop is running. Var isBoolRunningSkipIf = Var.AssignVar(IsGlobal); // Insert the SkipIf at the start of the rule. Actions.Insert(0, Element.Part <A_SkipIf> ( // Condition isBoolRunningSkipIf.GetVariable(), // Number of actions new V_Number(forActionStartIndex) ) ); // Create the for's temporary variable. DefinedVar forTempVar = Var.AssignDefinedVar( name: statementContext.@for().PART().GetText(), isGlobal: IsGlobal, token: statementContext.@for().start ); // Reset the counter. Actions.Add(forTempVar.SetVariable(new V_Number(0))); // Parse the for's block. ParseBlock(statementContext.@for().block()); // Take the variable out of scope. forTempVar.OutOfScope(); // Add the for's finishing elements //Actions.Add(SetIVarAtIndex(skipIndex, new V_Number(forActionStartIndex))); // Sets how many variables to skip in the next iteraction. Actions.Add(isBoolRunningSkipIf.SetVariable(new V_True())); // Enables the skip. Actions.Add(forTempVar.SetVariable( // Indent the index by 1. Element.Part <V_Add> ( forTempVar.GetVariable(), new V_Number(1) ) )); Actions.Add(Element.Part <A_Wait>(new V_Number(0.06), WaitBehavior.IgnoreCondition)); // Add the Wait() required by the workshop. Actions.Add(Element.Part <A_LoopIf>( // Loop if the for condition is still true. Element.Part <V_Compare> ( forTempVar.GetVariable(), Operators.LessThan, Element.Part <V_CountOf>(forArrayElement) ) )); Actions.Add(isBoolRunningSkipIf.SetVariable(new V_False())); return; } #endregion #region if if (statementContext.GetChild(0) is DeltinScriptParser.IfContext) { /* * Syntax after parse: * * If: * Skip If (Not (expr)) * (body) * Skip - Only if there is if-else or else statements. * Else if: * Skip If (Not (expr)) * (body) * Skip - Only if there is more if-else or else statements. * Else: * (body) * */ // Add dummy action, create after body is created. int skipIfIndex = Actions.Count(); Actions.Add(null); // Parse the if body. ParseBlock(statementContext.@if().block()); // Determines if the "Skip" action after the if block will be created. // Only if there is if-else or else statements. bool addIfSkip = statementContext.@if().else_if().Count() > 0 || statementContext.@if().@else() != null; // Create the inital "SkipIf" action now that we know how long the if's body is. // Add one to the body length if a Skip action is going to be added. Actions.RemoveAt(skipIfIndex); Actions.Insert(skipIfIndex, Element.Part <A_SkipIf>(Element.Part <V_Not>(ParseExpression(statementContext.@if().expr())), new V_Number(Actions.Count - skipIfIndex + (addIfSkip ? 1 : 0)))); // Create the "Skip" dummy action. int skipIndex = -1; if (addIfSkip) { skipIndex = Actions.Count(); Actions.Add(null); } // Parse else-ifs var skipIfContext = statementContext.@if().else_if(); int[] skipIfData = new int[skipIfContext.Length]; // The index where the else if's "Skip" action is. for (int i = 0; i < skipIfContext.Length; i++) { // Create the dummy action. int skipIfElseIndex = Actions.Count(); Actions.Add(null); // Parse the else-if body. ParseBlock(skipIfContext[i].block()); // Determines if the "Skip" action after the else-if block will be created. // Only if there is additional if-else or else statements. bool addIfElseSkip = i < skipIfContext.Length - 1 || statementContext.@if().@else() != null; // Create the "Skip If" action. Actions.RemoveAt(skipIfElseIndex); Actions.Insert(skipIfElseIndex, Element.Part <A_SkipIf>(Element.Part <V_Not>(ParseExpression(skipIfContext[i].expr())), new V_Number(Actions.Count - skipIfElseIndex + (addIfElseSkip ? 1 : 0)))); // Create the "Skip" dummy action. if (addIfElseSkip) { skipIfData[i] = Actions.Count(); Actions.Add(null); } } // Parse else body. if (statementContext.@if().@else() != null) { ParseBlock(statementContext.@if().@else().block()); } // Replace dummy skip with real skip now that we know the length of the if, if-else, and else's bodies. // Replace if's dummy. if (skipIndex != -1) { Actions.RemoveAt(skipIndex); Actions.Insert(skipIndex, Element.Part <A_Skip>(new V_Number(Actions.Count - skipIndex))); } // Replace else-if's dummy. for (int i = 0; i < skipIfData.Length; i++) { if (skipIfData[i] != 0) { Actions.RemoveAt(skipIfData[i]); Actions.Insert(skipIfData[i], Element.Part <A_Skip>(new V_Number(Actions.Count - skipIfData[i]))); } } return; } #endregion throw new Exception($"What's a {statementContext.GetChild(0)} ({statementContext.GetChild(0).GetType()})?"); }