예제 #1
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var env    = environment.NewEnv();
            var lst    = args.GetAsList("lst");
            var cmp    = args.HasArg("cmp") ? args.GetFunc("cmp") : environment.Get("compare", pos).AsFunc();
            var key    = args.HasArg("key") ? args.GetFunc("key") : environment.Get("identity", pos).AsFunc();
            var result = new List <Value>();

            result.AddRange(lst.GetValue());
            for (var i = 1; i < result.Count; i++)
            {
                var v = key.Execute(new Args(key.GetArgNames()[0], result[i], pos), env, pos);
                for (var j = i - 1; j >= 0; j--)
                {
                    var v2         = key.Execute(new Args(key.GetArgNames()[0], result[j], pos), env, pos);
                    var cmpargs    = new Args(cmp.GetArgNames()[0], cmp.GetArgNames()[1], v, v2, pos);
                    var comparison = (int)cmp.Execute(cmpargs, env, pos).AsInt().GetValue();
                    if (comparison < 0)
                    {
                        var temp = result[j + 1];
                        result[j + 1] = result[j];
                        result[j]     = temp;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(new ValueList(result));
        }
예제 #2
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var dir       = args.GetString("dir").GetValue();
            var recursive = false;

            if (args.HasArg("recursive"))
            {
                recursive = args.GetBoolean("recursive").GetValue();
            }
            var include_path = recursive;

            if (args.HasArg("include_path"))
            {
                include_path = args.GetBoolean("include_path").GetValue();
            }
            var include_dirs = false;

            if (args.HasArg("include_dirs"))
            {
                include_dirs = args.GetBoolean("include_dirs").GetValue();
            }
            var result = new ValueList();

            CollectFiles(dir, recursive, include_path, include_dirs, result);
            return(result);
        }
예제 #3
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.HasArg("a") && !args.HasArg("b"))
            {
                return(new ValueInt(random.Next((int)args.GetInt("a").GetValue())));
            }

            if (args.HasArg("a") && args.HasArg("b"))
            {
                return(new ValueInt(random.Next((int)args.GetInt("a").GetValue(), (int)args.GetInt("b").GetValue())));
            }
            return(new ValueDecimal((decimal)random.NextDouble()));
        }
예제 #4
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var    program   = args.GetString("program").GetValue();
            var    arguments = args.GetList("args").GetValue();
            string work_dir  = null;

            if (args.HasArg("work_dir"))
            {
                work_dir = args.GetString("work_dir").GetValue();
            }
            var echo = false;

            if (args.HasArg("echo"))
            {
                echo = args.GetBoolean("echo").GetValue();
            }
            try
            {
                var list = new StringBuilder();
                list.Append(program).Append(" ");
                foreach (var argument in arguments)
                {
                    list.Append(argument.AsString().GetValue()).Append(" ");
                }
                list.Remove(list.Length - 1, 1);
                var process = new Process();
                var info    = process.StartInfo;
                info.FileName  = program;
                info.Arguments = list.ToString();
                if (work_dir != null)
                {
                    info.WorkingDirectory = work_dir;
                }
                info.CreateNoWindow         = true;
                info.RedirectStandardOutput = true;
                info.RedirectStandardError  = true;
                info.RedirectStandardInput  = true;
                if (echo)
                {
                    System.Console.WriteLine(program + " " + list);
                }
                process.Start();
                process.WaitForExit();
                return(new ValueInt(process.ExitCode));
            } catch (Exception e) {
                throw new ControlErrorException(new ValueString("ERROR"), "Cannot execute " + program + ": " + e.Message, pos);
            }
        }
예제 #5
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var result = new ValueList();

            if (!args.HasArg("module"))
            {
                foreach (var symbol in environment.GetSymbols())
                {
                    result.AddItem(new ValueString(symbol));
                }
            }
            else
            {
                var moduleArg = args.Get("module");
                Dictionary <string, Value> module;
                if (moduleArg.IsString())
                {
                    module = environment.Get(moduleArg.AsString().GetValue(), pos).AsObject().value;
                }
                else
                {
                    module = args.Get("module").AsObject().value;
                }
                foreach (var symbol in module.Keys)
                {
                    result.AddItem(new ValueString(symbol));
                }
            }
            return(result);
        }
예제 #6
0
 public override Value Execute(Args args, Environment environment, SourcePos pos)
 {
     if (!args.HasArg("obj"))
     {
         return(new ValueDate(DateTime.Now));
     }
     return(args.GetAsDate("obj"));
 }
예제 #7
0
 public override Value Execute(Args args, Environment environment, SourcePos pos)
 {
     if (!args.HasArg("obj"))
     {
         return(new ValueObject());
     }
     return(args.GetAsObject("obj"));
 }
