Exemple #1
0
 /**
  * Function VisitReadStmt : it reads the new value for our variable
  * and checks both if the variable was previous declared and it has the same
  * type as the read value
  * Param : stmt with the new value for our variable
  */
 public Object VisitReadStmt(Stmt.Read stmt)
 {
     /* If the variable already exists and it is the proper type we update the symble table */
     if (SymbleTable.ContainsKey(stmt.Token.Text))
     {
         String x     = Console.ReadLine();
         Value  value = CheckReadStatement(x, stmt.Token, SymbleTable[stmt.Token.Text].Type);
         SymbleTable[stmt.Token.Text] = value;
         return(null);
     }
     /* Otherwise throw an error */
     throw new RuntimeError(stmt.Token, "Variable not previous declared.");
 }
Exemple #2
0
        /**
         * Function VisitReadStatement : it checks that the variable we are reading
         * already exists and it is not the loop counter inside a loop
         * Param : read statement to check
         */
        public object VisitReadStmt(Stmt.Read stmt)
        {
            /* Error if the variable was not previous declared */
            if (!SymbleTypeTable.ContainsKey(stmt.Token.Text))
            {
                throw new TypeError(stmt.Token, "Cannot read to a variable before it was declared.");
            }

            /* Error if we try to read the loop counter inside the loop */
            if (illegalAssignments.Contains(stmt.Token.Text))
            {
                throw new TypeError(stmt.Token, "Cannot assign to " + stmt.Token.Text + " in a for loop where it is used as a loop counter.");
            }
            return(null);
        }