public static IfStatementNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens) { if (tokens[1].Type != EScriptTokenType.L_PAREN) { Console.WriteLine("If statement needs to be followed by a condition encased in parenthesis."); return(null); } List <ConditionalNode> nodes = new List <ConditionalNode>(); nodes.Add(ParseConditional(tokens)); while (tokens.Count > 0 && EScriptTokenType.ELSE_IF == tokens[0].Type) { nodes.Add(ParseConditional(tokens)); } if (tokens.Count > 0 && EScriptTokenType.ELSE == tokens[0].Type) { tokens.RemoveAt(0); // Consume else nodes.Add(new ConditionalNode( new LiteralNode <bool>(true), ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.BRACE, false)[0]) )); } return(new IfStatementNode(nodes)); }
public static ForStatementNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens) { if (tokens[1].Type != EScriptTokenType.L_PAREN) { Console.WriteLine("Syntax error: Missing ('"); return(null); } if (tokens[3].Type != EScriptTokenType.OF) { Console.WriteLine("Syntax error: Missing of"); return(null); } tokens.RemoveAt(0); // consume for List <ScriptToken> loopDef = ScriptTree.GetEnclosedTokens(tokens); ScriptToken variableName = loopDef[0]; loopDef.RemoveAt(0); // consume variable name loopDef.RemoveAt(0); // consume of AstTreeNode list = ScriptTree.ProcessTokens(loopDef); AstTreeNode block = ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.BRACE, false)[0]); return(new ForStatementNode( new LiteralNode <string>(variableName.Value), list, block )); }
public static AssignmentNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens) { if (!(lastNode is IScopeMemberNode)) { Console.WriteLine("Invalid assignment syntax."); return(null); } tokens.RemoveAt(0); // consume = List <ScriptToken> assignmentTokens = ScriptTree.GetStatementTokens(tokens, false); return(new AssignmentNode( (IScopeMemberNode)lastNode, ScriptTree.ProcessTokens(assignmentTokens) )); }
public static ArithmeticAssignmentNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens) { if (lastNode == null || !(lastNode is IScopeMemberNode)) { Console.WriteLine("Invalid assignment syntax."); return(null); } tokens.RemoveAt(0); // consume += List <ScriptToken> assignmentTokens = ScriptTree.GetEnclosedTokens(tokens); return(new ArithmeticAssignmentNode( (IScopeMemberNode)Convert.ChangeType(lastNode, lastNode is RootScopeMemberNode ? typeof(RootScopeMemberNode) : typeof(ScopeMemberNode)), ScriptTree.ProcessTokens(assignmentTokens), scriptToken.Type )); }
public static ConditionalNode ParseConditional(List <ScriptToken> tokens) { List <EScriptTokenType> ifTokens = new List <EScriptTokenType>(); ifTokens.Add(EScriptTokenType.IF); ifTokens.Add(EScriptTokenType.ELSE_IF); if (!ifTokens.Contains(tokens[0].Type)) { Console.WriteLine("Invalid Syntax"); return(null); } tokens.RemoveAt(0); // consume if and else if return(new ConditionalNode( ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.PAREN, false)[0]), ScriptTree.ProcessTokens(ScriptTree.GetBlockTokens(tokens, EBlockType.BRACE, false)[0]) )); }
public static ComparisonNode Parse(AstTreeNode lastNode, ScriptToken scriptToken, List <ScriptToken> tokens) { tokens.RemoveAt(0); // Remove comparison operator return(new ComparisonNode(lastNode, ScriptTree.ProcessTokens(ScriptTree.GetEnclosedTokens(tokens)), scriptToken.Type)); }