Exemplo n.º 1
0
        /**
         * Function AssertStmt : it checks that the expression we are gonna
         * assert is of type BOOL, otherwise it throws an error
         * Param : assert statement to check
         */
        public object VisitAssertStmt(Stmt.Assert stmt)
        {
            VALTYPE right = GetType(stmt.Expr);

            if (right.Equals(VALTYPE.BOOL))
            {
                return(null);
            }
            else
            {
                throw new TypeError(stmt.AssertToken, "Asserts can only be done on boolean, got " + right.ToString() + " instead.");
            }
        }
Exemplo n.º 2
0
        /**
         * Function VisitAssertStmt --> "assert" "(" <expr> ")"
         * It evaluates the expression and prints it in the command line it it is false
         */
        public Object VisitAssertStmt(Stmt.Assert stmt)
        {
            Value right = Evaluate(stmt.Expr);

            if (right.Type.Equals(VALTYPE.BOOL))
            {
                if (!(bool)right.Val)
                {
                    Console.WriteLine("Assert failed:\tExpr: " + printer.Print(stmt.Expr) + " is false.");
                }
            }
            /* It should be unreachable, type system checks it */
            else
            {
                throw new RuntimeError(stmt.AssertToken, "Expression does not have the right type. Expected BOOL, got " + right.Type.ToString());
            }
            return(null);
        }