Represents a Var.

From the Clojure documentation:

"Vars provide a mechanism to refer to a mutable storage location that can be dynamically rebound (to a new storage location) on a per-thread basis. Every Var can (but needn't) have a root binding, which is a binding that is shared by all threads that do not have a per-thread binding."
Наследование: ARef, IFn, IRef, Settable
Пример #1
0
        public static void AddClojureDropDownBox(string labelText, Var variable, PersistentVector vals, IFn func)
        {
            string variableSymbolName = null;

            if(variable != null)
                variableSymbolName = variable.sym.Name;

            Label label = new Label();
            label.AutoSize = true;
            label.Text = labelText;

            Dictionary<string, object> labelCodeAssoc = new Dictionary<string, object>();

            ComboBox comboBox = new ComboBox();
            for (int i = 0; i < vals.Count; i += 2)
            {
                labelCodeAssoc.Add(vals[i].ToString(), vals[i + 1]);
                comboBox.Items.Add(vals[i]);
            }

            comboBox.SelectedIndex = 0;
            comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox.SelectedIndexChanged += (s, e) =>
                {
                    if (comboBox.SelectedItem == null)
                        return;

                    object code = labelCodeAssoc[comboBox.SelectedItem.ToString()];

                     if(variableSymbolName != null)
                    {
                        string newVal = code.ToString();
                        ClojureEngine.Eval("(def " + variableSymbolName + " " + newVal + ")");
                    }

                    if (func != null)
                        ClojureEngine.Log(func.invoke());
                };

            self.AddNewClojureControls(new Control[] { label, comboBox });
        }
Пример #2
0
 public static void CreateSliderDouble(string label, Var var, double min, double max, IFn func)
 {
     ClojureDefinedUI.AddClojureSlider(label, var, min, max, 10000, func);
 }
Пример #3
0
 public static void CreateSlider(string label, Var var, int min, int max, IFn func)
 {
     ClojureDefinedUI.AddClojureSlider(label, var, min, max, 1, func);
 }
Пример #4
0
 public static void CreateNumeric(string label, Var var, double min, double max, IFn func)
 {
     ClojureDefinedUI.AddClojureNumericUpDown(label, var, min, max, func);
 }
Пример #5
0
 /// <summary>
 /// Add a <see cref="Symbol">Symbol</see> to <see cref="Var">Var</see> reference.
 /// </summary>
 /// <param name="sym"></param>
 /// <param name="var"></param>
 /// <returns></returns>
 public Var refer(Symbol sym, Var var)
 {
     return((Var)reference(sym, var));
 }
Пример #6
0
 public Unbound(Var v)
 {
     _v = v;
 }
Пример #7
0
        static void InitializeCompilerOptions()
        {
            Object compilerOptions = null;

            foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
            {
                string name = (string)de.Key;
                string v = (string)de.Value;
                if (name.StartsWith("CLOJURE_COMPILER_"))
                {
                    compilerOptions = RT.assoc(compilerOptions,
                        RT.keyword(null, name.Substring(1 + name.LastIndexOf("_"))),
                        RT.readString(v));
                }

            }

            CompilerOptionsVar = Var.intern(Namespace.findOrCreate(Symbol.intern("clojure.core")),
                Symbol.intern("*compiler-options*"), compilerOptions).setDynamic();
        }
Пример #8
0
        internal static int RegisterProtocolCallsite(Var v)
        {
            if (!ProtocolCallsitesVar.isBound)
                throw new InvalidOperationException("PROTOCOL_CALLSITES is not bound");

            IPersistentVector protocolCallsites = (IPersistentVector)ProtocolCallsitesVar.deref();
            protocolCallsites = protocolCallsites.cons(v);
            ProtocolCallsitesVar.set(protocolCallsites);
            return protocolCallsites.count() - 1;
        }
Пример #9
0
 private static void RegisterVar(Var v)
 {
     // do nothing, I think, in my implementation.
     // However, this may be needed when writing out a binary file
 }
Пример #10
0
        private static Expression GenerateVarExpr(Var v, Symbol tag)
        {
            object tagToUse = tag ?? v.Tag;

            Expression expr = Expression.Call(Expression.Constant(v), Method_Var_get);  //asdf-tag
            //if (tagToUse != null)
            //    expr = Expression.Convert(expr, TagToType(tagToUse));  // NOPE
            return expr;
        }
Пример #11
0
        private static Expression GenerateVarAssignExpr(Var v, object init)
        {
            Expression initExpr = Generate(init);

            return Expression.Call(Expression.Constant(v), Method_Var_set, MaybeBox(initExpr));
        }
