/// <summary> Helper function: converts a cons list into a native list </summary> public static List <Val> ToNativeList(Val element) { List <Val> results = new List <Val>(); while (element.IsNotNil) { if (element.IsAtom) { throw new LanguageError("Only null-terminated lists of cons cells can be converted to arrays!"); } Cons cons = element.AsCons; results.Add(cons.first); element = cons.rest; } return(results); }
/// <summary> /// Helper function: converts an array of arguments to a cons list. /// Whether it's null-terminated or not depends on the existence of a "." in the penultimate position. /// </summary> public static Val MakeList(List <Val> values) { int len = values.Count; bool dotted = (len >= 3 && values[len - 2].AsSymbolOrNull?.fullName == "."); // the tail should be either the last value, or a cons containing the last value Val result = dotted ? values[len - 1] : len >= 1 ? new Cons(values[len - 1], Val.NIL) : Val.NIL; int iterlen = dotted ? len - 3 : len - 2; for (int i = iterlen; i >= 0; i--) { result = new Cons(values[i], result); } return(result); }
/// <summary> Returns true if the value is a properly nil-terminated cons list </summary> public static bool IsList(Val value) { if (value.IsNil) { return(true); } Cons cons = value.AsConsOrNull; while (cons != null) { if (cons.rest.IsNil) { return(true); } // found our terminating NIL cons = cons.rest.AsConsOrNull; } return(false); }
/// <summary> /// Helper function: converts an enumeration of native values to a proper (nil-terminated) cons list /// </summary> public static Val MakeListFromNative <T> (IEnumerable <T> values) { Cons first = null, last = null; foreach (T value in values) { var newcell = new Cons(new Val(value), Val.NIL); if (first == null) { first = newcell; } else { last.rest = newcell; } last = newcell; } return(first ?? Val.NIL); }
public Val(Cons value) : this() { type = Type.Cons; vcons = value; }
public Vector(Cons elements) : base(elements.ToNativeList()) { }