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

  1. %    $Id: shell_interface.lf,v 1.2 1994/12/09 00:24:12 duchier Exp $    
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %                                                                             %
  4. %               INTERFACE BETWEEN THE SHELL AND THE PARSER                    %
  5. %                                                                             %
  6. %                                                                             %
  7. %                                                                             %
  8. %                                                                             %
  9. %                                                                             %
  10. %                                                                             %
  11. % Author: Bruno Dumant                                                        %
  12. %                                                                             %
  13. % copyright 1992 Digital Equipment Corporation                                %
  14. % All rights reserved                                                         %
  15. %                                                                             %
  16. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  17.  
  18. module("shell") ?
  19. open("parser") ?
  20. open("tokenizer") ?
  21.  
  22. %%%
  23. %%% tokenizing
  24. %%%
  25.  
  26. module("tokenizer") ?
  27. public( next_shell_token ) ?
  28.  
  29. next_shell_token ->
  30.      L
  31.     |           
  32.         (
  33.         call_once(read_new_shell_token(Tok,copy_term(rest_chars))) = TT,
  34.         ( 
  35.         TT :== false, !, fail
  36.         ;
  37.         Tok :== none, !,
  38.         rest_token <<- "the end of the line",
  39.         L = []
  40.         ;
  41.         rest_token <<- `Tok,
  42.         fail 
  43.         )
  44.     ;
  45.         L = [copy_term(rest_token)|`next_shell_token]
  46.     ).
  47.  
  48. read_new_shell_token( Tok, X) :- 
  49.     void_chars(0 => X, rest => R1),!,
  50.     (
  51.         R1 = [], !, Tok = none
  52.     ;
  53.         token( 0 => R1, Tok, rest => R2),
  54.         rest_chars <<- R2
  55.     ).
  56.  
  57. %
  58. % parsing
  59. %
  60.  
  61. module("parser") ?
  62. public( first_shell_parse,next_shell_parse,sh_query ) ?
  63.  
  64.  
  65. first_shell_parse(Chars,Vars,Expr,Type,ExistVars,End) :-
  66.     init_shell_parse,
  67.     rest_chars <<- Chars,
  68.     read_new_shell_expr( next_shell_token, Bool, Expr, T, LeftToken, 
  69.                          vars => Vars ),
  70.         cond( Bool,
  71.           cond(  T :== assertion,
  72.              Type = declaration,
  73.              Type = sh_query ),
  74.           Type = error),
  75.         ExistVars = cond (features(Vars) :== [], false),
  76.     End = (LeftToken :== []).
  77.  
  78. next_shell_parse( Vars, Expr, Type, End) :-
  79.     NT = next_shell_token,
  80.     read_new_expr( NT, Bool, Expr, T, LeftToken, 
  81.                    vars => Vars),
  82.         cond( Bool,
  83.               cond(  T :== assertion,
  84.                  Type = declaration,
  85.              Type = sh_query ),
  86.           Type = error ),
  87.         End = (LeftToken :== []).
  88.  
  89. read_new_shell_expr( R1, Bool, Expr, T, LeftToken,
  90.                  vars => Vars) :- 
  91.     (
  92.         expr( Expr, vars => Vars, mask => 0,
  93.               0 => R1, rest => R2, max => 1200),
  94.         (
  95.         Expr :< string, T = sh_query, 
  96.         NE = root_sort(Expr), Expr <- `( @ = system(NE))
  97.         ;
  98.         parser_C(["."],false,R2,LeftToken), T = assertion
  99.         ;
  100.             parser_C(["?"],false,R2,LeftToken), T = sh_query 
  101.         ),
  102.         Bool = true,
  103.         !
  104.     ;
  105.         Bool = false
  106.     ).
  107.  
  108. init_shell_parse :- 
  109.     rest_token <<- "the beginning of the line".
  110.  
  111.  
  112. %
  113. % printing variables
  114. %
  115.  
  116. module("shell") ?
  117.  
  118. print_vars (@(vars => X)) :- 
  119.     cond( F:features(X) :\== [],
  120.           (
  121.           build_write_term(F,X) = WT,
  122.           pretty_write(WT),nl
  123.           )).
  124.  
  125. build_write_term([A],X) -> `( A = Val ) | Val = project(A,X).
  126. build_write_term([A|B],X) -> 
  127.     `( A = Val ),build_write_term(B,X) | Val =  project(A,X).
  128.  
  129.  
  130. write_parse_error :-
  131.     nl_err, write_err(
  132.         "**** Syntax error at line ",current_line_number," near: "),
  133.     cond( R:copy_term(rest_token) :=< string,
  134.           write(R),
  135.           cond( R :=< variable,
  136.                 write_err(project(1,R)),
  137.             writeq_err(project(1,R)))),
  138.         nl_err.
  139.  
  140.