public static string GenerateLaTexString(Expression exp) { switch (exp.ExpressionType) { case ExpressionType.Variable: return exp.ToString(); case ExpressionType.Constant: if (exp is BoolConstant) { return ((BoolConstant)exp).Value.ToString(); } else { return exp.ToString(); } case ExpressionType.Record: { Record record = exp as Record; StringBuilder sb = new StringBuilder("["); for (int i = 0; i < record.Associations.Length - 1; i++) { Expression con = record.Associations[i]; sb.Append(GenerateLaTexString(con) + ", "); } sb.Append(GenerateLaTexString(record.Associations[record.Associations.Length - 1])); sb.Append("]"); return sb.ToString(); } case ExpressionType.PrimitiveApplication: // First evaluate the first argument, then the second, and // then evaluate using evalPrimAppl. { PrimitiveApplication newexp = exp as PrimitiveApplication; if (newexp.Argument2 == null) { if (newexp.Operator == "~") { return "-" + GenerateLaTexString(newexp.Argument1); } else { return newexp.Operator + GenerateLaTexString(newexp.Argument1); } } else { if (newexp.Operator == ".") { return GenerateLaTexString(newexp.Argument1) + "[" + GenerateLaTexString(newexp.Argument2) + "]"; } else if (newexp.Operator == "mod") { return "(" + GenerateLaTexString(newexp.Argument1) + " \\% " + GenerateLaTexString(newexp.Argument2) + ")"; } else { string op = ""; switch (newexp.Operator) { case "&&": op = @"\land"; break; case "||": op = @"\lor"; break; case "==": op = @"=="; break; case "!=": op = @"\neq"; break; case ">=": op = @"\geq"; break; case "<=": op = @"\leq"; break; case "\\": op = @"\backslash"; break; default: op = newexp.Operator; break; } return "(" + GenerateLaTexString(newexp.Argument1) + " " + op + " " + GenerateLaTexString(newexp.Argument2) + ")"; } } } case ExpressionType.Assignment: { Assignment assign = exp as Assignment; return assign.LeftHandSide + " = " + GenerateLaTexString(assign.RightHandSide) + ";"; } case ExpressionType.PropertyAssignment: { PropertyAssignment pa = (PropertyAssignment)exp; return GenerateLaTexString(pa.RecordExpression) + "[" + GenerateLaTexString(pa.PropertyExpression) + "]=" + GenerateLaTexString(pa.RightHandExpression) + ";"; } case ExpressionType.If: // Conditionals are evaluated by evaluating the then-part or // else-part depending of the result of evaluating the condition. { If ifC = (If)exp; if (ifC.ElsePart != null) { return " if (" + GenerateLaTexString(ifC.Condition) + ") \\{" + GenerateLaTexString(ifC.ThenPart) + "\\} else \\{" + GenerateLaTexString(ifC.ElsePart) + "\\}"; } else { return " if (" + GenerateLaTexString(ifC.Condition) + ") \\{" + GenerateLaTexString(ifC.ThenPart) + "\\}"; } } case ExpressionType.Sequence: return GenerateLaTexString(((Sequence)exp).FirstPart) + ";" + GenerateLaTexString(((Sequence)exp).SecondPart); case ExpressionType.While: return "while (" + GenerateLaTexString(((While)exp).Test) + ") \\{" + GenerateLaTexString(((While)exp).Body) + "\\}"; case ExpressionType.StaticMethodCall: StaticMethodCall call = exp as StaticMethodCall; StringBuilder strbui = new StringBuilder("call(" + call.MethodName + ","); for (int i = 0; i < call.Arguments.Length; i++) { if (i == call.Arguments.Length - 1) { strbui.Append(call.Arguments[i].ToString()); } else { strbui.Append(call.Arguments[i] + ",~"); } } strbui.Append(")"); return strbui.ToString(); } return ""; }
public static void CheckExpressionWithGlobalVariable(Expression e1, List<string> parameters, CommonTree ParentTree, IToken defaultToken) { //if e has only one var, this means e is a input variable List<string> evars = e1.GetVars(); if (evars.Count >= 1) { foreach (string var in evars) { IToken token = Common.Utility.ParsingUltility.GetVariableTokenInTree(ParentTree, var); if (token == null) { throw new ParsingException("Parsing error in expression " + e1.ToString(), defaultToken); } //if not contained in the parameters, then the variable is in the global variables if (!parameters.Contains(var)) { throw new ParsingException("Only process parameter and constants can be used in expression " + e1 + "! Global variable is not supported here for performance reason!", token); } } } }