home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / SUPERLIN / C_UTILS.LF < prev    next >
Text File  |  1996-06-04  |  9KB  |  471 lines

  1. %
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %
  4. %
  5. %                       UTILITIES FOR C TOKENIZER
  6. %                       -------------------------
  7. %
  8. %
  9. %  AUTHOR : Arnaud Venet                        CREATION : July 28th 1993
  10. %  ------                                       --------
  11. %
  12. %
  13. %                            ---------------                        
  14. %                    
  15. %                   Last modification : October 21st 1993 
  16. %
  17. %
  18. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  19. %
  20. %  (C) Digital Equipment Corporation 1993 - 1994
  21. %
  22. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  23.  
  24.  
  25.  
  26. str2list(String) -> make_list_from_string(String, strlen(String)).
  27.  
  28.  
  29. %
  30. % ------------------------------------------------------------------------------
  31. %
  32.  
  33.  
  34. make_list_from_string("", 0) -> [].
  35.  
  36.  
  37. make_list_from_string(String, Length) ->
  38.   [asc(substr(String, 1, 1)) | 
  39.    make_list_from_string(substr(String, 2, Length - 1), Length - 1)].
  40.  
  41.  
  42. %
  43. % ------------------------------------------------------------------------------
  44. %
  45.  
  46.  
  47. chr_of(end_of_file) -> end_of_file.
  48.  
  49. chr_of(Char) -> chr(Char).
  50.  
  51.  
  52. %
  53. % ------------------------------------------------------------------------------
  54. %
  55.  
  56.  
  57. alpha(end_of_file) :-
  58.   !,
  59.   fail.
  60.  
  61. alpha(Code) :-
  62.   cond((Code :== asc("_")) 
  63.        or (Code >= 97 and Code =< 122) 
  64.        or (Code >= 65 and Code =< 90),
  65.     succeed,
  66.     fail
  67.   ).
  68.  
  69.  
  70. digit(Base, end_of_file) :-
  71.   !,
  72.   fail.
  73.  
  74. digit(decimal, Code) :-
  75.   !,
  76.   Code >= 48,
  77.   Code =< 57.
  78.  
  79. digit(octal, Code) :-
  80.   !,
  81.   Code >= 48,
  82.   Code =< 55.
  83.  
  84. digit(hexadecimal, Code) :-
  85.   (
  86.     digit(decimal, Code),
  87.     !
  88.   ;
  89.     cond((Code >= asc("a") and Code =< asc("f"))
  90.          or (Code >= asc("A") and Code =< asc("F")),
  91.       succeed,
  92.       fail
  93.     )
  94.   ).
  95.   
  96.  
  97. alphanum(Char) :-
  98.     alpha(Char),
  99.     !
  100.   ;
  101.     digit(decimal, Char).
  102.  
  103.  
  104. newline(Code) ->
  105.   Code :== 10.
  106.  
  107.  
  108. void_char(Code) ->
  109.   has_feature(Code, void_chars_table).
  110.  
  111.  
  112. %
  113. % ------------------------------------------------------------------------------
  114. %
  115.  
  116.  
  117. operator_symbols -> ["=", ",", "-", "*", "+", "!", "/", "<", ">", "&", "|", "?",
  118.                     "%", "^", "~"].
  119.  
  120. duplicatable_operators -> ["<", ">", "+", "-", "=", "|", "&"].
  121.  
  122. combinable_operators -> ["+", "-", "*", "/", "%", "&", "^", "|", "<", ">", "!"].
  123.  
  124.  
  125. %
  126. % ------------------------------------------------------------------------------
  127. %
  128.  
  129.  
  130. key_words -> ["auto", "break", "case", "char", "const", "continue", "default",
  131.               "do", "double", "else", "enum", "extern", "float", "for", "goto",
  132.               "if", "int", "long", "register", "return", "short", "signed",
  133.               "sizeof", "static", "struct", "switch", "typedef", "union",
  134.               "unsigned", "void", "volatile", "while"].
  135.  
  136.  
  137. %
  138. % ------------------------------------------------------------------------------
  139. %
  140.  
  141.  
  142. is_assignment(Operator) :-
  143.   member(Operator, assignment).
  144.  
  145.  
  146. %
  147. % ------------------------------------------------------------------------------
  148. %
  149.  
  150.  
  151. assignment -> ['=', '*=', '/=', '+=', '-=', '<<=', '>>=', '&=', '^=', '|='].
  152.  
  153. logical_or -> ['||'].
  154.  
  155. logical_and -> ['&&'].
  156.  
  157. inclusive_or -> ['|'].
  158.  
  159. exclusive_or -> ['^'].
  160.  
  161. c_and -> ['&'].
  162.  
  163. equality -> ['==', '!='].
  164.  
  165. relation -> ['<', '>', '>=', '<='].
  166.  
  167. shift -> ['<<', '>>'].
  168.  
  169. addition -> [str2psi("+"), str2psi("-")].
  170.  
  171. multiplication -> ['*', '/', '%'].
  172.  
  173.  
  174. %
  175. % ------------------------------------------------------------------------------
  176. %
  177.  
  178.  
  179. is_incrementation_operation('++') -> true.
  180.  
  181. is_incrementation_operation('--') -> true.
  182.  
  183.  
  184. %
  185. % ------------------------------------------------------------------------------
  186. %
  187.  
  188.  
  189. persistent(unary_operations) ?
  190.  
  191. unary_operations.'&' <<- '&' ?
  192. unary_operations.'*' <<- '*' ?
  193. unary_operations.str2psi("+") <<- str2psi("+") ?
  194. unary_operations.str2psi("-") <<- str2psi("-") ?
  195. unary_operations.'~' <<- '~' ?
  196. unary_operations.'!' <<- '!' ?
  197.  
  198.  
  199.  
  200. is_unary_operation(Operation) :-
  201.   has_feature(Operation, unary_operations).
  202.  
  203.  
  204. %
  205. % ------------------------------------------------------------------------------
  206. %
  207.  
  208.  
  209. get_precedence(Operator, Precedence) :-
  210.   has_feature(Operator, operations_table, Precedence).
  211.  
  212.  
  213. %
  214. % ------------------------------------------------------------------------------
  215. %
  216.  
  217.  
  218. left_parenthesis_symbol -> str2psi("(").
  219.  
  220. right_parenthesis_symbol -> str2psi(")").
  221.  
  222. left_bracket_symbol -> str2psi("[").
  223.  
  224. right_bracket_symbol -> str2psi("]").
  225.  
  226. left_brace_symbol -> str2psi("{").
  227.  
  228. right_brace_symbol -> str2psi("}").
  229.  
  230.  
  231. %
  232. % ------------------------------------------------------------------------------
  233. %
  234.  
  235.  
  236. store_class_specifier -> {auto; register; static; extern}.
  237.  
  238.  
  239. type_qualifier -> {const; volatile}.
  240.  
  241.  
  242. single_type_specifier -> {void; char; short; int; long; float; double; 
  243.                           signed; unsigned}.
  244.  
  245.  
  246. %
  247. % ------------------------------------------------------------------------------
  248. %
  249.  
  250.  
  251. displayable_form_of(Char) -> DisplayableForm
  252.   | DisplayableForm = cond(Char < 32,
  253.       strcon("ascii(", strcon(int2str(adjust(Char)), ")")),
  254.       chr_of(Char)
  255.     ).
  256.  
  257.  
  258. %
  259. % ------------------------------------------------------------------------------
  260. %
  261.  
  262.  
  263. adjust(Code) -> AdjustedCode
  264.   | AdjustedCode = cond(Code < 0,
  265.       256 + Code,
  266.       Code
  267.     ).
  268.  
  269.  
  270. %
  271. % ------------------------------------------------------------------------------
  272. %
  273.  
  274.  
  275. string_of([]) -> "".
  276.  
  277. string_of([Char | LChars]) -> strcon(chr(Char), string_of(LChars)).
  278.  
  279.  
  280. %
  281. % ------------------------------------------------------------------------------
  282. %
  283.  
  284.  
  285. int_of_digits_list(DigitsList) -> calc_int_from_list(DigitsList, 0).
  286.  
  287. calc_int_from_list([], Acc) -> Acc.
  288.  
  289. calc_int_from_list([Digit | DigitsList], Acc) ->
  290.   calc_int_from_list(DigitsList, 10 * Acc + Digit - asc("0")).
  291.  
  292.  
  293. %
  294. % ------------------------------------------------------------------------------
  295. %
  296.  
  297.  
  298. hash_code(CharList) -> get_hash_code(CharList, 0).
  299.  
  300. %HashCode
  301. %  | BigCode = get_hash_code(CharList, 0),
  302. %    (
  303. %      BigCode >= 2 ^ 31,
  304. %      !,
  305. %      HashCode = BigCode - floor(BigCode / hash_radix) * hash_radix
  306. %    ;   
  307. %      HashCode = BigCode mod hash_radix
  308. %    ).
  309.  
  310.  
  311. get_hash_code([], Acc) -> Acc.
  312.  
  313. get_hash_code([Char | LChars], Acc) -> 
  314.   get_hash_code(LChars, (Acc * 65599 + Char) mod hash_radix).
  315.  
  316. %%%  multiplication by 65599
  317.  
  318.  
  319. hash_radix -> 2039.
  320.  
  321.  
  322. %
  323. % ------------------------------------------------------------------------------
  324. %
  325.  
  326.  
  327. hex2dec(List) -> iter_hex2dec(List, 0).
  328.  
  329.  
  330. iter_hex2dec([], Acc) -> Acc.
  331.  
  332. iter_hex2dec([Digit | Digits], Acc) -> 
  333.   iter_hex2dec(Digits, 16 * Acc + TheDigit)
  334.   | (
  335.       digit(decimal, Digit),
  336.       !,
  337.       TheDigit = Digit - asc("0")
  338.     ;
  339.       Digit >= asc("A"),
  340.       Digit =< asc("F"),
  341.       !,
  342.       TheDigit = Digit - asc("A") + 10
  343.     ;
  344.       TheDigit = Digit - asc("a") + 10
  345.     ).
  346.  
  347.  
  348. %
  349. % ------------------------------------------------------------------------------
  350. %
  351.  
  352.  
  353. oct2dec(List) -> iter_oct2dec(List, 0).
  354.  
  355.  
  356. iter_oct2dec([], Acc) -> Acc.
  357.  
  358. iter_oct2dec([Digit | Digits], Acc) -> 
  359.   iter_oct2dec(Digits, 8 * Acc + Digit - asc("0")).
  360.  
  361.  
  362. %
  363. % ------------------------------------------------------------------------------
  364. %
  365.  
  366.  
  367. search(String, Void, NewString) :-
  368.   Void :== @,
  369.   !,
  370.   Void <<- [String | @],
  371.   NewString = Void.1.
  372.  
  373. search(String, [StoredString | LStrings], StoredString) :-
  374.   String :== StoredString,
  375.   !.
  376.  
  377. search(String, [AString | LStrings], StoredString) :-
  378.   search(String, LStrings, StoredString).
  379.  
  380.  
  381. %
  382. % ------------------------------------------------------------------------------
  383. %
  384.  
  385.  
  386. normal_search(IncompleteList, What, Data) :-
  387.   IncompleteList :== @,
  388.   !,
  389.   fail.
  390.  
  391.  
  392. normal_search([@(What, Data) | Tail], What, Data) :- !.  
  393.  
  394.  
  395. normal_search([@ | Tail], What, Data) :-
  396.   normal_search(Tail, What, Data).
  397.  
  398.  
  399. %
  400. % ------------------------------------------------------------------------------
  401. %
  402.  
  403.  
  404. incremental_search(List, What, Data) :-
  405.   residuate(List, 
  406.     (
  407.       launch_search(List, ListTail, What, CurrentData),
  408.       (
  409.         CurrentData :== @,
  410.         !,
  411.         incremental_search(ListTail, What, FutureData),
  412.         residuate(FutureData, (Data = FutureData))
  413.       ;
  414.         Data = CurrentData
  415.       )
  416.     )
  417.   ).
  418.   
  419.  
  420. launch_search([@(What, Data) | Tail], Tail, What, Data) :- !.
  421.  
  422. launch_search([@ | Tail], Tail, What, Data).
  423.  
  424.  
  425. %
  426. % ------------------------------------------------------------------------------
  427. %
  428.  
  429.  
  430. display_token(identifier(Identifier)) -> Identifier.
  431.  
  432.  
  433. display_token(number(integer(Number))) -> Number.
  434.  
  435.  
  436. display_token(number(float(integer_part => IntegerPart, 
  437.                            decimal_part => DecimalPart))) -> 
  438.   strcon(IntegerPart, strcon(".", DecimalPart)).
  439.  
  440.  
  441. display_token(characters(Characters)) -> Characters.
  442.  
  443.  
  444. display_token(characters_string(String)) -> String.
  445.  
  446.  
  447. display_token(nothing(file => File)) -> strcon("end of file ", File).
  448.  
  449.  
  450. display_token(Token) -> root_sort(Token).
  451.  
  452.  
  453. %
  454. % ------------------------------------------------------------------------------
  455. %
  456.  
  457.  
  458. cpp_name -> "cc -E -D__STDC__".
  459.  
  460. tmp_dir -> "/tmp/".
  461.  
  462.  
  463. %
  464. % ------------------------------------------------------------------------------
  465. %
  466.  
  467.  
  468.  
  469.  
  470.  
  471.