summaryrefslogtreecommitdiff
path: root/ipl/progs/c2icn.icn
blob: f19267094b2039c66a04a7f7f719a1866394b963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
############################################################################
#
#	File:     c2icn.icn
#
#	Subject:  Program to assist C-to-Icon porting
#
#	Author:   Robert J. Alexander
#
#	Date:     March 11, 1993
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  Filter to do some of the mundane work involved in porting a C
#  program to Icon.
#
#  - Reformats comments, moving embedded comments to end of line
#  - Removes the ";" from ends of lines
#  - Reformats line-continued strings
#  - Changes = to :=
#  - Changes -> to .
#
############################################################################

procedure main(arg)
   local c, comment, line, tline

   while line := trim(read(),' \t') do line ? {
      line := comment := ""
      while line ||:= tab(upto('\'"/=-')) do {
	 case c := move(1) of {
	    "\"" | "'": {
	       line ||:= c
	       repeat {
		  until line ||:= tab(find(c) + 1) do {
		     line ||:= tab(0)
		     if line[-1] == "\\" then line[-1] := "_"
		     else stop("unbalanced quotes")
		     Out(line)
		     line := ""
		     &subject := read()
		     }
		  if not (line[-2] == "\\" & not (line[-3] == "\\")) then break
		  }
	       }
	    "/": {
	       if ="*" then {
		  until comment ||:= trim(tab(find("*/")),' \t') do {
		     comment ||:= trim(tab(0),' \t')
		     Out(line,comment)
		     line := comment := ""
		     &subject := trim(read(),' \t')
		     }
		  move(2)
		  }
	       }
	    "=": {
	       if ="=" then line ||:= "=="
	       else if any('<>!',line[-1]) then line ||:= c
	       else line ||:= ":="
	       }
	    "-": {
	       if =">" then line ||:= "."
	       else line ||:= c
	       }
	    default: line ||:= c
	    }
	 }
      line ||:= tab(0)
      tline := trim(line)
      if tline[-1] == ";" then {
	 line := tline[1:-1] || line[*tline + 1:0]
	 }
      Out(line,comment)
      }
end


procedure Out(line,comment)
   line ||:= "#" || ("" ~== \comment)
   line := trim(line,' \t')
   write(line)
   return
end