Пример #1
0
 public PrgState(ImyStack <Istmt> stk, ImyDict <string, int> sym, ImyList <int> output, ImyDict <int, Tuple <string, StreamReader> > file)
 {
     this.stk    = stk;
     this.sym    = sym;
     this.output = output;
     this.file   = file;
 }
Пример #2
0
        public override int eval(ImyDict <string, int> symTable)
        {
            if (op == 0)
            {
                return(e1.eval(symTable) + e2.eval(symTable));
            }

            if (op == 1)
            {
                return(e1.eval(symTable) - e2.eval(symTable));
            }

            if (op == 2)
            {
                return(e1.eval(symTable) * e2.eval(symTable));
            }

            if (op == 3)
            {
                if (e2.eval(symTable) == 0)
                {
                    throw new ExpException("Expression " + this.ToString() + " is invalid (Division by 0)");
                }

                return(e1.eval(symTable) / e2.eval(symTable));
            }

            return(0);
        }
Пример #3
0
        public void execute(PrgState state)
        {
            ImyDict <int, Tuple <string, StreamReader> > fileTable = state.getFileTable();
            ImyDict <string, int> symTable = state.getSymTable();

            try {
                if (fileTable.isInTuple(this.filename))
                {
                    throw new StmtException("File allready opened");
                }
            } catch (Exception e1) {
                throw new StmtException("Statement \"" + this.ToString() + "\" encountered a dictionary exception: " + e1.Message);
            }
            StreamReader buffReader;

            try {
                buffReader = new StreamReader(filename);
            } catch (FileNotFoundException) {
                throw new StmtException("File not found \"" + filename + "\"");
            }

            Tuple <string, StreamReader> entry = new Tuple <string, StreamReader>(filename, buffReader);
            int id = fileTable.generateId();

            fileTable.add(id, entry);
            symTable.add(var_file_id, id);
        }
Пример #4
0
        public void execute(PrgState state)
        {
            ImyList <int>         output   = state.getOutput();
            ImyDict <String, int> symTable = state.getSymTable();

            output.add(e.eval(symTable));
        }
Пример #5
0
 public override int eval(ImyDict <string, int> sym)
 {
     try
     {
         return(sym.get(name));
     }
     catch (Exception) {
         throw new ExpException("Variable " + name + " is not in the sym table\n");
     }
 }
Пример #6
0
        public void execute(PrgState state)
        {
            ImyDict <string, int> symTable = state.getSymTable();

            int val;

            try {
                val = e.eval(symTable);
                symTable.add(id, val);
            } catch (ExpException e) {
                throw new StmtException("Statement \"" + this.ToString() + "\": " + e.Message);
            }
        }
Пример #7
0
        public void execute(PrgState state)
        {
            ImyStack <Istmt>      stk      = state.getExeStack();
            ImyDict <string, int> symTable = state.getSymTable();

            try {
                if (e.eval(symTable) != 0)
                {
                    stk.push(thenS);
                }
                else
                {
                    stk.push(elseS);
                }
            } catch (ExpException) {
                throw new StmtException("Id in \"" + this.ToString() + "\" statement has no value asociated with it");
            }
        }
Пример #8
0
        public void execute(PrgState state)
        {
            ImyDict <string, int> symTable = state.getSymTable();
            ImyDict <int, Tuple <string, StreamReader> > fileTable = state.getFileTable();

            int          v;
            StreamReader buffReader;

            try {
                v = exp_file_id.eval(symTable);
            } catch (ExpException e) {
                throw new StmtException("Expression \"" + exp_file_id + "\" in statement + \"" + this.ToString() + "\" is invalid: " + e.Message);
            }

            try {
                buffReader = fileTable.get(v).Item2;
            } catch (Exception) {
                throw new StmtException("File does not exist or hasn't been open \"" + exp_file_id.ToString() + "\"");
            }

            try {
                string readValue = buffReader.ReadLine();

                int value;

                if (readValue == null)
                {
                    value = 0;
                }
                else
                {
                    Int32.TryParse(readValue, out value);
                }

                symTable.add(var_name, value);
            } catch (IOException e2) {
                throw new StmtException("Statement \"" + this.ToString() + "\" encountered an exception: " + e2.Message);
            }
        }
Пример #9
0
        public PrgState execute(PrgState state)
        {
            ImyDict <String, int> symTable = state.getSymTable();
            ImyDict <int, Tuple <String, StreamReader> > fileTable = state.getFileTable();

            int          v;
            StreamReader buffReader;

            try {
                v = exp_file_id.eval(symTable);
            } catch (ExpException e) {
                throw new StmtException("Expression \"" + exp_file_id + "\" in statement + \"" + this.ToString() + "\" is invalid: " + e.Message);
            }

            try {
                buffReader = fileTable.get(v).Item2;
                buffReader.Close();
                fileTable.remove(v);
            } catch (Exception) {
                throw new StmtException("File does not exist or hasn't been open");
            }

            return(null);
        }
Пример #10
0
 public override int eval(ImyDict <string, int> symTable)
 {
     return(value);
 }
Пример #11
0
 abstract public int eval(ImyDict <string, int> sym);