Esempio n. 1
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;
            }
        }
Esempio n. 2
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;
        }
Esempio n. 3
0
        public string Print()
        {
            StringBuilder      result = new StringBuilder("Dim ");
            SymbolTableElement ste    = SymbolTable.GetInstance.Get(symbolId);

            result.Append(ste.Name);
            result.Append(" as ");
            if (ste is SymbolTableIntegerElement)
            {
                result.Append("integer");
            }
            if (ste is SymbolTableDoubleElement)
            {
                result.Append("double");
            }
            if (ste is SymbolTableStringElement)
            {
                result.Append("string");
            }
            if (ste is SymbolTableBoolElement)
            {
                result.Append("bool");
            }
            return(result.ToString());
        }
Esempio n. 4
0
        public void Execute()
        {
            SymbolTableElement st = SymbolTable.GetInstance.Get(symbolId);

            if (st is SymbolTableIntegerElement)
            {
                int value = Convert.ToInt32(Console.ReadLine());
                SymbolTableIntegerElement ist = st as SymbolTableIntegerElement;
                ist.Value = value;
                return;
            }
            if (st is SymbolTableBoolElement)
            {
                bool value = Convert.ToBoolean(Console.ReadLine());
                SymbolTableBoolElement bst = st as SymbolTableBoolElement;
                bst.Value = value;
                return;
            }
            if (st is SymbolTableDoubleElement)
            {
                double value = Convert.ToDouble(Console.ReadLine());
                SymbolTableDoubleElement dst = st as SymbolTableDoubleElement;
                dst.Value = value;
                return;
            }
            if (st is SymbolTableStringElement)
            {
                string value = Console.ReadLine();
                SymbolTableStringElement sst = st as SymbolTableStringElement;
                sst.Value = value;
                return;
            }
        }
Esempio n. 5
0
        public string Print()
        {
            string             result = "input ";
            SymbolTableElement ste    = SymbolTable.GetInstance.Get(symbolId);

            return(result + ste.Name);
        }
        public void Execute()
        {
            SymbolTableElement elem = SymbolTable.GetInstance.Get(id);

            if (elem is SymbolTableIntegerElement)
            {
                SymbolTableIntegerElement ielem = (SymbolTableIntegerElement)elem;
                if (exp.Type == SimpleScriptTypes.Integer)
                {
                    ielem.Value = (long)exp.Evaluate();
                }
                else
                {
                    throw new InvalidOperationException("Invalide assignment.");
                }
                return;
            }
            if (elem is SymbolTableBoolElement)
            {
                SymbolTableBoolElement belem = (SymbolTableBoolElement)elem;
                if (exp.Type == SimpleScriptTypes.Boolean)
                {
                    belem.Value = (bool)exp.Evaluate();
                }
                else
                {
                    throw new InvalidOperationException("Invalide assignment.");
                }
                return;
            }
            if (elem is SymbolTableDoubleElement)
            {
                SymbolTableDoubleElement delem = (SymbolTableDoubleElement)elem;
                if (exp.Type == SimpleScriptTypes.Double || exp.Type == SimpleScriptTypes.Integer)
                {
                    delem.Value = (double)exp.Evaluate();
                }
                else
                {
                    throw new InvalidOperationException("Invalide assignment.");
                }
            }
            if (elem is SymbolTableStringElement)
            {
                SymbolTableStringElement selem = (SymbolTableStringElement)elem;
                if (exp.Type == SimpleScriptTypes.Double)
                {
                    selem.Value = exp.Evaluate().ToString();
                }
                else
                {
                    throw new InvalidOperationException("Invalide assignment.");
                }
            }
        }
Esempio n. 7
0
        public int Add(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException();
            }
            name = name.ToUpper();
            if (symbolNames.ContainsKey(name))
            {
                throw new ApplicationException("Variable with name " + name + "has been already defined");
            }
            SymbolTableElement x = new SymbolTableElement(name);

            symbolTable.Add(counter, x);
            symbolNames.Add(name, counter);
            ++counter;
            return(counter - 1);
        }