Esempio n. 1
0
        ProgramState IStmt.execute(ProgramState programState)
        {
            FileTable fileTable             = programState.getFileTable();
            SymTable <string, int> symTable = programState.getSymTable();

            bool found = false;

            foreach (Tuple <string, TextReader> t in fileTable.Values)
            {
                if (t.Item1.Equals(fileName))
                {
                    found = true;
                    break;
                }
            }

            if (found)
            {
                throw new Exception("File already open!\n");
            }
            if (symTable.ContainsKey(var_file_id))
            {
                throw new Exception("File id already exists!\n");
            }

            TextReader textReader = File.OpenText(fileName);
            int        id         = fileTable.Add(new Tuple <string, TextReader>(fileName, textReader));

            symTable.Add(var_file_id, id);
            return(null);
        }
Esempio n. 2
0
        ProgramState IStmt.execute(ProgramState programState)
        {
            SymTable <string, int> symTable = programState.getSymTable();

            if (symTable.ContainsKey(varName))
            {
                symTable[varName] = exp.evaluate(symTable);
            }
            else
            {
                symTable.Add(varName, exp.evaluate(symTable));
            }

            return(null);
        }
Esempio n. 3
0
        ProgramState IStmt.execute(ProgramState programState)
        {
            FileTable fileTable             = programState.getFileTable();
            SymTable <string, int> symTable = programState.getSymTable();

            int id = exp_file_id.evaluate(symTable);

            if (!fileTable.ContainsKey(id))
            {
                throw new Exception("Invalid text reader id!\n");
            }
            TextReader textReader = fileTable[id].Item2;

            string line = textReader.ReadLine();
            int    value;

            if (line == null)
            {
                value = 0;
            }
            else
            {
                value = int.Parse(line);
            }

            if (symTable.ContainsKey(varName))
            {
                symTable[varName] = value;
            }
            else
            {
                symTable.Add(varName, value);
            }

            return(null);
        }