home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / SUPERLIN / C_BUILDE.LF next >
Text File  |  1996-06-04  |  59KB  |  2,529 lines

  1. %
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %
  4. %
  5. %                            TREE CONSTRUCTORS
  6. %                            -----------------
  7. %
  8. %  
  9. %
  10. %
  11. %  AUTHOR : Arnaud Venet                     CREATION : August 9th 1993
  12. %  ------                                    --------
  13. %
  14. %
  15. %                             ---------------                        
  16. %
  17. %                    
  18. %                   Last modification : March 3rd 1994 
  19. %
  20. %
  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22. %
  23. %
  24. %  (C) Digital Equipment Corporation 1993 - 1994
  25. %
  26. %
  27. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  28. %
  29.  
  30.  
  31.  
  32. load("expand_type") ?
  33.  
  34.  
  35. %
  36. % ------------------------------------------------------------------------------
  37. %
  38.  
  39.  
  40. persistent(the_current_declarations) ?
  41.  
  42.  
  43. persistent(top_declarations) ?
  44.  
  45.  
  46. persistent(the_current_declaration) ?
  47.  
  48.  
  49. persistent(the_previous_declaration) ?
  50.  
  51.  
  52. persistent(the_current_instruction) ?
  53.  
  54.  
  55. persistent(the_previous_instruction) ?
  56.  
  57.  
  58. persistent(the_instruction_scope) ?
  59.  
  60.  
  61. %
  62. % ------------------------------------------------------------------------------
  63. %
  64.  
  65.  
  66. current_declaration -> the_current_declaration.1.
  67.  
  68.  
  69. previous_declaration -> the_previous_declaration.1.
  70.  
  71.  
  72. current_declarations -> the_current_declarations.1.
  73.  
  74.  
  75. previous_instruction -> the_previous_instruction.1.
  76.  
  77.  
  78. current_instruction -> the_current_instruction.1.
  79.  
  80.  
  81. instruction_scope -> the_instruction_scope.1.
  82.  
  83.  
  84. %
  85. % ------------------------------------------------------------------------------
  86. %
  87.  
  88.  
  89. local_error(Message, Cause : @(line => Line, file => File), Warning) :-
  90.   cond(the_error_mode :== talkie,
  91.     (
  92.       nl_err,
  93.       cond(Warning :== @,
  94.         Prompt = "error",
  95.         Prompt = "warning"
  96.       ),
  97.       write_err(">>> ", Prompt, " : ", File, ", line ", Line, " "),
  98.       cond(Cause :\== anonymous, 
  99.         write_err("near '", display_token(Cause), "' ")
  100.       ),
  101.       write_err("-- "),
  102.       cond(Message :\== @,
  103.         write_err(Message)
  104.       )
  105.     )
  106.   ),
  107.   cond(Warning :== @,
  108.     parse_error <<- true
  109.   ),
  110.   error_number <<- error_number + 1,
  111.   Error = the_error_log.error_number,
  112.   Error.type <<- error,
  113.   Error.message <<- Message,
  114.   Error.cause <<- Cause.
  115.  
  116.  
  117. %
  118. % ------------------------------------------------------------------------------
  119. %
  120.  
  121.  
  122. persistent(operations_table) ?
  123.  
  124.  
  125. %
  126. % ------------------------------------------------------------------------------
  127. %
  128.  
  129.  
  130. put_operation(Operation, Precedence) -> @
  131.   | operations_table.Operation <<- Precedence.
  132.  
  133.  
  134. %
  135. % ------------------------------------------------------------------------------
  136. %
  137.  
  138.  
  139. set_precedence(Operations, Precedence) :-
  140.   (
  141.     @ = map(put_operation(2 => Precedence), Operations),
  142.     fail
  143.   ;
  144.     succeed
  145.   ).
  146.  
  147.  
  148. %
  149. % ------------------------------------------------------------------------------
  150. %
  151.  
  152.  
  153. init_operations :-
  154.   set_precedence(assignment, 1),
  155.   set_precedence(logical_or, 2),
  156.   set_precedence(logical_and, 3),
  157.   set_precedence(inclusive_or, 4),
  158.   set_precedence(exclusive_or, 5),
  159.   set_precedence(c_and, 6),
  160.   set_precedence(equality, 7),
  161.   set_precedence(relation, 8),
  162.   set_precedence(shift, 9),
  163.   set_precedence(addition, 10),
  164.   set_precedence(multiplication, 11).
  165.  
  166.  
  167. %
  168. % ------------------------------------------------------------------------------
  169. %
  170.  
  171. clear_tree :-
  172.   init_operations,
  173.   string_storage <<- @,
  174.   top_declarations <<- toplevel,
  175.   top_declarations.files <<- files,
  176.   top_declarations.parent_declarations <<- nothing,
  177.   top_declarations.type_definitions <<- type_definitions,
  178.   top_declarations.struct_definitions <<- struct_definitions,
  179.   top_declarations.union_definitions <<- union_definitions,
  180.   top_declarations.enum_definitions <<- enum_definitions,
  181.   top_declarations.struct_declarations <<- struct_declarations,
  182.   top_declarations.union_declarations <<- union_declarations,
  183.   top_declarations.enum_declarations <<- enum_declarations,
  184.   top_declarations.external_declarations <<- external_declarations,
  185.   top_declarations.function_definitions <<- function_definitions,
  186.   the_previous_declaration <<- ref(nothing),
  187.   the_previous_instruction <<- ref(nothing),
  188.   the_current_declarations <<- ref(top_declarations).
  189.  
  190.  
  191. %
  192. % ------------------------------------------------------------------------------
  193. %
  194.  
  195.  
  196. chain_declarations :-
  197.   current_declaration.previous <<- previous_declaration,
  198.   cond(previous_declaration :\== nothing,
  199.     previous_declaration.next <<- current_declaration
  200.   ),
  201.   the_previous_declaration <<- ref(current_declaration).
  202.  
  203.  
  204. %
  205. % ------------------------------------------------------------------------------
  206. %
  207.  
  208.  
  209. end_declarations_chain :-
  210.   current_declaration.next <<- nothing.
  211.  
  212.  
  213. %
  214. % ------------------------------------------------------------------------------
  215. %
  216.  
  217.  
  218. set_current_instruction(Instruction) :-
  219.   the_current_instruction <<- ref(Instruction).
  220.  
  221.  
  222. %
  223. % ------------------------------------------------------------------------------
  224. %
  225.  
  226.  
  227. chain_instructions(Instruction, NewInstruction) :-
  228.   current_instruction.previous <<- previous_instruction,
  229.   cond(previous_instruction :\== nothing,
  230.     previous_instruction.next <<- current_instruction
  231.   ),
  232.   the_previous_instruction <<- ref(current_instruction),
  233.   NewInstruction = current_instruction.
  234.  
  235.  
  236. %
  237. % ------------------------------------------------------------------------------
  238. %
  239.  
  240.  
  241. end_instructions_chain :-
  242.   current_instruction.next <<- nothing.
  243.  
  244.  
  245. %
  246. % ------------------------------------------------------------------------------
  247. %
  248.  
  249.  
  250. isolate_instruction(Instruction) :-
  251.   Instruction.next <<- nothing,
  252.   Instruction.previous <<- nothing.
  253.  
  254.  
  255. %
  256. % ------------------------------------------------------------------------------
  257. %
  258.  
  259. set_local_declarations(LocalDeclarations) :-
  260.   the_current_declarations = ref(ParentDeclarations),
  261.   LocalDeclarations <<- block_declarations,
  262.   LocalDeclarations.parent_declarations <<- ParentDeclarations,
  263.   the_current_declarations <<- ref(LocalDeclarations).
  264.  
  265.  
  266. %
  267. % ------------------------------------------------------------------------------
  268. %
  269.  
  270.  
  271. set_function_local_declarations :-
  272.   the_current_declarations <<- ref(parameters_declarations).
  273.  
  274.  
  275. %
  276. % ------------------------------------------------------------------------------
  277. %
  278.  
  279.  
  280. restore_function_local_declarations :-
  281.   the_current_declarations <<- ref(top_declarations).
  282.  
  283.  
  284. %
  285. % ------------------------------------------------------------------------------
  286. %
  287.  
  288.  
  289. init_complex_declaration(LocalDeclarations) :-
  290.   LocalDeclarations.local_declarations <<- local_declarations,
  291.   LocalDeclarations.type_definitions <<- type_definitions,
  292.   LocalDeclarations.struct_definitions <<- struct_definitions,
  293.   LocalDeclarations.enum_definitions <<- enum_definitions,
  294.   LocalDeclarations.union_definitions <<- union_definitions,
  295.   LocalDeclarations.struct_declarations <<- struct_declarations,
  296.   LocalDeclarations.enum_declarations <<- enum_declarations,
  297.   LocalDeclarations.union_declarations <<- union_declarations.
  298.  
  299.  
  300. %
  301. % ------------------------------------------------------------------------------
  302. %
  303.  
  304.  
  305. restore_declarations_from(LocalDeclarations) :-
  306.   the_current_declarations <<- ref(LocalDeclarations.parent_declarations).
  307.  
  308.  
  309. %
  310. % ------------------------------------------------------------------------------
  311. %
  312.  
  313.  
  314. set_previous_declaration(PreviousDeclaration) :-
  315.   PreviousDeclaration <<- previous_declaration,
  316.   the_previous_declaration <<- ref(nothing).
  317.  
  318.  
  319. %
  320. % ------------------------------------------------------------------------------
  321. %
  322.  
  323.  
  324. restore_previous_declaration_from(PreviousDeclaration) :-
  325.   the_previous_declaration <<- ref(PreviousDeclaration).
  326.  
  327.  
  328. %
  329. % ------------------------------------------------------------------------------
  330. %
  331.  
  332.  
  333. set_previous_instruction(PreviousInstruction) :-
  334.   the_previous_instruction = ref(PreviousInstruction),
  335.   the_previous_instruction <<- ref(nothing).
  336.  
  337.  
  338. %
  339. % ------------------------------------------------------------------------------
  340. %
  341.  
  342.  
  343. restore_previous_instruction_from(PreviousInstruction) :-
  344.   the_previous_instruction <<- ref(PreviousInstruction).
  345.  
  346.  
  347. %
  348. % ------------------------------------------------------------------------------
  349. %
  350.  
  351.  
  352. set_instruction_scope(Scope) :-
  353.   the_instruction_scope <<- ref(Scope).
  354.  
  355.  
  356. %
  357. % ------------------------------------------------------------------------------
  358. %
  359.  
  360.  
  361. set_previous_instruction_scope(TheBlock) :-
  362.   TheBlock <<- instruction_scope.
  363.  
  364.  
  365. %
  366. % ------------------------------------------------------------------------------
  367. %
  368.  
  369.  
  370. restore_instruction_scope_from(TheBlock) :-
  371.   the_instruction_scope <<- ref(TheBlock).
  372.  
  373.  
  374. %
  375. % ------------------------------------------------------------------------------
  376. %
  377.  
  378. set_scope_for_instruction(Instruction) :-
  379.   Instruction.scope <<- instruction_scope.
  380.  
  381.  
  382. %
  383. % ------------------------------------------------------------------------------
  384. %
  385.  
  386.  
  387. is_type_name(Identifier) :-
  388.   type_occurence(Identifier, current_declarations).
  389.  
  390.  
  391. %
  392. % ------------------------------------------------------------------------------
  393. %
  394.  
  395.  
  396. type_occurence(Name, Declarations, EquivalentType) :-
  397.   (
  398.     search_enumerator(Name, Declarations),
  399.     !,
  400.     fail
  401.   ;
  402.     has_feature(local_declarations, Declarations, LocalDeclarations),
  403.     has_feature(Name.1, LocalDeclarations),
  404.     !,
  405.     fail
  406.   ;
  407.     has_feature(type_definitions, Declarations, TypeDefinitions),
  408.     has_feature(Name.1, TypeDefinitions, EquivalentType),
  409.     !
  410.   ;
  411.     Declarations :== parameters_declarations,
  412.     !,
  413.     type_occurence(Name, top_declarations, EquivalentType)
  414.   ;
  415.     ParentDeclarations : (Declarations.parent_declarations) :\== nothing,
  416.     type_occurence(Name, ParentDeclarations, EquivalentType)
  417.   ).
  418.  
  419.  
  420. %
  421. % ------------------------------------------------------------------------------
  422. %
  423.  
  424.  
  425. search_enumerator(Name, Declarations) :-
  426.   has_feature(enum_definitions, Declarations),
  427.   F = features(Declarations.enum_definitions, current_module),
  428.   search_enumerators(Name, F, Declarations.enum_definitions).
  429.  
  430.  
  431. %
  432. % ------------------------------------------------------------------------------
  433. %
  434.  
  435.  
  436. search_enumerators(Name, [Enum | Enums], Enumerations) :-
  437.   (
  438.     has_feature(Name.1, Enumerations.Enum.body),
  439.     !
  440.   ;
  441.     search_enumerators(Name, Enums, Enumerations)
  442.   ).
  443.  
  444.  
  445. %
  446. % ------------------------------------------------------------------------------
  447. %
  448.  
  449.  
  450. struct_occurence(Name, Declarations, EquivalentType) :-
  451.   (
  452.     has_feature(struct_definitions, Declarations, Structures),
  453.     has_feature(Name.1, Structures, EquivalentType),
  454.     !
  455.   ;
  456.     Declarations :== parameters_declarations,
  457.     !,
  458.     struct_occurence(Name, top_declarations, EquivalentType)
  459.   ;
  460.     ParentDeclarations : (Declarations.parent_declarations) :\== nothing,
  461.     !,
  462.     struct_occurence(Name, ParentDeclarations, EquivalentType)
  463.   ).
  464.  
  465.  
  466. %
  467. % ------------------------------------------------------------------------------
  468. %
  469.  
  470.  
  471. union_occurence(Name, Declarations, EquivalentType) :-
  472.   (
  473.     has_feature(union_definitions, Declarations, Unions),
  474.     has_feature(Name.1, Unions, EquivalentType),
  475.     !
  476.   ;
  477.     Declarations :== parameters_declarations,
  478.     !,
  479.     union_occurence(Name, top_declarations, EquivalentType)
  480.   ;
  481.     ParentDeclarations : (Declarations.parent_declarations) :\== nothing,
  482.     !,
  483.     union_occurence(Name, ParentDeclarations, EquivalentType)
  484.   ).
  485.  
  486.  
  487. %
  488. % ------------------------------------------------------------------------------
  489. %
  490.  
  491.  
  492. enum_occurence(Name, Declarations, EquivalentType) :-
  493.   (
  494.     has_feature(enum_definitions, Declarations, Enumerations),
  495.     has_feature(Name.1, Enumerations, EquivalentType),
  496.     !
  497.   ;
  498.     Declarations :== parameters_declarations,
  499.     !,
  500.     enum_occurence(Name, top_declarations, EquivalentType)
  501.   ;
  502.     ParentDeclarations : (Declarations.parent_declarations) :\== nothing,
  503.     !,
  504.     enum_occurence(Name, ParentDeclarations, EquivalentType)
  505.   ).
  506.  
  507.  
  508. %
  509. % ------------------------------------------------------------------------------
  510. %
  511.  
  512.  
  513. build_anonymous -> anonymous(line => current_token.line,
  514.                              file => current_token.file,
  515.                              white_spaces => store([])).
  516.  
  517.  
  518. %
  519. % ------------------------------------------------------------------------------
  520. %
  521.  
  522.  
  523. persistent(tag_number) ?
  524.  
  525.  
  526. %
  527. % ------------------------------------------------------------------------------
  528. %
  529.  
  530.  
  531. non_strict(is_a_function_definition) ?
  532.  
  533.  
  534. is_a_function_definition([Declaration : @(type => Type, 
  535.                                           name => TokenName)], 
  536.                           Name, Style) :-
  537.   (
  538.     has_feature(separator, Declaration),
  539.     !,
  540.     fail
  541.   ;
  542.     RootType = root_type(Type),
  543.     TokenName = identifier(Name),
  544.     Parameters = RootType.parameters,
  545.     (
  546.       Parameters :== nothing,
  547.       !,
  548.       Style = old
  549.     ;
  550.       Parameters.1 :== label_parameter,
  551.       !,
  552.       Style = old
  553.     ;
  554.       Style = modern
  555.     )
  556.   ).
  557.  
  558.  
  559. %
  560. % ------------------------------------------------------------------------------
  561. %
  562.  
  563.  
  564. root_type(protected_type(InnerType)) -> root_type(InnerType).
  565.  
  566.  
  567. root_type(Type) -> Type.
  568.  
  569.  
  570. %
  571. % ------------------------------------------------------------------------------
  572. %
  573.  
  574.  
  575. non_strict(build_type) ?
  576.  
  577.  
  578. build_type(DeclarationSpecifiers, Qualification, StoreClass) -> Type
  579.   | collect_the_type(DeclarationSpecifiers, RoughType, Signed, Long),
  580.     Type.specifiers = DeclarationSpecifiers,
  581.     cond(StoreClass :== @,
  582.       Type.store_class = nothing,
  583.       Type.store_class = root_sort(StoreClass)
  584.     ),
  585.     cond(Qualification :== @,
  586.       Type.qualification = nothing,
  587.       Type.qualification = root_sort(Qualification)
  588.     ),
  589.     cond(RoughType :== @,
  590.       cond(Signed :\== @,
  591.         RoughType = int,
  592.         cond(Long :\== @,
  593.           RoughType = int,
  594.           (
  595.             DeclarationSpecifiers = [FirstSpecifier | @],
  596.             local_error("Bad type", FirstSpecifier)
  597.           )
  598.         )
  599.       )
  600.     ),
  601.     (
  602.       RoughType :== {int; double},
  603.       !,
  604.       (
  605.         Long :\== @,
  606.         !,
  607.         cond(Long :== true,
  608.           Size = "long_",
  609.           cond(RoughType :== int,
  610.             Size = "short_",
  611.             (
  612.               Size = "",
  613.               DeclarationSpecifiers = [FirstSpecifier | @],
  614.               local_error("Bad type", FirstSpecifier)
  615.             )
  616.           )
  617.         ),
  618.         Type = str2psi(strcon(Size, psi2str(RoughType)))
  619.       ;
  620.         Type = RoughType
  621.       )
  622.     ;
  623.       Type = RoughType,
  624.       cond(Long :\== @,
  625.         (
  626.           DeclarationSpecifiers = [FirstSpecifier | @],
  627.           local_error("Bad type", FirstSpecifier)
  628.         )
  629.       )
  630.     ),
  631.     (
  632.       Type :== {int; short_int; long_int; float; double; long_double},
  633.       !,
  634.       cond(Signed :== @,
  635.         Type.signed = true, 
  636.         Type.signed = Signed
  637.       )
  638.     ;
  639.       Type :== char,
  640.       !,
  641.       cond(Signed :== @,
  642.         Type.signed = false,
  643.         Type.signed = Signed
  644.       )
  645.     ;
  646.       cond(Signed :\== @,
  647.         (
  648.           DeclarationSpecifiers = [FirstSpecifier | @],
  649.           local_error("Bad type", FirstSpecifier)
  650.         )
  651.       )
  652.     ).
  653.  
  654.  
  655. %
  656. % ------------------------------------------------------------------------------
  657. %
  658.  
  659.  
  660. non_strict(collect_the_type) ?
  661.  
  662.  
  663. collect_the_type([Int : int | LSpecifiers], Type, Signed, Long) :-
  664.   !,
  665.   Type = int,
  666.   (
  667.     has_feature(inserted, Int),
  668.     !,
  669.     Type.inserted = true
  670.   ;
  671.     succeed
  672.   ),
  673.   collect_the_type(LSpecifiers, Type, Signed, Long).
  674.  
  675.  
  676. collect_the_type([SingleType | LSpecifiers], Type, Signed, Long) :-
  677.   SingleType :== {void; char; float; double},
  678.   !,
  679.   Type = root_sort(SingleType),
  680.   collect_the_type(LSpecifiers, Type, Signed, Long).
  681.  
  682.  
  683. collect_the_type([Size | LSpecifiers], Type, Signed, Long) :-
  684.   Size :== {short; long},
  685.   !,
  686.   cond(Size :== long,
  687.     IsLong = true,
  688.     IsLong = false
  689.   ),
  690.   (
  691.     Long :== @,
  692.     !,
  693.     Long = IsLong 
  694.   ;
  695.     local_error("Bad size", Size)
  696.   ),
  697.   collect_the_type(LSpecifiers, Type, Signed, Long).
  698.  
  699.  
  700. collect_the_type([Sign | LSpecifiers], Type, Signed, Long) :-
  701.   Sign :== {signed; unsigned},
  702.   !,
  703.   cond(Sign :== signed,
  704.     IsSigned = true,
  705.     IsSigned = false
  706.   ),
  707.   (
  708.     Signed :== @,
  709.     !,
  710.     Signed = IsSigned 
  711.   ;
  712.     local_error("Bad sign", Sign)
  713.   ),
  714.   collect_the_type(LSpecifiers, Type, Signed, Long).
  715.  
  716.  
  717. collect_the_type([ComplexType | LSpecifiers], Type, Signed, Long) :-
  718.   !,
  719.   Type = ComplexType,
  720.   collect_the_type(LSpecifiers, Type, Signed, Long).
  721.  
  722.  
  723. collect_the_type([]).
  724.  
  725.  
  726. %
  727. % ------------------------------------------------------------------------------
  728. %
  729.  
  730.  
  731. non_strict (add_separator) ?
  732.  
  733.  
  734. add_separator(Separator, What) :-
  735.   What.separator <<- Separator.
  736.  
  737.  
  738. %
  739. % ------------------------------------------------------------------------------
  740. %
  741.  
  742.  
  743. add_semi_colon(SemiColon, What) :-
  744.   What.semi_colon <<- SemiColon.
  745.  
  746.  
  747. %
  748. % ------------------------------------------------------------------------------
  749. %
  750.  
  751.  
  752. complete_definition([S : struct_name(Name)], Qualification, StoreClass,
  753.                      SemiColon, Scope) :-
  754.   !,
  755.   check_are_empty(Qualification, StoreClass),
  756.   add_struct_declaration(S, SemiColon, Scope).
  757.  
  758.  
  759. complete_definition([U : union_name(Name)], Qualification, StoreClass,
  760.                      SemiColon, Scope) :-
  761.   !,
  762.   check_are_empty(Qualification, StoreClass),
  763.   add_union_declaration(U, SemiColon, Scope).
  764.  
  765.  
  766. complete_definition([E : enum_name(Name)], Qualification, StoreClass,
  767.                      SemiColon, Scope) :-
  768.   !,
  769.   (
  770.     enum_occurence(Name, current_declarations),
  771.     !,
  772.     check_are_empty(Qualification, StoreClass),
  773.     add_enum_declaration(E, SemiColon, Scope)
  774.   ;
  775.     local_error("Cannot define incomplete enum definitions", Name)
  776.   ).
  777.  
  778.  
  779. %
  780. % ------------------------------------------------------------------------------
  781. %
  782.  
  783.  
  784. check_are_empty(Qualification, StoreClass) :-
  785.   cond(Qualification :\== @,
  786.     local_error("Useless qualifier", Qualification, warning)
  787.   ),
  788.   cond(StoreClass :\== @,
  789.     local_error("Useless specifier", StoreClass, warning)
  790.   ).
  791.  
  792.  
  793. %
  794. % ------------------------------------------------------------------------------
  795. %
  796.  
  797.  
  798. default_type -> int(inserted => true).
  799.  
  800.  
  801. %
  802. % ------------------------------------------------------------------------------
  803. %
  804.  
  805.  
  806. scan_specifiers([T : typedef(Keyword, Declarators) | RoughSpecifiers],
  807.                 [typedef | Specifiers], Qualification, StoreClass, Where) :-
  808.   !,
  809.   (
  810.     Where :== {toplevel; block},
  811.     !,
  812.     build_type_definition(Keyword, Declarators)
  813.   ;
  814.     local_error("Cannot make typedef", Keyword)
  815.   ),
  816.   scan_specifiers(RoughSpecifiers, Specifiers, Qualification, StoreClass, Where).
  817.  
  818.  
  819. scan_specifiers([S : struct(Body, name => Name, keyword => Keyword) 
  820.                  | RoughSpecifiers],
  821.                 Specifiers, Qualification, StoreClass, Where) :-
  822.   !,
  823.   (
  824.     Where :== {toplevel; block; typedef; struct; union},
  825.     !,
  826.     cond(Name :== anonymous,  
  827.       Specifiers = [struct(Body, keyword => Keyword) | LSpecifiers],
  828.       (
  829.         add_struct_definition(Keyword, Name, Body),
  830.         Specifiers = [build_struct_name(Keyword, Name) | LSpecifiers]
  831.       )
  832.     ),
  833.     scan_specifiers(RoughSpecifiers, LSpecifiers, Qualification, StoreClass, Where)
  834.   ;
  835.     local_error("Cannot define structure", Keyword),
  836.     scan_specifiers(RoughSpecifiers, Specifiers, Qualification, StoreClass, Where)
  837.   ).
  838.  
  839.  
  840. scan_specifiers([S : union(Body, name => Name, keyword => Keyword) 
  841.                  | RoughSpecifiers],
  842.                 Specifiers, Qualification, StoreClass, Where) :-
  843.   !,
  844.   (
  845.     Where :== {toplevel; block; typedef; struct; union},
  846.     !,
  847.     cond(Name :== anonymous,  
  848.       Specifiers = [union(Body, keyword => Keyword) | LSpecifiers],
  849.       (
  850.         add_union_definition(Keyword, Name, Body),
  851.         Specifiers = [build_union_name(Keyword, Name) | LSpecifiers]
  852.       )
  853.     ),
  854.     scan_specifiers(RoughSpecifiers, LSpecifiers, Qualification, StoreClass, Where)
  855.   ;
  856.     local_error("Cannot define union", Keyword),
  857.     scan_specifiers(RoughSpecifiers, Specifiers, Qualification, StoreClass, Where)
  858.   ).
  859.  
  860.  
  861. scan_specifiers([S : enum(Body, name => Name, keyword => Keyword) 
  862.                  | RoughSpecifiers],
  863.                 Specifiers, Qualification, StoreClass, Where) :-
  864.   !,
  865.   (
  866.     Where :== {toplevel; block; typedef; struct; union},
  867.     !,
  868.     cond(Name :== anonymous,  
  869.       Specifiers = [enum(Body, keyword => Keyword) | LSpecifiers],
  870.       (
  871.         add_enum_definition(Keyword, Name, Body),
  872.         Specifiers = [build_enum_name(Keyword, Name) | LSpecifiers]
  873.       )
  874.     ),
  875.     scan_specifiers(RoughSpecifiers, LSpecifiers, Qualification, StoreClass, Where)
  876.   ;
  877.     local_error("Cannot define enumeration", Keyword),
  878.     scan_specifiers(RoughSpecifiers, Specifiers, Qualification, StoreClass, Where)
  879.   ).
  880.  
  881.  
  882. scan_specifiers([S : store_class_specifier | RoughSpecifiers], Specifiers,
  883.                  Qualification, StoreClass, Where) :-
  884.   !,
  885.   (
  886.     Where :== typedef,
  887.     !,
  888.     local_error("Useless specifier in typedef", S, warning)
  889.   ;
  890.     cond(StoreClass :== @,
  891.       StoreClass = S,
  892.       cond(StoreClass :== S,
  893.         local_error("Duplicate specifier", S, warning),
  894.         local_error("Bad specifier", S)
  895.       )
  896.     )
  897.   ),
  898.   scan_specifiers(RoughSpecifiers, Specifiers, Qualification, 
  899.                   StoreClass, Where).
  900.  
  901.  
  902. scan_specifiers([Q : type_qualifier | RoughSpecifiers], Specifiers,
  903.                 Qualification, StoreClass, Where) :-
  904.   !,
  905.   (
  906.     Where :== typedef,
  907.     !,
  908.     local_error("Useless qualifier in typedef", Q, warning)
  909.   ;
  910.     cond(Qualification :== @,
  911.       Qualification = Q,
  912.       cond(Qualifier :== Q,
  913.         local_error("Duplicate qualifier", Q, warning),
  914.         local_error("Bad qualifier", Q)
  915.       )
  916.     )
  917.   ),
  918.   scan_specifiers(RoughSpecifiers, Specifiers, Qualification, 
  919.                   StoreClass, Where).
  920.  
  921.  
  922. scan_specifiers([Specifier | RoughSpecifiers], [Specifier | Specifiers],
  923.                 Qualification, StoreClass, Where) :-
  924.   !,
  925.   scan_specifiers(RoughSpecifiers, Specifiers, Qualification, 
  926.                   StoreClass, Where).
  927.  
  928.  
  929. scan_specifiers([], []).
  930.  
  931.  
  932. %
  933. % ------------------------------------------------------------------------------
  934. %
  935.  
  936.  
  937. non_strict(build_type_name) ?
  938.  
  939.  
  940. build_type_name(Identifier) ->
  941.   type_alias(Identifier).
  942.  
  943.  
  944. %
  945. % ------------------------------------------------------------------------------
  946. %
  947.  
  948.  
  949. non_strict(build_typedef) ?
  950.  
  951.  
  952. build_typedef(Keyword, Declarators) -> typedef(Keyword, Declarators).
  953.  
  954.  
  955. %
  956. % ------------------------------------------------------------------------------
  957. %
  958.  
  959.  
  960. non_strict (build_type_definition) ?
  961.  
  962.  
  963. build_type_definition(Typedef, 
  964.                       [D : @(name => Name : anonymous) | TypeDeclarators]) :-
  965.   !,
  966.   local_error("Bad type definition", Name),
  967.   fail.
  968.  
  969.  
  970. build_type_definition(Typedef, 
  971.                       [Def : @(name => Identifier : identifier(Name), 
  972.                                type => Type)
  973.                        | TypeDeclarators]) :-
  974.   !,
  975.   TypeDefinition = current_declarations.type_definitions.Name,
  976.   (
  977.     TypeDefinition :== @,
  978.     !,
  979.     TypeDefinition <<- type_definition,
  980.     TypeDefinition.type <<- Type,
  981.     TypeDefinition.name <<- Identifier,
  982.     TypeDefinition.keyword <<- Typedef,
  983.     (
  984.       has_feature(initialization, Def),
  985.       !,
  986.       local_error("No initialization in typedef", Identifier)
  987.     ;
  988.       succeed
  989.     ),
  990.     (
  991.       TypeDeclarators :== [],
  992.       !,
  993.       (
  994.         has_feature(separator, Def, Separator),
  995.         Separator :== ';',
  996.         !
  997.       ;
  998.         recover_token(Separator, ';')
  999.       ),
  1000.       TypeDefinition.separator <<- Separator
  1001.     ;
  1002.       TypeDefinition.separator <<- Def.separator
  1003.     ),
  1004.     the_current_declaration <<- ref(TypeDefinition),
  1005.     chain_declarations
  1006.   ;
  1007.     local_error(strcon("Duplicate definition of ", Name),
  1008.                 Identifier, warning)
  1009.   ),
  1010.   build_type_definition(Typedef, TypeDeclarators).
  1011.  
  1012.  
  1013. build_type_definition(Typedef, []).
  1014.  
  1015.  
  1016. %
  1017. % ------------------------------------------------------------------------------
  1018. %
  1019.  
  1020.  
  1021. single_sort(external_declarations) -> external_declaration.
  1022.  
  1023.  
  1024. single_sort(local_declarations) -> local_declaration.
  1025.  
  1026.  
  1027. %
  1028. % ------------------------------------------------------------------------------
  1029. %
  1030.  
  1031.  
  1032. non_strict (build_declaration) ?
  1033.  
  1034.  
  1035. build_declaration([@(name => Name : anonymous) | Declarators], Where,
  1036.                   NodeName) :-
  1037.   !,
  1038.   local_error("Bad Declaration", Name),
  1039.   fail.
  1040.  
  1041.  
  1042. build_declaration([Declarator : @(name => Identifier : identifier(Name), 
  1043.                                   type => Type) 
  1044.                    | Declarators], Where, NodeName) :-
  1045.   !,
  1046.   Declaration = current_declarations.NodeName.Name,
  1047.   (
  1048.     Declaration :== @,
  1049.     !,
  1050.     Declaration <<- single_sort(NodeName),
  1051.     Declaration.type <<- Type,
  1052.     Declaration.name <<- Identifier,
  1053.     Declaration.scope <<- Where,
  1054.     (
  1055.       Declarators :== [],
  1056.       !,
  1057.       (
  1058.         has_feature(separator, Declarator, Separator),
  1059.         Separator :== ';',
  1060.         !
  1061.       ;
  1062.         recover_token(Separator, ';')
  1063.       ),
  1064.       Declaration.separator <<- Separator
  1065.     ;
  1066.       Declaration.separator <<- Declarator.separator
  1067.     ),
  1068.     (
  1069.       has_feature(initialization, Declarator, Initialization),
  1070.       !,
  1071.       Declaration.initialization <<- Initialization
  1072.     ;
  1073.       Declaration.initialization <<- nothing
  1074.     ),
  1075.     the_current_declaration <<- ref(Declaration),
  1076.     chain_declarations,
  1077.     (
  1078.       not_a_function(Name),
  1079.       !
  1080.     ;
  1081.       local_error(strcon("Redeclaration of function ", Name),
  1082.                   Identifier)
  1083.     ),
  1084.     (
  1085.       not_an_enumerator(Name),
  1086.       !
  1087.     ;
  1088.       local_error(strcon("Conflict with enumerator ", Name),
  1089.                   Identifier)
  1090.     )
  1091.   ;
  1092.     local_error(strcon("Duplicate declaration of ", Name),
  1093.                 Identifier, warning)
  1094.   ),
  1095.   build_declaration(Declarators, Where, NodeName).
  1096.  
  1097.  
  1098. build_declaration([]).
  1099.  
  1100.  
  1101. %
  1102. % ------------------------------------------------------------------------------
  1103. %
  1104.  
  1105.  
  1106. check_struct_and_union_declarations :-
  1107.   S = features(current_declarations.struct_declarations, current_module),
  1108.   check_struct_declarations(S),
  1109.   U = features(current_declarations.union_declarations, current_module),
  1110.   check_union_declarations(S).
  1111.  
  1112.  
  1113. %
  1114. % ------------------------------------------------------------------------------
  1115. %
  1116.  
  1117.  
  1118. check_struct_declarations([Name | LNames]) :-
  1119.   !,
  1120.   (
  1121.     struct_occurence(current_declarations.struct_declarations.Name.name,
  1122.                      current_declarations),
  1123.     !
  1124.   ;
  1125.     local_error(strcon("Undefined structure ", psi2str(Name)))
  1126.   ),
  1127.   check_struct_declarations(LNames).
  1128.  
  1129.  
  1130. check_struct_declarations([]).
  1131.  
  1132.  
  1133. %
  1134. % ------------------------------------------------------------------------------
  1135. %
  1136.  
  1137.  
  1138. check_union_declarations([Name | LNames]) :-
  1139.   !,
  1140.   (
  1141.     union_occurence(current_declarations.union_declarations.Name.name,
  1142.                     current_declarations),
  1143.     !
  1144.   ;
  1145.     local_error(strcon("Undefined union ", psi2str(Name)))
  1146.   ),
  1147.   check_struct_declarations(LNames).
  1148.  
  1149.  
  1150. check_union_declarations([]).
  1151.  
  1152.  
  1153. %
  1154. % ------------------------------------------------------------------------------
  1155. %
  1156.  
  1157.  
  1158.  
  1159. non_strict (build_local_declarations) ?
  1160.  
  1161.  
  1162. build_local_declarations([@(name => Name : anonymous) | Declarators],
  1163.                          Where) :-
  1164.   !,
  1165.   local_error("Bad Declaration", Name),
  1166.   fail.
  1167.  
  1168.  
  1169. build_local_declarations([@(name => Identifier : identifier(Name),
  1170.                             type => Type,
  1171.                             separator => Separator) 
  1172.                         | Declarators], Where) :-
  1173.   !,
  1174.   Declaration = current_declarations.Name, 
  1175.   (
  1176.     Declaration :== @,
  1177.     !,
  1178.     Declaration <<- parameter_declaration,
  1179.     Declaration.1 <<- parameter,
  1180.     Declaration.1.name <<- Identifier,
  1181.     Declaration.1.type <<- Type,
  1182.     Declaration.1.separator <<- Separator,
  1183.     Declaration.scope <<- Where,
  1184.     the_current_declaration <<- ref(Declaration),
  1185.     chain_declarations
  1186.   ;
  1187.     local_error(strcon("Duplicate declaration of ", Name),
  1188.                 Identifier, warning)
  1189.   ),
  1190.   build_local_declarations(Declarators, Where).
  1191.  
  1192.  
  1193. build_local_declarations([]).
  1194.  
  1195.  
  1196. %
  1197. % ------------------------------------------------------------------------------
  1198. %
  1199.  
  1200.  
  1201. check_coherence(FunctionName, 
  1202.                 [@(type => function(parameters => Parameters))]) :-
  1203.   Number = Parameters.parameters_number,
  1204.   TheNumber = Number + 1,
  1205.   cond(Number :\== 0,
  1206.     check_labels(FunctionName, Parameters, 1, TheNumber)
  1207.   ).
  1208.  
  1209.  
  1210. %
  1211. % ------------------------------------------------------------------------------
  1212. %
  1213.  
  1214.  
  1215. non_strict(check_labels) ?
  1216.  
  1217.  
  1218. check_labels(FunctionName, Parameters, Number, Number) :- !.
  1219.  
  1220.  
  1221. check_labels(FunctionName, Parameters, Number, Total) :-
  1222.   Parameters.Number = label_parameter(label => Token : @(Name)),
  1223.   (
  1224.     has_feature(Name, current_declarations),
  1225.     !
  1226.   ;
  1227.     add_default_declaration(FunctionName, Token),
  1228.     local_error("Parameter undefined", Token, warning)
  1229.   ),
  1230.   NewNumber = Number + 1,
  1231.   check_labels(FunctionName, Parameters, NewNumber, Total).
  1232.  
  1233.  
  1234. %
  1235. % ------------------------------------------------------------------------------
  1236. %
  1237.  
  1238.  
  1239. add_default_declaration(FunctionName, TokenName : @(Name)) :-
  1240.   Declaration = current_declarations.Name,
  1241.   Declaration <<- parameter_declaration,
  1242.   Declaration.1 <<- parameter,
  1243.   Declaration.1.name <<- TokenName,
  1244.   DefaultType = default_type,
  1245.   Declaration.1.type <<- build_type([DefaultType], @, @),
  1246.   Declaration.1.separator <<- nothing,
  1247.   Declaration.scope <<- function_head(FunctionName),
  1248.   the_current_declaration <<- ref(Declaration),
  1249.   chain_declarations.
  1250.  
  1251.  
  1252. %
  1253. % ------------------------------------------------------------------------------
  1254. %
  1255.  
  1256.  
  1257. non_strict (build_function) ?
  1258.  
  1259.  
  1260. build_function(style => Style, FunctionDeclarator, LocalDeclarations,
  1261.                FunctionBody) :-
  1262.   !,
  1263.   FunctionDeclarator = [@(name => TokenName : identifier(Name), type => Type)],
  1264.   FunctionDef = top_declarations.function_definitions.Name,
  1265.   (
  1266.     FunctionDef :== @,
  1267.     !,
  1268.     FunctionDef <<- function_definition,
  1269.     FunctionDef.name <<- TokenName,
  1270.     FunctionDef.style <<- Style,
  1271.     FunctionDef.body <<- FunctionBody,
  1272.     FunctionDef.type <<- Type,
  1273.     FunctionDef.parameters_declarations <<- LocalDeclarations,
  1274.     FunctionDef.body.scope <<- function_body(Name),
  1275.     the_current_declaration <<- ref(FunctionDef),
  1276.     chain_declarations,
  1277.     (
  1278.       not_an_enumerator(Name),
  1279.       !
  1280.     ;
  1281.       local_error(strcon("Conflict with enumerator ", Name),
  1282.                   TokenName)
  1283.     )
  1284.   ;
  1285.     local_error(strcon("Duplicate definition of function ", Name),
  1286.                 TokenName, warning)
  1287.   ).
  1288.  
  1289.  
  1290. %
  1291. % ------------------------------------------------------------------------------
  1292. %
  1293.  
  1294.  
  1295. non_strict(build_modern_local_declarations) ?
  1296.  
  1297.  
  1298. build_modern_local_declarations([FunctionDeclarator], Where) :-
  1299.   Parameters = (root_type(FunctionDeclarator.type)).parameters,
  1300.   ParametersNumber = Parameters.parameters_number,
  1301.   (
  1302.     ParametersNumber :== 1,
  1303.     expand_type(Parameters.1.type) :== void,
  1304.     Parameters.1.name :== anonymous,
  1305.     !
  1306.   ;
  1307.     set_previous_declaration(PreviousDeclaration),
  1308.     build_declarations_from_parameters(Parameters, 1, ParametersNumber, Where),
  1309.     restore_previous_declaration_from(PreviousDeclaration)
  1310.   ).
  1311.  
  1312.  
  1313. %
  1314. % ------------------------------------------------------------------------------
  1315. %
  1316.  
  1317.  
  1318. non_strict(build_declarations_from_parameters) ?
  1319.  
  1320.  
  1321. build_declarations_from_parameters(Parameters, CurrentParameterNumber, 
  1322.                                    ParametersNumber, Where) :-
  1323.   CurrentParameterNumber :== ParametersNumber + 1,
  1324.   !,
  1325.   end_declarations_chain.
  1326.  
  1327.  
  1328. build_declarations_from_parameters(Parameters, CurrentNumber, 
  1329.                                    ParametersNumber, Where) :-
  1330.   Parameters.CurrentNumber = Parameter & @(name => TokenName),
  1331.   (
  1332.     TokenName :== anonymous,
  1333.     !,
  1334.     local_error("Bad parameter", TokenName)
  1335.   ; 
  1336.     TokenName = identifier(Name),
  1337.     Declaration = current_declarations.Name,
  1338.     (
  1339.       Declaration :== @,
  1340.       !,
  1341.       Declaration <<- parameter_declaration(Parameter),
  1342.       Declaration.scope <<- Where,
  1343.       the_current_declaration <<- ref(Declaration),
  1344.       chain_declarations
  1345.     ;
  1346.       local_error(strcon("Duplicate parameter declaration", Name),
  1347.                   TokenName, warning)
  1348.     )
  1349.   ),
  1350.   NewCurrentNumber = CurrentNumber + 1,
  1351.   build_declarations_from_parameters(Parameters, NewCurrentNumber, 
  1352.                                      ParametersNumber, Where).
  1353.  
  1354.  
  1355. %
  1356. % ------------------------------------------------------------------------------
  1357. %
  1358.  
  1359.  
  1360. non_strict (build_pointer) ?
  1361.  
  1362.  
  1363. build_pointer(BaseType, Qualifiers, Star) ->
  1364.   Pointer : pointer(to => BaseType, star => Star)
  1365.   | Pointer.qualification = build_qualifiers(Qualifiers).
  1366.  
  1367.  
  1368. %
  1369. % ------------------------------------------------------------------------------
  1370. %
  1371.  
  1372.  
  1373. build_array(BaseType, Array) ->
  1374.   array(dimensions => Array, of => BaseType).
  1375.  
  1376.  
  1377. %
  1378. % ------------------------------------------------------------------------------
  1379. %
  1380.  
  1381.  
  1382. non_strict (build_qualifiers) ?
  1383.  
  1384.  
  1385. build_qualifiers([]) -> nothing.
  1386.  
  1387. build_qualifiers([Const : const]) -> const(qualification => Const).
  1388.  
  1389. build_qualifiers([Volatile : volatile]) -> volatile(qualification => Volatile).
  1390.  
  1391. build_qualifiers([Qualifier | @]) -> nothing
  1392.   | local_error("Bad qualifier", Qualifier).
  1393.  
  1394.  
  1395. %
  1396. % ------------------------------------------------------------------------------
  1397. %
  1398.  
  1399.  
  1400. non_strict (build_struct_name) ?
  1401.  
  1402.  
  1403.  
  1404. build_struct_name(Struct : struct, Name) ->
  1405.   struct_name(Name, keyword => Struct).
  1406.  
  1407.  
  1408. %
  1409. % ------------------------------------------------------------------------------
  1410. %
  1411.  
  1412.  
  1413. non_strict (build_union_name) ?
  1414.  
  1415.  
  1416. build_union_name(Union : union, Name) ->
  1417.   union_name(Name, keyword => Union).
  1418.  
  1419.  
  1420. %
  1421. % ------------------------------------------------------------------------------
  1422. %
  1423.  
  1424.  
  1425. non_strict (build_enum_name) ?
  1426.  
  1427.  
  1428. build_enum_name(Enum, Name) ->
  1429.   enum_name(Name, keyword => Enum).
  1430.  
  1431.  
  1432. %
  1433. % ------------------------------------------------------------------------------
  1434. %
  1435.  
  1436.  
  1437. non_strict(add_struct_definition) ?
  1438.  
  1439.  
  1440. add_struct_definition(Keyword : struct, TokenName, Body) :-
  1441.   Name = TokenName.1,
  1442.   StructDef = current_declarations.struct_definitions.Name,
  1443.   (
  1444.     StructDef :== @,
  1445.     !,
  1446.     StructDef <<- struct_definition,
  1447.     StructDef.name <<- TokenName,
  1448.     StructDef.keyword <<- Keyword,
  1449.     StructDef.body <<- Body,
  1450.     the_current_declaration <<- ref(StructDef),
  1451.     chain_declarations
  1452.   ;
  1453.     local_error(strcon("Duplicate declaration of ", Name), 
  1454.                 TokenName, warning)
  1455.   ).
  1456.  
  1457.  
  1458. %
  1459. % ------------------------------------------------------------------------------
  1460. %
  1461.  
  1462.  
  1463. non_strict(add_union_definition) ?
  1464.  
  1465.  
  1466. add_union_definition(Keyword : union, TokenName, Body) :-
  1467.   Name = TokenName.1,
  1468.   UnionDef = current_declarations.union_definitions.Name,
  1469.   (
  1470.     UnionDef :== @,
  1471.     !,
  1472.     UnionDef <<- union_definition,
  1473.     UnionDef.name <<- TokenName,
  1474.     UnionDef.keyword <<- Keyword,
  1475.     UnionDef.body <<- Body,
  1476.     the_current_declaration <<- ref(UnionDef),
  1477.     chain_declarations
  1478.   ;
  1479.     local_error(strcon("Duplicate declaration of ", Name), 
  1480.                 TokenName, warning)
  1481.   ).
  1482.  
  1483.  
  1484. %
  1485. % ------------------------------------------------------------------------------
  1486. %
  1487.  
  1488.  
  1489. non_strict(add_enum_definition) ?
  1490.  
  1491.  
  1492. add_enum_definition(Keyword, TokenName, Body) :-
  1493.   Name = TokenName.1,
  1494.   EnumDef = current_declarations.enum_definitions.(TokenName.1),
  1495.   (
  1496.     EnumDef :== @,
  1497.     !,
  1498.     EnumDef <<- enum_definition,
  1499.     EnumDef.name <<- TokenName,
  1500.     EnumDef.keyword <<- Keyword,
  1501.     EnumDef.body <<- Body,
  1502.     the_current_declaration <<- ref(EnumDef),
  1503.     chain_declarations
  1504.   ;
  1505.     local_error(strcon("Duplicate declaration of ", Name), 
  1506.                 TokenName, warning)
  1507.   ).
  1508.  
  1509.  
  1510. %
  1511. % ------------------------------------------------------------------------------
  1512. %
  1513.  
  1514.  
  1515. non_strict (build_struct_definition) ?
  1516.  
  1517.  
  1518. build_struct_definition(Specifier, Name, Body) ->
  1519.   root_sort(Specifier) & @(Body, name => Name, keyword => Specifier).
  1520.  
  1521.  
  1522. %
  1523. % ------------------------------------------------------------------------------
  1524. %
  1525.  
  1526.  
  1527. non_strict (build_enum_definition) ?
  1528.  
  1529.  
  1530. build_enum_definition(Specifier, Name, Body) ->
  1531.   root_sort(Specifier) & @(Body, name => Name, keyword => Specifier).
  1532.  
  1533.  
  1534. %
  1535. % ------------------------------------------------------------------------------
  1536. %
  1537.  
  1538.  
  1539. non_strict(add_struct_declaration) ?
  1540.  
  1541.  
  1542. add_struct_declaration(struct_name(Name, keyword => Keyword), SemiColon,
  1543.                        Scope) :-
  1544.   StructDeclaration <<- struct_declaration,
  1545.   StructDeclaration.name <<- Name, 
  1546.   StructDeclaration.keyword <<- Keyword, 
  1547.   StructDeclaration.semi_colon <<- SemiColon,
  1548.   StructDeclaration.scope <<- Scope,
  1549.   the_current_declaration <<- ref(StructDeclaration),
  1550.   chain_declarations,
  1551.   (
  1552.     has_feature(Name.1, current_declarations.struct_declarations),
  1553.     !,
  1554.     local_error("Redundant struct declaration", Name, warning)
  1555.   ;
  1556.     succeed
  1557.   ).
  1558.  
  1559.  
  1560. %
  1561. % ------------------------------------------------------------------------------
  1562. %
  1563.  
  1564.  
  1565. non_strict(add_union_declaration) ?
  1566.  
  1567.  
  1568. add_union_declaration(union_name(Name, keyword => Keyword), SemiColon,
  1569.                       Scope) :-
  1570.   UnionDeclaration <<- union_declaration,
  1571.   UnionDeclaration.name <<- Name, 
  1572.   UnionDeclaration.keyword <<- Keyword, 
  1573.   UnionDeclaration.semi_colon <<- SemiColon,
  1574.   UnionDeclaration.scope <<- Scope,
  1575.   the_current_declaration <<- ref(UnionDeclaration),
  1576.   chain_declarations,
  1577.   (
  1578.     has_feature(Name.1, current_declarations.union_declarations),
  1579.     !,
  1580.     local_error("Redundant union declaration", Name, warning)
  1581.   ;
  1582.     succeed
  1583.   ).
  1584.  
  1585.  
  1586. %
  1587. % ------------------------------------------------------------------------------
  1588. %
  1589.  
  1590.  
  1591. non_strict(add_enum_declaration) ?
  1592.  
  1593.  
  1594. add_enum_declaration(enum_name(Name, keyword => Keyword), SemiColon,
  1595.                      Scope) :-
  1596.   EnumDeclaration <<- enum_declaration,
  1597.   EnumDeclaration.name <<- Name, 
  1598.   EnumDeclaration.keyword <<- Keyword, 
  1599.   EnumDeclaration.semi_colon <<- SemiColon,
  1600.   EnumDeclaration.scope <<- Scope,
  1601.   the_current_declaration <<- ref(StructDeclaration),
  1602.   chain_declarations,
  1603.   (
  1604.     has_feature(Name.1, current_declarations.enum_declarations),
  1605.     !,
  1606.     local_error("Redundant enum declaration", Name, warning)
  1607.   ;
  1608.     succeed
  1609.   ).
  1610.  
  1611.  
  1612. %
  1613. % ------------------------------------------------------------------------------
  1614. %
  1615.  
  1616.  
  1617. non_strict (build_struct_or_union_body) ?
  1618.  
  1619.  
  1620. build_struct_or_union_body(struct, LeftBrace, StructDeclarations, RightBrace) ->
  1621.   struct_body(left_brace => LeftBrace,
  1622.               fields =>  StructDeclarations & fields,
  1623.               right_brace => RightBrace).
  1624.  
  1625.  
  1626. build_struct_or_union_body(union, LeftBrace, StructDeclarations, RightBrace) ->
  1627.   union_body(left_brace => LeftBrace,
  1628.              fields =>  StructDeclarations & fields,
  1629.              right_brace => RightBrace).
  1630.  
  1631.  
  1632. %
  1633. % ------------------------------------------------------------------------------
  1634. %
  1635.  
  1636.  
  1637. non_strict (build_enum_body) ?
  1638.  
  1639.  
  1640. build_enum_body(LeftBrace, Enumeration, RightBrace) ->
  1641.   enum_body(left_brace => LeftBrace,
  1642.             enumerators => Enumeration & enumerators,
  1643.             right_brace => RightBrace).
  1644.  
  1645.  
  1646. %
  1647. % ------------------------------------------------------------------------------
  1648. %
  1649.  
  1650.  
  1651. non_strict (build_struct_declaration) ?
  1652.  
  1653.  
  1654. build_struct_declaration([@(name => Name : anonymous) | Declarators],
  1655.                          Declarations) :-
  1656.   !,
  1657.   local_error("Bad declaration", Name),
  1658.   fail.
  1659.  
  1660.  
  1661. build_struct_declaration([Declarator : @(name => Identifier, % : identifier(Name), 
  1662.                                          type => Type,
  1663.                                          separator => Separator) 
  1664.                           | Declarators], Declarations) :-
  1665.   Identifier = {identifier(Name); anonymous_tag(id => Name)},
  1666.   !,
  1667.   Declaration = Declarations.Name,
  1668.   (
  1669.     Declaration :== @,
  1670.     !,
  1671.     Declaration <<- field,
  1672.     Declaration.type <<- Type, 
  1673.     Declaration.name <<- Identifier,
  1674.     Declaration.separator <<- Separator,
  1675.     (
  1676.       has_feature(initialization, Declarator, Initialization),
  1677.       !,
  1678.       Declaration.initialization <<- Initialization
  1679.     ;
  1680.       Declaration.initialization <<- nothing
  1681.     ),
  1682.     the_current_declaration <<- ref(Declaration),
  1683.     chain_declarations
  1684.   ;
  1685.     local_error(strcon("Duplicate definition of ", Name), 
  1686.                 Identifier, warning)
  1687.   ),    
  1688.   build_struct_declaration(Declarators, Declarations).
  1689.  
  1690.  
  1691. build_struct_declaration([], Declarations).
  1692.  
  1693.  
  1694. %
  1695. % ------------------------------------------------------------------------------
  1696. %
  1697.  
  1698.  
  1699. non_strict (build_enum_declaration) ?
  1700.  
  1701.  
  1702. build_enum_declaration([Declarator : @(name => Identifier : identifier(Name),
  1703.                                        separator => Separator) 
  1704.                          | Declarators], Declarations) :-
  1705.   Declaration = Declarations.Name, 
  1706.   (
  1707.     Declaration :== @,
  1708.     no_conflict_for_enumerator(Name),
  1709.     !,
  1710.     Declaration <<- enumerator,
  1711.     Declaration.name <<- Identifier,
  1712.     Declaration.separator <<- Separator,
  1713.     (
  1714.       has_feature(initialization, Declarator, Initialization),
  1715.       !,
  1716.       Declaration.initialization <<- Initialization
  1717.     ;
  1718.       Declaration.initialization <<- nothing
  1719.     ),
  1720.     the_current_declaration <<- ref(Declaration),
  1721.     chain_declarations
  1722.   ;
  1723.     local_error(strcon("Duplicate definition of ", Name), 
  1724.                 Identifier, warning)
  1725.   ),
  1726.   (
  1727.     Declarators :== [],
  1728.     !,
  1729.     (
  1730.       Separator :== ',',
  1731.       !,
  1732.       local_error("Unexpected comma", Separator)
  1733.     ;
  1734.       succeed
  1735.     )
  1736.   ;
  1737.     build_enum_declaration(Declarators, Declarations)
  1738.   ).
  1739.  
  1740.  
  1741. %
  1742. % ------------------------------------------------------------------------------
  1743. %
  1744.  
  1745.  
  1746. build_tag_name -> anonymous_tag(id => strcon("tag*", int2str(tag_number)))
  1747.   | tag_number <<- tag_number + 1.
  1748.  
  1749.  
  1750. %
  1751. % ------------------------------------------------------------------------------
  1752. %
  1753.  
  1754.  
  1755. non_strict(build_function_declaration) ?
  1756.  
  1757.  
  1758. build_function_declaration(BaseType, LeftParenthesis, Parameters,
  1759.                            RightParenthesis) ->
  1760.   F : function(return_type => BaseType,
  1761.                left_parenthesis => LeftParenthesis,
  1762.                right_parenthesis => RightParenthesis)
  1763.   | set_previous_declaration(PreviousDeclaration),
  1764.     cond(Parameters :== nothing,
  1765.       TheParameters <<- nothing,
  1766.       (
  1767.         TheParameters <<- parameters,
  1768.         build_parameters(Parameters, TheParameters, 1)
  1769.       )
  1770.     ),
  1771.     F.parameters <<- TheParameters,
  1772.     restore_previous_declaration_from(PreviousDeclaration).
  1773.  
  1774.  
  1775. %
  1776. % ------------------------------------------------------------------------------
  1777. %
  1778.  
  1779.  
  1780. non_strict(build_parameters) ?
  1781.  
  1782.  
  1783. build_parameters([Parameter | Parameters], Where, Number) :-
  1784.   !,
  1785.   (
  1786.     Parameter :== '...',
  1787.     !,
  1788.     Where.vararg <<- true,
  1789.     end_declarations_chain,
  1790.     Where.parameters_number <<- Number - 1,
  1791.     Where.suspension_points <<- Parameter,
  1792.     cond(Parameters :\== [],
  1793.       local_error("Garbage parameters after '...'", Parameter)
  1794.     )
  1795.   ;
  1796.     the_current_declaration <<- ref(Parameter),
  1797.     chain_declarations,
  1798.     Where.Number <<- current_declaration,
  1799.     NewNumber = Number + 1,
  1800.     build_parameters(Parameters, Where, NewNumber)
  1801.   ).
  1802.  
  1803.  
  1804. build_parameters([], Where, Number) :-
  1805.   !,
  1806.   cond(Where.vararg :== @,
  1807.     Where.vararg <<- false
  1808.   ),
  1809.   Where.parameters_number <<- Number - 1.
  1810.  
  1811.  
  1812. %
  1813. % ------------------------------------------------------------------------------
  1814. %
  1815.  
  1816.  
  1817. non_strict (build_tag) ?
  1818.  
  1819.  
  1820. build_tag(type => BaseType) ->
  1821.   tag(type => BaseType).
  1822.  
  1823.  
  1824. %
  1825. % ------------------------------------------------------------------------------
  1826. %
  1827.  
  1828.  
  1829. build_tag_value(Initialization, Colon) ->
  1830.   setting(keyword => Colon, setting => Initialization).
  1831.  
  1832.  
  1833. %
  1834. % ------------------------------------------------------------------------------
  1835. %
  1836.  
  1837.  
  1838. non_strict (build_protected_type) ?
  1839.  
  1840.  
  1841. build_protected_type(InnerType, LeftParenthesis, RightParenthesis) ->
  1842.   protected_type(InnerType, 
  1843.                  left_parenthesis => LeftParenthesis, 
  1844.                  right_parenthesis => RightParenthesis).
  1845.  
  1846.  
  1847. %
  1848. % ------------------------------------------------------------------------------
  1849. %
  1850.  
  1851.  
  1852. build_vararg(SuspensionPoints) -> SuspensionPoints.
  1853.  
  1854.  
  1855. %
  1856. % ------------------------------------------------------------------------------
  1857. %
  1858.  
  1859.  
  1860. non_strict (build_parameter) ?
  1861.  
  1862.  
  1863. build_parameter(Type, Name, Comma) ->
  1864.   Parameter : parameter(type => Type, name => Name) 
  1865.    |  cond(Comma :\== @,
  1866.         add_separator(Comma, Parameter),
  1867.         add_separator(nothing, Parameter)
  1868.       ).   
  1869.  
  1870.  
  1871. %
  1872. % ------------------------------------------------------------------------------
  1873. %
  1874.  
  1875.  
  1876. non_strict(build_identifier_parameter) ?
  1877.  
  1878.  
  1879. build_identifier_parameter(Identifier) -> Parameter
  1880.   | the_current_declaration <<- ref(label_parameter(label => Identifier)),
  1881.     chain_declarations,
  1882.     Parameter = current_declaration.
  1883.  
  1884.  
  1885. %
  1886. % ------------------------------------------------------------------------------
  1887. %
  1888.  
  1889.  
  1890. non_strict(build_function_call) ?
  1891.  
  1892.  
  1893. build_function_call(LeftExpression, LeftParenthesis, Arguments, 
  1894.                     RightParenthesis) ->
  1895.   function_call(call => LeftExpression,
  1896.                 mode => special,
  1897.                 operator => nothing,
  1898.                 left_parenthesis => LeftParenthesis, 
  1899.                 arguments => TheArguments,
  1900.                 right_parenthesis => RightParenthesis)
  1901.   | set_previous_declaration(PreviousDeclaration),
  1902.     TheArguments <<- arguments,
  1903.     collect_expressions_arguments(Arguments, TheArguments, 0),
  1904.     end_declarations_chain,
  1905.     restore_previous_declaration_from(PreviousDeclaration).
  1906.  
  1907.  
  1908. %
  1909. % ------------------------------------------------------------------------------
  1910. %
  1911.  
  1912.  
  1913. non_strict(collect_expressions_arguments) ?
  1914.  
  1915.  
  1916. collect_expressions_arguments([], Arguments, Number) :-
  1917.   !,
  1918.   Arguments.arguments_number <<- Number.
  1919.  
  1920.  
  1921. collect_expressions_arguments([Expression | Expressions], Arguments, N) :-
  1922.   NN = N + 1,
  1923.   the_current_declaration <<- ref(Expression),
  1924.   chain_declarations,
  1925.   Arguments.NN <<- current_declaration,
  1926.   collect_expressions_arguments(Expressions, Arguments, NN).
  1927.  
  1928.  
  1929. %
  1930. % ------------------------------------------------------------------------------
  1931. %
  1932.  
  1933.  
  1934. build_argument(Expression) -> argument(Expression).
  1935.  
  1936.  
  1937. %
  1938. % ------------------------------------------------------------------------------
  1939. %
  1940.  
  1941.  
  1942. non_strict(build_protected_expression) ?
  1943.  
  1944.  
  1945. build_protected_expression(Expression, LeftParenthesis, RightParenthesis) ->
  1946.   protected_expression(Expression,
  1947.                        operator => nothing,
  1948.                        left_parenthesis => LeftParenthesis, 
  1949.                        right_parenthesis => RightParenthesis,
  1950.                        mode => special).
  1951.  
  1952.  
  1953. %
  1954. % ------------------------------------------------------------------------------
  1955. %
  1956.  
  1957.  
  1958. non_strict(build_sequence_expression) ?
  1959.  
  1960.  
  1961. build_sequence_expression(LeftExpression, AssignmentExpression, Comma) ->
  1962.   sequence(LeftExpression, AssignmentExpression,
  1963.            mode => infix, operator => Comma).
  1964.  
  1965.  
  1966. %
  1967. % ------------------------------------------------------------------------------
  1968. %
  1969.  
  1970.  
  1971. non_strict(build_binary_expression) ?
  1972.  
  1973.  
  1974. build_binary_expression(Operator, LeftExpression, RightExpression) ->
  1975.   root_sort(Operator) & @(LeftExpression, RightExpression, 
  1976.                          operator => Operator, mode => infix).
  1977.  
  1978.  
  1979. %
  1980. % ------------------------------------------------------------------------------
  1981. %
  1982.  
  1983.  
  1984. non_strict(build_prefix_expression) ?
  1985.  
  1986.  
  1987. build_prefix_expression(Operator, InnerExpression) ->
  1988.   root_sort(Operator) & @(operator => Operator, InnerExpression, mode => prefix).
  1989.  
  1990.  
  1991. %
  1992. % ------------------------------------------------------------------------------
  1993. %
  1994.  
  1995.  
  1996. non_strict(build_postfix_expression) ?
  1997.  
  1998.  
  1999. build_postfix_expression(InnerExpression, Operator) ->
  2000.   root_sort(Operator) & @(operator => Operator, InnerExpression, mode => postfix).
  2001.  
  2002.  
  2003. %
  2004. % ------------------------------------------------------------------------------
  2005. %
  2006.  
  2007.  
  2008. non_strict (build_conditional_expression) ?
  2009.  
  2010.  
  2011. build_conditional_expression(Condition, QuestionMark, Then, Colon, Else) ->
  2012.   '?'(condition => Condition, then => Then, else => Else,
  2013.       operator => QuestionMark, colon => Colon, mode => special).
  2014.  
  2015.  
  2016. %
  2017. % ------------------------------------------------------------------------------
  2018. %
  2019.  
  2020.  
  2021. non_strict (build_cast) ?
  2022.  
  2023.  
  2024. build_cast(LeftParenthesis, TypeName, CastExpression,
  2025.            RightParenthesis) ->
  2026.   cast(left_parenthesis => LeftParenthesis, 
  2027.        type => TypeName,
  2028.        operator => nothing,
  2029.        mode => special,
  2030.        expression => CastExpression,
  2031.        right_parenthesis => RightParenthesis).
  2032.  
  2033.  
  2034. %
  2035. % ------------------------------------------------------------------------------
  2036. %
  2037.  
  2038.  
  2039. non_strict (build_array_reference) ?
  2040.  
  2041.  
  2042. build_array_reference(LeftExpression, LeftBracket, Index, RightBracket) ->
  2043.   array_reference(array => LeftExpression,
  2044.                   mode => special,
  2045.                   operator => nothing,
  2046.                   left_bracket => LeftBracket, 
  2047.                   index => Index, 
  2048.                   right_bracket => RightBracket).
  2049.  
  2050.  
  2051. %
  2052. % ------------------------------------------------------------------------------
  2053. %
  2054.  
  2055.  
  2056. non_strict (build_struct_reference) ?
  2057.  
  2058.  
  2059. build_struct_reference(LeftExpression, Operator, Field) ->
  2060.   root_sort(Operator) & @(LeftExpression, operator => Operator, 
  2061.                           Field, mode => infix).
  2062.   
  2063.  
  2064. %
  2065. % ------------------------------------------------------------------------------
  2066. %
  2067.  
  2068.  
  2069. non_strict (build_initializer) ?
  2070.  
  2071.  
  2072. build_initializer(Operator, InitializerBody) ->
  2073.   setting(keyword => Operator, setting => InitializerBody).
  2074.  
  2075.  
  2076. %
  2077. % ------------------------------------------------------------------------------
  2078. %
  2079.  
  2080.  
  2081. non_strict (build_dimension) ?
  2082.  
  2083.  
  2084. build_dimension(Dimension, LeftBracket, RightBracket) ->
  2085.   dimension(size => Dimension, left_bracket => LeftBracket, 
  2086.             right_bracket => RightBracket).
  2087.  
  2088.  
  2089. %
  2090. % ------------------------------------------------------------------------------
  2091. %
  2092.  
  2093.  
  2094. check_for_type_name(anonymous) :- !.
  2095.  
  2096.  
  2097. check_for_type_name(DeclaratorName) :-
  2098.   local_error(strcon(strcon("Bad declarator '", 
  2099.                      psi2str(display_token(DeclaratorName))),
  2100.                      "' in cast expression"), DeclaratorName).
  2101.  
  2102.  
  2103. %
  2104. % ------------------------------------------------------------------------------
  2105. %
  2106.  
  2107.  
  2108. validate_identifier(Identifier : identifier) :-
  2109.   !,
  2110.   (
  2111.     search_declaration_of(Identifier.1, current_declarations),
  2112.     !
  2113.   ;
  2114.     local_error("Unknown identifier", Identifier)
  2115.   ).
  2116.  
  2117.  
  2118. validate_identifier.
  2119.  
  2120.  
  2121. %
  2122. % ------------------------------------------------------------------------------
  2123. %
  2124.  
  2125.  
  2126. search_declaration_of(Name, nothing) :-
  2127.   !,
  2128.   fail.
  2129.  
  2130.  
  2131. search_declaration_of(Name, Declarations : toplevel) :-
  2132.   has_feature(Name, Declarations.external_declarations),
  2133.   !.
  2134.  
  2135.  
  2136. search_declaration_of(Name, Declarations : block_declarations) :-
  2137.   has_feature(Name, Declarations.local_declarations),
  2138.   !.
  2139.  
  2140.  
  2141. search_declaration_of(Name, Declarations : parameters_declarations) :-
  2142.   has_feature(Name, Declarations),
  2143.   !.
  2144.  
  2145.  
  2146. search_declaration_of(Name, Declarations) :-
  2147.   has_feature(enum_definitions, Declarations),
  2148.   Enumerations = features(E : (Declarations.enum_definitions)),
  2149.   search_enumerator(Name, Enumerations, E),
  2150.   !.
  2151.  
  2152.  
  2153. search_declaration_of(Name, Declarations) :-
  2154.   (
  2155.     Declarations :== parameters_declarations,
  2156.     !,
  2157.     search_declaration_of(Name, top_declarations)
  2158.   ;
  2159.     search_declaration_of(Name, Declarations.parent_declarations)
  2160.   ).
  2161.  
  2162.  
  2163. %
  2164. % ------------------------------------------------------------------------------
  2165. %
  2166.  
  2167.  
  2168. search_enumerator(What, [Enumeration | Enumerations], EnumDefinitions) :-
  2169.   !,
  2170.   (
  2171.     has_feature(What, EnumDefinitions.Enumeration.body.enumerators),
  2172.     !
  2173.   ;
  2174.     search_enumerator(What, Enumerations, EnumDefinitions)
  2175.   ).
  2176.  
  2177.  
  2178. search_enumerator(What, []) :-
  2179.   fail.
  2180.  
  2181.  
  2182. %
  2183. % ------------------------------------------------------------------------------
  2184. %
  2185.  
  2186.  
  2187. no_conflict_for_enumerator(What) :-
  2188.   not_an_enumerator(What),
  2189.   not_a_variable(What).
  2190.  
  2191.  
  2192. %
  2193. % ------------------------------------------------------------------------------
  2194. %
  2195.  
  2196.  
  2197. not_an_enumerator(What) :-
  2198.   Enumerations = features(E : (current_declarations.enum_definitions)),
  2199.   (
  2200.     search_enumerator(What, Enumerations, E),
  2201.     !,
  2202.     fail
  2203.   ;
  2204.     succeed
  2205.   )
  2206. .
  2207.  
  2208.  
  2209. %
  2210. % ------------------------------------------------------------------------------
  2211. %
  2212.  
  2213.  
  2214. not_a_variable(What) :-
  2215.   cond(current_declarations :== toplevel,
  2216.     Declarations = external_declarations,
  2217.     Declarations = local_declarations
  2218.   ),
  2219.   (
  2220.     has_feature(What, current_declarations.Declarations),
  2221.     !,
  2222.     fail
  2223.   ;
  2224.     succeed
  2225.   ).
  2226.  
  2227.  
  2228. %
  2229. % ------------------------------------------------------------------------------
  2230. %
  2231.  
  2232.  
  2233. not_a_function(What) :-
  2234.   (
  2235.     current_declarations :== toplevel,
  2236.     !,
  2237.     (
  2238.       has_feature(What, current_declarations.function_definitions),
  2239.       !,
  2240.       fail
  2241.     ;
  2242.       succeed
  2243.     )
  2244.   ;
  2245.     succeed
  2246.   ).
  2247.  
  2248.  
  2249. %
  2250. % ------------------------------------------------------------------------------
  2251. %
  2252. %  Modified : 2 15 1994
  2253. %
  2254. % ------------------------------------------------------------------------------
  2255. %
  2256.  
  2257. non_strict (build_labeled_instruction) ?
  2258.  
  2259.  
  2260. build_labeled_instruction(Label, Colon, Body) ->
  2261.   labeled_instruction(body => Body, label => Label, colon => Colon)
  2262.   | Body.previous <<- N:nothing,
  2263.     Body.next <<- N.
  2264.  
  2265.  
  2266. build_case(Case, Expression, Colon, Body) ->
  2267.   case(body => Body, condition => Expression, keyword => Case, colon => Colon)
  2268.   | Body.previous <<- N:nothing,
  2269.     Body.next <<- N.
  2270.  
  2271.  
  2272. build_default(Default, Colon, Body) ->
  2273.   default(body => Body, keyword => Default, colon => Colon)
  2274.   | Body.previous <<- N:nothing,
  2275.     Body.next <<- N.
  2276.  
  2277.  
  2278. %
  2279. % ------------------------------------------------------------------------------
  2280. %
  2281.  
  2282.  
  2283. non_strict (build_expression_instruction) ?
  2284.  
  2285.  
  2286. build_expression_instruction(Expression, SemiColon) ->
  2287.   expression_instruction(Expression, semi_colon => SemiColon).
  2288.  
  2289.  
  2290. %
  2291. % ------------------------------------------------------------------------------
  2292. %
  2293.  
  2294.  
  2295. non_strict (build_compound_instruction) ?
  2296.  
  2297.  
  2298. build_compound_instruction(LeftBrace, Body, RightBrace) ->
  2299.   I : block(left_brace => LeftBrace, 
  2300.             block_declarations => current_declarations, 
  2301.             instructions => Body,
  2302.             right_brace => RightBrace)
  2303.   | check_struct_and_union_declarations.
  2304.  
  2305.  
  2306. %
  2307. % ------------------------------------------------------------------------------
  2308. %
  2309.  
  2310.  
  2311. build_scope(Where) :-
  2312.   build_scope_for(current_declarations.type_definitions, Where),
  2313.   build_scope_for(current_declarations.struct_definitions, Where),
  2314.   build_scope_for(current_declarations.union_definitions, Where),
  2315.   build_scope_for(current_declarations.enum_definitions, Where),
  2316.   build_scope_for(current_declarations.struct_declarations, Where),
  2317.   build_scope_for(current_declarations.union_declarations, Where),
  2318.   build_scope_for(current_declarations.enum_declarations, Where).
  2319.  
  2320.  
  2321. %
  2322. % ------------------------------------------------------------------------------
  2323. %
  2324.  
  2325.  
  2326. build_scope_for(What, Where) :-
  2327.   F = features(What, current_module),
  2328.   F = [First | @],
  2329.   !,
  2330.   iter_build_scope_for(What.First, Where).
  2331.  
  2332.  
  2333. build_scope_for.
  2334.  
  2335.  
  2336. %
  2337. % ------------------------------------------------------------------------------
  2338. %
  2339.  
  2340.  
  2341. iter_build_scope_for(What, Where) :-
  2342.   What :\== nothing,
  2343.   !,
  2344.   What.scope <<- Where,
  2345.   iter_build_scope_for(What.next, Where).
  2346.  
  2347.  
  2348. iter_build_scope_for.
  2349.  
  2350.  
  2351. %
  2352. % ------------------------------------------------------------------------------
  2353. %
  2354.  
  2355.  
  2356. chain_body([Instruction | Instructions], Block) :-
  2357.   !,
  2358.   Instruction.scope <<- Block,
  2359.   chain_body(Instructions, Block).
  2360.  
  2361.  
  2362. chain_body([], Block).
  2363.  
  2364.  
  2365. %
  2366. % ------------------------------------------------------------------------------
  2367. %
  2368.  
  2369.  
  2370. non_strict (build_if) ?
  2371.  
  2372.  
  2373. build_if(If, LeftParenthesis, Condition, RightParenthesis, ThenBody, 
  2374.          Else, ElseBody) ->
  2375.   Instruction : if(keyword => If, 
  2376.                    left_parenthesis => LeftParenthesis,
  2377.                    condition => Condition,
  2378.                    right_parenthesis => RightParenthesis,
  2379.                    then_body => ThenBody)
  2380.   | cond(Else :\== @,
  2381.       true | (
  2382.         Instruction = @(else_body => ElseBody, else_keyword => Else),
  2383.         ElseBody.previous <<- N:nothing,
  2384.         ElseBody.next <<- N
  2385.       ),
  2386.       Instruction = @(else_body => N, else_keyword => N)
  2387.     ),
  2388.     ThenBody.previous <<- N,
  2389.     ThenBody.next <<- N.
  2390.  
  2391.  
  2392. %
  2393. % ------------------------------------------------------------------------------
  2394. %
  2395.  
  2396.  
  2397. non_strict (build_switch) ?
  2398.  
  2399.  
  2400. build_switch(Switch, LeftParenthesis, Condition, RightParenthesis, Body) ->
  2401.   switch(keyword => Switch,
  2402.          left_parenthesis => LeftParenthesis,
  2403.          condition => Condition,
  2404.          right_parenthesis => RightParenthesis,
  2405.          body => Body)
  2406.   | Body.previous <<- N:nothing,
  2407.     Body.next <<- N.
  2408.  
  2409.  
  2410. %
  2411. % ------------------------------------------------------------------------------
  2412. %
  2413.  
  2414.  
  2415. non_strict (build_while) ?
  2416.  
  2417.  
  2418. build_while(While, LeftParenthesis, Condition, RightParenthesis, Body) ->
  2419.   while(keyword => While,
  2420.         left_parenthesis => LeftParenthesis,
  2421.         condition => Condition,
  2422.         right_parenthesis => RightParenthesis,
  2423.         body => Body)
  2424.   | Body.previous <<- N:nothing,
  2425.     Body.next <<- N.
  2426.  
  2427.  
  2428. %
  2429. % ------------------------------------------------------------------------------
  2430. %
  2431.  
  2432.  
  2433. non_strict (build_do_while) ?
  2434.  
  2435.  
  2436. build_do_while(Do, Body, While, LeftParenthesis, Condition, 
  2437.                RightParenthesis, SemiCOlon) ->
  2438.   do_while(do_keyword => Do,
  2439.            while_keyword => While,
  2440.            left_parenthesis => LeftParenthesis,
  2441.            condition => Condition,
  2442.            right_parenthesis => RightParenthesis,
  2443.            body => Body,
  2444.            semi_colon => SemiColon)
  2445.   | Body.previous <<- N:nothing,
  2446.     Body.next <<- N.
  2447.  
  2448.  
  2449. %
  2450. % ------------------------------------------------------------------------------
  2451. %
  2452.  
  2453.  
  2454. non_strict (build_for) ?
  2455.  
  2456.  
  2457. build_for(For, LeftParenthesis, Initialization, SemiColon1,
  2458.           Condition, SemiColon2, Step, RightParenthesis, Body) ->
  2459.   for(keyword => For,
  2460.       left_parenthesis => LeftParenthesis,
  2461.       initialization => Initialization,
  2462.       first_semi_colon => SemiColon1,
  2463.       condition => Condition,
  2464.       second_semi_colon => SemiColon2,
  2465.       step => Step,
  2466.       right_parenthesis => RightParenthesis,
  2467.       body => Body)
  2468.   | Body.previous <<- N:nothing,
  2469.     Body.next <<- N.
  2470.  
  2471.  
  2472. %
  2473. % ------------------------------------------------------------------------------
  2474. %
  2475.  
  2476.  
  2477. build_jump(Jump) -> root_sort(Jump) & @(keyword => Jump).
  2478.  
  2479.  
  2480. %
  2481. % ------------------------------------------------------------------------------
  2482. %
  2483.  
  2484.  
  2485. build_goto(Goto, Label) -> goto(label => Label, keyword => Goto).
  2486.  
  2487.  
  2488. %
  2489. % ------------------------------------------------------------------------------
  2490. %
  2491.  
  2492.  
  2493. build_return(Return, Value) -> return(value => Value, keyword => Return).
  2494.  
  2495.  
  2496. %
  2497. % ------------------------------------------------------------------------------
  2498. %
  2499.  
  2500.  
  2501. build_complex_initialization(LeftBrace, Initializers, RightBrace) ->
  2502.   complex_initialization(left_brace => LeftBrace, 
  2503.                          body => Initializers, 
  2504.                          right_brace => RightBrace).
  2505.  
  2506.  
  2507. %
  2508. % ------------------------------------------------------------------------------
  2509. %
  2510.  
  2511.  
  2512. build_void_instruction(SemiColon) ->
  2513.   void_instruction(semi_colon => SemiColon).
  2514.  
  2515.  
  2516. %
  2517. % ------------------------------------------------------------------------------
  2518. %
  2519.  
  2520.  
  2521.  
  2522.  
  2523.  
  2524.  
  2525.  
  2526.  
  2527.  
  2528.  
  2529.