home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / icn2c.icn < prev    next >
Text File  |  2000-07-29  |  2KB  |  98 lines

  1. ############################################################################
  2. #
  3. #    File:     icn2c.icn
  4. #
  5. #    Subject:  Program to assist Icon-to-C porting
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     March 11, 1993
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  Filter to do some mundane aspects of conversion of Icon to C.
  18. #
  19. #  - Reformats comments
  20. #  - Reformats line-continued strings
  21. #  - Changes := to =
  22. #  - Reformats procedure declarations
  23. #  - Changes end to "}"
  24. #
  25. ############################################################################
  26.  
  27. procedure main(arg)
  28.    local c, comment, line, parenLevel, suffix, tline
  29.  
  30.    parenLevel := 0
  31.    while line := trim(read(),' \t') do line ? {
  32.       line := comment := suffix := ""
  33.       ="procedure" & tab(many(' \t')) & suffix := " {"
  34.       ="end" & tab(many(' \t')) | pos(0) & line ||:= "}"
  35.       while line ||:= tab(upto('\'":#')) do {
  36.      case c := move(1) of {
  37.         "\"" | "'": {
  38.            #
  39.            #  Handle character strings.
  40.            #
  41.            line ||:= c
  42.            repeat {
  43.           until line ||:= tab(find(c) + 1) do {
  44.              line ||:= tab(0)
  45.              if line[-1] == "_" then line[-1] := "\""
  46.              else stop("unbalanced quotes")
  47.              Out(line)
  48.              line := ""
  49.              &subject := read()
  50.              line := (tab(many(' \t')) | "") || "\""
  51.              }
  52.           if not (line[-2] == "\\" & not (line[-3] == "\\")) then break
  53.           }
  54.            }
  55.         "#": {
  56.            #
  57.            #  Handle comments.
  58.            #
  59.            comment := trim(tab(0),' \t')
  60.            }
  61.         ":": {
  62.            #
  63.            #  Change := to =
  64.            #
  65.            if ="=" then line ||:= "="
  66.            else line ||:= c
  67.            }
  68.         "(": {
  69.            parenLevel +:= 1
  70.            line ||:= c
  71.            }
  72.         ")": {
  73.            parenLevel -:= 1
  74.            line ||:= c
  75.            }
  76.         default: line ||:= c
  77.         }
  78.      }
  79.       line ||:= tab(0) || suffix
  80.       tline := trim(line,' \t')
  81.       if not (parenLevel > 0 | *tline = 0 |
  82.         any('{}(!%&*+,-./:<=>?@\\^',tline,-1) |
  83.         (tline[-4:0] == ("else" | "then") &
  84.         not tline[-5] | any(' \t',tline[-5]))) then {
  85.      line := tline || ";" || line[*tline + 1:0]
  86.      }
  87.       Out(line,comment)
  88.       }
  89. end
  90.  
  91.  
  92. procedure Out(line,comment)
  93.    line ||:= "/*" || ("" ~== \comment) || " */"
  94.    line := trim(line,' \t')
  95.    write(line)
  96.    return
  97. end
  98.