コード例 #1
0
        private static NumExpr Division(List <Expr> args, Dictionary <string, Expr> env)
        {
            int len = args.Count;

            if (len < 1)
            {
                throw new EvaluatorException(String.Format("the expected number of arguments does not match the given number{0} expected: at least 1", "\n"));
            }
            Expr f;

            f = args[0].eval(env);
            if (f.GetType() != typeof(NumExpr))
            {
                throw new EvaluatorException("expected number?");
            }
            NumExpr num_f = (NumExpr)f;
            long    num   = num_f.getValue();

            if (len == 1)
            {
                if (num == 0)
                {
                    throw new EvaluatorException("divide by zero");
                }
                return(new NumExpr(1 / num));
            }
            Expr    arg;
            NumExpr n;
            long    res = num;

            for (int i = 1; i < len; i++)
            {
                arg = args[i].eval(env);
                if (arg.GetType() != typeof(NumExpr))
                {
                    throw new EvaluatorException("expected number?");
                }
                n   = (NumExpr)arg;
                res = res / n.getValue();
            }
            return(new NumExpr(res));
        }
コード例 #2
0
        private static NumExpr Modulo(List <Expr> args, Dictionary <string, Expr> env)
        {
            int len = args.Count;

            if (len != 2)
            {
                throw new EvaluatorException(String.Format("the expected number of arguments does not match the given number{0} expected: 2", "\n"));
            }
            Expr res1 = args[0].eval(env);
            Expr res2 = args[1].eval(env);

            if (res1.GetType() != typeof(NumExpr) || res2.GetType() != typeof(NumExpr))
            {
                throw new EvaluatorException("expected number?");
            }
            NumExpr num_res1 = (NumExpr)res1;
            NumExpr num_res2 = (NumExpr)res2;

            return(new NumExpr(num_res1.getValue() % num_res2.getValue()));
        }
コード例 #3
0
        private static NumExpr Plus(List <Expr> args, Dictionary <string, Expr> env)
        {
            Expr  arg;
            Expr  res;
            int   len = args.Count;
            Int64 sum = 0;

            for (int i = 0; i < len; i++)
            {
                arg = args[i];
                res = arg.eval(env);
                if (res.GetType() != typeof(NumExpr))
                {
                    throw new EvaluatorException("expected number?");
                }
                NumExpr num_res = (NumExpr)res;
                sum = sum + num_res.getValue();
            }
            return(new NumExpr(sum));
        }
コード例 #4
0
        private static NumExpr Minus(List <Expr> args, Dictionary <string, Expr> env)
        {
            Expr arg;
            int  len = args.Count;

            if (len < 1)
            {
                throw new EvaluatorException(String.Format("the expected number of arguments does not match the given number{0} expected: at least 1", "\n"));
            }
            arg = args[0];
            Expr res = arg.eval(env);

            if (res.GetType() != typeof(NumExpr))
            {
                throw new EvaluatorException("expected number?");
            }
            NumExpr res_num = (NumExpr)res;
            long    f_num   = res_num.getValue();

            if (len == 1)
            {
                return(new NumExpr(-1 * f_num));
            }


            long sum = f_num;

            for (int i = 1; i < len; i++)
            {
                arg = args[i];
                res = arg.eval(env);
                if (res.GetType() != typeof(NumExpr))
                {
                    throw new EvaluatorException("expected number?");
                }
                res_num = (NumExpr)res;
                sum     = sum - res_num.getValue();
            }
            return(new NumExpr(sum));
        }
コード例 #5
0
        private static BoolExpr GreaterThanEqual(List <Expr> args, Dictionary <string, Expr> env)
        {
            int len = args.Count;

            if (len < 2)
            {
                throw new EvaluatorException(String.Format("the expected number of arguments does not match the given number{0} expected: at least 2", "\n"));
            }
            Expr first = args[0].eval(env);

            if (first.GetType() != typeof(NumExpr))
            {
                throw new EvaluatorException("expected number?");
            }
            NumExpr num_f = (NumExpr)first;
            long    min   = num_f.getValue();
            Expr    arg;

            for (int i = 1; i < len; i++)
            {
                arg = args[i].eval(env);
                if (arg.GetType() != typeof(NumExpr))
                {
                    throw new EvaluatorException("expected number?");
                }
                num_f = (NumExpr)arg;
                long num = num_f.getValue();
                if (num > min)
                {
                    return(new BoolExpr(false));
                }

                min = num;
            }
            return(new BoolExpr(true));
        }