示例#1
0
        public Value Evaluate(Environment environment)
        {
            var value = expression.Evaluate(environment);

            if (value.IsList() || value.IsSet())
            {
                var   list   = value.AsList().GetValue();
                Value result = ValueNull.NULL;
                for (var i = 0; i < identifiers.Count; i++)
                {
                    if (!environment.IsDefined(identifiers[i]))
                    {
                        throw new ControlErrorException(new ValueString("ERROR"), "Variable '" + identifiers[i] + "' is not defined", pos);
                    }
                    if (i < list.Count)
                    {
                        environment.Set(identifiers[i], list[i]);
                        result = list[i];
                    }
                    else
                    {
                        environment.Set(identifiers[i], ValueNull.NULL);
                        result = ValueNull.NULL;
                    }
                }
                return(result);
            }
            throw new ControlErrorException(new ValueString("ERROR"), "Destructuring assign expected list or set but got " + value.Type(), pos);
        }
示例#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);
        }
示例#3
0
 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));
 }
示例#4
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);
        }
示例#5
0
        public static SortedSet <string> Get(Node node, Environment environment)
        {
            var freeVars            = new SortedSet <string>();
            var boundVars           = new SortedSet <string>();
            var additionalboundVars = new SortedSet <string>();

            node.CollectVars(freeVars, boundVars, additionalboundVars);
            var result = new SortedSet <string>();

            foreach (var freeVar in freeVars)
            {
                if (!environment.IsDefined(freeVar))
                {
                    result.Add(freeVar);
                }
            }
            return(result);
        }
示例#6
0
        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);
        }