home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / LIB / LOADER.LF < prev    next >
Text File  |  1996-06-04  |  5KB  |  202 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %    $Id: loader.lf,v 1.2 1994/12/08 23:58:54 duchier Exp $    
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4. %                                                                          
  5. %                   LOADING FILES USING THE LIFE PARSER IN LIFE   
  6. %          
  7. % This File contains an implementation of simple_load that uses the parser
  8. % written in Life. It also contains some extensions that allow a usage similar
  9. % to that of load (except that only one file may be specified).
  10. %
  11. % The term_expansion predicate is supported.
  12. %
  13. % Use of this file:
  14. %      load_l(Filename) ? 
  15. % looks for the file Filename.lf or Filename in specified directories, and
  16. % loads the first it finds. Reloading the same file is not possible.
  17. %
  18. %      load_gr(Filename) ? 
  19. % looks for the file Filename.gr in specified directories, and
  20. % loads the first it finds. Reloading the same file is not possible.
  21. %
  22. %      load_in(Filename) ?
  23. % looks for the file Filename.in in specified directories, and
  24. % loads the first it finds. Reloading the same file is possible.
  25. %    
  26. % All the necessary files are automatically loaded if they are in the same
  27. % directory.  
  28. %
  29. % Author: Bruno Dumant
  30. %
  31. % Copyright 1992 Digital Equipment Corporation
  32. % All Rights Reserved
  33. %                                                          
  34. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  35. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36.  
  37. module("parser") ?
  38.  
  39. public( load_life,load_l,load_gr,load_in) ?
  40.  
  41. %%% load the parser
  42.  
  43. load("parser")?
  44.  
  45. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  46. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  47.  
  48. load_life(File:string) :-
  49.     X = current_module,
  50.     exists_file(File),
  51.     open_in(File,S1),
  52.     load_first(S1),
  53.     close(S1),
  54.     set_module(X).
  55.  
  56. load_first(S1) :-
  57.     FT = first_token,
  58.     (
  59.         FT = [],
  60.         !,
  61.         nl,nl,
  62.         write("Empty File"),
  63.         nl
  64.     ;
  65.             read_new_expr( FT, Bool, Expr, T, LeftToken),
  66.         cond( Bool,
  67.           cond(  T :== assertion,
  68.                  assert_declaration(Expr,S1),
  69.              prove_query(Expr,S1)),
  70.           (!,write_syntax_error(S1))),
  71.         (
  72.         LeftToken = [],!,
  73.         nl,
  74.         write("*** File '",S1.input_file_name,"' loaded"),
  75.         nl
  76.         ;
  77.         fail
  78.         )
  79.     ;
  80.         load_next(S1)
  81.     ).
  82.  
  83. load_next(S1) :-
  84.     (
  85.             read_new_expr( [copy_term(rest_token)|`next_token], Bool, Expr, 
  86.                           T, LeftToken),
  87.         cond( Bool,
  88.           cond( T :== assertion,
  89.                 assert_declaration(Expr,S1),
  90.             prove_query(Expr,S1)),
  91.           (!,write_syntax_error(S1))),
  92.          (
  93.          LeftToken = [],!,
  94.              nl,
  95.              write("*** File '",S1.input_file_name,"' loaded"),
  96.              nl 
  97.          ;
  98.          fail
  99.          )
  100.      ;
  101.          load_next(S1)
  102.      ).
  103.  
  104. non_strict(prove_query) ?
  105. prove_query(Query,Stream) :-
  106.     ( 
  107.         RC = copy_term(rest_chars),
  108.         RT = copy_term(rest_token),
  109.         open_in("stdin",_),
  110.         retract(( load_query :- @ )),
  111.         assert(( load_query :- Query )),
  112.         @ = call_once(load_query), 
  113.         set_input(Stream),
  114.         check_changes(RC,RT),
  115.         fail
  116.     ;
  117.         succeed
  118.     ).
  119.  
  120. dynamic(load_query) ?
  121. load_query.
  122.  
  123. check_changes(RC,RT) :-
  124.     rest_chars <<- RC,
  125.     rest_token <<- RT.
  126.  
  127. assert_declaration(Declaration,Stream) :-
  128.     (
  129.         'built_ins#term_xpand'(Declaration,NewDefs),!,
  130.         cond( NewDefs :=< list,
  131.               maprel(assert, NewDefs),
  132.           assert(NewDefs))
  133.     ;
  134.         nl_err,
  135.         write_err("*** Error in term expansion in file '",
  136.                       Stream.input_file_name,"'"),
  137.         nl_err
  138.     ).
  139.  
  140. write_syntax_error(Stream) :-
  141.     close(Stream),
  142.     nl_err,
  143.     write_err("*** Syntax error near line ",Stream.line_count,
  144.               " in file '",Stream.input_file_name,"'"),
  145.     nl_err,
  146.     fail.    
  147.  
  148. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  149. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  150. %                                                                             %
  151. % extensions for an easier use                                                %
  152. %                                                                             %
  153. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  154. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  155. %
  156. % load selectif
  157. %
  158.  
  159. load_l(X)  :- 
  160.     loadpath2(X,{".lf";""},true).
  161.  
  162. load_gr(X) :- 
  163.     loadpath2(X,".gr",true).
  164.  
  165. load_in(X) :-
  166.     loadpath2(X,".in",false).
  167.  
  168. non_strict(loadpath2) ?
  169. loadpath2(X,Suffix,Bool) :- 
  170.     cond( X :=< string,
  171.           S = X,
  172.           S = psi2str(X)),
  173.     F = strcon(S, eval(Suffix)),
  174.     (
  175.         exists_file(F),!,load_custom2(F,Bool)
  176.     ;
  177.         exists_file(CF:strcon(load_path,F)), !,load_custom2(CF,Bool)
  178.     ).
  179.  
  180. loadpath2(X) :-  write_err("*** No file '",X,"' found"),nl_err.
  181.  
  182.  
  183. load_custom2(F,true) :-
  184.     !,
  185.     (
  186.         loaded_file(F), !,
  187.         write("*** File '",F,"' was already loaded"),nl
  188.     ;
  189.         load_life(F),
  190.         assert(loaded_file(F))
  191.     ).
  192. load_custom2(F,false) :- 
  193.     exists_file(F),
  194.     !,
  195.     load_life(F).
  196.  
  197. assert(loaded_file),retract(loaded_file) ?
  198.  
  199. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  200. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  201.