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


C# Cons.Length方法代码示例

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


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

示例1: Eval

 /// <summary>
 /// Evaluates an LSharp expression in a given environment
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Eval(Cons args, Environment environment)
 {
     if (args.Length() == 1)
         return Runtime.Eval(args.First(), environment);
     else
         throw new LSharpException("Incorrect arguments given to eval");
 }
开发者ID:westybsa,项目名称:MP.LSharp,代码行数:13,代码来源:Functions.cs

示例2: Nconc

        /// <summary>
        /// (nconc list*)
        /// Returns a list whose elements are the elements of each list in
        /// order. Destructively modifies all but the last list, such that
        /// the cdr of the last cons in each list is the next list.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Nconc(Cons args, Environment environment)
        {
            // With no argument, returns null
            if (args == null)
                return null;

            // With one argument, returns that argument
            if (args.Length() < 2)
                return args.First();

            for (int i = 0; i < args.Length() -1; i ++)
            {
                Cons cons = (Cons)args.Nth(i);
                cons = (Cons)cons.Last();
                cons.Rplacd(args.Nth(i+1));
            }

            return args.First();
        }
开发者ID:westybsa,项目名称:MP.LSharp,代码行数:28,代码来源:Functions.cs

示例3: Read

        /// <summary>
        /// (read TextReader [eof])
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Read(Cons args, Environment environment)
        {
            ReadTable readTable = (ReadTable)environment.GetValue(Symbol.FromName("*readtable*"));

            TextReader textReader = (TextReader)args.First();
            object eofValue = null;
            if (args.Length() > 1)
                eofValue = args.Second();
            
            return Reader.Read(textReader, readTable, eofValue);
        }
开发者ID:westybsa,项目名称:MP.LSharp,代码行数:17,代码来源:Functions.cs

示例4: Cons

        /// <summary>
        /// Creates a fresh cons, the car of which is object-1 and the cdr of which is object-2.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Cons(Cons args, Environment environment)
        {
            if (args.Length() == 1)
                return args.First();
            if (args.Length() == 2)
                return new Cons(args.First(),Cons((Cons)args.Rest(), environment));

            throw new LSharpException("Too many arguments given to cons");
        }
开发者ID:westybsa,项目名称:MP.LSharp,代码行数:15,代码来源:Functions.cs

示例5: Try

		/// <summary>
		/// (try expression catch [finally])
		/// The try special form corresponds to the try-catch-finally construct found 
		/// in C#. If catch is null then there is deemed to be no catch block 
		/// at all. If an exception occurs, the variable "it" is bound to the Exception 
		/// object in the local environment. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object Try(Cons args, Environment environment) 
		{
			try 
			{
				return Runtime.Eval(args.First(),environment);
			} 
			catch (Exception e) 
			{
				environment.AssignLocal(Symbol.IT,e);
						
				// If a catch form is specified then evaluate it
				if (args.Second() == Symbol.NULL)
					throw;
				return Runtime.Eval(args.Second(),environment);
			}
			finally 
			{
				// If a finally form was specified then evaluate it
				if  (args.Length() > 2)
					Runtime.Eval(args.Third(),environment);
			}
		}
开发者ID:mlnlover11,项目名称:MP.LSharp,代码行数:32,代码来源:SpecialForms.cs

