Changeset 4574 for trunk/compiler

Show
Ignore:
Timestamp:
11/06/08 20:16:44 (2 months ago)
Author:
pmoura
Message:

Added conditional compilation support, as found in some Prolog compilers, using the new directives if/1, elif/1, else/0, and endif/0.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/compiler/logtalk.pl

    r4572 r4574  
    265265 
    266266:- dynamic('$lgt_pp_file_rclause_'/1).          % '$lgt_pp_file_rclause_'(Clause) 
     267 
     268:- dynamic('$lgt_pp_cc_if_found_'/0).           % '$lgt_pp_cc_if_found_' 
     269:- dynamic('$lgt_pp_cc_skipping_'/0).           % '$lgt_pp_cc_skipping_' 
     270:- dynamic('$lgt_pp_cc_mode_'/1).               % '$lgt_pp_cc_mode_'(Action) 
    267271 
    268272 
     
    47494753    !. 
    47504754 
     4755'$lgt_tr_file'(Term, _, _, Input, Output) :- 
     4756    '$lgt_pp_cc_skipping_', 
     4757    \+ '$lgt_lgt_cc_directive'(Term), 
     4758    !, 
     4759    '$lgt_read_term'(Input, Next, [singletons(NextSingletons)], NextLine), 
     4760    '$lgt_tr_file'(Next, NextSingletons, NextLine, Input, Output). 
     4761 
    47514762'$lgt_tr_file'(Term, Singletons, Line, Input, Output) :- 
    47524763    '$lgt_report_singletons'(Singletons, Term, Line, Input), 
     
    50225033    retractall('$lgt_pp_file_bom_'(_)), 
    50235034    retractall('$lgt_pp_file_path_'(_, _)), 
    5024     retractall('$lgt_pp_file_rclause_'(_)). 
     5035    retractall('$lgt_pp_file_rclause_'(_)), 
     5036    retractall('$lgt_pp_cc_if_found_'), 
     5037    retractall('$lgt_pp_cc_skipping_'), 
     5038    retractall('$lgt_pp_cc_mode_'(_)). 
    50255039 
    50265040 
     
    53145328    throw(error(instantiantion_error, directive(Dir))). 
    53155329 
     5330 
     5331% conditional compilation directives 
     5332 
     5333'$lgt_tr_directive'(if(Goal), _, _, _) :- 
     5334    var(Goal), 
     5335    throw(error(instantiantion_error, directive(if(Goal)))). 
     5336 
     5337'$lgt_tr_directive'(if(Goal), _, _, _) :- 
     5338    \+ callable(Goal), 
     5339    throw(error(type_error(callable, Goal), directive(if(Goal)))). 
     5340 
     5341'$lgt_tr_directive'(if(Goal), _, Input, _) :- 
     5342    '$lgt_pp_cc_mode_'(Value),                  % not top-level if 
     5343    !, 
     5344    assertz('$lgt_pp_cc_if_found_'), 
     5345    (   Value == seek ->                        % we're looking for an else 
     5346        asserta('$lgt_pp_cc_mode_'(ignore))     % so ignore this if ... endif  
     5347    ;   % Value == skip -> 
     5348        (   catch(Goal, Error, '$lgt_report_compiler_error'(Input, Error)) -> 
     5349            asserta('$lgt_pp_cc_mode_'(skip)) 
     5350        ;   asserta('$lgt_pp_cc_mode_'(seek)), 
     5351            retractall('$lgt_pp_cc_skipping_'), 
     5352            assertz('$lgt_pp_cc_skipping_') 
     5353        ) 
     5354    ). 
     5355 
     5356'$lgt_tr_directive'(if(Goal), _, _, _) :- 
     5357    !, 
     5358    assertz('$lgt_pp_cc_if_found_'), 
     5359    (   call(Goal) -> 
     5360        asserta('$lgt_pp_cc_mode_'(skip)) 
     5361    ;   asserta('$lgt_pp_cc_mode_'(seek)), 
     5362        retractall('$lgt_pp_cc_skipping_'), 
     5363        assertz('$lgt_pp_cc_skipping_') 
     5364    ). 
     5365 
     5366'$lgt_tr_directive'(elif(Goal), _, _, _) :- 
     5367    var(Goal), 
     5368    throw(error(instantiantion_error, directive(elif(Goal)))). 
     5369 
     5370'$lgt_tr_directive'(elif(Goal), _, _, _) :- 
     5371    \+ callable(Goal), 
     5372    throw(error(type_error(callable, Goal), directive(elif(Goal)))). 
     5373 
     5374'$lgt_tr_directive'(elif(Goal), _, _, _) :- 
     5375    \+ '$lgt_pp_cc_if_found_', 
     5376    throw(error(unmatched_directive, directive(elif(Goal)))). 
     5377 
     5378'$lgt_tr_directive'(elif(Goal), _, Input, _) :- 
     5379    retract('$lgt_pp_cc_mode_'(Value)), 
     5380    (   Value == ignore ->                      % we're inside an if ... endif  
     5381        true                                    % that we're ignoring; do nothing (continue skipping) 
     5382    ;   Value == skip ->                        % the corresponding if is true 
     5383        retractall('$lgt_pp_cc_skipping_'),     % so we must skip this elif 
     5384        assertz('$lgt_pp_cc_skipping_'), 
     5385        asserta('$lgt_pp_cc_mode_'(skip)) 
     5386    ;   % Value == seek ->                      % the corresponding if is false 
     5387        (   catch(Goal, Error, '$lgt_compiler_error_handler'(Input, Error)) -> 
     5388            retract('$lgt_pp_cc_skipping_'), 
     5389            asserta('$lgt_pp_cc_mode_'(skip)) 
     5390        ;   asserta('$lgt_pp_cc_mode_'(seek)) 
     5391        ) 
     5392    ), 
     5393    !. 
     5394 
     5395'$lgt_tr_directive'(else, _, _, _) :- 
     5396    \+ '$lgt_pp_cc_if_found_', 
     5397    throw(error(unmatched_directive, directive(else))). 
     5398 
     5399'$lgt_tr_directive'(else, _, _, _) :- 
     5400    retract('$lgt_pp_cc_mode_'(Value)), 
     5401    (   Value == ignore ->                      % we're inside an if ... endif  
     5402        true                                    % that we're ignoring 
     5403    ;   Value == skip ->                        % the corresponding if is true 
     5404        retractall('$lgt_pp_cc_skipping_'),     % so we must skip this else 
     5405        assertz('$lgt_pp_cc_skipping_'), 
     5406        asserta('$lgt_pp_cc_mode_'(skip)) 
     5407    ;   % Value == seek ->                      % the corresponding if is false 
     5408        retract('$lgt_pp_cc_skipping_'), 
     5409        asserta('$lgt_pp_cc_mode_'(skip)) 
     5410    ), 
     5411    !. 
     5412 
     5413'$lgt_tr_directive'(endif, _, _, _) :- 
     5414    \+ '$lgt_pp_cc_if_found_', 
     5415    throw(error(unmatched_directive, directive(endif))). 
     5416 
     5417'$lgt_tr_directive'(endif, _, _, _) :- 
     5418    retract('$lgt_pp_cc_if_found_'), 
     5419    retract('$lgt_pp_cc_mode_'(Value)), 
     5420    (   Value == ignore -> 
     5421        true 
     5422    ;   retractall('$lgt_pp_cc_skipping_') 
     5423    ), 
     5424    !. 
     5425 
     5426 
     5427% remaining directives 
     5428 
    53165429'$lgt_tr_directive'(Dir, _, _, _) :-                    % closing entity directive occurs before the opening 
    53175430    \+ '$lgt_pp_entity'(_, _, _, _, _),                 % entity directive; the opening directive is probably 
     
    1149311606 
    1149411607'$lgt_lgt_predicate_directive'(alias, 3). 
     11608 
     11609 
     11610 
     11611% conditional compilation directives 
     11612 
     11613'$lgt_lgt_cc_directive'(Term) :- 
     11614    nonvar(Term), 
     11615    Term = (:- Directive), 
     11616    nonvar(Directive), 
     11617    functor(Directive, Functor, Arity), 
     11618    '$lgt_lgt_cc_directive'(Functor, Arity). 
     11619 
     11620 
     11621'$lgt_lgt_cc_directive'(if,    1). 
     11622'$lgt_lgt_cc_directive'(elif,  1). 
     11623'$lgt_lgt_cc_directive'(else,  0). 
     11624'$lgt_lgt_cc_directive'(endif, 0). 
    1149511625 
    1149611626