示例#1
0
        public static Value invoke(ValueFunc fn, List <string> names_, List <Node> args, Environment environment, SourcePos pos)
        {
            var values = new List <Value>();
            var names  = new List <string>();

            for (var i = 0; i < args.Count; i++)
            {
                var arg = args[i];
                if (arg is NodeSpread)
                {
                    var argvalue = arg.Evaluate(environment);
                    if (argvalue.IsMap())
                    {
                        var map = argvalue.AsMap();
                        foreach (var entry in map.GetValue())
                        {
                            values.Add(entry.Value);
                            if (entry.Key.IsString())
                            {
                                names.Add(entry.Key.AsString().GetValue());
                            }
                            else
                            {
                                names.Add(null);
                            }
                        }
                    }
                    else
                    {
                        var list = argvalue.AsList();
                        foreach (var value in list.GetValue())
                        {
                            values.Add(value);
                            names.Add(null);
                        }
                    }
                }
                else
                {
                    values.Add(arg.Evaluate(environment));
                    names.Add(names_[i]);
                }
            }

            var args_ = new Args(fn.AsFunc().GetArgNames(), pos);

            args_.SetArgs(names, values);

            try
            {
                return(fn.AsFunc().Execute(args_, environment, pos));
            }
            catch (ControlErrorException e)
            {
                e.AddStacktraceElement(GetFuncallString(fn.AsFunc(), args_), pos);
                throw e;
            }
        }
示例#2
0
        public override string ToString()
        {
            var fn = Resolve("_str_");

            if (fn != null)
            {
                var args_  = new Args(fn.GetArgNames(), SourcePos.Unknown);
                var names  = new List <string>();
                var values = new List <Value>();
                names.Add(null);
                values.Add(this);
                args_.SetArgs(names, values);

                try {
                    return(fn.AsFunc().Execute(args_, null, SourcePos.Unknown).AsString().GetValue());
                } catch (ControlErrorException e) {
                    e.AddStacktraceElement("_str_", SourcePos.Unknown);
                    throw e;
                }
            }
            else
            {
                var builder = new StringBuilder();
                builder.Append("<*");
                foreach (var item in value.Keys)
                {
                    if (item.StartsWith("_"))
                    {
                        continue;
                    }
                    builder.Append(item).Append("=").Append(value[item]).Append(", ");
                }

                if (builder.Length > 2)
                {
                    builder.Remove(builder.Length - ", ".Length, ", ".Length);
                }
                builder.Append("*>");
                return(builder.ToString());
            }
        }