public ProgramState execute(ProgramState state)
        {
            if (!state.GetSymTable().ContainsKey(var))
            {
                throw new Exception("Variable: " + var + " not registered!\n");
            }
            if (!state.GetSymTable()[var].GetTypeInter().HasSameType(new IntType()))
            {
                throw new Exception("Type of variable should be IntType!\n");
            }

            if (!expression.evaluate(state.GetSymTable(), state.GetHeap()).GetTypeInter().HasSameType(new StringType()))
            {
                throw new Exception("Expression should reduce to StringValue!\n");
            }
            if (!state.GetFileTable().ContainsKey(((StringValue)expression.evaluate(state.GetSymTable(), state.GetHeap())).GetValue()))
            {
                throw new Exception("File " + expression.ToString() + " not registered!\n");
            }

            FileStream fileStream =
                state.GetFileTable()[((StringValue)expression.evaluate(state.GetSymTable(), state.GetHeap())).GetValue()];
            StreamReader streamReader = new StreamReader(fileStream);
            String       line         = streamReader.ReadLine();

            if (line == null)
            {
                state.GetSymTable()[var] = new IntValue(0);
            }
            else
            {
                state.GetSymTable()[var] = new IntValue(Int32.Parse(line));
            }
            return(null);
        }
        public ProgramState execute(ProgramState state)
        {
            if (!state.GetSymTable().ContainsKey(id))
            {
                throw new Exception("Variable " + id + " is not defined!\n");
            }

            if (!state.GetSymTable()[id].GetTypeInter().HasSameType(expr.evaluate(state.GetSymTable(), state.GetHeap()).GetTypeInter()))
            {
                throw new Exception("Incompatible Assignment! Types Do Not Match!\n");
            }

            state.GetSymTable()[id] = expr.evaluate(state.GetSymTable(), state.GetHeap());
            return(null);
        }
        public ProgramState execute(ProgramState state)
        {
            if (!expression.evaluate(state.GetSymTable(), state.GetHeap()).GetTypeInter().HasSameType(new StringType()))
            {
                throw new Exception("Invalid value for expression, String Type needed!");
            }
            String val = ((StringValue)expression.evaluate(state.GetSymTable(), state.GetHeap())).GetValue();

            if (state.GetFileTable().ContainsKey(val))
            {
                throw new Exception("File already registered!");
            }

            state.GetFileTable()[val] = File.OpenRead(val);
            return(null);
        }
        public ProgramState execute(ProgramState state)
        {
            if (!cond.evaluate(state.GetSymTable(), state.GetHeap()).GetTypeInter().HasSameType(new BoolType()))
            {
                throw new Exception("Invalid While Condition!\n");
            }

            if (!((BoolValue)cond.evaluate(state.GetSymTable(), state.GetHeap())).getValue())
            {
                return(null);
            }

            state.GetExeStack().Push(this);
            state.GetExeStack().Push(body);
            return(null);
        }
Exemplo n.º 5
0
        public ProgramState execute(ProgramState state)
        {
            if (!expression.evaluate(state.GetSymTable(), state.GetHeap()).GetTypeInter().HasSameType(new StringType()))
            {
                throw new Exception("File name should be of String Type!\n");
            }
            String val = ((StringValue)expression.evaluate(state.GetSymTable(), state.GetHeap())).GetValue();

            if (!state.GetFileTable().ContainsKey(val))
            {
                throw new Exception("File " + val + " not registered!");
            }
            FileStream fileStream = state.GetFileTable()[val];

            state.GetFileTable().TryRemove(val, out fileStream);
            fileStream.Close();
            return(null);
        }
        public ProgramState execute(ProgramState state)
        {
            Console.WriteLine(expr.evaluate(state.GetSymTable(), state.GetHeap()).GetTypeInter());
            if (!(expr.evaluate(state.GetSymTable(), state.GetHeap()).GetTypeInter().HasSameType(new BoolType())))
            {
                throw new Exception("Invalid If Statement!\n");
            }
            Boolean val = ((BoolValue)expr.evaluate(state.GetSymTable(), state.GetHeap())).getValue();

            if (!val)
            {
                state.GetExeStack().Push(elseStat);
            }
            else
            {
                state.GetExeStack().Push(thenStat);
            }

            return(null);
        }
Exemplo n.º 7
0
        public ProgramState execute(ProgramState state)
        {
            /*
             * it is a statement which takes a variable name and an expression
             * • check whether var_name is a variable in SymTable and its type is a RefType. If not, the
             * execution is stopped with an appropriate error message.*/
            if (!state.GetSymTable().ContainsKey(var))
            {
                throw new Exception("Variable " + var + " is not declared!\n");
            }

            if (!(state.GetSymTable()[var].GetTypeInter() is ReferenceType))
            {
                throw new Exception("The type of the variable " + var + " has to be of Reference Type!\n");
            }

            /*
             * • Evaluate the expression to a value and then compare the type of the value to the
             * locationType from the value associated to var_name in SymTable. If the types are not equal,
             * the execution is stopped with an appropriate error message.*/
            Value.Value val = expr.evaluate(state.GetSymTable(), state.GetHeap());
            Type.Type   t1  = ((ReferenceValue)state.GetSymTable()[var]).getInnerType();
            if (!val.GetTypeInter().HasSameType(t1))
            {
                throw new Exception("Incompatible types!\n");
            }

            /*
             * • Create a new entry in the Heap table such that a new key (new free address) is generated and
             * it is associated to the result of the expression evaluation*/
            Int32 generatedAddress = state.GetHeap().generateAddress();

            ((Heap)state.GetHeap()).TryAdd(generatedAddress, val);

            /*
             * • in SymTable update the RefValue associated to the var_name such that the new RefValue
             * has the same locationType and the address is equal to the new key generated in the Heap at
             * the previous step
             */
            state.GetSymTable()[var] = new ReferenceValue(generatedAddress, val.GetTypeInter());
            return(null);
        }
Exemplo n.º 8
0
        public ProgramState execute(ProgramState state)
        {
            /*
             * it is a statement which takes a variable and an expression, the variable contains the heap
             *  address, the expression represents the new value that is going to be stored into the heap
             * • first we check if var_name is a variable defined in SymTable, if its type is a Ref type and if
             *  the address from the RefValue associated in SymTable is a key in Heap. If not, the execution
             *  is stopped with an appropriate error message.*/
            if (!state.GetSymTable().ContainsKey(this.var))
            {
                throw new Exception("Variable " + this.var + " is not registered!\n");
            }

            if (!(state.GetSymTable()[this.var] is ReferenceValue))
            {
                throw new Exception("Not a reference type found for var: " + this.var);
            }

            Int32 address = ((ReferenceValue)state.GetSymTable()[this.var]).getAddress();

            if (!((Heap)state.GetHeap()).ContainsKey(address))
            {
                throw new Exception("Invalid address!\n");
            }

            /*
             *  • Second the expression is evaluated and the result must have its type equal to the
             *  locationType of the var_name type. If not, the execution is stopped with an appropriate
             *  message.*/
            Value.Value val = expr.evaluate(state.GetSymTable(), state.GetHeap());

            /*
             *  • Third we access the Heap using the address from var_name and that Heap entry is updated
             *  to the result of the expression evaluation.
             */
            ((Heap)state.GetHeap())[address] = val;
            return(null);
        }