示例6: Call

		/// <summary>
		/// Calls a .NET method.
		/// The first argument is the object to which the method is attached.
		/// Passes the rest of the arguments to the appropriate constructor
		/// </summary>
		/// <param name="method"></param>
		/// <param name="arguments"></param>
		/// <returns></returns>
		public static object Call(String method, Cons arguments) 
		{
			BindingFlags bindingFlags = BindingFlags.IgnoreCase  
				| BindingFlags.Public 
				| BindingFlags.NonPublic; 

			// Is it a method on a static type or an object instance ?
			Type type;
			if (arguments.First().GetType() == typeof(LSharp.Symbol)) 
			{
				bindingFlags = bindingFlags | BindingFlags.Static | BindingFlags.FlattenHierarchy;
				// Find the type object from its name
				type = TypeCache.Instance().FindType(arguments.First().ToString());
			}
			else 
			{
				bindingFlags = bindingFlags | BindingFlags.Instance;
				type = arguments.First().GetType();
			}

			Type[] types = new Type[arguments.Length() -1];
			object[] parameters = new object[arguments.Length() -1];
			int loop = 0;
			if (arguments.Rest() != null)
				foreach (object argument in (Cons)arguments.Rest()) 
				{
					types[loop] = argument.GetType();
					parameters[loop] = argument;
					loop++;
				}

			// Start by looking for a method call
			MethodInfo m = type.GetMethod(method.ToString(), 
						bindingFlags | BindingFlags.InvokeMethod
						,null,types,null);
			if (m != null)
				return m.Invoke(arguments.First(),parameters);

			// Now loook for a property get
			PropertyInfo p = type.GetProperty(method.ToString(),bindingFlags | BindingFlags.GetProperty,
				null,null, types,null);
			if (p != null)
				return p.GetGetMethod().Invoke(arguments.First(),parameters);

			// Now look for a field get
			FieldInfo f = type.GetField(method.ToString(),bindingFlags | BindingFlags.GetField);
			if (f != null)
				return f.GetValue(arguments.First());
			

			// FIXME: or an event ?
			EventInfo e  = type.GetEvent(method.ToString(), bindingFlags); // | BindingFlags.Event)
			if (e != null) // attempt to call the click event
			    return e.GetRaiseMethod().Invoke(arguments.First(), parameters);
			
			throw new LSharpException(string.Format("Call: No such method, property, field, or event '{0}' on '{1}'", method.ToString(),type));

			
		}
开发者ID:westybsa,项目名称:MP.LSharp,代码行数:67,代码来源:Runtime.cs

示例7: If

		/// <summary>
		/// (if test then [else])
		/// The if special form corresponds to the if-then-else construct found in 
		/// most algebraic programming languages. First the form test is evauated, 
		/// if true then the form then is evaluated.Otherwise, optionally the form 
		/// else is evaluated. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object If(Cons args, Environment environment) 
		{
			if (Conversions.ObjectToBoolean(Runtime.Eval(args.First(),environment)))
				// Evaluate the then part
				return Runtime.Eval(args.Second(),environment);
			else
				if (args.Length() > 2)
				// Evaluate the optional else part
				return Runtime.Eval(args.Third(),environment);
			else
				return null;
		}
开发者ID:mlnlover11,项目名称:MP.LSharp,代码行数:22,代码来源:SpecialForms.cs

示例8: Spawn

 /// <summary>
 /// (spawn expression) is like eval except that the expression
 /// is evaluated on a new thread. Returns immediately with
 /// the new thread object, but execution of expressions
 /// continues synchronously. Experimental.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Spawn(Cons args, Environment environment)
 {
     if (args.Length() == 1)
         return ThreadAdapter.Fork(args.First(), environment, System.Threading.ApartmentState.MTA);
     else if (args.Length() == 2)
         return ThreadAdapter.Fork(args.First(), environment, 
             (System.Threading.ApartmentState) Runtime.Eval(args.Second(), environment));
     else
         throw new LSharpException("Incorrect arguments given to spawn");
 }
开发者ID:mlnlover11,项目名称:MP.LSharp,代码行数:19,代码来源:SpecialForms.cs

示例9: DefClass

		/// <summary>
		/// Creates a dynamic class
		/// (defclass "classname" "inheritsfrom" [defmethod]) 
		/// e.g. (defclass "class1" "Object" DefinedMethods*)
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object DefClass(Cons args, Environment environment) 
		{
			string className = args.First().ToString();
			string superClass = args.Second().ToString();
			
			DefinedMethod[] methods = new DefinedMethod[args.Length() - 2];
			for (int i = 2; i < args.Length(); i++) 
			{
				try {
					Symbol toFind = (Symbol) args.Nth(i);
					object foundMethod =  environment.GetValue(toFind);
					methods[i - 2] = (DefinedMethod) foundMethod;
				} catch (Exception e ) {
					Console.WriteLine("DEFCLASS ERROR: " + e.Message + " " + e.StackTrace);
				}
			}
			return ClassBuilder.CreateClass(className, superClass, "", methods);
		}
