home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / grs / g / DataBase < prev    next >
Encoding:
Text File  |  1991-05-14  |  5.5 KB  |  196 lines

  1. (* generic database written in GRS *)
  2. (* shows off most of the capabilities of the language *)
  3. (* Guy Verbist April 1991 *)
  4.  
  5.  
  6. write("Enter the attributes of object:\n");
  7. write("Enter 'end' to end.\n");
  8.  
  9. let listof string attribs := [];
  10. let listof string attribtypes := [];
  11.  
  12. let string attrib := "";
  13. let string attribtype := "";
  14.  
  15. loop
  16.    write("Enter attribute name:\n");
  17.    read(attrib);
  18.    exiton(attrib = "end");
  19.    write("Enter attribute type:\n");
  20.    read(attribtype);
  21.    attribs := attrib::attribs;
  22.    attribtypes := attribtype::attribtypes;
  23. endloop;
  24.  
  25. let listof string attribs_copy := attribs;
  26. let listof string attribtypes_copy := attribtypes;
  27.  
  28. let listof string main_attribs := attribs;
  29. let listof string main_types := attribtypes;
  30.  
  31.  
  32.  
  33. (* now create the class *)
  34.  
  35. instanceof class data;
  36.                                         
  37. let string tempstring :=                      
  38.        "data := class.new(\"data\",[{null function hello(){write(\"Hello\n\");};} " ;
  39.  
  40. (*add in methods*)
  41.  
  42. let string methodstring := "";
  43.  
  44. loop
  45.    exiton(attribs_copy = []);
  46.    methodstring := methodstring + ",{null function set_"+head(attribs_copy)+
  47.                                   "("+head(attribtypes_copy)+ " param){"+
  48.                                   "assume "+head(attribtypes_copy)
  49.                                       +" "+head(attribs_copy)+" in "
  50.                                   +head(attribs_copy)+":= param;};}" ;
  51.    attribs_copy := tail(attribs_copy);
  52.    attribtypes_copy := tail(attribtypes_copy);
  53. endloop;
  54.  
  55.  
  56. methodstring := methodstring + "],[{";
  57.  
  58. (*add in slots*)
  59.  
  60. let string slotstring := "";
  61.  
  62. loop
  63.    exiton (attribs = []);
  64.    slotstring := slotstring + head(attribtypes) + " " + head(attribs)+ ";";
  65.    attribtypes := tail(attribtypes);
  66.    attribs := tail(attribs);
  67. endloop;
  68.  
  69. slotstring := slotstring+"}]);";
  70.  
  71. exec(compile(tempstring+methodstring+slotstring));
  72.                           
  73. (*now add show method for each attribute*)
  74.  
  75. attribs_copy := main_attribs;
  76. attribtypes_copy := main_types;
  77.  
  78. loop
  79.    exiton(attribs_copy = []);
  80.    methodstring := "null function show_" + head(attribs_copy)
  81.                    + "(){assume " + head(attribtypes_copy) + " "
  82.                    + head(attribs_copy) + " in write(\""
  83.                    + head(attribs_copy) + " is \"," + head(attribs_copy)
  84.                    + ",\"\n\");};" ;
  85.  
  86.    let (null) expression new_method := compile(methodstring);
  87.  
  88.  
  89.    exec(compile("data.add_method(new_method);"));
  90.  
  91.  
  92.    attribs_copy := tail(attribs_copy);
  93.    attribtypes_copy := tail(attribtypes_copy);
  94. endloop;
  95.  
  96. write("Type 'db();' to access the database.\n");
  97.  
  98.  
  99. null function db()
  100. {
  101.    integer function menu(listof string menu)
  102.    {
  103.       let integer options := 0;
  104.  
  105.       write("\n");
  106.       loop
  107.          exiton(menu = []);
  108.          options := options +1;
  109.          write(options,") ",head(menu),"\n");
  110.          menu := tail(menu);
  111.       endloop;
  112.       write("\n");
  113.  
  114.       integer selection;
  115.  
  116.       loop
  117.          write("Select an option between 1 and ",options," :");
  118.          read(selection);
  119.          exiton( (selection > 0) and (selection <= options));
  120.       endloop;
  121.       write("\n");
  122.       return selection;
  123.    };
  124.  
  125.    integer choice;
  126.    string entry_name, attrib_name, attrib_val;
  127.  
  128.    loop
  129.       choice := menu(["add an entry", "delete an entry", "modify entry",
  130.                       "show an entry", "show all entries", 
  131.                       "show all attributes", "quit"]);
  132.       exiton(choice=7);
  133.  
  134.       if (choice=1) then
  135.          write("Please enter name of entry : ");
  136.          read(entry_name);
  137.          exec(compile("#data "+entry_name+";"));
  138.       endif;
  139.  
  140.       (* these are declared locally to the db() function so cannot be
  141.          accessed from the command line, and will not conflict with anything
  142.          else *)
  143.  
  144.       if (choice=2) then
  145.          write("Please enter name of entry : ");
  146.          read(entry_name);
  147.          exec(compile("sys_delete_object("+entry_name+",true);"));
  148.       endif;
  149.  
  150.       if (choice=3) then
  151.          (*modify*)
  152.          write("Please enter name of entry : ");
  153.          read(entry_name);
  154.          write("Please enter name of attribute to change : ");
  155.          read(attrib_name);
  156.          write("and the new value for that attribute : ");
  157.          read(attrib_val);
  158.          exec(compile(entry_name+".set_"+attrib_name+"("+attrib_val+");"));
  159.       endif;
  160.       if (choice=4) then
  161.          (*show*)
  162.          write("Please enter name of entry : ");
  163.          read(entry_name);
  164.          attribs_copy := main_attribs;
  165.          string meth_str;
  166.          exec(compile("loop exiton(attribs_copy = []);" +
  167.                       "meth_str := \".show_\" + head(attribs_copy)+\"();\";"
  168.                       + "exec(compile(entry_name + meth_str));"
  169.                       + "attribs_copy := tail(attribs_copy);"
  170.                       + "endloop;" ));
  171.       endif;
  172.       if (choice=5) then
  173.          (*show*)
  174.          exec(compile("foreach inst in data.get_instances() do {"
  175.                       + "attribs_copy := main_attribs;"
  176.                       + "loop exiton(attribs_copy = []);" +
  177.                       "meth_str := \"show_\" + head(attribs_copy)+\"();\";"
  178.                       + "exec(compile(\"inst.\"+ meth_str));"
  179.                       + "attribs_copy := tail(attribs_copy);"
  180.                       + "endloop;};" ));
  181.       endif;
  182.  
  183.       if (choice=6) then
  184.          listof string slts;
  185.          let integer sltcount := 0;
  186.          foreach slt in data.get_slots() do
  187.          {
  188.             sltcount := sltcount +1;
  189.             write("Attribute ",sltcount," is ",slt,".\n"); 
  190.          };         
  191.       endif;
  192.  
  193.    endloop;
  194. };
  195.  
  196.