public override IPStmt VisitPrintStmt(PParser.PrintStmtContext context) { string message = context.StringLiteral().GetText(); message = message.Substring(1, message.Length - 2); // strip beginning / end double quote int numNecessaryArgs = TypeCheckingUtils.PrintStmtNumArgs(message); if (numNecessaryArgs == -1) { throw handler.InvalidPrintFormat(context, context.StringLiteral().Symbol); } List <IPExpr> args = TypeCheckingUtils.VisitRvalueList(context.rvalueList(), exprVisitor).ToList(); foreach (IPExpr arg in args) { if (arg is LinearAccessRefExpr) { throw handler.PrintStmtLinearArgument(arg.SourceLocation); } } if (args.Count != numNecessaryArgs) { throw handler.IncorrectArgumentCount(context, args.Count, numNecessaryArgs); } return(new PrintStmt(context, message, args)); }
public override IPStmt VisitPrintStmt(PParser.PrintStmtContext context) { string message = context.StringLiteral().GetText(); int numNecessaryArgs = (from Match match in Regex.Matches(message, @"(?:{{|}}|{(\d+)}|[^{}]+|{|})") where match.Groups[1].Success select int.Parse(match.Groups[1].Value) + 1) .Concat(new[] { 0 }) .Max(); var args = (from arg in context.rvalueList()?.rvalue() ?? Enumerable.Empty <PParser.RvalueContext>() select exprVisitor.Visit(arg)).ToList(); if (args.Count != numNecessaryArgs) { throw handler.IncorrectArgumentCount((ParserRuleContext)context.rvalueList() ?? context, args.Count, numNecessaryArgs); } return(new PrintStmt(context, message, args)); }