//Print all scopes available in Algo. public static void PrintScopes() { //Printing variable scopes. Console.WriteLine("\nGlobal Namespace"); Console.WriteLine("SCOPES:"); var scopes = algoVisitor.Scopes.GetScopes(); for (int i = 0; i < scopes.Count; i++) { //Check variable isn't null. Console.WriteLine("Scope " + (i + 1) + "\n---"); foreach (var variable in scopes[i]) { string varValue = "Null"; if (variable.Value.Type != AlgoValueType.Null) { try { varValue = AlgoConversion.GetStringRepresentation(variable.Value); } catch { varValue = "Undefined / Unavailable"; } } Console.WriteLine(variable.Key + " of type " + variable.Value.Type.ToString() + ", value " + varValue + "."); } Console.WriteLine(""); } //Printing library scopes. var libs = algoVisitor.Scopes.GetLibraries(); foreach (var lib in libs) { Console.WriteLine("LIBRARY '" + lib.Key + "'"); var libscopes = lib.Value.GetScopes(); for (int i = 0; i < libscopes.Count; i++) { Console.WriteLine("Scope " + (i + 1) + "\n---"); foreach (var variable in libscopes[i]) { Console.WriteLine(variable.Key + " of type " + variable.Value.Type.ToString() + ", value " + AlgoConversion.GetStringRepresentation(variable.Value) + "."); } Console.WriteLine(""); } } }
//A "print" statement. public override object VisitStat_print([NotNull] algoParser.Stat_printContext context) { //Evaluate the expression. AlgoValue toPrint = (AlgoValue)VisitExpr(context.expr()); //Depending on the type, set string accordingly via. casts. string printString = ""; printString = AlgoConversion.GetStringRepresentation(toPrint); //Check if a rounding expression is present. if (context.rounding_expr() != null) { //Evaluate the rounding expression. AlgoValue roundingNum = (AlgoValue)VisitExpr(context.rounding_expr().expr()); //Check if it's an integer. if (roundingNum.Type != AlgoValueType.Integer) { Error.Warning(context, "Given rounding expression is not an integer, so can't round. Rounding was ignored."); } else { //Check if the rounding value is too large. if ((BigInteger)roundingNum.Value > int.MaxValue) { Error.Warning(context, "Given rounding number is too large to process, so rounding was ignored."); } else { //Round the actual value. int roundingInt = int.Parse(((BigInteger)roundingNum.Value).ToString()); toPrint = AlgoOperators.Round(context, toPrint, roundingInt); //Set print string. printString = AlgoConversion.GetStringRepresentation(toPrint); } } } //Print and return. Console.WriteLine(printString); return(null); }