public OperationsStackItem(OperationStackItemKind kind, object value) { if (value == null) { throw new ArgumentNullException("value"); } // this.kind = kind; switch (kind) { case OperationStackItemKind.Delimiter: { delimiter = (DelimiterKind)value; break; } case OperationStackItemKind.Operation: { operationName = (string)value; break; } case OperationStackItemKind.PartialSignature: { partialSignature = (PartialSignature)value; break; } default: { throw new InvalidOperationException("Unexpected item kind."); } } }
public PreparedExpressionItem(PreparedExpressionItemKind kind, object value) { if (value == null) { throw new ArgumentNullException("value"); } // this.kind = kind; switch (kind) { case PreparedExpressionItemKind.Constant: { constant = (double)value; break; } case PreparedExpressionItemKind.Variable: { variableName = (string)value; break; } case PreparedExpressionItemKind.Delimiter: { delimiterKind = (DelimiterKind)value; break; } case PreparedExpressionItemKind.Signature: { signature = (string)value; break; } default: { throw new InvalidOperationException("Unexpected item kind."); } } }
public void PushDelimiter(DelimiterKind delimiterKind) { if (delimiterKind == DelimiterKind.OpeningBrace) { stack.Add(new OperationsStackItem(OperationStackItemKind.Delimiter, DelimiterKind.OpeningBrace)); } if (delimiterKind == DelimiterKind.ClosingBrace) { // Pop all items before previous OpeningBrace includes it int j = stack.Count - 1; while (j >= 0) { if ((stack[j].Kind == OperationStackItemKind.Delimiter) && (stack[j].Delimiter == DelimiterKind.OpeningBrace)) { stack.RemoveAt(j); break; } // switch (stack[j].Kind) { case OperationStackItemKind.Operation: { res.Add(new CompiledExpressionItem(CompiledExpressionItemKind.Operation, stack[j].OperationName)); stack.RemoveAt(j); break; } default: { throw new CompilerSyntaxException("Unexpected item in stack."); } } j--; } if (j < 0) { throw new CompilerSyntaxException("Braces syntax error."); } // If previous item is function - pop it if (stack.Count > 0) { if ((stack[stack.Count - 1].Kind == OperationStackItemKind.Operation) && (operationsRegistry.GetOperationByName(stack[stack.Count - 1].OperationName).Kind == OperationKind.Function)) { // res.Add(new CompiledExpressionItem(CompiledExpressionItemKind.Operation, stack[stack.Count - 1].OperationName)); stack.RemoveAt(stack.Count - 1); } } } if (delimiterKind == DelimiterKind.Comma) { // Pop all items before previous OpeningBrace excludes it int j = stack.Count - 1; while (j >= 0) { if ((stack[j].Kind == OperationStackItemKind.Delimiter) && (stack[j].Delimiter == DelimiterKind.OpeningBrace)) { break; } // switch (stack[j].Kind) { case OperationStackItemKind.Operation: { res.Add(new CompiledExpressionItem(CompiledExpressionItemKind.Operation, stack[j].OperationName)); stack.RemoveAt(j); break; } default: { throw new CompilerSyntaxException("Unexpected item in stack."); } } j--; } if (j < 0) { throw new CompilerSyntaxException("Braces syntax error."); } } }
/// <summary> /// Prepares source string for compilation. /// </summary> public PreparedExpression Parse(string sourceString) { if (sourceString == null) throw new ArgumentNullException("sourceString"); if (sourceString.Length == 0) throw new ArgumentException("String is empty.", "sourceString"); // Signatures lenghts int[] lens = _OperationsRegistry.SignaturesLens; // List<PreparedExpressionItem> res = new List<PreparedExpressionItem>(); bool operandStarted = false; int operandStartIndex = 0; // for (int i = 0; i < sourceString.Length; i++) { PreparedExpressionItem additionalItem = null; // Check for delimiters if ((sourceString[i] == '(') || (sourceString[i] == ')') || (sourceString[i] == ',')) { // Storing delimiter DelimiterKind delimiterKind = new DelimiterKind(); switch (sourceString[i]) { case '(': { delimiterKind = DelimiterKind.OpeningBrace; break; } case ')': { delimiterKind = DelimiterKind.ClosingBrace; break; } case ',': { delimiterKind = DelimiterKind.Comma; break; } } additionalItem = new PreparedExpressionItem(PreparedExpressionItemKind.Delimiter, delimiterKind); } // If not found, check for signatures, from max length to min if (additionalItem == null) { for (int j = lens.Length - 1; j >= 0; j--) { if (i + lens[j] <= sourceString.Length) { // If signature found if (_OperationsRegistry.IsSignatureDefined(sourceString.Substring(i, lens[j]))) { // Storing signature additionalItem = new PreparedExpressionItem(PreparedExpressionItemKind.Signature, sourceString.Substring(i, lens[j])); break; } } } } // If not found, working with operand if (additionalItem == null) { if (!operandStarted) { operandStarted = true; operandStartIndex = i; } } else { // NOTE: Duplicate code // Storing operand (constant or variable) if (operandStarted) { string operandString = sourceString.Substring(operandStartIndex, i - operandStartIndex); double constant; if (Double.TryParse(operandString, out constant)) { res.Add(new PreparedExpressionItem(PreparedExpressionItemKind.Constant, constant)); } else { if (!IsValidVariableName(operandString)) throw new CompilerSyntaxException(String.Format("{0} is not valid variable identifier.", operandString)); // res.Add(new PreparedExpressionItem(PreparedExpressionItemKind.Variable, operandString)); } operandStarted = false; } // Delayed storing a delimiter or signature res.Add(additionalItem); // If additionalItem was a signature, we should add correct i index according to signature lenght if (additionalItem.Kind == PreparedExpressionItemKind.Signature) i += additionalItem.Signature.Length - 1; } } // Storing operand (constant or variable) if (operandStarted) { string operandString = sourceString.Substring(operandStartIndex); double constant; if (Double.TryParse(operandString, out constant)) { res.Add(new PreparedExpressionItem(PreparedExpressionItemKind.Constant, constant)); } else { if (!IsValidVariableName(operandString)) throw new CompilerSyntaxException(String.Format("{0} is not valid variable identifier.", operandString)); // res.Add(new PreparedExpressionItem(PreparedExpressionItemKind.Variable, operandString)); } } // return new PreparedExpression(res); }
/// <summary> /// Prepares source string for compilation. /// </summary> public PreparedExpression Parse(string sourceString) { if (sourceString == null) { throw new ArgumentNullException("sourceString"); } if (sourceString.Length == 0) { throw new ArgumentException("String is empty.", "sourceString"); } // Signatures lenghts int[] lens = _OperationsRegistry.SignaturesLens; List <PreparedExpressionItem> res = new List <PreparedExpressionItem>(); bool operandStarted = false; int operandStartIndex = 0; for (int i = 0; i < sourceString.Length; i++) { PreparedExpressionItem additionalItem = null; // Check for delimiters if ((sourceString[i] == '(') || (sourceString[i] == ')') || (sourceString[i] == ',')) { // Storing delimiter DelimiterKind delimiterKind = new DelimiterKind(); switch (sourceString[i]) { case '(': { delimiterKind = DelimiterKind.OpeningBrace; break; } case ')': { delimiterKind = DelimiterKind.ClosingBrace; break; } case ',': { delimiterKind = DelimiterKind.Comma; break; } } additionalItem = new PreparedExpressionItem(PreparedExpressionItemKind.Delimiter, delimiterKind); } // If not found, check for signatures, from max length to min if (additionalItem == null) { for (int j = lens.Length - 1; j >= 0; j--) { if (i + lens[j] <= sourceString.Length) { // If signature found if (_OperationsRegistry.IsSignatureDefined(sourceString.Substring(i, lens[j]))) { // Storing signature additionalItem = new PreparedExpressionItem(PreparedExpressionItemKind.Signature, sourceString.Substring(i, lens[j])); break; } } } } // If not found, working with operand if (additionalItem == null) { if (!operandStarted) { operandStarted = true; operandStartIndex = i; } } else { // NOTE: Duplicate code // Storing operand (constant or variable) if (operandStarted) { string operandString = sourceString.Substring(operandStartIndex, i - operandStartIndex); double constant; if (Double.TryParse(operandString, System.Globalization.NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out constant)) { res.Add(new PreparedExpressionItem(PreparedExpressionItemKind.Constant, constant)); } else { if (!IsValidVariableName(operandString)) { throw new CompilerSyntaxException(String.Format("{0} is not valid variable identifier.", operandString)); } res.Add(new PreparedExpressionItem(PreparedExpressionItemKind.Variable, operandString)); } operandStarted = false; } // Delayed storing a delimiter or signature res.Add(additionalItem); // If additionalItem was a signature, we should add correct i index according to signature lenght if (additionalItem.Kind == PreparedExpressionItemKind.Signature) { i += additionalItem.Signature.Length - 1; } } } // Storing operand (constant or variable) if (operandStarted) { string operandString = sourceString.Substring(operandStartIndex); double constant; if (Double.TryParse(operandString.Replace('.', ','), out constant)) { res.Add(new PreparedExpressionItem(PreparedExpressionItemKind.Constant, constant)); } else { if (!IsValidVariableName(operandString)) { throw new CompilerSyntaxException(String.Format("{0} is not valid variable identifier.", operandString)); } res.Add(new PreparedExpressionItem(PreparedExpressionItemKind.Variable, operandString)); } } return(new PreparedExpression(res)); }
public OperationsStackItem(OperationStackItemKind kind, object value) { if (value == null) throw new ArgumentNullException("value"); // this.kind = kind; switch (kind) { case OperationStackItemKind.Delimiter: { delimiter = (DelimiterKind) value; break; } case OperationStackItemKind.Operation: { operationName = (string) value; break; } case OperationStackItemKind.PartialSignature: { partialSignature = (PartialSignature) value; break; } default: { throw new InvalidOperationException("Unexpected item kind."); } } }
public void PushDelimiter(DelimiterKind delimiterKind) { if (delimiterKind == DelimiterKind.OpeningBrace) { stack.Add(new OperationsStackItem(OperationStackItemKind.Delimiter, DelimiterKind.OpeningBrace)); } if (delimiterKind == DelimiterKind.ClosingBrace) { // Pop all items before previous OpeningBrace includes it int j = stack.Count - 1; while (j >= 0) { if ((stack[j].Kind == OperationStackItemKind.Delimiter) && (stack[j].Delimiter == DelimiterKind.OpeningBrace)) { stack.RemoveAt(j); break; } // switch (stack[j].Kind) { case OperationStackItemKind.Operation: { res.Add(new CompiledExpressionItem(CompiledExpressionItemKind.Operation, stack[j].OperationName)); stack.RemoveAt(j); break; } default: { throw new CompilerSyntaxException("Unexpected item in stack."); } } j--; } if (j < 0) throw new CompilerSyntaxException("Braces syntax error."); // If previous item is function - pop it if (stack.Count > 0) { if ((stack[stack.Count - 1].Kind == OperationStackItemKind.Operation) && (operationsRegistry.GetOperationByName(stack[stack.Count - 1].OperationName).Kind == OperationKind.Function)) { // res.Add(new CompiledExpressionItem(CompiledExpressionItemKind.Operation, stack[stack.Count - 1].OperationName)); stack.RemoveAt(stack.Count - 1); } } } if (delimiterKind == DelimiterKind.Comma) { // Pop all items before previous OpeningBrace excludes it int j = stack.Count - 1; while (j >= 0) { if ((stack[j].Kind == OperationStackItemKind.Delimiter) && (stack[j].Delimiter == DelimiterKind.OpeningBrace)) { break; } // switch (stack[j].Kind) { case OperationStackItemKind.Operation: { res.Add(new CompiledExpressionItem(CompiledExpressionItemKind.Operation, stack[j].OperationName)); stack.RemoveAt(j); break; } default: { throw new CompilerSyntaxException("Unexpected item in stack."); } } j--; } if (j < 0) throw new CompilerSyntaxException("Braces syntax error."); } }
public PreparedExpressionItem(PreparedExpressionItemKind kind, object value) { if (value == null) throw new ArgumentNullException("value"); // this.kind = kind; switch (kind) { case PreparedExpressionItemKind.Constant: { constant = (double) value; break; } case PreparedExpressionItemKind.Variable: { variableName = (string) value; break; } case PreparedExpressionItemKind.Delimiter: { delimiterKind = (DelimiterKind) value; break; } case PreparedExpressionItemKind.Signature: { signature = (string) value; break; } default: { throw new InvalidOperationException("Unexpected item kind."); } } }