public void Translate(ActionSet actionSet) { // Add the if action. Element newIf = Element.If(Expression.Parse(actionSet)); newIf.Comment = Comment; actionSet.AddAction(newIf); // Translate the if block. Block.Translate(actionSet); // Add the else-ifs. for (int i = 0; i < ElseIfs.Length; i++) { // Add the else-if action. actionSet.AddAction(Element.ElseIf(ElseIfs[i].Expression.Parse(actionSet))); // Translate the else-if block. ElseIfs[i].Block.Translate(actionSet); } // If there is an else block, translate it. if (ElseBlock != null) { actionSet.AddAction(Element.Else()); ElseBlock.Translate(actionSet); } // Add the end of the if. var end = Element.End(); end.Comment = EndComment; actionSet.AddAction(end); }
public override LuaValue Execute(LuaTable enviroment, out bool isBreak) { LuaValue condition = Condition.Evaluate(enviroment); if (condition.GetBooleanValue() == true) { return(ThenBlock.Execute(enviroment, out isBreak)); } else { foreach (ElseifBlock elseifBlock in ElseifBlocks) { condition = elseifBlock.Condition.Evaluate(enviroment); if (condition.GetBooleanValue() == true) { return(elseifBlock.ThenBlock.Execute(enviroment, out isBreak)); } } if (ElseBlock != null) { return(ElseBlock.Execute(enviroment, out isBreak)); } } isBreak = false; return(null); }
public override void SetChildrenScopes(NovaScope scope) { TryBlock.SetScope(scope); RescueBlocks.ForEach(block => block.SetScope(scope)); EnsureBlock.SetScope(scope); ElseBlock.SetScope(scope); }
public void Test2() { var classBlock = new ClassBlock("TestClass"); var method1 = new MethodBlock("LoopName"); method1.Parameters = new FieldBlock[] { new FieldBlock("name", typeof(string).ToDynamic()) }; IfBlock if1 = new IfBlock(); ElseBlock else1 = new ElseBlock(); if1.Condition = new OperationBlock( new GetterBlock(method1.Parameters[0], new PropertyBlock("Length", null)), OperationType.Equal, new ValueBlock(3)); if1.Add(new ReturnBlock(method1.Parameters[0])); else1.Add(new ReturnBlock(new ValueBlock("!"))); method1.Add(if1); method1.Add(else1); classBlock.AddMethod(method1); }
public void Translate(ActionSet actionSet) { // Add the if action. actionSet.AddAction(Element.Part <A_If>(Expression.Parse(actionSet))); // Translate the if block. Block.Translate(actionSet.Indent()); // Add the else-ifs. for (int i = 0; i < ElseIfs.Length; i++) { // Add the else-if action. actionSet.AddAction(Element.Part <A_ElseIf>(ElseIfs[i].Expression.Parse(actionSet))); // Translate the else-if block. ElseIfs[i].Block.Translate(actionSet.Indent()); } // If there is an else block, translate it. if (ElseBlock != null) { actionSet.AddAction(new A_Else()); ElseBlock.Translate(actionSet.Indent()); } // Add the end of the if. actionSet.AddAction(new A_End()); }
public IfBlock(int index, LineCollection fullCode) { Else = null; Header = fullCode[index]; LineCollection ifLines = new LineCollection(); LineCollection elseLines = new LineCollection(); int expected_endif = 1; // How many 'END IF' statements are expected index++; bool isElse = false; // Whether this should be added to the else block for (; index < fullCode.Count; index++) { Line cur = fullCode[index]; if (cur.Name.EqualsIgnoreCase("IF")) { expected_endif++; } if (expected_endif > 0) { if (expected_endif == 1 && cur.Name.EqualsIgnoreCase("ELSE")) // we are now in an else block { isElse = true; continue; // We don't need to add the word 'ELSE' } } if (cur.Text.EqualsIgnoreCase("END IF")) { expected_endif--; } if (expected_endif == 0) { if (elseLines.Count > 0) { Else = new ElseBlock(elseLines); } Footer = cur; Body = ifLines; return; } if (isElse) { elseLines.Add(cur); } else { ifLines.Add(cur); } } throw ThrowHelper.UnterminatedBlock(Header.VisibleName); }
public ElseBlock Build() { var elseBlock = new ElseBlock(_block); elseBlock.Body = new OrderedBlockBuilder(_reader, "else", elseBlock).Build(); return(elseBlock); }
public void IfTest2() { VariableExpression vaexp = new VariableExpression(); vaexp.Name = "VAR1"; VariableExpression vaexp2 = new VariableExpression(); vaexp2.Name = "VAR2"; ConstExpression cst = new ConstExpression(); cst.SetValue(true); ConstExpression cst2 = new ConstExpression(); cst2.SetValue(10); ConstExpression cst3 = new ConstExpression(); cst3.SetValue(5); ConstExpression cst4 = new ConstExpression(); cst4.SetValue(7); IfBlock ifblk = new IfBlock(); ElseBlock elseblk = new ElseBlock(); SetVarBlock setvar1 = new SetVarBlock(); SetVarBlock setvar2 = new SetVarBlock(); SetVarBlock setvar3 = new SetVarBlock(); ifblk.Condition = cst; ifblk.InnerBlock = setvar1; ifblk.NextBlock = elseblk; elseblk.InnerBlock = setvar2; elseblk.NextBlock = setvar3; setvar1.VarName = "VAR1"; setvar1.ValueExp = cst2; setvar2.VarName = "VAR1"; setvar2.ValueExp = cst3; setvar3.VarName = "VAR2"; setvar3.ValueExp = cst4; ifblk.Execute(); Assert.AreEqual(vaexp.Value, 10); Assert.AreEqual(vaexp2.Value, 7); cst.SetValue(false); ifblk.Execute(); Assert.AreEqual(vaexp.Value, 5); Assert.AreEqual(vaexp2.Value, 7); }
public void Translate(ActionSet actionSet) { SkipStartMarker ifStart = new SkipStartMarker(actionSet, Expression.Parse(actionSet)); actionSet.AddAction(ifStart); Block.Translate(actionSet); List <SkipStartMarker> blockCaps = new List <SkipStartMarker>(); if (ElseBlock != null || ElseIfs.Length > 0) { SkipStartMarker ifCap = new SkipStartMarker(actionSet); actionSet.AddAction(ifCap); blockCaps.Add(ifCap); } SkipEndMarker ifEnd = new SkipEndMarker(); actionSet.AddAction(ifEnd); ifStart.SkipCount = ifStart.GetSkipCount(ifEnd); // Get the else-ifs. for (int i = 0; i < ElseIfs.Length; i++) { bool isLast = i == ElseIfs.Length - 1 && ElseBlock == null; SkipStartMarker elseIfStart = new SkipStartMarker(actionSet, ElseIfs[i].Expression.Parse(actionSet)); actionSet.AddAction(elseIfStart); ElseIfs[i].Block.Translate(actionSet); if (!isLast) { SkipStartMarker elseIfCap = new SkipStartMarker(actionSet); actionSet.AddAction(elseIfCap); blockCaps.Add(elseIfCap); } SkipEndMarker elseIfEnd = new SkipEndMarker(); actionSet.AddAction(elseIfEnd); elseIfStart.SkipCount = elseIfStart.GetSkipCount(elseIfEnd); } if (ElseBlock != null) { ElseBlock.Translate(actionSet); } SkipEndMarker contextCap = new SkipEndMarker(); actionSet.AddAction(contextCap); foreach (var blockCap in blockCaps) { blockCap.SkipCount = blockCap.GetSkipCount(contextCap); } }
public override void VisitElseDirectiveTrivia(ElseDirectiveTriviaSyntax node) { var ifDirectiveBlock = ifDirectiveBlockStack.Peek(); var elseBlock = new ElseBlock(node); ifDirectiveBlock.AddChild(elseBlock); base.VisitElseDirectiveTrivia(node); }
internal void Execute(Context context) { if ((dynamic)ConditionCallback(context)) { IfBlock.Execute(); } else { ElseBlock.Execute(); } }
public override void AcceptVisitor(StatementVisitor visitor) { visitor.VisitIfStatement(this); if (Condition != null) { Condition.AcceptVisitor(visitor); } if (ThenBlock != null) { ThenBlock.AcceptVisitor(visitor); } if (ElseBlock != null) { ElseBlock.AcceptVisitor(visitor); } }
public void TranslateSkip(ActionSet actionSet) { // Create the skip for the start of the if statement. SkipStartMarker ifStart = new SkipStartMarker(actionSet, Expression.Parse(actionSet)); actionSet.AddAction(ifStart); // Translate the if block. Block.Translate(actionSet); // 'block caps' are skips that are added to the end of the if block and each else-if block. // The skips skip to the end of the entire if/else-if/else. List <SkipStartMarker> blockCaps = new List <SkipStartMarker>(); // Add the if cap if there is an else block or there is an else-if block. if (ElseBlock != null || ElseIfs.Length > 0) { SkipStartMarker ifCap = new SkipStartMarker(actionSet); actionSet.AddAction(ifCap); blockCaps.Add(ifCap); } // Marks the end of the if statement. If the if-condition is false, `ifStart` will skip to here. SkipEndMarker ifEnd = new SkipEndMarker(); actionSet.AddAction(ifEnd); // Set the if-skip's count. ifStart.SetEndMarker(ifEnd); // Get the else-ifs. for (int i = 0; i < ElseIfs.Length; i++) { // This will equal true if this is at the last else-if and there is no else. bool isLast = i == ElseIfs.Length - 1 && ElseBlock == null; // Create the skip for the start of the else-if. SkipStartMarker elseIfStart = new SkipStartMarker(actionSet, ElseIfs[i].Expression.Parse(actionSet)); actionSet.AddAction(elseIfStart); // Translate the else-if block. ElseIfs[i].Block.Translate(actionSet); // If this is not the last block in the entire if/else-if/else list, add the 'block cap'. if (!isLast) { SkipStartMarker elseIfCap = new SkipStartMarker(actionSet); actionSet.AddAction(elseIfCap); blockCaps.Add(elseIfCap); } // Marks the end of the else-if statement. If the condition is false, `elseIfStart` will skip to here. SkipEndMarker elseIfEnd = new SkipEndMarker(); actionSet.AddAction(elseIfEnd); elseIfStart.SetEndMarker(elseIfEnd); } // If there is an else block, translate it. if (ElseBlock != null) { ElseBlock.Translate(actionSet); } // contextCap marks the end of the entire if/else-if/list. SkipEndMarker contextCap = new SkipEndMarker(); actionSet.AddAction(contextCap); // Set all the block caps so they skip to the end of the list. foreach (var blockCap in blockCaps) { blockCap.SetEndMarker(contextCap); } }
public bool HasSideEffects() => IfBlock.HasSideEffects() || (HasElseBlock && ElseBlock.HasSideEffects());
private void CompileElse(ElseBlock data) { Write("else"); CompileStmtList(data.Statements); }
public List <Stmt> GenerateAST() { Block currentBlock = new Block(); var blockstack = new Stack <Block>(); Token tok = null; var tree = new List <Stmt>(); var running = true; while (running) { try { tok = CurrentToken; CurrentIndex++; if (tok == null) { break; } } catch { } if (currentBlock is IfBlock ifb) { if (BlockEndsAtThisLine != -1 && tok.Line > BlockEndsAtThisLine) { // currentBlock.AddStmt( new EndIf() ); Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } BlockEndsAtThisLine = -1; } } if (currentBlock is ForBlock || currentBlock is WhileBlock) { if (BlockEndsAtThisLine != -1 && tok.Line > BlockEndsAtThisLine) { Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } BlockEndsAtThisLine = -1; } } switch (tok.TokenName) { case Lexer.Tokens.LeftBrace: case Lexer.Tokens.Comment: case Lexer.Tokens.NewLine: continue; case Lexer.Tokens.Call: currentBlock.AddStmt(ParseCall()); continue; case Lexer.Tokens.Tile: currentBlock.AddStmt(ParseTile()); continue; case Lexer.Tokens.Scanjournal: currentBlock.AddStmt(ParseScanJournal()); continue; case Lexer.Tokens.ExEvent: currentBlock.AddStmt(ParseExEvent()); continue; case Lexer.Tokens.Target: currentBlock.AddStmt(ParseTarget()); continue; case Lexer.Tokens.Msg: currentBlock.AddStmt(ParseMessage()); continue; case Lexer.Tokens.Move: currentBlock.AddStmt(ParseMove()); continue; case Lexer.Tokens.Menu: currentBlock.AddStmt(ParseMenu()); continue; case Lexer.Tokens.Click: currentBlock.AddStmt(ParseClick()); continue; case Lexer.Tokens.Pause: currentBlock.AddStmt(new PauseStmt() { Line = tok.Line }); continue; case Lexer.Tokens.Continue: currentBlock.AddStmt(new Continue() { Line = tok.Line }); continue; case Lexer.Tokens.IgnoreItem: currentBlock.AddStmt(ParseIgnoreItem()); continue; case Lexer.Tokens.LinesPerCycle: currentBlock.AddStmt(new LinesPerCycle(ParseExpr()) { Line = tok.Line }); continue; case Lexer.Tokens.Wait: currentBlock.AddStmt(new WaitStmt(ParseExpr()) { Line = tok.Line }); continue; case Lexer.Tokens.Label: currentBlock.AddStmt(new Label(tok.TokenValue.TrimEnd(new[] { ':' }).ToLowerInvariant()) { Line = tok.Line }); CurrentIndex++; continue; case Lexer.Tokens.Break: currentBlock.AddStmt(new Break() { Line = tok.Line }); CurrentIndex++; continue; case Lexer.Tokens.Goto: { currentBlock.AddStmt(new Goto(CurrentToken.TokenValue.TrimEnd(new[] { ':' }).ToLowerInvariant()) { Line = tok.Line }); CurrentIndex++; Block block = currentBlock; /* if ( blockstack.Count > 0 && block is Func ) * { * currentBlock = blockstack.Pop(); * currentBlock.AddStmt( block ); * }*/ } continue; case Lexer.Tokens.Event: currentBlock.AddStmt(ParseEvent()); continue; case Lexer.Tokens.FindItem: currentBlock.AddStmt(ParseFindItem()); continue; } if (tok.TokenName == Lexer.Tokens.Import) { // Program.imports.Add( ParseImport() ); } else if (tok.TokenName == Lexer.Tokens.Function) { Block block = currentBlock; if (blockstack.Count > 0 && block is Func) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } Func func = ParseFunc(); if (currentBlock != null) { blockstack.Push(currentBlock); currentBlock = func; } } else if (tok.TokenName == Lexer.Tokens.If) { IfBlock ifblock = ParseIf(); if (currentBlock != null) { blockstack.Push(currentBlock); currentBlock = ifblock; } } /* else if ( tok.TokenName == Lexer.Tokens.ElseIf ) * { * ElseIfBlock elseifblock = ParseElseIf(); * * if ( currentBlock != null ) * { * blockstack.Push( currentBlock ); * currentBlock = elseifblock; * } * }*/ else if (tok.TokenName == Lexer.Tokens.Else) { if (currentBlock != null) { blockstack.Push(currentBlock); currentBlock = new ElseBlock() { Line = tok.Line }; } } else if (tok.TokenName == Lexer.Tokens.For) { blockstack.Push(currentBlock); currentBlock = ParseFor(); } else if (tok.TokenName == Lexer.Tokens.While) { blockstack.Push(currentBlock); currentBlock = ParseWhile(); } else if (tok.TokenName == Lexer.Tokens.Repeat) { if (currentBlock != null) { blockstack.Push(currentBlock); currentBlock = new RepeatBlock() { Line = tok.Line }; } } else if (tok.TokenName == Lexer.Tokens.Set) { Assign a = ParseAssign(); currentBlock.AddStmt(a); } /*else if ( tok.TokenName == Lexer.Tokens.Ident ) * { * if ( tokens.Peek().TokenName == Lexer.Tokens.Equal ) * { * tokens.pos--; * Assign a = ParseAssign(); * currentBlock.AddStmt( a ); * } * else if ( tokens.Peek().TokenName == Lexer.Tokens.LeftParan ) * { * tokens.pos--; * Call c = ParseCall(); * currentBlock.AddStmt( c ); * } * }*/ else if (tok.TokenName == Lexer.Tokens.Return) { Return r = ParseReturn(); currentBlock.AddStmt(r); Block block = currentBlock; if (blockstack.Count > 0 && block is Func) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } else if (tok.TokenName == Lexer.Tokens.RightBrace) { if (currentBlock is Func) { currentBlock.AddStmt(new Return(null)); //tree.Add( currentBlock ); //currentBlock = null; Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } else if (currentBlock is IfBlock || currentBlock is ElseIfBlock || currentBlock is ElseBlock) { //currentBlock.AddStmt( new EndIf() ); Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } else if (currentBlock is RepeatBlock) { Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } else if (currentBlock is ForBlock || currentBlock is WhileBlock) { Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } } else if (tok.TokenName == Lexer.Tokens.EOF) { if (currentBlock is Func) { currentBlock.AddStmt(new Return(null)); //tree.Add( currentBlock ); //currentBlock = null; Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } tree.Add(currentBlock); running = false; } else { Console.WriteLine($"Unhandled Token at Line: {tok.Line} " + tok.TokenName + " " + tok.TokenValue); } } return(tree); }
public List <Stmt> GenerateAST() { Block currentBlock = new Block(); var blockstack = new Stack <Block>(); Token tok = null; var tree = new List <Stmt>(); var running = true; while (running) { try { tok = CurrentToken; CurrentIndex++; if (tok == null) { break; } } catch { } if (currentBlock is IfBlock ifb) { if (IfEndsAtThisLine != -1 && tok.Line > IfEndsAtThisLine) { currentBlock.AddStmt(new EndIf()); Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } } switch (tok.TokenName) { case Lexer.Tokens.Call: currentBlock.AddStmt(ParseCall()); break; case Lexer.Tokens.Label: currentBlock.AddStmt(new Label(tok.TokenValue.TrimEnd(new[] { ':' }).ToLowerInvariant()) { Line = tok.Line }); CurrentIndex++; break; case Lexer.Tokens.Goto: { currentBlock.AddStmt(new Goto(CurrentToken.TokenValue.TrimEnd(new[] { ':' }).ToLowerInvariant()) { Line = tok.Line }); CurrentIndex++; Block block = currentBlock; /* if ( blockstack.Count > 0 && block is Func ) * { * currentBlock = blockstack.Pop(); * currentBlock.AddStmt( block ); * }*/ } break; case Lexer.Tokens.Event: currentBlock.AddStmt(ParseEvent()); break; case Lexer.Tokens.FindItem: currentBlock.AddStmt(ParseFindItem()); break; } if (tok.TokenName == Lexer.Tokens.Import) { // Program.imports.Add( ParseImport() ); } else if (tok.TokenName == Lexer.Tokens.Function) { Block block = currentBlock; if (blockstack.Count > 0 && block is Func) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } Func func = ParseFunc(); if (currentBlock != null) { blockstack.Push(currentBlock); currentBlock = func; } } else if (tok.TokenName == Lexer.Tokens.If) { IfBlock ifblock = ParseIf(); if (currentBlock != null) { blockstack.Push(currentBlock); currentBlock = ifblock; } } /* else if ( tok.TokenName == Lexer.Tokens.ElseIf ) * { * ElseIfBlock elseifblock = ParseElseIf(); * * if ( currentBlock != null ) * { * blockstack.Push( currentBlock ); * currentBlock = elseifblock; * } * }*/ else if (tok.TokenName == Lexer.Tokens.Else) { if (currentBlock != null) { blockstack.Push(currentBlock); currentBlock = new ElseBlock() { Line = tok.Line }; } } else if (tok.TokenName == Lexer.Tokens.Repeat) { if (currentBlock != null) { blockstack.Push(currentBlock); currentBlock = new RepeatBlock() { Line = tok.Line }; } } else if (tok.TokenName == Lexer.Tokens.Set) { Assign a = ParseAssign(); currentBlock.AddStmt(a); } /*else if ( tok.TokenName == Lexer.Tokens.Ident ) * { * if ( tokens.Peek().TokenName == Lexer.Tokens.Equal ) * { * tokens.pos--; * Assign a = ParseAssign(); * currentBlock.AddStmt( a ); * } * else if ( tokens.Peek().TokenName == Lexer.Tokens.LeftParan ) * { * tokens.pos--; * Call c = ParseCall(); * currentBlock.AddStmt( c ); * } * }*/ else if (tok.TokenName == Lexer.Tokens.Return) { Return r = ParseReturn(); currentBlock.AddStmt(r); Block block = currentBlock; if (blockstack.Count > 0 && block is Func) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } else if (tok.TokenName == Lexer.Tokens.RightBrace) { if (currentBlock is Func) { currentBlock.AddStmt(new Return(null)); //tree.Add( currentBlock ); //currentBlock = null; Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } else if (currentBlock is IfBlock || currentBlock is ElseIfBlock || currentBlock is ElseBlock) { currentBlock.AddStmt(new EndIf()); Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } else if (currentBlock is RepeatBlock) { Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } } else if (tok.TokenName == Lexer.Tokens.EOF) { if (currentBlock is Func) { currentBlock.AddStmt(new Return(null)); //tree.Add( currentBlock ); //currentBlock = null; Block block = currentBlock; if (blockstack.Count > 0) { currentBlock = blockstack.Pop(); currentBlock.AddStmt(block); } } tree.Add(currentBlock); running = false; } } return(tree); }