home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd3.lzh / SBPROLOG2.2 / MODLIB / MODLIB_SRC / $readloop.P.unix < prev    next >
Text File  |  1991-08-10  |  13KB  |  412 lines

  1. /************************************************************************
  2. *                                    *
  3. *    The SB-Prolog System                        *
  4. *    Copyright SUNY at Stony Brook, 1986                *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25.  
  26. /* This is the top-level read-eval-print loop for Prolog. 
  27.  
  28.    $readloop also sets up some global values to allow it to handle
  29.    break levels. It defines break/0 and abort/0. (repeat/0 is also
  30.    defined here, for lack of a better place for now.)
  31.  
  32. $readl_export([break/0,abort/0,repeat/0]).
  33.  */
  34.  
  35. $readloop :- 
  36.     $init_sys,
  37.     $load_mod($io),
  38.     $load_mod($read),
  39.     $load($prorc),call($prorc),
  40.     /* default size of hash table for assert_indexing */
  41.     $globalset('_$tab_size'(17)),
  42.     $globalset('_$abort_cutpoint'(0)),
  43.     $globalset('_$break_level'(0)),
  44.     $writename('SB-Prolog Version 2.2'),$nl,
  45.     $readlp1.
  46.  
  47. $readlp1 :- repeat,'_$savecp'(Cp),
  48.     $globalset_a('_$abort_cutpoint'(Cp),Cp),
  49.     $flags(2,0), /* set flag to intercept stack overflow */
  50.     $globalset('_$break_level'(0)),
  51.     $readl_userio(I,O),
  52.     $writename('| ?- '),$read(X, Vars),
  53.     $readl_resetio(I,O),
  54.     ($readl_stop(X),!;$readl_procq(X,Vars)).
  55.  
  56. $readl_stop(halt) :- halt. /* quit entire system, regardless of break level */
  57. $readl_stop(end_of_file).    /* pop a break level, out if at top */
  58.  
  59. $readl_procq(X,[]) :- !,$readl_docall(X),!,
  60.     $readl_userio(I,O),$writename(yes),$nl,$readl_resetio(I,O),fail.
  61.  
  62. $readl_procq(X,Vars) :- $readl_docall(X),
  63.     $readl_userio(I,O),$readl_printans(Vars),
  64.     $readl_ifnomo(I,O),!,$writename(yes),$nl,$readl_resetio(I,O),fail.
  65.  
  66. $readl_ifnomo(_,_) :- $get0(10),!.
  67. $readl_ifnomo(I,O) :- $readl_ifnomo(I,O),$readl_resetio(I,O),fail.
  68.  
  69. $readl_docall(X) :- call(X).
  70. $readl_docall(_) :- $readl_userio(I,O),$writename(no),$nl,
  71.     $readl_resetio(I,O),fail.
  72.  
  73. $readl_printans([]) :- !.
  74. $readl_printans([=(Name, Val)|Tail]) :-
  75.         $nl,$writename(Name),
  76.         $writename(' = '),
  77.         $write(Val),
  78.         $readl_printans(Tail).
  79.  
  80. break :- '_$break_level'(Blevel),
  81.     Nblevel is Blevel+1, $globalset('_$break_level'(Nblevel)),
  82.     $readl_userio(I,O),
  83.     $writename('[ Break (level '),$writename(Nblevel),
  84.     $writename(') ]'),$nl,
  85.     $readl_brklp1,
  86.     $globalset('_$break_level'(Blevel)),
  87.     $writename('[ End break (level '),$writename(Nblevel),
  88.     $writename(') ]'),$nl,
  89.     $readl_resetio(I,O).    /* should we reset here ? */
  90.  
  91. $readl_brklp1 :- repeat,'_$break_level'(Blevel),
  92.     $writename(Blevel),$writename(': ?- '),
  93.     $read(X, Vars),
  94.     ($readl_stop(X),!;$readl_procq(X,Vars)).
  95.  
  96. abort :- '_$abort_cutpoint'(Cp),'_$cutto'(Cp),fail.
  97.  
  98. $readl_userio(Oi,Oo) :- 
  99.     $seeing(Oi),$see(user),
  100.     $telling(Oo),$tell(user).
  101.  
  102. $readl_resetio(Oi,Oo) :- $see(Oi),$tell(Oo).
  103.  
  104. repeat.
  105. repeat :- repeat.
  106.  
  107. $flags(Which,What) :- '_$builtin'(113).
  108.  
  109.  
  110. /* These are the module handling routines. The main routine is the
  111.    undefined_pred interrupt handler. The system keeps a table of modules
  112.    and routines that are needed from each. When a predicate is found to
  113.    be undefined, the table is searched to see if it is defined by 
  114.    some module. If so, that module is loaded (if it hasn't been previously
  115.    loaded) and the association is made between the routine name as 
  116.    defined in the module, and the routine name as used by the invoker.
  117.  
  118.    The table of modules and needed routines is:
  119.    defined_mods(modname,[pred1/arity1,...,predn/arityn]).
  120.      where modname is the name of the module. The module exports n
  121.     predicate definitions. The first exported pred is of arity 
  122.     arity1, and needs to be invoked by the name of pred1.
  123.  
  124.    The table of modules that have already been loaded is:
  125.    loaded_mods(modname).
  126.  
  127.    A module is a file of predicate definitions. Consider a module name
  128.    `mod'. It contains a single fact, named mod_export, that is true of
  129.    the list of predicate/arity's that are exported. 
  130.    e.g. mod_export([mod_p1/2,mod_p2/4]).
  131.    For each module, m, which contains predicates needed by this module,
  132.    there is a mod_use fact, describing what module is needed and the names
  133.    of the predicates defined there that are needed. For example, if module
  134.    mod needs to import predicates from mod m, there would be a fact:
  135.    mod_use(m,[mod_ip1/2,mod_ip2/4]), where m is a module that exports two
  136.    predicates: one 2-ary and one 4-ary. This list corresponds to the export
  137.    list of module m.
  138.  
  139. */
  140. $init_sys_export([$define_mod/2,$load/1,$load_mod/1]).
  141.  
  142. $init_sys :- 
  143.     $load($assert),
  144.     $load($db),
  145.     $load($dbcmpl),
  146.     $load($bio),
  147.     $load($bmeta),
  148.     $load($name),
  149.     $load($blist),
  150.     $load($call),
  151.     $load($glob),
  152.     $assert_union(loaded_mods(_),$loaded_mods(_)), /* nec for compile */
  153.     $assert_abolish_i(defined_mods(_,_)),    /* nec for compile [a] */
  154.     $globalset('_$nofile_msg'(1)).
  155.  
  156. $loaded_mods($buff). /* with readloop */
  157. $loaded_mods($init_sys). /* with readloop */
  158. $loaded_mods($assert).
  159. $loaded_mods($db).
  160. $loaded_mods($dbcmpl).
  161. $loaded_mods($bio).
  162. $loaded_mods($bmeta).
  163. $loaded_mods($name).
  164. $loaded_mods($blist).
  165. $loaded_mods($call).
  166. $loaded_mods($glob).
  167.  
  168.  
  169. $define_mod(Mod,Implist) :- defined_mods(Mod,Implist),!. /* no-op if there */
  170. $define_mod(Mod,Implist) :- $assert(defined_mods(Mod,Implist)).
  171.  
  172. /* the undefined_pred interrupt handler: */
  173.  
  174. '_$undefined_pred'(Term) :-
  175.     not(not('_$definable'(Term))),'_$call'(Term).
  176.  
  177. '_$definable'(Term) :- 
  178.     $functor0(Term,Pred),$arity(Term,Arity),
  179. /*      $telling(F),$tell(user),$writename('loading: '),$writename(Pred),
  180.       $writename('/'),$writename(Arity),$nl,$tell(F), */
  181.     defined_mods(Module_name,Pred_name_list),  /* for each module.. */
  182.     $init_membernv(Pred/Arity,Pred_name_list), 
  183.         /* is needed pred in this one? */
  184.     !,                /* yes! */
  185.     $load_mod(Module_name),        /* go load it if necessary */
  186.     $name(Module_name,Mod_name_chars),
  187.     $append(Mod_name_chars,"_export",Exp_name_chars),
  188.     $name(Exp_name,Exp_name_chars),
  189.     $bldstr(Exp_name,1,Modcall),$arg(1,Modcall,Explist),
  190.     '_$call'(Modcall),
  191.     $load_preds(Explist,Pred_name_list,Module_name). 
  192.  
  193. '_$definable'(Term) :-    /* no module to define pred */
  194.     $functor0(Term,Pred),
  195.     $load(Pred),!,        /* file exists */
  196.     (not($pred_undefined(Term))    /* file defines pred */
  197.      ;
  198.      $arity(Term,Arity),
  199.      $telling(Fi),$tell(user),
  200.      $writename(Pred),$writename('/'),$writename(Arity),
  201.      $writename(' not defined by file'),$nl,$tell(Fi),fail
  202.     ).
  203.  
  204. '_$definable'(Term) :- 
  205.     '_$nofile_msg'(1),    /* if message is to be displayed */
  206.     $telling(Fi),$tell(user),
  207.     $writename('No file to define '),
  208.     $functor0(Term,Pred),$arity(Term,Arity),
  209.     $writename(Pred),$writename('/'),$writename(Arity),$nl,$tell(Fi),fail.
  210.  
  211. $load_mod(Module_name) :-
  212.     loaded_mods(Module_name),!.    /* already loaded, noop */
  213. $load_mod(Module_name) :-
  214. /*       $telling(Fi),$tell(user),$writename('load module: '),
  215.       $writename(Module_name),$nl,$tell(Fi), */
  216.     $load(Module_name),!,         /* now loaded */
  217.     $assert(loaded_mods(Module_name)).
  218.     /*    $load_sub_mods(Module_name). */
  219. $load_mod(Module_name) :-        /* no such file */
  220.     $telling(Fi),$tell(user),
  221.     $writename('No file to define module '),
  222.     $writename(Module_name),$nl,$tell(Fi),fail.
  223.  
  224. /* $load_sub_mods(Module_name) :-
  225.     $name(Module_name,Mod_name_chars),
  226.     $append(Mod_name_chars,"_use",Use_name_chars),
  227.     $name(Use_name,Use_name_chars),
  228.     $bldstr(Use_name,2,Modusecall),
  229.     not($pred_undefined(Modusecall)),
  230.     $arg(1,Modusecall,M),$arg(2,Modusecall,U),
  231.     '_$call'(Modusecall),not(defined_mods(M,U)),!,
  232.     $assert_union(defined_mods(_,_),Modusecall).
  233.  
  234. $load_sub_mods(_). */
  235.  
  236. $load_preds([],[],_) :- !.
  237. $load_preds([],[_|_],Mod) :- !,
  238.     $telling(Fi),$tell(user),
  239.     $writename('Illegal use list for module: '),
  240.     $writename(Mod),$nl,$tell(Fi),fail.
  241. $load_preds([_|_],[],Mod) :- !,
  242.     $telling(Fi),$tell(user),
  243.     $writename('Illegal use list for module: '),
  244.     $writename(Mod),$nl,$tell(Fi),fail.
  245. $load_preds([Inname|Explist],[Inname|Pred_name_list],Mod) :- !,
  246.     $load_preds(Explist,Pred_name_list,Mod).
  247. $load_preds([Inname/Arity|Explist],[Outname/Arity|Pred_name_list],
  248.         Mod) :- !,
  249.     $bldstr(Outname,Arity,Outstr),$bldstr(Inname,Arity,Instr),
  250.     ($pred_undefined(Outstr),!,$assert_union(Outstr,Instr),
  251.         $load_preds(Explist,Pred_name_list,Mod)
  252.     ;
  253.         $telling(Fi),$tell(user),
  254.         $writename('Attempt to redefine '),$writename(Outname),
  255.         $writename('/'),$writename(Arity),$nl,
  256.         $tell(Fi),
  257.         $load_preds(Explist,Pred_name_list,Mod)
  258.     ).
  259. $load_preds([Inname/Inarity|Explist],[Outname/Outarity|Pred_name_list],
  260.         Mod) :-
  261.     $telling(Fi),$tell(user),
  262.     $writename('Incorrect import arity: '),$writename(Outname),
  263.     $writename('/'),$writename(Outarity),$nl,
  264.     $tell(Fi),
  265.     $load_preds(Explist,Pred_name_list,Mod),
  266.     fail.
  267.  
  268.  
  269. $init_membernv(Pa,[Tpa|_]) :- nonvar(Tpa),Pa=Tpa.
  270. $init_membernv(Pa,Pn_list) :- nonvar(Pn_list),
  271.     Pn_list=[_|Pn_tail],$init_membernv(Pa,Pn_tail).
  272.  
  273.  
  274. /* the keyboard interrupt handler */
  275.  
  276. '_$keyboard_int'(Call) :- break,'_$call'(Call).
  277.  
  278. /* the general interrupt handler! */
  279.  
  280. '_$interrupt'(Call,Code) :-
  281.     Code =:= 0 -> '_$undefined_pred'(Call);
  282.       (Code =:= 1 -> '_$keyboard_int'(Call);
  283.           (Code =:= 2 -> 
  284.         $tell(user),$writename('Stack Overflow!!'),$nl,abort
  285.         ;
  286.         $writename('Illegal interrupt code'),halt
  287.           )
  288.       ).
  289.  
  290. /********************************************************/
  291. /* This is the dynamic load routine. It gets the SIMPATH global variable
  292. and uses it to determine what files to try to load. */
  293.  
  294. load(File) :- $load(File).    /* for now */
  295. $load(File) :- $buff_code(File,0,6 /*gb*/,0'/) -> $loadqual(File,0);
  296.             not(not($nnload(File))).
  297.  
  298. $loadqual(File,Rc) :- '_$builtin'(11).
  299.  
  300. $nnload(File) :- 
  301.     $conlength(File,Flen),
  302.     $readl_simpath(Dir),
  303.     $conlength(Dir,Dlen),Wlen is Flen+Dlen+1,
  304.     $alloc_heap(Wlen,Wname),
  305.     $substring(0,Dlen,Dir,0,Wname,Dlen),
  306.     $substring(0,1,'/',Dlen,Wname,Floc),
  307.     $substring(0,Flen,File,Floc,Wname,Wlen),
  308.     $loadqual(Wname,0),! 
  309.     /*,$writename('loaded: '),$writename(Wname),$nl */ 
  310.     .
  311.  
  312. $readl_simpath(Dir) :- $getenv('SIMPATH',Simpath),
  313.     $readl_getenvdir(Simpath,0,Dir).
  314.  
  315. $readl_getenvdir(Simpath,Loc,Dir) :- 
  316.     $subdelim(1,':',Dir1,Loc,Simpath,Nloc) ->
  317.         (Dir = Dir1;
  318.          $readl_getenvdir(Simpath,Nloc,Dir)
  319.         )
  320.     /* else */ ;
  321.         $conlength(Simpath,Len),Llen is Len-Loc,
  322.         $substring(1,Llen,Dir,Loc,Simpath,_).
  323.  
  324. eq(X,X).
  325.  
  326. /* get environment variable value */
  327.  
  328. $getenv(Variable,Value) :-
  329.     $alloc_heap(4,Argbuff),$alloc_heap(4,Retbuff),
  330.     $conlength(Variable,Len),Nlen is Len+1,
  331.     $alloc_perm(Nlen,Nstr),$buff_code(Argbuff,0,26 /*pba*/ ,Nstr),
  332.     $substring(0,Len,Variable,0,Nstr,_),$buff_code(Nstr,Len,3 /*pb*/,0),
  333.     $brocall(2,Argbuff,Retbuff),
  334.     $buff_code(Retbuff,0,25 /*bb*/,Value),
  335.     $trimbuff(-1,Value,1,0).
  336.  
  337.  
  338. $brocall(Callno,Argbuff,Retbuff) :- '_$builtin'(100).
  339.  
  340. /* $buff *******************************************************/
  341. /* needed for dynamic loader */
  342.  
  343. $buff_export([$alloc_perm/2,$alloc_heap/2,$trimbuff/3,$buff_code/4,$symtype/2,
  344.         $substring/6,$subnumber/6,$subdelim/6,$conlength/2,
  345.         $pred_undefined/1, $hashval/3]).
  346.  
  347. $alloc_perm(Size, Buff) :- $alloc_buff(Size,Buff,0,0,0).
  348.  
  349. $alloc_heap(Size, Buff) :- $alloc_buff(Size,Buff,1,0,0).
  350.  
  351. /* Type 0: perm, 1: heap, 2: from Supbuff */
  352. $alloc_buff(Size,Buff,Type,Supbuff,Retcode) :- 
  353.     $alloc_buff1(Size,Buff,Type,Supbuff,Retcode),
  354.     (Retcode =\= 0 -> 
  355.         $writename('alloc failed'),$nl,fail;
  356.         true).
  357. $alloc_buff1(Size,Buff,Type,Supbuff,Retcode) :- '_$builtin'(76).
  358.  
  359. $trimbuff(Size,Buff,Type) :- '_$builtin'(79).
  360. $trimbuff(Size,Buff,Type,Supbuff) :- '_$builtin'(79).
  361.  
  362. $buff_code(Buff,Offset,Disc,Term) :- '_$builtin'(77).
  363.  
  364. /* Type = 0: no ep, 1: dynamic, 2: ep to compiled code, 3: buffer */
  365. $symtype(Term,Type) :- '_$builtin'(42).
  366.  
  367. $substring(Dir,NumBytes,Const,Locin,Buff,Locout) :- '_$builtin'(51).
  368.  
  369. $subnumber(Dir,NumBytes,NumCon,Locin,Buff,Locout) :- '_$builtin'(52).
  370.  
  371. $subdelim(Dir,Delim,Const,Locin,Buff,Locout) :- '_$builtin'(53).
  372.  
  373. $conlength(Const,Len) :- '_$builtin'(54).
  374.  
  375. $pred_undefined(Term) :- 
  376.     $symtype(Term,D),
  377.     (D=:=0;
  378.      D=\=0,D=:=3).
  379.  
  380. $hashval(Arg, Size, Hashval) :- '_$builtin'(43).
  381.  
  382. /* These routines put numbers into buffers in internet format */
  383. $buff_putnum_n(Buff,Loc,Len,Num) :-
  384.     Len =< 1 -> 
  385.         $buff_code(Buff,Loc,3 /*pb*/ ,Num)
  386.     /* else */ ;
  387.         Byte is Num /\ 255,
  388.         Rest is Num >> 8,
  389.         Nlen is Len-1,Sloc is Loc+Nlen,
  390.         $buff_code(Buff,Sloc,3,Byte),
  391.         $buff_putnum_n(Buff,Loc,Nlen,Rest).
  392.  
  393. $buff_getnum_n(Buff,Loc,Len,Num) :-
  394.     Len =< 1 -> 
  395.         $buff_code(Buff,Loc,6 /*gb*/ ,Num)
  396.     /*else*/ ;
  397.         Nlen is Len-1,Sloc is Loc+Nlen,
  398.         $buff_code(Buff,Sloc,6,Byte),
  399.         $buff_getnum_n(Buff,Loc,Nlen,Rest),
  400.         Num is (Rest << 8) + Byte.
  401.  
  402. $globalset_a(Place,Value) :-
  403.     $buff_code(Place,0,7 /*gepb*/ ,Buff),
  404.     $buff_code(Buff,4,3 /*pb*/ ,14 /*getnumcon*/),
  405.     $buff_code(Buff,6,2 /*pn*/ ,Value).
  406.  
  407.  
  408. /* for debugging */
  409. $writename(X) :- '_$builtin'(133).
  410. $nl :- '_$builtin'(26).
  411.  
  412.