当前位置: 首页>>代码示例>>C#>>正文


C# IScope.Exists方法代码示例

本文整理汇总了C#中IScope.Exists方法的典型用法代码示例。如果您正苦于以下问题:C# IScope.Exists方法的具体用法?C# IScope.Exists怎么用?C# IScope.Exists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IScope的用法示例。


在下文中一共展示了IScope.Exists方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetOnScope

        /// <summary>
        /// If bCreate, always set value on current scope,
        /// else, change value on scope value exists on or throw exception if it doesn't exist
        /// </summary>
        /// <returns>true if it actually set a value</returns>
        internal static bool SetOnScope(IScope scope, string key, Value value, bool bCreate, bool bOverload, bool bInitOnly)
        {
            // figure out the scope to modify
            IScope where;
            if (bCreate)
            {
                where = scope;
            }
            else
            {
                where = scope.Exists(key);
                if (where == null)
                    throw new Loki3Exception().AddBadToken(new Token(key));
            }

            // if we're setting a function, it may be an overload
            if (bOverload && value.Type == ValueType.Function)
            {
                // first, if there's nothing there, see if key exists on an ancestor scope
                bool bFound = where.AsMap.ContainsKey(key);
                if (!bFound)
                {
                    IScope ancestor = scope.Exists(key);
                    if (ancestor != null)
                    {
                        Value existing = ancestor.AsMap[key];
                        if (existing.Type == ValueType.Function)
                        {	// copy function(s) to this scope so we can overload it
                            // and yet this definition only exists in this scope
                            Value copiedToThisScope = existing.Copy();
                            where.SetValue(key, copiedToThisScope);
                            bFound = true;
                        }
                    }
                }

                if (bFound)
                {
                    Value existing = where.AsMap[key];
                    ValueFunctionOverload overload = null;
                    if (existing.Type == ValueType.Function)
                    {	// change function value into an overload value
                        overload = existing as ValueFunctionOverload;
                        if (overload == null)
                        {
                            overload = new ValueFunctionOverload(existing as ValueFunction);
                            overload.Add(value as ValueFunction);
                            where.SetValue(key, overload);
                        }
                        else
                        {
                            overload.Add(value as ValueFunction);
                        }
                        return true;
                    }
                }
            }

            if (bInitOnly && where.AsMap.ContainsKey(key))
                return false;
            where.SetValue(key, value);
            return true;
        }
开发者ID:loki3,项目名称:loki-pl1,代码行数:68,代码来源:Utility.cs

示例2: Eval

            internal override Value Eval(Value arg, IScope scope)
            {
                Map map = arg.AsMap;

                // get parameter
                Value value = map["function"];
                if (value.IsNil)
                {
                    Value valueKey = map["key"];
                    if (!valueKey.IsNil)
                    {
                        string key = valueKey.AsString;
                        IScope container = scope.Exists(key);
                        if (container == null)
                            throw new Loki3Exception().AddMissingKey(map["key"]);
                        value = container.GetValue(new Token(key));
                    }
                }
                if (value.IsNil)
                    throw new Loki3Exception().AddMissingKey(new ValueString("function"));

                // fetch body of function
                ValueFunction function = value as ValueFunction;
                if (function == null)
                    throw new Loki3Exception().AddWrongType(ValueType.Function, value.Type);
                List<DelimiterList> body = function.GetBody(scope);
                if (body == null)
                    return ValueNil.Nil;

                // make an array
                List<Value> array = new List<Value>();
                foreach (DelimiterList line in body)
                {	// attach scope to the value, preferring the lines's scope
                    // & falling back to the function's scope if not present
                    IScope rawScope = (line.Scope != null ? line.Scope : scope);
                    array.Add(new ValueRaw(line, rawScope));
                }
                return new ValueArray(array);
            }
开发者ID:loki3,项目名称:loki-pl1,代码行数:39,代码来源:Values.cs

示例3: Do

        /// <summary>
        /// Start a read-eval-print loop at the console
        /// </summary>
        /// <param name="scope">scope for parse-eval</param>
        internal static void Do(IScope scope, string prompt)
        {
            prompt += " ";
            string s = "";
            do
            {
                Console.Write(prompt);

                // keep reading lines as long as they end with \
                List<string> lines = new List<string>();
                bool bMore = false;
                do
                {
                    s = Console.ReadLine();
                    bMore = (s.Length > 2 && s[s.Length - 1] == '\\' && s[s.Length - 2] == ' ');
                    if (bMore)
                        s = s.Substring(0, s.Length - 2);
                    lines.Add(s);
                } while (bMore);
                LineConsumer consumer = new LineConsumer(lines);

                // eval the line(s)
                try
                {
                    Value v = EvalLines.Do(consumer, scope);
                    Console.WriteLine(v.ToString());
                }
                catch (Loki3Exception error)
                {
                    // if we're at the root scope, remove it to avoid infinite
                    // recursion in Map.ToString
                    if (error.Errors.ContainsKey("l3.error.scope"))
                        if (error.Errors["l3.error.scope"].AsMap == scope.AsMap)
                            error.Errors.Raw.Remove("l3.error.scope");

                    if (scope.Exists("prettify") != null)
                    {
                        scope.SetValue("lastError", new ValueMap(error.Errors));
                        Value v = loki3.builtin.test.TestSupport.ToValue("prettify lastError", scope);
                        Console.WriteLine("LOKI3 ERROR:\n" + v.AsString);
                        if (error.Errors.ContainsKey(Loki3Exception.keyScope))
                        {
                            scope.SetValue("lastScope", error.Errors[Loki3Exception.keyScope]);
                            try
                            {
                                v = loki3.builtin.test.TestSupport.ToValue("dumpStack lastScope", scope);
                                Console.WriteLine("STACK:\n" + v.AsString);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("ERROR PRINTING STACK:\n" + e.ToString());
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine(error.ToString());
                    }
                }
                catch (Exception error)
                {
                    Console.WriteLine("INTERNAL ERROR: " + error.ToString());
                }
            } while (s != "");
        }
开发者ID:loki3,项目名称:loki-pl1,代码行数:69,代码来源:Repl.cs


注:本文中的IScope.Exists方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。