home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / SOURCE / LIB.C < prev    next >
C/C++ Source or Header  |  1996-06-16  |  9KB  |  494 lines

  1. /* Copyright 1991 Digital Equipment Corporation.
  2.  ** All Rights Reserved.
  3.  *****************************************************************/
  4. /*     $Id: lib.c,v 1.2 1994/12/08 23:26:47 duchier Exp $     */
  5.  
  6. #ifndef lint
  7. static char vcid[] = "$Id: lib.c,v 1.2 1994/12/08 23:26:47 duchier Exp $";
  8. #endif /* lint */
  9.  
  10. /* VERSION of Wild-LIFE for calling from C */
  11. /*  RM: Mar 31 1993  */
  12.  
  13. #include "extern.h"
  14. #include "trees.h"
  15. #include "print.h"
  16. #include "parser.h"
  17. #include "info.h"
  18. #include "login.h"
  19. #include "lefun.h"
  20. #ifndef OS2_PORT
  21. #include "built_ins.h"
  22. #else
  23. #include "built_in.h"
  24. #endif
  25. #include "types.h"
  26. #include "copy.h"
  27. #include "token.h"
  28. #ifndef OS2_PORT
  29. #include "interrupt.h"
  30. #else
  31. #include "interrup.h"
  32. #endif
  33. #include "error.h"
  34. #include "modules.h" /*  RM: Jan  8 1993  */
  35.  
  36. #include "c_life.h"
  37.  
  38.  
  39. #ifdef X11
  40. #include "xpred.h"
  41. #endif
  42.  
  43. #ifdef SOLARIS
  44. #include <stdlib.h>
  45. static unsigned int libseed;
  46. #endif
  47.  
  48. long noisy=TRUE;
  49. long file_date=3;
  50. long types_done=FALSE;
  51.  
  52. #ifndef OS2_PORT
  53. struct tms life_start,life_end;
  54. #else
  55. float life_start,life_end;
  56. #endif
  57.  
  58. float garbage_time=0;
  59.  
  60. extern int rand_array[256];
  61.  
  62.  
  63. int c_query_level;
  64.  
  65. extern jmp_buf env;
  66.  
  67.  
  68.  
  69. char **group_features(f,n)
  70.      char **f;
  71.      ptr_node n;
  72. {
  73.   *f=NULL;
  74.   if(n) {
  75.     if(n->left)
  76.       f=group_features(f,n->left);
  77.     *f=n->key;
  78.     f++;
  79.     if(n->right)
  80.       f=group_features(f,n->right);
  81.   }
  82.   
  83.   return f;
  84. }
  85.  
  86.  
  87. void exit_if_true(exitflag)
  88.      long exitflag;
  89. {
  90.   if (exitflag) {
  91.     printf("\n\n*** Execution is not allowed to continue.\n");
  92.     /*exit_life(TRUE);*/
  93.     exit(1);
  94.   }
  95. }
  96.  
  97.  
  98.  
  99. /* I/O initialization */
  100. void init_io()
  101. {
  102. #ifndef OS2_PORT
  103.   struct stat buffer;
  104.   
  105.   fstat(fileno(stdin), &buffer);
  106.   /* True iff stdin is from a terminal */
  107.   stdin_terminal=(S_IFCHR & buffer.st_mode)!=0;
  108.   input_state=NULL;
  109.   stdin_state=NULL;
  110.   output_stream=stdout;
  111. #else
  112.   stdin_terminal=TRUE ;
  113.   input_state=NULL;
  114.   stdin_state=NULL;
  115.   output_stream=stdout;
  116. #endif
  117. }
  118.  
  119.  
  120. extern char prompt_buffer[];
  121.  
  122.  
  123. /* Initial state of system to begin a query */
  124. void init_system()
  125. {
  126. #ifdef X11
  127.   x_window_creation=FALSE;
  128. #endif
  129.   stack_pointer=mem_base;
  130.   goal_stack=NULL;
  131.   choice_stack=NULL;
  132.   undo_stack=NULL; /* 7.8 */
  133.   var_tree=NULL;
  134.  
  135.   /*  RM: Oct 13 1993  */
  136.   if(current_module==user_module)
  137.     prompt=PROMPT;
  138.   else {
  139.     prompt=prompt_buffer;
  140.     sprintf(prompt_buffer,"%s%s",current_module->module_name,PROMPT);
  141.   }
  142.     
  143.   resid_aim=NULL;
  144.   exit_if_true(!memory_check());
  145.   
  146. #ifdef X11
  147.   /*  RM: Dec 15 1992  */
  148.   xevent_list=stack_nil();
  149. #endif
  150.   
  151.   init_global_vars(); /*  RM: Feb 15 1993  */
  152. }
  153.  
  154.  
  155. extern int rand_array[];
  156.  
  157.  
  158.  
  159. /******** WFINIT(argc,argv)
  160.   This routine contains the Read-Solve-Prlong loop.
  161.   */
  162.  
  163. WFInit(argc, argv)
  164.      
  165.      long argc;
  166.      char **argv;
  167. {
  168.   ptr_psi_term s;  
  169.   ptr_stack save_undo_stack;
  170.   long sort,exitflag;
  171.   int c; /* 21.12 (prev. char) */ 
  172.   
  173.   
  174.   int i;
  175. #ifdef SOLARIS
  176.   for(i=0;i<256;i++)
  177.     rand_array[i]=rand_r(&libseed);
  178. #else
  179.   for(i=0;i<256;i++)
  180.     rand_array[i]=random();
  181. #endif
  182.   
  183.   
  184.   arg_c=argc;
  185.   arg_v=argv;
  186.   
  187.   quietflag = TRUE; /*  RM: Mar 31 1993  */
  188.   
  189.   init_io();
  190.   init_memory();
  191.   exit_if_true(!mem_base || !other_base);
  192.   assert(stack_pointer==mem_base); /* 8.10 */
  193.   init_copy();
  194.   assert(stack_pointer==mem_base); /* 8.10 */
  195.   init_print();
  196.   assert(stack_pointer==mem_base); /* 8.10 */
  197.   
  198.   /* Timekeeping initialization */
  199.   tzset();
  200.   times(&life_start);
  201.   assert(stack_pointer==mem_base); /* 8.10 */
  202.   
  203.   init_modules(); /*  RM: Jan  8 1993  */
  204.   
  205.   init_built_in_types();
  206.   assert(stack_pointer==mem_base); /* 8.10 */
  207. #ifdef X11
  208.   x_setup_builtins();
  209.   assert(stack_pointer==mem_base); /* 8.10 */
  210. #endif
  211.   init_interrupt();
  212.   assert(stack_pointer==mem_base); /* 8.10 */
  213.   title();
  214.   assert(stack_pointer==mem_base); /* 8.10 */
  215.   init_trace();
  216.   noisy=FALSE;
  217.   
  218.   assert(stack_pointer==mem_base); /* 8.10 */
  219.   
  220.   
  221.   set_current_module(user_module); /*  RM: Jan 27 1993  */
  222.   
  223.   /* Read in the .set_up file */
  224.   init_system();
  225.   
  226. #ifdef ARITY  /*  RM: Mar 29 1993  */
  227.   arity_init();
  228. #endif
  229.   
  230. #ifndef OS2_PORT   
  231.   open_input_file("+SETUP+");
  232. #else
  233.   open_input_file("~SETUP");
  234. #endif
  235.   push_goal(load,input_state,file_date,heap_copy_string("+SETUP+"));
  236.   file_date+=2;
  237.   main_prove();
  238.   
  239.   
  240.   setjmp(env);
  241.   /* printf("%ld\n",(long)(stack_pointer-mem_base)); */ /* 8.10 */
  242.   init_system(); 
  243.   init_trace();
  244.   begin_terminal_io();
  245.   var_occurred=FALSE;
  246.   save_undo_stack=undo_stack;
  247.   stdin_cleareof();
  248.   
  249.   c_query_level=0;
  250. }
  251.  
  252.  
  253.  
  254. int WFInput(query)
  255.      
  256.      char *query;
  257. {
  258.   ptr_psi_term t;
  259.   long sort;
  260.   parse_block pb;
  261.   int result=WFno;
  262.   ptr_stack save_undo_stack;
  263.   ptr_choice_point old_choice;
  264.   
  265.   
  266.   save_undo_stack=undo_stack;
  267.   old_choice=choice_stack;
  268.  
  269.   
  270.   if(!strcmp(query,".")) {
  271.     reset_stacks();
  272.     result=WFyes;
  273.     c_query_level=0;
  274.   }
  275.   else {
  276.     if(!strcmp(query,";")) {
  277.       sort=QUERY;
  278.       push_goal(fail,NULL,NULL,NULL);
  279.     }
  280.     else {
  281.       /* Parse the string in its own state */
  282.       save_parse_state(&pb);
  283.       init_parse_state();
  284.       stringparse=TRUE;
  285.       stringinput=query;
  286.       
  287.       /* old_var_occurred=var_occurred; */
  288.       var_occurred=FALSE;
  289.       t=stack_copy_psi_term(parse(&sort));
  290.       
  291.       /* Main loop of interpreter */
  292.       if(sort==QUERY) {
  293.     ignore_eff=TRUE;
  294.     goal_count=0;
  295.         
  296.     push_choice_point(c_what_next,c_query_level,NULL,NULL);
  297.     c_query_level++;
  298.     push_goal(c_what_next,c_query_level,var_occurred,NULL);
  299.     push_goal(prove,t,DEFRULES,NULL);
  300.     /* reset_step(); */
  301.       }
  302.       else if (sort==FACT) {
  303.     assert_first=FALSE;
  304.     assert_clause(t);
  305.     if(assert_ok)
  306.       result=WFyes;
  307.     undo(save_undo_stack);
  308.     var_occurred=FALSE;
  309.     encode_types();
  310.       }
  311.     }
  312.  
  313.     if(sort==QUERY) {
  314.       start_chrono();
  315.       main_prove();
  316.       
  317.       if(goal_stack && goal_stack->type==c_what_next) {
  318.       
  319.     if((int)(goal_stack->a)==c_query_level)
  320.       if(choice_stack==old_choice) {
  321.         result=WFyes;
  322.         c_query_level--;
  323.       }
  324.       else
  325.         result=WFmore;
  326.     else {
  327.       result=WFno;
  328.       c_query_level--;
  329.     }
  330.     
  331.     goal_stack=goal_stack->next;
  332.       }
  333.     }
  334.   }
  335.   
  336.   return result;
  337. }
  338.  
  339.  
  340.  
  341. PsiTerm WFGetVar(name)
  342.      
  343.      char *name;
  344. {
  345.   ptr_psi_term result=NULL;
  346.   ptr_node n;
  347.   
  348.   n=find(strcmp,name,var_tree);
  349.   if(n) {
  350.     result=(ptr_psi_term)n->data;
  351.     if(result)
  352.       deref_ptr(result);
  353.   }
  354.   
  355.   return result;
  356. }
  357.  
  358.  
  359. int WFfeature_count_loop(n)
  360.      
  361.      ptr_node n;
  362. {
  363.   int result=0;
  364.  
  365.   if(n) {
  366.     if(n->left)
  367.       result+=WFfeature_count_loop(n->left);
  368.     result++;
  369.     if(n->right)
  370.       result+=WFfeature_count_loop(n->right);
  371.   }
  372.  
  373.   return result;
  374. }
  375.  
  376.  
  377.  
  378. int WFFeatureCount(psi)
  379.  
  380.      ptr_psi_term psi;
  381. {
  382.   int result=0;
  383.  
  384.   if(psi) {
  385.     deref_ptr(psi);
  386.     result=WFfeature_count_loop(psi->attr_list);
  387.   }
  388.   
  389.   return result;
  390. }
  391.  
  392.  
  393.  
  394. char *WFType(psi)
  395.  
  396.      ptr_psi_term psi;
  397. {
  398.   char *result=NULL;
  399.   if(psi) {
  400.     deref_ptr(psi);
  401.     result=psi->type->keyword->combined_name;
  402.   }
  403.   return result;
  404. }
  405.  
  406.  
  407.  
  408. char **WFFeatures(psi)
  409.  
  410.      ptr_psi_term psi;
  411. {
  412.   char **features=NULL;
  413.   int n;
  414.   
  415.   if(psi) {
  416.     deref_ptr(psi);
  417.     
  418.     n=WFfeature_count_loop(psi->attr_list);
  419.     if(n) {
  420.       features=(char **)malloc((n+1)*sizeof(char *));
  421.       group_features(features,psi->attr_list);
  422.     }
  423.   }
  424.  
  425.   return features;
  426. }
  427.  
  428.  
  429.  
  430.  
  431. double WFGetDouble(psi,ok)
  432.      ptr_psi_term psi;
  433.      int *ok;
  434. {
  435.   double value=0.0;
  436.   
  437.   if(ok)
  438.     *ok=FALSE;
  439.   
  440.   if(psi) {
  441.     deref_ptr(psi);
  442.     
  443.     if(sub_type(psi->type,real) && psi->value) {
  444.       value= *((double *)psi->value);
  445.       if(ok)
  446.     *ok=TRUE;
  447.     }
  448.   }
  449.   return value;
  450. }
  451.  
  452.  
  453.  
  454. char *WFGetString(psi,ok)
  455.      ptr_psi_term psi;
  456.      int *ok;
  457. {
  458.   char *value=NULL;
  459.   
  460.   if(ok)
  461.     *ok=FALSE;
  462.   
  463.   if(psi) {
  464.     deref_ptr(psi);
  465.     
  466.     if(sub_type(psi->type,quoted_string) && psi->value) {
  467.       value=(char *)psi->value;
  468.       if(ok)
  469.     *ok=TRUE;
  470.     }
  471.   }
  472.   return value;
  473. }
  474.  
  475.  
  476.  
  477. PsiTerm WFGetFeature(psi,feature)
  478.  
  479.      ptr_psi_term psi;
  480.      char *feature;
  481. {
  482.   ptr_psi_term  result=NULL;
  483.   ptr_node n;
  484.  
  485.   if(psi && feature) {
  486.     deref_ptr(psi);
  487.     n=find(featcmp,feature,psi->attr_list);
  488.     if(n)
  489.       result=(PsiTerm)n->data;
  490.   }
  491.   
  492.   return result;
  493. }
  494.