예제 #8
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var start = 0;
            var end   = 0;
            var step  = 1;

            if (args.HasArg("a") && !args.HasArg("b"))
            {
                end = (int)args.GetInt("a").GetValue();
            }
            else if (args.HasArg("a") && args.HasArg("b"))
            {
                start = (int)args.GetInt("a").GetValue();
                end   = (int)args.GetInt("b").GetValue();
            }
            if (args.HasArg("step"))
            {
                step = (int)args.GetInt("step").GetValue();
            }

            var result = new ValueList();
            var i      = start;

            if (step > 0)
            {
                while (i < end)
                {
                    result.AddItem(new ValueInt(i));
                    i += step;
                }
            }
            else if (step < 0)
            {
                while (i > end)
                {
                    result.AddItem(new ValueInt(i));
                    i += step;
                }
            }
            return(result);
        }
예제 #9
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var filename = args.GetString("filename").GetValue();
            var encoding = args.GetString("encoding", "UTF-8").GetValue();
            var append   = ValueBoolean.FALSE;

            if (args.HasArg("append"))
            {
                append = args.Get("append").AsBoolean();
            }
            return(new ValueOutput(new StreamWriter(filename, append.GetValue(), encoding == "UTF-8" ? Encoding.UTF8 : Encoding.GetEncoding(encoding))));
        }
예제 #10
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var    nativeName = args.GetString("native").GetValue();
            string alias      = null;

            if (args.HasArg("alias"))
            {
                alias = args.GetString("alias").GetValue();
            }
            BindNative.Bind(environment, nativeName, alias, pos);
            return(ValueNull.NULL);
        }
예제 #11
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("date"))
            {
                return(ValueNull.NULL);
            }
            var value = args.GetDate("date");
            var date  = value.AsDate().GetValue();
            var fmt   = args.HasArg("fmt") ? args.GetString("fmt").GetValue() : "yyyy-MM-dd HH:mm:ss";

            return(new ValueString(date.ToString(fmt, DateTimeFormatInfo.InvariantInfo)));
        }
예제 #12
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("x"))
            {
                return(ValueNull.NULL);
            }
            var digits = 0;

            if (args.HasArg("digits"))
            {
                digits = (int)args.GetInt("digits").GetValue();
            }
            return(new ValueDecimal((decimal)Math.Round((double)args.GetNumerical("x").GetValue(), digits)));
        }
예제 #13
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("str"))
            {
                return(ValueNull.NULL);
            }
            var value = args.GetString("str").GetValue();
            var delim = "[ \\t]+";

            if (args.HasArg("delim"))
            {
                delim = args.GetAsString("delim").GetValue();
            }
            return(SplitValue(value, delim));
        }
예제 #14
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            ValueString obj;

            if (args.HasArg("obj"))
            {
                obj = args.GetAsString("obj");
            }
            else
            {
                obj = new ValueString("");
            }
            var output = args.GetOutput("out", environment.Get("stdout", pos).AsOutput());

            output.WriteLine(obj.GetValue());
            return(ValueNull.NULL);
        }
예제 #15
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var dir = args.GetString("dir").GetValue();

            if (Directory.Exists(dir))
            {
                return(ValueNull.NULL);
            }
            var with_parents = false;

            if (args.HasArg("with_parents"))
            {
                with_parents = args.GetBoolean("with_parents").GetValue();
            }
            if (with_parents)
            {
                try
                {
                    Directory.CreateDirectory(dir);
                }
                catch
                {
                    throw new ControlErrorException(new ValueString("ERROR"), "Cannot create directory " + dir, pos);
                }
            }
            else
            {
                if (!Directory.GetParent(dir).Exists)
                {
                    throw new ControlErrorException(new ValueString("ERROR"), "Cannot create directory " + dir, pos);
                }

                try
                {
                    Directory.CreateDirectory(dir);
                }
                catch
                {
                    throw new ControlErrorException(new ValueString("ERROR"), "Cannot create directory " + dir, pos);
                }
            }
            return(ValueNull.NULL);
        }
예제 #16
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var a = args.Get("a");
            var b = args.Get("b");

            if (a.IsNull() || b.IsNull())
            {
                return(ValueNull.NULL);
            }

            Value div_0_value;

            if (args.HasArg("div_0_value"))
            {
                div_0_value = args.Get("div_0_value");
            }
            else
            {
                div_0_value = new ValueInt(long.MaxValue);
            }

            if (a.IsInt() && b.IsInt())
            {
                var divisor = b.AsInt().GetValue();
                if (divisor == 0)
                {
                    return(div_0_value);
                }
                return(new ValueInt(a.AsInt().GetValue() / divisor));
            }

            if (a.IsNumerical() && b.IsNumerical())
            {
                var divisor = b.AsDecimal().GetValue();
                if (divisor == 0)
                {
                    return(div_0_value);
                }
                return(new ValueDecimal(a.AsDecimal().GetValue() / divisor));
            }

            throw new ControlErrorException(new ValueString("ERROR"), "Cannot div0 " + a + " by " + b, pos);
        }