Пример #12
0
        public static void AddClojureSlider(string labelText, Var variable, double min, double max, int divisor, IFn func)
        {
            double variableVal = 0;
            string variableSymbolName = variable.sym.Name;

            Label label = new Label();
            label.AutoSize = true;
            label.Text = labelText;

            if (!double.TryParse(variable.get().ToString(), out variableVal))
            {
                label.Text = "Error: Could not convert variable '" + variableSymbolName + "' value of '" + variable.get().ToString() + "' to a numeric type";
                self.AddNewClojureControl(label);
                return;
            }

            int iMin = (int)Math.Round(min * divisor);
            int iMax = (int)Math.Round(max * divisor);
            int iValue = (int)Math.Round(variableVal * divisor);

            NumericUpDown numericUpDown = new NumericUpDown();
            TrackBar trackbar = new TrackBar();
            bool disableNumbericUpDownUpdate = false;

            trackbar.AutoSize = false;
            trackbar.Width = 128;
            trackbar.Height = 24;
            trackbar.Minimum = iMin;
            trackbar.Maximum = iMax;
            trackbar.Value = iValue;
            trackbar.ValueChanged += (s, e) =>
            {
                double tbVal = (double)trackbar.Value / divisor;
                ClojureEngine.Eval("(def " + variableSymbolName + " " + tbVal + ")");
                disableNumbericUpDownUpdate = true;
                numericUpDown.Value = (decimal)tbVal;
                disableNumbericUpDownUpdate = false;

                if (func != null)
                {
                    try
                    {
                        object res = func.invoke();
                        ClojureEngine.Log(res);
                    }
                    catch (Exception ex)
                    {
                        ClojureEngine.Log(ex.ToString());
                    }
                }
            };

            Label valLabel = new Label();
            valLabel.AutoSize = true;
            valLabel.Text = "Value: ";

            numericUpDown.DecimalPlaces = (int)Math.Log10(divisor) + 1;
            numericUpDown.Minimum = (decimal)min;
            numericUpDown.Maximum = (decimal)max;
            numericUpDown.Value = (decimal)trackbar.Value / divisor;
            numericUpDown.ValueChanged += (s, e) =>
            {
                if (disableNumbericUpDownUpdate)
                    return;

                trackbar.Value = (int)(numericUpDown.Value * divisor);
            };

            Label minmaxLabel = new Label();
            minmaxLabel.AutoSize = true;
            minmaxLabel.Text = "Min: " + min + ", Max: " + max;

            self.AddNewClojureControls(new Control[] { label, trackbar, valLabel, numericUpDown, minmaxLabel });
        }
Пример #13
0
        public static void AddClojureNumericUpDown(string labelText, Var variable, double min, double max, IFn func)
        {
            double variableVal = 0;
            string variableSymbolName = variable.sym.Name;

            Label label = new Label();
            label.AutoSize = true;
            label.Text = labelText;

            if (!double.TryParse(variable.get().ToString(), out variableVal))
            {
                label.Text = "Error: Could not convert variable '" + variableSymbolName + "' value of '" + variable.get().ToString() + "' to a numeric type";
                self.AddNewClojureControl(label);
                return;
            }

            NumericUpDown numericUpDown = new NumericUpDown();
            numericUpDown.DecimalPlaces = 8;
            numericUpDown.Increment = 0.01m;
            numericUpDown.Minimum = (decimal)min;
            numericUpDown.Maximum = (decimal)max;
            numericUpDown.Value = (decimal)variableVal;
            numericUpDown.ValueChanged += (s, e) =>
                {
                    double tbVal = (double)numericUpDown.Value;
                    ClojureEngine.Eval("(def " + variableSymbolName + " " + tbVal + ")");

                    if (func != null)
                        ClojureEngine.Log(func.invoke());
                };

            self.AddNewClojureControls(new Control[] { label, numericUpDown });
        }
Пример #14
0
        /// <summary>
        /// Find the <see cref="Var">Var</see> mapped to a <see cref="Symbol">Symbol</see>.
        /// </summary>
        /// <param name="sym">The symbol to look up.</param>
        /// <returns>The mapped var.</returns>
        public Var FindInternedVar(Symbol sym)
        {
            Var v = Mappings.valAt(sym) as Var;

            return((v != null && v.Namespace == this) ? v : null);
        }
Пример #15
0
        /// <summary>
        /// Intern a <see cref="Symbol">Symbol</see> in the namespace, with a (new) <see cref="Var">Var</see> as its value.
        /// </summary>
        /// <param name="sym">The symbol to intern.</param>
        /// <returns>The <see cref="Var">Var</see> associated with the symbol.</returns>
        /// <remarks>
        /// <para>It is an error to intern a symbol with a namespace.</para>
        /// <para>This has to deal with other threads also interning.</para>
        /// </remarks>
        public Var intern(Symbol sym)
        {
            if (sym.Namespace != null)
                throw new ArgumentException("Can't intern a namespace-qualified symbol");

            IPersistentMap map = Mappings;
            object o;
            Var v = null;
            // race condition
            while ((o = map.valAt(sym)) == null)
            {
                if (v == null)
                    v = new Var(this, sym);
                IPersistentMap newMap = map.assoc(sym, v);
                _mappings.CompareAndSet(map, newMap);
                map = Mappings;
            }
            if ((o is Var) && ((Var)o).Namespace == this)
                return (Var)o;

            if (v == null)
                v = new Var(this, sym);

            WarnOrFailOnReplace(sym, o, v);

            while (!_mappings.CompareAndSet(map, map.assoc(sym, v)))
                map = Mappings;

            return v;
        }
