public static void /*StringVar*/ op_AssignAddition(StringVar a, StringVar b) { if (!b.empty) { a.v += b.v; a.empty = false; } return; }
public static StringVar op_Id(StringVar a) { StringVar result = new StringVar(a); Regex r = new Regex(@"\W"); result.v = r.Replace(result.v, "_"); return(result); }
public static StringVar op_If(BooleanVar a, StringVar b, StringVar c) { if (a.empty) { throw new EmptyValueNotSuitableForOperationException(a.name, "'If'-function"); } return(new StringVar(a.v ? b.v : c.v)); }
public static DecimalVar op_ToDecimal(StringVar a) { if (a.empty) { throw new EmptyValueNotSuitableForOperationException(a.name, "'Decimal'-function"); } DecimalVar r = new DecimalVar(); Decimal.TryParse(a.v, out r.v); return(r); }
private Var CheckConstantValue(StringVar token) { if (String.Compare(token.v, "true", true) == 0) { token.v = ""; return(new BooleanVar(true)); } if (String.Compare(token.v, "false", true) == 0) { token.v = ""; return(new BooleanVar(false)); } return(null); }
public static StringVar op_Substring(StringVar what, DecimalVar from, DecimalVar into) { if (from.v + into.v <= what.v.Length) { return(new StringVar(what.v.Substring((int)from.v, (int)into.v))); } else if (from.v < what.v.Length) { return(new StringVar(what.v.Substring((int)from.v))); } else { return(new StringVar()); } }
public static StringVar op_ShiftRight(StringVar a, DecimalVar b) { if (b.empty) { throw new EmptyValueNotSuitableForOperationException(b.name, "'>>'-operator"); } string extraspace = ""; if ((int)b.v - a.v.Length > 0) { extraspace = new string(' ', (int)b.v - a.v.Length); } return(new StringVar(extraspace + a.v)); }
public static StringVar op_Casing(StringVar a, StringVar b) { if (a.v == "") { return(new StringVar("")); } switch (b.v) { case "U": return(new StringVar(a.v.ToUpper())); case "L": return(new StringVar(a.v.ToLower())); case "C": case "P": string result = ""; string[] parts = a.v.Split(new char[] { ' ', '_' }); foreach (string s in parts) { result += s[0].ToString().ToUpper(); if (s.Length > 1) { result += s.Substring(1); //.ToLower(); } } // camel case is pascal case but with lower first char if (result.Length > 0 && b.v == "C") { result = result[0].ToString().ToLower() + result.Substring(1); } return(new StringVar(result)); default: throw new StringCasingException(b.v); } }
public static BooleanVar op_CaseInsensitiveInEquality(StringVar a, StringVar b) { return(new BooleanVar(a.v.ToLower() != b.v.ToLower())); }
public StringVar(StringVar s) { name = s.name; v = s.v; empty = s.empty; }
public static BooleanVar op_Empty(StringVar a) { return(new BooleanVar(a.empty)); }
public static DecimalVar op_In(StringVar a, StringVar b) { return(new DecimalVar(a.v.IndexOf(b.v))); }
public static StringVar op_Replace(StringVar what, StringVar from, StringVar into) { return(new StringVar(what.v.Replace(from.v, into.v))); }
public static void /*StringVar*/ op_Assign(StringVar a, StringVar b) { a.v = b.v; a.empty = b.empty; return; }
public static void /*ConceptVar*/ op_Assign(ConceptVar a, StringVar b) { a.tvalue = null; a.empty = true; return; }
public static DecimalVar op_Length(StringVar what) { return(new DecimalVar(what.v.Length)); }
public Var Evaluate(string expression) { System.Globalization.NumberFormatInfo numberformat = new System.Globalization.NumberFormatInfo(); numberformat.NumberDecimalSeparator = "."; int i; mOperands = new Stack(); mOperators = new Stack(); // Simulate expression in brackets PushOperator(new OperatorInfo("(", -1, "")); expression = expression + ")."; bool bLastTokenWasOperand = false; while (expression != ".") { // Allow for continue from multiple levels. bool bGotoNextPart = false; // first eat all spaces if (Char.IsWhiteSpace(expression[0])) { do { expression = expression.Remove(0, 1); } while (Char.IsWhiteSpace(expression[0])); continue; } // Look for an operator. If found process it... foreach (OperatorInfo op in OperatorInfo.cvOperators.Values) { if (expression.StartsWith(op.name) && (!op.IsText || !Char.IsLetter(expression[op.name.Length]))) { if (op.name == ")" || op.name == ",") { // Make sure any awaiting operations are processed first do { ProcessOperator(); } while (mTopOperator.name != "(" && mTopOperator.name != ","); // Now push the ) and process the () or function call. PushOperator(op); ProcessOperator(); } //else if (!bLastTokenWasOperand && !op.function) //{ // throw new OperandExpectedException(0, expression.Substring(0, expression.Length - 2)); //} else if ((op.precedence > mTopOperator.precedence) || (mTopOperator.name == "(")) { // Next operator is higher in precedence. Process it first. PushOperator(op); } else { do { ProcessOperator(); } while (op.precedence <= mTopOperator.precedence && mTopOperator.name != "(" && mTopOperator.name != ","); PushOperator(op); } bLastTokenWasOperand = false; expression = expression.Remove(0, op.name.Length); bGotoNextPart = true; break; } } if (bGotoNextPart) { continue; } if (bLastTokenWasOperand) { throw new Exceptions.OperatorExpectedException(null, expression.Substring(0, expression.Length - 2)); } // Now check to see if first part of expression is a number if (Char.IsDigit(expression, 0)) { DecimalVar v = new DecimalVar(); i = 0; // Eat the integer part of the number do { i++; } while (Char.IsDigit(expression, i)); if (expression[i] == '.') { // It is a decimal do { i++; } while (Char.IsDigit(expression, i)); v.v = Decimal.Parse(expression.Substring(0, i), System.Globalization.NumberStyles.Number, numberformat); } else { // It is an integer v.v = Int32.Parse(expression.Substring(0, i)); } v.empty = false; mOperands.Push(v); expression = expression.Remove(0, i); bLastTokenWasOperand = true; continue; } // Check if first part is string constant if (expression[0] == '\"') { i = 1; StringVar v = new StringVar(); while (i < expression.Length && expression[i] != '\"') { if (expression[i] == '\\') { if ((i + 1) == expression.Length) { throw new StringTerminatorExpectedException(null, expression); } switch (expression[i + 1]) { case '\"': v.v += "\""; break; case '\\': v.v += "\\"; break; case 't': v.v += "\t"; break; case 'n': v.v += "\n"; break; default: v.v += expression[i + 1].ToString(); break; } // Shift additional position i++; } else { v.v += expression[i].ToString(); } i++; v.empty = false; } if (i >= expression.Length) { throw new StringTerminatorExpectedException(null, expression.Substring(0, expression.Length - 2)); } mOperands.Push(v); expression = expression.Remove(0, i + 1); bLastTokenWasOperand = true; continue; } // Eat all identifiers, separated by '.' and make it // a big token. Let the delegate decide which part it // can process. bool bTokenEnded = false; i = 0; while (!bTokenEnded) { if (!Char.IsLetter(expression, i)) { bTokenEnded = true; break; } i++; // identifier consists of letter + 0/more letter/digit/_ while (Char.IsLetterOrDigit(expression, i) || expression[i] == '_') { i++; } if (expression[i] != '.') { bTokenEnded = true; break; } i++; // Read over the '.' and get next identifier } if (i > 0) { StringVar sToken = new StringVar(expression.Substring(0, i)); Var r = CheckConstantValue(sToken); if (r == null) { r = myTokenResolver.TokenEvaluator(sToken); } if (r == null) { FunctionInfo fi = myTokenResolver.FunctionFinder(sToken.v); if (fi != null) { OperatorInfo oi = new OperatorInfo(fi.Name, 98, null, false, true); PushOperator(oi); expression = expression.Remove(0, i); bLastTokenWasOperand = true; continue; } } else { // Store the result on the operand stack. mOperands.Push(r); // Remove the tokens we ate, and replace it with the // remainders of the tokenevaluation expression = expression.Remove(0, i); expression = sToken.v + expression; bGotoNextPart = true; bLastTokenWasOperand = true; } } if (bGotoNextPart) { continue; } throw new UnknownTokenException(-1, expression.Substring(0, i)); //this new StringVar(String.Format("Syntax error. '{0}' unexpected.", expression.Substring(0,i))); //throw new ApplicationException(String.Format("Syntax error. '{0}' unexpected.", expression.Substring(0,i))); } if (bLastTokenWasOperand) { throw new OperatorExpectedException(-1, expression.Substring(-1, expression.Length - 2)); } //ProcessOperator(); if (mOperands.Count == 0) { return(null); } else { return(mOperands.Pop() as Var); } }