예제 #1
0
 public DecompiledExpressionItem(bool isComplex, PreparedExpression expression) {
     if (expression == null)
         throw new ArgumentNullException("expression");
     //
     this.isComplex = isComplex;
     this.expression = expression;
 }
예제 #2
0
 public DecompiledExpressionItem(bool isComplex, PreparedExpression expression, Operation lastOperation)
     : this(isComplex, expression) {
     //
     if (lastOperation == null)
         throw new ArgumentNullException("lastOperation");
     //
     this.lastOperation = lastOperation;
 }
예제 #3
0
 /// <summary>
 /// Returns a compiled expression for specified source string.
 /// </summary>
 public CompiledExpression Compile(PreparedExpression preparedExpression) {
     if (preparedExpression == null)
         throw new ArgumentNullException("preparedExpression");
     //
     OperationsStack operationsStack = new OperationsStack(operationsRegistry);
     //
     for (int itemIndex = 0; itemIndex < preparedExpression.PreparedExpressionItems.Count; itemIndex++) {
         PreparedExpressionItem item = preparedExpression.PreparedExpressionItems[itemIndex];
         // If constant or variable - add to result
         if (item.Kind == PreparedExpressionItemKind.Constant)
             operationsStack.PushConstant(item.Constant);
         if (item.Kind == PreparedExpressionItemKind.Variable)
             operationsStack.PushVariable(item.VariableName);
         // If delimiter
         if (item.Kind == PreparedExpressionItemKind.Delimiter) {
             operationsStack.PushDelimiter(item.DelimiterKind);
         }
         // Signature (operator signature / part of signature / function)
         if (item.Kind == PreparedExpressionItemKind.Signature) {
             List<Operation> operations = new List<Operation>(operationsRegistry.GetOperationsUsingSignature(item.Signature));
             operations.Sort(new Comparison<Operation>(compareOperationsByOperandsCount));
             //
             for (int i = 0; i < operations.Count; i++) {
                 Operation operation = operations[i];
                 // Operator
                 if (operation.Kind == OperationKind.Operator) {
                     // Unary operator
                     if (operation.OperandsCount == 1) {
                         // If operator placed at the start of subexpression
                         if ((itemIndex == 0) ||
                             ((itemIndex > 0) && (preparedExpression.PreparedExpressionItems[itemIndex - 1].Kind == PreparedExpressionItemKind.Delimiter) && (preparedExpression.PreparedExpressionItems[itemIndex - 1].DelimiterKind == DelimiterKind.OpeningBrace)|| (preparedExpression.PreparedExpressionItems[itemIndex-1].DelimiterKind == DelimiterKind.Comma))) {
                             //
                             operationsStack.PushUnaryOperator(operation);
                             break;
                         }
                     }
                     // Binary operator
                     if (operation.OperandsCount == 2) {
                         operationsStack.PushBinaryOperator(operation);
                         break;
                     }
                     // Ternary and more
                     if (operation.OperandsCount > 2) {
                         int partNumber = 0;
                         for (int k = 0; k < operation.Signature.Length; k++) {
                             if (operation.Signature[k] == item.Signature) {
                                 partNumber = k + 1;
                                 break;
                             }
                         }
                         // If it is start part in signature
                         if (partNumber == 1) {
                             operationsStack.PushComplexOperatorFirstSignature(operation);
                             break;
                         }
                         //
                         operationsStack.PushComplexOperatorNonFirstSignature(operation, partNumber);
                         break;
                     }
                 }
                 // Function
                 if (operation.Kind == OperationKind.Function) {
                     operationsStack.PushFunction(operation);
                     break;
                 }
             }
         }
     }
     // 
     operationsStack.DoFinalFlush();
     //
     CompiledExpression res = operationsStack.GetResult();
     if (!isCompiledExpressionStackBalanced(res))
         throw new CompilerSyntaxException("Operands disbalance detected.");
     return res;
 }