예제 #1
0
        /**
         * Function VisitVarStmt --> "var" <var_ident> ":" <type> [ ":=" <expr> ]
         * It adds the new variable to the SymbleTable
         * If it already exists throws an error
         * If it is not initialized we put a default value : 0 for ints, "" for strings
         * and false for bools
         * Param : var statement to evaluate
         */
        public Object VisitVarStmt(Stmt.Var stmt)
        {
            String  name = stmt.Name.Text;
            VALTYPE type = ExpectedType(stmt);

            /* Case the variable is already in the symble table */
            if (SymbleTable.ContainsKey(name))
            {
                throw new RuntimeError(stmt.Name, "This variable already exists.");
            }

            /* Case the variable is not initialized : assign default values */
            else if (stmt.Initializer is null)
            {
                switch (type)
                {
                case VALTYPE.INT: SymbleTable.Add(name, new Value(type, 0)); break;

                case VALTYPE.STRING: SymbleTable.Add(name, new Value(type, "")); break;

                case VALTYPE.BOOL: SymbleTable.Add(name, new Value(type, false)); break;
                }
            }

            /* Case the variable is initialized : evaluate the ini expression */
            else
            {
                SymbleTable.Add(name, Evaluate(stmt.Initializer));
            }
            return(null);
        }
예제 #2
0
        /**
         * Function CheckReadStatement : checks if the read value and the variable
         * has the same type (int or string)
         * Param : the string obj we have read
         *		   token of the variable to store the value
         *		   value expected token to have
         * Return : the value of the variable or an error
         */
        private Value CheckReadStatement(String obj, Token token, VALTYPE value)
        {
            /* If the value is an int we try to parse it or throw an error */
            if (value.Equals(VALTYPE.INT))
            {
                if (int.TryParse(obj, out int output) == true)
                {
                    return(new Value(VALTYPE.INT, output));
                }

                throw new RuntimeError(token, "Read value " + obj + " is not the proper type.");
            }

            /* If the value is a string just return it */
            else if (value.Equals(VALTYPE.STRING))
            {
                return(new Value(VALTYPE.STRING, obj));
            }

            /* We cannot read bools */
            else
            {
                throw new RuntimeError(token, "Read value " + obj + " is not the proper type.");
            }
        }
예제 #3
0
 public Elem(int n)
 {
     numVal = n;
     strVal = null;
     _type  = VALTYPE.INT;
 }
예제 #4
0
 public Elem(string str)
 {
     numVal = null;
     strVal = (string)str.Clone();
     _type  = VALTYPE.STRING;
 }
예제 #5
0
 public Value(VALTYPE type, object val)
 {
     Type = type;
     Val  = val;
 }