예제 #17
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("str"))
            {
                return(ValueNull.NULL);
            }
            var str  = args.GetString("str").GetValue();
            var fmts = new List <string>();

            if (args.HasArg("fmt"))
            {
                if (args.Get("fmt").IsList())
                {
                    foreach (var fmt_ in args.GetList("fmt").GetValue())
                    {
                        fmts.Add(fmt_.AsString().GetValue());
                    }
                }
                else
                {
                    fmts.Add(args.GetString("fmt").GetValue());
                }
            }
            else
            {
                fmts.Add("yyyyMMdd");
            }

            foreach (var fmt in fmts)
            {
                try
                {
                    return(new ValueDate(DateTime.ParseExact(str, fmt, DateTimeFormatInfo.InvariantInfo)));
                }
                catch
                {
                    // continue;
                }
            }

            return(ValueNull.NULL);
        }
예제 #18
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("obj"))
            {
                return(ValueNull.NULL);
            }
            var obj = args.Get("obj");
            var key = args.HasArg("key") ? args.GetFunc("key") : null;

            if (obj.IsString())
            {
                var str   = obj.AsString().GetValue();
                var start = (int)args.GetInt("start", str.Length - 1).GetValue();
                var part  = args.GetString("part").GetValue();
                return(new ValueInt(str.LastIndexOf(part, start, StringComparison.Ordinal)));
            }
            if (obj.IsList())
            {
                var env = environment;
                if (key != null)
                {
                    env = environment.NewEnv();
                }
                var item  = args.Get("part");
                var list  = obj.AsList().GetValue();
                var start = (int)args.GetInt("start", list.Count - 1).GetValue();
                for (var idx = start; idx >= 0; idx--)
                {
                    var elem = list[idx];
                    if (key != null)
                    {
                        elem = key.Execute(new Args(key.GetArgNames()[0], elem, pos), env, pos);
                    }
                    if (elem.IsEquals(item))
                    {
                        return(new ValueInt(idx));
                    }
                }
                return(new ValueInt(-1));
            }
            throw new ControlErrorException(new ValueString("ERROR"), "Find_last only works with strings and lists", pos);
        }
예제 #19
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var env = lexicalEnv.NewEnv();

            for (var i = 0; i < argNames.Count; i++)
            {
                if (args.HasArg(argNames[i]))
                {
                    env.Put(argNames[i], args.Get(argNames[i]));
                }
                else if (defValues[i] != null)
                {
                    env.Put(argNames[i], defValues[i].Evaluate(env));
                }
                else
                {
                    throw new ControlErrorException(new ValueString("ERROR"), "Missing argument " + argNames[i], pos);
                }
            }

            var result = body.Evaluate(env);

            if (result.IsReturn())
            {
                return(result.AsReturn().value);
            }
            if (result.IsBreak())
            {
                throw new ControlErrorException(new ValueString("ERROR"), "Cannot use break without surrounding for loop", result.AsBreak().pos);
            }
            if (result.IsContinue())
            {
                throw new ControlErrorException(new ValueString("ERROR"), "Cannot use continue without surrounding for loop", result.AsContinue().pos);
            }
            return(result);
        }
예제 #20
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("list"))
            {
                return(ValueNull.NULL);
            }

            var list   = args.GetList("list").GetValue();
            var ignore = new List <Value>();

            if (args.HasArg("ignore"))
            {
                ignore.AddRange(args.GetList("ignore").GetValue());
            }

            var resultdecimal   = (decimal)0;
            var resultint       = 0L;
            var decimalrequired = false;

            foreach (var value in list)
            {
                var skipvalue = false;
                foreach (var ignoreval in ignore)
                {
                    if (ignoreval.IsEquals(value))
                    {
                        skipvalue = true;
                        break;
                    }
                }
                if (skipvalue)
                {
                    continue;
                }

                if (decimalrequired)
                {
                    resultdecimal += value.AsDecimal().GetValue();
                }
                else if (value.IsInt())
                {
                    resultint += value.AsInt().GetValue();
                }
                else if (value.IsDecimal())
                {
                    if (!decimalrequired)
                    {
                        decimalrequired = true;
                        resultdecimal   = resultint;
                    }

                    resultdecimal += value.AsDecimal().GetValue();
                }
                else
                {
                    throw new ControlErrorException(new ValueString("ERROR"), "Cannot sum " + value, pos);
                }
            }

            if (decimalrequired)
            {
                return(new ValueDecimal(resultdecimal));
            }
            return(new ValueInt(resultint));
        }