Пример #16
0
        internal static int RegisterVarCallsite(Var v)
        {
            if (!VAR_CALLSITES.isBound)
                throw new InvalidOperationException("VAR_CALLSITES is not bound");

            IPersistentVector varCallsites = (IPersistentVector)VAR_CALLSITES.deref();
            varCallsites = varCallsites.cons(v);
            VAR_CALLSITES.set(varCallsites);
            return varCallsites.count() - 1;
        }
Пример #17
0
 /// <summary>
 /// Add a <see cref="Symbol">Symbol</see> to <see cref="Var">Var</see> reference.
 /// </summary>
 /// <param name="sym"></param>
 /// <param name="var"></param>
 /// <returns></returns>
 public Var refer(Symbol sym, Var var)
 {
     return (Var)reference(sym, var);
 }
Пример #18
0
        public static void Initialize(TextBox newLogControl)
        {
            SetLogControl(newLogControl);

            //Setup clojure system
            RT_ReadString = RT.var("clojure.core", "read-string");
            RT_Eval = RT.var("clojure.core", "eval");
            RT_Eval.invoke(RT_ReadString.invoke(outstrval_macro)); //Allows correct returns for logging

            Eval("(use 'clojure.repl)");
            Eval("(def x 0)");
            Eval("(def y 0)");
            Eval("(def z 0)");
            //Load clojure functions from this runtime
            LoadClojureFuncsFromAllAssemblies();
            InitRuntimeMethods();

            //Load all the clojure function files
            foreach (string file in Directory.GetFiles("functions"))
            {
                Eval(File.ReadAllText(file), true);
            }

            ClearLog();
            Eval("(println \"" + Clojure_InitCompleteString + "\")", true);
        }
Пример #19
0
        internal static void RegisterVarCallsite(Var v)
        {
            if (!VarCallsitesVar.isBound)
                throw new InvalidOperationException("VAR_CALLSITES is not bound");

            IPersistentCollection varCallsites = (IPersistentCollection)VarCallsitesVar.deref();
            varCallsites = varCallsites.cons(v);
            VarCallsitesVar.set(varCallsites);
            //return varCallsites.count() - 1;
        }
Пример #20
0
        /// <summary>
        /// Intern a <see cref="Symbol">Symbol</see> in the namespace, with a (new) <see cref="Var">Var</see> as its value.
        /// </summary>
        /// <param name="sym">The symbol to intern.</param>
        /// <returns>The <see cref="Var">Var</see> associated with the symbol.</returns>
        /// <remarks>
        /// <para>It is an error to intern a symbol with a namespace.</para>
        /// <para>This has to deal with other threads also interning.</para>
        /// </remarks>
        public Var intern(Symbol sym)
        {
            if (sym.Namespace != null)
                throw new ArgumentException("Can't intern a namespace-qualified symbol");

            IPersistentMap map = Mappings;
            object o;
            Var v = null;
            // race condition
            while ((o = map.valAt(sym)) == null)
            {
                if (v == null)
                    v = new Var(this, sym);
                IPersistentMap newMap = map.assoc(sym, v);
                _mappings.CompareAndSet(map, newMap);
                map = Mappings;
            }
            if ((o is Var) && ((Var)o).Namespace == this)
                return (Var)o;

            // race condition
            throw new InvalidOperationException(String.Format("{0} already refers to: {1} in namespace: {2}", sym, o, _name));
        }
Пример #21
0
 public static void RegisterVar(Var v)
 {
     if (!VarsVar.isBound)
         return;
     IPersistentMap varsMap = (IPersistentMap)VarsVar.deref();
     Object id = RT.get(varsMap, v);
     if (id == null)
     {
         VarsVar.set(RT.assoc(varsMap, v, RegisterConstant(v)));
     }
 }
Пример #22
0
 public static void CreateDropdown(string label, Var var, PersistentVector keyvaluepairs, IFn func)
 {
     ClojureDefinedUI.AddClojureDropDownBox(label, var, keyvaluepairs, func);
 }
Пример #23
0
 private static void RegisterVar(Var v)
 {
     if (!VARS.IsBound)
         return;
     IPersistentMap varsMap = (IPersistentMap)VARS.deref();
     Object id = RT.get(varsMap, v);
     if (id == null)
     {
         VARS.set(RT.assoc(varsMap, v, RegisterConstant(v)));
     }
 }
Пример #24
0
 public Unbound(Var v)
 {
     _v = v;
 }