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));
        }
Exemple #2
0
        private static StreamReader GetModuleStream(String moduleidentifier, Environment environment, SourcePos pos)
        {
            string userdir   = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile);
            string moduledir = userdir + "/.ckl/modules";
            string module    = moduledir + "/" + new FileInfo(moduleidentifier).Name;

            if (File.Exists(module))
            {
                return(new StreamReader(module));
            }
            if (environment.IsDefined("checkerlang_module_path"))
            {
                var modulepath = environment.Get("checkerlang_module_path", pos);
                foreach (var dir in modulepath.AsList().GetValue())
                {
                    module = dir.AsString().GetValue() + "/" + new FileInfo(moduleidentifier).Name;
                    if (File.Exists(module))
                    {
                        return(new StreamReader(module));
                    }
                }
            }
            module = "./" + new FileInfo(moduleidentifier).Name;
            if (File.Exists(module))
            {
                return(new StreamReader(module));
            }
            return(null);
        }
Exemple #3
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);
        }
Exemple #4
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var obj    = args.GetAsString("obj");
            var output = args.GetOutput("out", environment.Get("stdout", pos).AsOutput());

            output.Write(obj.GetValue());
            return(ValueNull.NULL);
        }
 public Value Evaluate(Environment environment)
 {
     if (!environment.IsDefined(identifier))
     {
         throw new ControlErrorException(new ValueString("ERROR"), "Variable '" + identifier + "' is not defined", pos);
     }
     environment.Set(identifier, expression.Evaluate(environment));
     return(environment.Get(identifier, pos));
 }
Exemple #6
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);
            }

            if (a.IsInt() && b.IsInt())
            {
                var divisor = b.AsInt().GetValue();
                if (divisor == 0)
                {
                    if (environment.IsDefined("DIV_0_VALUE") &&
                        environment.Get("DIV_0_VALUE", pos) != ValueNull.NULL)
                    {
                        return(environment.Get("DIV_0_VALUE", pos));
                    }
                    throw new ControlDivideByZeroException("divide by zero", pos);
                }
                return(new ValueInt(a.AsInt().GetValue() / divisor));
            }

            if (a.IsNumerical() && b.IsNumerical())
            {
                var divisor = b.AsDecimal().GetValue();
                if (divisor == 0)
                {
                    if (environment.IsDefined("DIV_0_VALUE") &&
                        environment.Get("DIV_0_VALUE", pos) != ValueNull.NULL)
                    {
                        return(environment.Get("DIV_0_VALUE", pos));
                    }
                    throw new ControlDivideByZeroException("divide by zero", pos);
                }
                return(new ValueDecimal(a.AsDecimal().GetValue() / divisor));
            }

            throw new ControlErrorException(new ValueString("ERROR"), "Cannot divide values", pos);
        }
Exemple #7
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var input = args.GetInput("input", environment.Get("stdin", pos).AsInput());
            var line  = input.ReadLine();

            if (line == null)
            {
                return(ValueNull.NULL);
            }
            return(new ValueString(line));
        }
Exemple #8
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);
        }
        public Value Evaluate(Environment environment)
        {
            var modules = environment.GetModules();
            // resolve module file, identifier and name
            string spec;

            if (modulespec is NodeIdentifier)
            {
                spec = ((NodeIdentifier)this.modulespec).GetValue();
                if (environment.IsDefined(spec))
                {
                    var val = environment.Get(spec, this.pos);
                    if (!(val.IsObject() && val.AsObject().isModule))
                    {
                        if (!val.IsString())
                        {
                            throw new ControlErrorException(new ValueString("ERROR"), "Expected string or identifier modulespec but got " + val.Type(), pos);
                        }
                        spec = val.AsString().GetValue();
                    }
                }
            }
            else
            {
                var specval = this.modulespec.Evaluate(environment);
                if (!specval.IsString())
                {
                    throw new ControlErrorException(new ValueString("ERROR"), "Expected string or identifier modulespec but got " + specval.Type(), pos);
                }
                spec = specval.AsString().GetValue();
            }
            var modulefile = spec;

            if (!modulefile.EndsWith(".ckl"))
            {
                modulefile += ".ckl";
            }
            string moduleidentifier;
            var    modulename = this.name;
            var    parts      = spec.Split('/');
            var    name       = parts[parts.Length - 1];

            if (name.EndsWith(".ckl"))
            {
                name = name.Substring(0, name.Length - 4);
            }
            moduleidentifier = name;
            if (modulename == null)
            {
                modulename = name;
            }
            environment.PushModuleStack(moduleidentifier, pos);

            // lookup or read module
            Environment moduleEnv = null;

            if (modules.ContainsKey(moduleidentifier))
            {
                moduleEnv = modules[moduleidentifier];
            }
            else
            {
                moduleEnv = environment.GetBase().NewEnv();
                var  modulesrc = ModuleLoader.LoadModule(modulefile, environment, pos);
                Node node      = null;
                try {
                    node = Parser.Parse(modulesrc, modulefile);
                } catch (Exception) {
                    throw new ControlErrorException(new ValueString("ERROR"), "Cannot parse module " + moduleidentifier, pos);
                }
                node.Evaluate(moduleEnv);
                modules[moduleidentifier] = moduleEnv;
            }
            environment.PopModuleStack();

            // bind module or contents of module
            if (unqualified)
            {
                foreach (var symbol in moduleEnv.GetLocalSymbols())
                {
                    if (symbol.StartsWith("_"))
                    {
                        continue;                         // skip private module symbols
                    }
                    environment.Put(symbol, moduleEnv.Get(symbol, pos));
                }
            }
            else if (symbols != null)
            {
                foreach (var symbol in moduleEnv.GetLocalSymbols())
                {
                    if (symbol.StartsWith("_"))
                    {
                        continue;                         // skip private module symbols
                    }
                    if (!symbols.ContainsKey(symbol))
                    {
                        continue;
                    }
                    environment.Put(symbols[symbol], moduleEnv.Get(symbol, pos));
                }
            }
            else
            {
                var obj = new ValueObject {
                    isModule = true
                };
                foreach (var symbol in moduleEnv.GetLocalSymbols())
                {
                    if (symbol.StartsWith("_"))
                    {
                        continue;                         // skip private module symbols
                    }
                    var val = moduleEnv.Get(symbol, pos);
                    if (val.IsObject() && val.AsObject().isModule)
                    {
                        continue;                                            // do not re-export modules!
                    }
                    obj.AddItem(symbol, val);
                }
                environment.Put(modulename, obj);
            }
            return(ValueNull.NULL);
        }
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var input = args.GetInput("input", environment.Get("stdin", pos).AsInput());

            return(new ValueString(input.ReadAll()));
        }
 public Value Evaluate(Environment environment)
 {
     return(environment.Get(value, pos));
 }