summaryrefslogtreecommitdiff
path: root/ipl/progs/icn2c.icn
blob: e668988f1cf77cade77d5ed28895aae7886e4fd3 (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
88
89
90
91
92
93
94
95
96
97
############################################################################
#
#	File:     icn2c.icn
#
#	Subject:  Program to assist Icon-to-C porting
#
#	Author:   Robert J. Alexander
#
#	Date:     March 11, 1993
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  Filter to do some mundane aspects of conversion of Icon to C.
#
#  - Reformats comments
#  - Reformats line-continued strings
#  - Changes := to =
#  - Reformats procedure declarations
#  - Changes end to "}"
#
############################################################################

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

   parenLevel := 0
   while line := trim(read(),' \t') do line ? {
      line := comment := suffix := ""
      ="procedure" & tab(many(' \t')) & suffix := " {"
      ="end" & tab(many(' \t')) | pos(0) & line ||:= "}"
      while line ||:= tab(upto('\'":#')) do {
	 case c := move(1) of {
	    "\"" | "'": {
	       #
	       #  Handle character strings.
	       #
	       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()
		     line := (tab(many(' \t')) | "") || "\""
		     }
		  if not (line[-2] == "\\" & not (line[-3] == "\\")) then break
		  }
	       }
	    "#": {
	       #
	       #  Handle comments.
	       #
	       comment := trim(tab(0),' \t')
	       }
	    ":": {
	       #
	       #  Change := to =
	       #
	       if ="=" then line ||:= "="
	       else line ||:= c
	       }
	    "(": {
	       parenLevel +:= 1
	       line ||:= c
	       }
	    ")": {
	       parenLevel -:= 1
	       line ||:= c
	       }
	    default: line ||:= c
	    }
	 }
      line ||:= tab(0) || suffix
      tline := trim(line,' \t')
      if not (parenLevel > 0 | *tline = 0 |
	    any('{}(!%&*+,-./:<=>?@\\^',tline,-1) |
	    (tline[-4:0] == ("else" | "then") &
	    not tline[-5] | any(' \t',tline[-5]))) then {
	 line := tline || ";" || line[*tline + 1:0]
	 }
      Out(line,comment)
      }
end


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