// Build a number, string, or character literal: v = 1234 public static void BuildLiteral(IronyParser parser, Root root, Expression parentExpression, ParseTreeNode currentNode) { var e = new Literal(parentExpression, currentNode.FindToken().Convert()); parentExpression.ChildExpressions.Add(e); e.ParentExpression = parentExpression; e.Value = currentNode.Token.Value; }
// Build a boolean literal: v = true public static void BuildBoolLiteral(IronyParser parser, Root root, Expression parentExpression, ParseTreeNode currentNode) { var e = new Literal(parentExpression, currentNode.FindToken().Convert()); parentExpression.ChildExpressions.Add(e); e.ParentExpression = parentExpression; if (currentNode.ChildNodes[0].FindTokenAndGetText() == "true") e.Value = true; else e.Value = false; }
// Build a for loop statement. public static void BuildForLoop(IronyParser parser, Root root, Expression parentExpression, ParseTreeNode currentNode) { var loop = new ForLoop(parentExpression, currentNode.Token.Convert()); parentExpression.ChildExpressions.Add(loop); // Build the initializer expression for the loop. parser.ConsumeParseTree(root, loop.Initialization, currentNode.ChildNodes[1]); // Build the conditional expression for the loop. switch(currentNode.ChildNodes[3].ChildNodes[0].Term.ToString()) { case "for_range": parser.ConsumeParseTree(root, loop.Condition, currentNode.ChildNodes[3].ChildNodes[0].ChildNodes[0]); parser.ConsumeParseTree(root, loop.Condition, currentNode.ChildNodes[3].ChildNodes[0].ChildNodes[2]); break; default: parser.ConsumeParseTree(root, loop.Condition, currentNode.ChildNodes[3].ChildNodes[0]); break; } // Build the increment/decrement step expression for the loop. if(currentNode.ChildNodes[4].ChildNodes.Count > 0) { parser.ConsumeParseTree(root, loop.Step, currentNode.ChildNodes[4]); } else { var literal = new Literal(loop, null); literal.Value = 1; loop.Step.ChildExpressions.Add(literal); } // Form the body of the loop. if(currentNode.ChildNodes[5].ChildNodes.Count > 0) { parser.ConsumeParseTree(root, loop, currentNode.ChildNodes[5]); } }
// Emit a codedom literal expression: int, long, bool, string, etc. public static CodeExpression Emit(Literal literal) { return new CodePrimitiveExpression(literal.Value); }