home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / CLisp / LispRuntime.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  3.9 KB  |  220 lines

  1.  
  2. using System;
  3. using System.Collections;
  4.  
  5. public class CList
  6. {
  7.     internal ArrayList la;
  8.     
  9.     public CList()
  10.     {
  11.     la = new ArrayList();
  12.     }
  13.  
  14.     public CList(CList l)
  15.     {
  16.     la = new ArrayList(l.la);
  17.     }
  18.     
  19.  
  20.     public int Count 
  21.     {
  22.     get 
  23.         {
  24.         return la.Count;
  25.         }
  26.     }
  27.     
  28.     public Object Item(int index)
  29.     {
  30.     return la[index];
  31.     }
  32.     
  33.     public void Add(Object o)
  34.     {
  35.     la.Add(o);
  36.     }
  37.     
  38.     
  39. }
  40.  
  41.  
  42. public class LispRuntime
  43. {
  44.     public static CList Car(CList l)
  45.     {
  46.     if (l.la.Count == 0)
  47.         return new CList();
  48.     if (l.la[0] is CList)
  49.         return (CList)l.la[0];
  50.     CList newlist = new CList();
  51.     newlist.Add(l.la[0]);
  52.     return newlist;
  53.     }
  54.  
  55.     public static CList Cdr(CList l)
  56.     {
  57.     if (l.la.Count == 0)
  58.         return new CList();
  59.     
  60.     CList newlist = new CList(l);
  61.     newlist.la.RemoveRange(0, 1);
  62.     return newlist;
  63.     }
  64.     
  65.     public static CList Cons(CList l, CList r)
  66.     {
  67.     CList newlist = new CList(l);
  68.     for(int i = 0; i < r.Count; i++)
  69.         newlist.la.Add(r.la[i]);
  70.     return newlist;
  71.     }
  72.  
  73.     public static CList Subst(CList r, CList f, CList exp)
  74.     {
  75.     String rstr = (String)r.la[0];
  76.     String fstr = (String)f.la[0];
  77.     
  78.     for (int i = 0; i < exp.la.Count; i++){
  79.         if (exp.la[i] is CList){
  80.         exp.la[i] = Subst(r, f, (CList)exp.la[i]);
  81.         }
  82.         else if (exp.la[i] is String)
  83.         if(((String)exp.la[i]).CompareTo(fstr) == 0){
  84.             exp.la[i] = rstr;
  85.         }
  86.     }
  87.     return exp;
  88.     }
  89.     
  90.     private static CList InitMultiple(String str, ref int index)
  91.     {
  92.     CList newList = new CList();
  93.     //String tempstr = str.SubString(1);
  94.     //tempstr = str.SubString(0, str.Length - 1);
  95.     String temp = null;
  96.     bool InState = false;
  97.     index++;
  98.     
  99.     while (index < str.Length){
  100.         if(Char.IsWhiteSpace(str[index])){
  101.         if (InState){
  102.             newList.Add(temp);
  103.             temp = null;
  104.         }
  105.         index++;
  106.         InState = false;
  107.         }
  108.         else if(str[index] == '('){
  109.         newList.Add(InitMultiple(str, ref index));
  110.         }
  111.         else if(str[index] == ')'){
  112.         if(InState)
  113.             newList.Add(temp);
  114.         index++;
  115.         return newList;
  116.         }
  117.         else{
  118.         InState = true;
  119.         temp = temp + str[index];
  120.         index++;
  121.         }
  122.     }
  123.     Console.WriteLine("IlFormed Parens");
  124.     return null;
  125.     }
  126.  
  127.     public static CList Init(String item)
  128.     {
  129.     CList newlist = new CList();
  130.     int index = 0;
  131.  
  132.     if (item.CompareTo(String.Empty) == 0)
  133.         return newlist;                 //Empty CList is NIL
  134.     if(item[0] == '('){
  135.         return InitMultiple(item, ref index);
  136.     }
  137.     else
  138.         newlist.Add(item);
  139.  
  140.     return newlist;
  141.     }
  142.     
  143.  
  144.     public static void Print(CList item)
  145.     {
  146.     if (item.Count == 0){
  147.         Console.Write("NIL");
  148.         return;
  149.     }
  150.     
  151.     if(item.Count > 1){
  152.         Console.Write("(");
  153.         for (int i = 0; i < item.Count; i++){
  154.         if (item.la[i] is CList)
  155.             Print((CList)item.la[i]);
  156.         else
  157.             Console.Write((String)item.la[i]);
  158.         if (i < item.Count - 1)
  159.             Console.Write(" ");
  160.         }
  161.         Console.Write(")");
  162.     }
  163.     else{
  164.         if(item.la[0] is CList)
  165.         Print((CList)item.la[0]);
  166.         else
  167.         Console.Write((String)item.la[0]);
  168.     }
  169.      
  170.     }
  171.  
  172.     public static void PrintBool(bool val)
  173.     {
  174.     if (val)
  175.         Console.WriteLine("T");
  176.     else 
  177.         Console.WriteLine("NIL");
  178.     }
  179.     
  180.  
  181.     public static bool IsNull(CList value)
  182.     {
  183.     return (value.Count == 0);
  184.     }
  185.  
  186.     public static bool IsAtom(CList value)
  187.     {
  188.     return (value.Count == 1);
  189.     }
  190.     
  191.     public static int ToInt(CList l)
  192.     {
  193.     if (l.la.Count == 0)
  194.         Console.WriteLine("Somethings wrong");
  195.     
  196.     if (l.la[0] is String){
  197.         try {
  198.         return Convert.ToInt32((String)l.la[0]);
  199.         }
  200.         catch(FormatException ){
  201.         return 0;
  202.         }
  203.     }
  204.     else{
  205.         Console.WriteLine("Runtime Error 1: Cannot Convert to Int");
  206.         return 0;
  207.     }
  208.     }
  209.     
  210.     public static CList ToList(int Val)
  211.     {
  212.     CList c = new CList();
  213.     c.la.Add(Convert.ToString(Val));
  214.     return c;
  215.     }
  216.     
  217.     
  218. }
  219.  
  220.