Пример #1
0
        public Expression(SymbolTableElement elem)
        {
            if (elem == null)
            {
                throw new ArgumentNullException("elem");
            }

            this.operation = Operation.SymbolElement;
            this.element   = elem;

            if (elem is SymbolTableIntegerElement)
            {
                type = SimpleScriptTypes.Integer;
            }
            if (elem is SymbolTableBoolElement)
            {
                type = SimpleScriptTypes.Boolean;
            }
            if (elem is SymbolTableDoubleElement)
            {
                type = SimpleScriptTypes.Double;
            }
            if (elem is SymbolTableStringElement)
            {
                type = SimpleScriptTypes.String;
            }
            element = elem;
        }
Пример #2
0
        public void SetType(int id, SimpleScriptTypes type)
        {
            if (!symbolTable.ContainsKey(id))
            {
                throw new ArgumentException("Undeclared identifier.");
            }
            SymbolTableElement x = symbolTable[id];

            switch (type)
            {
            case SimpleScriptTypes.Integer:
                SymbolTableIntegerElement stie = new SymbolTableIntegerElement(x.Name, 0);
                symbolTable[id] = stie;
                break;

            case  SimpleScriptTypes.Double:
                SymbolTableDoubleElement stde = new SymbolTableDoubleElement(x.Name, 0.0);
                symbolTable[id] = stde;
                break;

            case SimpleScriptTypes.Boolean:
                SymbolTableBoolElement stbe = new SymbolTableBoolElement(x.Name, false);
                symbolTable[id] = stbe;
                break;

            case SimpleScriptTypes.String:
                SymbolTableStringElement stse = new SymbolTableStringElement(x.Name, "");
                symbolTable[id] = stse;
                break;
            }
        }
Пример #3
0
 public Expression(double value)
 {
     this.operation = Operation.Constant;
     type           = SimpleScriptTypes.Double;
     constValue     = value;
 }
Пример #4
0
 public Expression(bool value)
 {
     this.operation = Operation.Constant;
     type           = SimpleScriptTypes.Boolean;
     constValue     = value;
 }
Пример #5
0
 public Expression(long value)
 {
     this.operation = Operation.Constant;
     type           = SimpleScriptTypes.Integer;
     constValue     = value;
 }
Пример #6
0
        /// <summary>
        /// Builds a complex expression.
        /// </summary>
        /// <param name="op">Operation</param>
        /// <param name="left">Left side expression.</param>
        /// <param name="right">Right side expression.</param>
        public Expression(Operation op, Expression left, Expression right)
        {
            #region Argument validation

            if (right == null)
            {
                throw new ArgumentNullException("Expression right handside is null.");
            }

            if (left == null)
            {
                // Left can be null only if the operation is unary minus.
                if (operation == Operation.UnaryMinus)
                {
                    switch (right.Type)
                    {
                    case SimpleScriptTypes.Integer:
                        type = SimpleScriptTypes.Integer;
                        break;

                    case SimpleScriptTypes.Double:
                        type = SimpleScriptTypes.Double;
                        break;

                    default:
                        throw new InvalidOperationException();
                        break;
                    }
                }
                else
                {
                    throw new ArgumentNullException("");
                }
            }
            #endregion

            this.leftExpression  = left;
            this.rightExpression = right;
            this.operation       = op;

            #region Operation result type validation
            OperationValidator opValid = new OperationValidator();
            switch (operation)
            {
            case Operation.Add:
            case Operation.Sub:
            case Operation.Mul:
            case Operation.Div:
            case Operation.Modul:
                type = opValid.CheckType(left.Type, right.Type);
                if (type == SimpleScriptTypes.None)
                {
                    throw new InvalidOperationException();
                }
                break;

            case Operation.Great:
            case Operation.Less:
            case Operation.Equ:
            case Operation.NotEqu:
                type = opValid.CheckOperation(left.Type, right.Type);
                if (type == SimpleScriptTypes.None)
                {
                    throw new InvalidOperationException();
                }
                break;
            }
            #endregion
        }
Пример #7
0
 public Expression(string value)
 {
     this.operation = Operation.Constant;
     type           = SimpleScriptTypes.String;
     constValue     = value;
 }
Пример #8
0
 public SimpleScriptTypes CheckOperation(SimpleScriptTypes leftType, SimpleScriptTypes rigthType)
 {
     return(logicaOp[(int)leftType, (int)rigthType]);
 }
Пример #9
0
 public SimpleScriptTypes CheckType(SimpleScriptTypes leftType, SimpleScriptTypes rigthType)
 {
     return(arithmeticOp[(int)leftType, (int)rigthType]);
 }