开发者ID:mlnlover11,项目名称:MP.LSharp,代码行数:26,代码来源:SpecialForms.cs

示例10: DefMethod

		/// <summary>
		/// (defmethod name "arg1 arg2" "(expression1)" "(expression 2)" [...])
		/// Created methods used for DefClass <br />
		/// </summary>
		/// <param name="args"></param>
		/// <param name="e"></param>
		/// <returns></returns>
		public static object DefMethod(Cons args, Environment e)
		{
			string name;
			string[] _args;
			string commands = "";
			name = args.First().ToString();
			_args = args.Second().ToString().Split(new string[] {" "}, StringSplitOptions.None);;
			for (int i = 2; i < args.Length(); i++)
				commands += args.Nth(i) + " ";
			commands = commands.Replace("\\", "\\\\");
			commands = commands.Replace("\"", "\\\"");
			//FIXME: 
			//code = code.Replace("\n", "\\n");
			DefinedMethod ret = new DefinedMethod(commands, name, _args);
			e.AssignLocal(Symbol.FromName(name), ret);
			Console.WriteLine("Assigned '" + ret.Name + "' as a DefinedMethod");
			return ret;
		}
开发者ID:mlnlover11,项目名称:MP.LSharp,代码行数:25,代码来源:SpecialForms.cs

