/// <summary> /// Parser for EachStatement /// </summary> /// <returns>Parsed EachStatement</returns> public EachStatement ParseEachStatement() { EachStatement eachStatement = new EachStatement(); //Skip each token NextToken("each", "each (identifier : expression) Statement", "each"); //Skip ( token NextToken("(", "each (identifier : expression) Statement", '('); //Parse identifier CurrentToken = TokenStream.NextToken(); eachStatement.SetIdentifier(CurrentToken.GetValue().ToString()); //Skip : token NextToken(":", "each (identifier : expression) Statement", ':'); //Parse expression eachStatement.SetExpression(expressionParser.ParseExpression()); //Skip ) token NextToken(")", "each (identifier : expression) Statement", ')'); //Parse statement eachStatement.SetStatement(ParseStatement()); return(eachStatement); }
/// <summary> /// Interpret EachStatement /// </summary> /// <param name="statement">EachStatement to interpret</param> public override void Visit(EachStatement statement) { //If no root element, create one if (Root == null) { XHTMLElement newRoot = new XHTMLElement("html", null, true); Root = newRoot; Current = Root; } Expression expr = statement.GetExpression(); //Different looping with different expression types if (expr is ListExpression) { //Iterate through list ListExpression listExpression = (ListExpression)expr; //Iterate through list with expression XHTMLElement temp = Current; foreach (Expression currentExpr in listExpression.GetExpressions()) { Current = temp; //New scope SymbolTable = new SymbolTable(SymbolTable); //Define variable and interpret statement with this scope SymbolTable.AddVariableDefinition(statement.GetIdentifier(), currentExpr); statement.GetStatement().AcceptVisitor(this); //Return to parent scope SymbolTable = SymbolTable.GetParentSymbolTable(); } } else if (expr is FieldExpression) { //Get expression in referenced record Expression expression = GetExpression((FieldExpression)expr); //Visit each for expression EachStatement eachStatement = new EachStatement(); eachStatement.SetExpression(expression); eachStatement.SetStatement(statement.GetStatement()); eachStatement.SetIdentifier(statement.GetIdentifier()); Visit(eachStatement); } else if (expr is VarExpression) { //Get expression of variable Expression expression = SymbolTable.GetVariableDefinition(((VarExpression)expr).GetVariableIdentifier()); //Visit each for expression EachStatement eachStatement = new EachStatement(); eachStatement.SetExpression(expression); eachStatement.SetStatement(statement.GetStatement()); eachStatement.SetIdentifier(statement.GetIdentifier()); Visit(eachStatement); } }