summaryrefslogtreecommitdiff
path: root/macros/generic/c_pascal/cap_c.tex
blob: 3aaa2d33f8e0de9e8c6058b42711873d8fc9a867 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%     CAP  ---  Macros for typesetting programs in C and Pascal     %
%       Micha\l{} Gulczy\'nski, Szczecin, Feb 1997 / Feb 1998       %
%                     mgulcz@we.tuniv.szczecin.pl                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  version 1.2d
%    Added DVIPS color support (actually, any output is replaced with
%    a macro call to \@Output --- search for it to see the changes)
%     -- by Danilo {\v S}egan, mm01142@alas.matf.bg.ac.yu
%  version 1.2
%    This file contains macros for typesetting programs in C.
%    It belongs to the CAP package. Do not distribute separately.
%
%    All font definitions are located in the cap_comm.tex file.
%

%
%      set of keywords; they must be separated with spaces
%      a space must precede the first one and follow the last one, too
%
\def\KeywordsC{
void char int float double enum long short signed unsigned far near huge
auto register extern static volatile inline const typedef
new delete sizeof struct union
for do while if else switch case default break continue return goto
asm cdecl pascal interrupt
class private public protected this friend virtual template operator
catch throw try
}
\catcode`\@ = 11
%
%      that's where we begin
%
\def\BeginC{%
  \@PrepareCPas
  \@LetsStartC
}%
% Some people in Poland use so-called ``slash notation''
% to represent certain Polish letters --- in this situation
% slash is an active character. On the other hand we use slash
% in pathnames: directory/subdirectory/file. I made this part
% sooo complicated, bacause I had to neutralise slash in
% \InputC.
\def\@InputC#1{%
  \message{(C++: #1}%
  \openin\@InFile = #1
  \@PrepareCPas
  % The file is read line by line and each line is typeset
  % just like a separate program. Therefore the size of program
  % typeset using this macro is (almost) unlimited.
  \loop
    \global\read\@InFile to \@TextOfProgram
    \@WriteTextOfProgramC
  \if \neof\@InFile \repeat
  \closein\@InFile
  \endgroup    % this group was begun by \@PrepareCPas
  \endgroup    % this group was begun by \InputC
  \message{)}%
}
\def\InputC{%
  \begingroup
  \catcode`\/ = 11
  \@InputC
}
%
%      delimiter \EndC will be ordinary text
%
{ \catcode`\|=0   \catcode`\\=12
  |gdef|@LetsStartC#1\EndC{%
    |gdef|@TextOfProgram{#1}%
    |@WriteTextOfProgramC
    |endgroup    % this group was begun by \@PrepareCPas
  }
}
%
%      macro \@TextOfProgram contains the whole text of program
%
\def\@WriteTextOfProgramC{%
  \expandafter\@ReadCharC\@TextOfProgram\@EndOfProgram
}
%
%      heart of the program -- the argument is a single char
%
\def\@ReadCharC#1{%
  % this macro calls itself until the argument #1 is \@EndOfProgram
  \if\@Identical{\string#1}{\string\@EndOfProgram}%
    \let\@WhatNext = \relax
  \else
    \let\@WhatNext = \@ReadCharC
    \global\@CharCode = `#1\relax
    \ifcase \@WhereAmI
      %%%%%%%%%%%%%%%%%%%
      % \@NothingSpecial:
      %%%%%%%%%%%%%%%%%%%
      \ifnum \@PrevChar=`\/
        \ifnum \@CharCode=`\/
          \global\@WhereAmI = \@ShortComment
          \@CommentState
          \@Output{\char47}%
        \else
          \ifnum \@CharCode=`\*
            \global\@WhereAmI = \@LongComment
            \@CommentState
          \fi
        \fi
        \@Output{\char47}%
      \fi
      % the longest possible string containing only letters and digits
      % is either a keyword or an identifier
      \if\@DigitLetter{\@CharCode}%
        \edef\@Word{\@Word#1}%
      \else
        \if\@Identical{\@Word}{}%
        \else
          \@WriteWord{\@Word}{\KeywordsC}%
          \def\@Word{}%
        \fi
        \ifnum \@CharCode=`\#
          \global\@WhereAmI = \@Directive
          \@DirectiveState
        \fi
        \ifnum \@CharCode=`\"
          \global\@WhereAmI = \@Text
          \@TextState
        \fi
        \ifnum \@CharCode=`\/
        \else
          \@WriteChar{#1}%
        \fi
      \fi
    \or
      %%%%%%%%%
      % \@Text:
      %%%%%%%%%
      \@WriteChar{#1}%
      \ifnum \@CharCode=`\"
        \global\@WhereAmI = \@NothingSpecial
        \@SymbolState
      \fi
    \or
      %%%%%%%%%%%%%%
      % \@Directive:
      %%%%%%%%%%%%%%
      \@WriteChar{#1}%
      \ifnum \@CharCode=13
        \global\@WhereAmI = \@NothingSpecial
        \@SymbolState
      \fi
    \or
      %%%%%%%%%%%%%%%%%
      % \@ShortComment:
      %%%%%%%%%%%%%%%%%
      \@WriteChar{#1}%
      \ifnum \@CharCode=13
        \global\@WhereAmI = \@NothingSpecial
        \@SymbolState
      \fi
    \or
      %%%%%%%%%%%%%%%%
      % \@LongComment:
      %%%%%%%%%%%%%%%%
      \@WriteChar{#1}%
      \ifnum \@PrevChar=`\*
        \ifnum \@CharCode=`\/
          \global\@WhereAmI = \@NothingSpecial
          \@SymbolState
          \@CharCode = 32
        \fi
      \fi
    \fi
    \global\@PrevChar = \@CharCode
  \fi
  \@WhatNext
}

\ifx \@PrepareCPas \@Dont@Know@What@It@Is
  \input cap_comm
\fi

\catcode`\@ = 12