示例11: If

    /// <summary>
    /// (if test then [else])
    /// </summary>
    public static Object If(Cons args, LSharp.Environment environment)
    {
        string v = //"//(if " + Printer.ConsToString(args) + ")" + NewLine +
            Generate(args.First(),environment) +
            string.Format(@"
if (LSharp.Conversions.ObjectToBoolean(retval))
{{  // Evaluate the then part
  {0}
}}
", Generate(args.Second(),environment));

        if (args.Length() > 2)
        {
            // Evaluate the optional else part
            v += string.Format(@"
else
{{
  {0}
}}
", Generate(args.Third(),environment));
        }

        return v;
    }
开发者ID:mlnlover11,项目名称:MP.LSharp,代码行数:27,代码来源:NewCompiler.cs

示例12: Call

    /// <summary>
    /// (call method object argument*)
    /// </summary>
    public static Object Call(Cons args, LSharp.Environment environment)
    {
        string v = //"//(call " + Printer.ConsToString(args) + ")" + NewLine +
            "{" + NewLine;

        ArrayList argtypes = new ArrayList();
        ArrayList argz = new ArrayList();

        if (args.Length() > 2)
        {
            foreach (object arg in (args.Cddr() as Cons))
            {
                if (Primitives.IsAtom(arg))
                {
                    argz.Add(Printer.WriteToString(arg));
                }
                else
                {
                    string argn = MakeUnique("arg");
                    string sv = Generate(arg, environment);
                    sv += string.Format(@"{0} {1} = ({0}) retval;
", typeof(object) , argn);

                    argz.Add(argn);

                    v += sv;
                }
                argtypes.Add(typeof(object));
            }
        }

        string typemethname = args.Car().ToString();

        string methname = typemethname;
        string typename = string.Empty;
        Type type = typeof(object);

        int i = methname.LastIndexOf(".");
        
        if (i >= 0)
        {
            methname = methname.Substring(i + 1);
            typename = typemethname.Substring(0, i);
            type = TypeCache.FindType(typename);
        }
        MethodInfo mi = null;
        
        mi = type.GetMethod(methname, BindingFlags.IgnoreCase
                            | BindingFlags.Public | BindingFlags.Instance, binder, argtypes.ToArray(typeof(Type)) as Type[], null);

        string objn = string.Empty;

        if (mi == null)
        {
            type = TypeCache.FindType(args.Second().ToString());
            mi = type.GetMethod(methname, BindingFlags.IgnoreCase
                                | BindingFlags.Public | BindingFlags.Static, binder, argtypes.ToArray(typeof(Type)) as Type[], null);

            if (mi == null)
            {
                // use reflection
                v += Generate(args.Second(), environment);
                v += string.Format(@"retval = retval.GetType().InvokeMember(""{0}"",
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance, null, retval,
new object[]{{", methname) +
                    string.Join(", ", argz.ToArray(typeof(string)) as string[]) + @"});
}
";
                return v;
            }
            else
            {
                objn = type.ToString();
            }
        }
        else
        {
            objn = MakeUnique("obj");
            v += Generate(args.Second(), environment);
            v += string.Format(@"{0} {1} = ({0}) retval;
", type, objn);
        }

        v += "retval = " + (mi.ReturnType == typeof(void) ? @"null;
" : "") + objn + "." + mi.Name + "(" +
            string.Join(", ", argz.ToArray(typeof(string)) as string[]);
        
        return v + @");
}
";
    }
开发者ID:mlnlover11,项目名称:MP.LSharp,代码行数:94,代码来源:NewCompiler.cs

示例13: GenerateFuncCall

        public static string GenerateFuncCall(string type, string method, Cons rest, LSharp.Environment environment)
    {
        string v = string.Empty;
        string typemeth = method;
        if (type != null)
        {
            typemeth = type + "." + method;
        }

        if (rest == null)
        {
            return v + "retval = " + typemeth
                + string.Format(@"(null, environment);
");
        }
        else
        {
            if (rest.Length() == 1)
            {
                object argo = rest.First();
                string argn = "retval";
                if (argo is Symbol)
                {
                    Symbol sarg = (Symbol)argo;

                    if (environment.Contains(sarg))
                    {
                        argn = environment.GetValue(sarg) as string;
                    }
                    else
                    {
                        v += Generate(argo, environment);
                    }
                }
                else if (argo is Cons)
                {
                    v += Generate(argo, environment);
                }
                else
                {
                    // load primitive
                    argn = Printer.WriteToString(argo);
                }

                return v + "retval = " + typemeth
                    + string.Format(@"(new LSharp.Cons({0}), environment);
", argn);
                
            }
            else
            {
                string margs = GetArgs();
                v += GenerateList(rest, environment, margs);

                return v + "retval = " + typemeth
                    + string.Format(@"(LSharp.Cons.FromList({0}), environment);
", margs);
            }
        }
    }
开发者ID:mlnlover11,项目名称:MP.LSharp,代码行数:60,代码来源:NewCompiler.cs

示例14: GenerateList

        public static string GenerateList(Cons rest, LSharp.Environment environment, string margs)
    {
        string v = string.Format(@"System.Collections.ArrayList {0} = new System.Collections.ArrayList({1});
", margs, rest.Length());
        // load args
        foreach (object argo in rest)
        {
            if (argo is Symbol)
            {
                Symbol sarg = (Symbol)argo;

                if (environment.Contains(sarg))
                {
                    v += string.Format(@"{1}.Add({0});
", environment.GetValue(sarg), margs);
                }
                else
                {
                    v += Generate(argo, environment);
                    v += margs + @".Add(retval);
";
                }
            }
            else if (argo is Cons)
            {
                v += Generate(argo, environment);
                v += margs + @".Add(retval);
";
            }
            else
            {
                // load primitive
                v += string.Format(@"{1}.Add({0});
", Printer.WriteToString(argo), margs);
            }
        }
        return v;
    }
开发者ID:mlnlover11,项目名称:MP.LSharp,代码行数:38,代码来源:NewCompiler.cs

示例15: Try

    /// <summary>
    /// (try expression catch [finally])
    /// </summary>
    public static Object Try(Cons args, LSharp.Environment environment)
    {
        string v = "";//"//(try " + Printer.ConsToString(args) + ")" + NewLine +
        string.Format(@"
try
{{
  {0}
}}
catch (Exception e)
{{
  {1}
}}", Generate(args.First(), environment) ,(args.Second() as Symbol == Symbol.NULL) ?
                      "throw" :
                      Generate(args.Second(),environment));
        if  (args.Length() > 2)
        {
            v += string.Format(@"
finally
{{
  {0}
}}
", Generate(args.Third(),environment));
        }
        return v;
    }
开发者ID:mlnlover11,项目名称:MP.LSharp,代码行数:28,代码来源:NewCompiler.cs


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