source: doc/uC++toCFA/uC++toCFA.tex @ fa761c2

Last change on this file since fa761c2 was 946a6e4, checked in by Peter A. Buhr <pabuhr@…>, 9 months ago

updates to the uC++toCFA cheat sheet

  • Property mode set to 100644
File size: 13.8 KB
Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%
2%%
3%% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4%%
5%% The contents of this file are covered under the licence agreement in the
6%% file "LICENCE" distributed with Cforall.
7%%
8%% user.tex --
9%%
10%% Author           : Peter A. Buhr
11%% Created On       : Wed Apr  6 14:53:29 2016
12%% Last Modified By : Peter A. Buhr
13%% Last Modified On : Sun Oct 15 23:09:58 2023
14%% Update Count     : 5926
15%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
17% requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended
18
19\documentclass[11pt]{article}
20
21\makeatletter
22\def\@maketitle{%
23    \newpage
24    \null
25%   \vskip 2em%
26    \begin{center}%
27        \let \footnote \thanks
28        {\LARGE\bf \@title \par}%
29        \@ifundefined{@author}{}
30        {
31            \ifx\@empty\@author
32            \else
33                \vskip 1.5em%
34                {\large
35                    \lineskip .5em%
36                    \begin{tabular}[t]{c}%
37                        \@author
38                    \end{tabular}%
39                    \par
40                }%
41            \fi
42        }%
43        \ifx\@empty\@date
44        \else
45            \vskip 1em%
46            {\large \@date}%
47        \fi
48    \end{center}%
49    \par
50%   \vskip 1.5em
51}%maketitle
52\makeatother
53
54%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55
56% Latex packages used in the document.
57%\usepackage[T1]{fontenc}                                % allow Latin1 (extended ASCII) characters
58%\usepackage{textcomp}
59%\usepackage[latin1]{inputenc}
60
61\usepackage{fullpage,times,comment}
62\usepackage{epic,eepic}
63\usepackage{upquote}                                                                    % switch curled `'" to straight
64\usepackage[labelformat=simple,aboveskip=0pt,farskip=0pt]{subfig}
65\renewcommand{\thesubfigure}{\alph{subfigure})}
66\usepackage{latexsym}                                   % \Box glyph
67\usepackage{mathptmx}                                   % better math font with "times"
68\usepackage[usenames]{color}
69\usepackage[pagewise]{lineno}
70\renewcommand{\linenumberfont}{\scriptsize\sffamily}
71\input{common}                                                                                  % common CFA document macros
72\usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
73\usepackage{breakurl}
74
75\renewcommand\footnoterule{\kern -3pt\rule{0.3\linewidth}{0.15pt}\kern 2pt}
76\newcommand{\uC}{$\mu$\CC}
77
78% Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
79% removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
80% AFTER HYPERREF.
81\renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
82
83\setlength{\topmargin}{-0.45in}                                                 % move running title into header
84\setlength{\headsep}{0.25in}
85
86%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87
88\setlength{\gcolumnposn}{3in}
89\CFAStyle                                                                                               % use default CFA format-style
90\lstset{language=CFA}                                                                   % CFA default lnaguage
91\lstnewenvironment{C++}[1][]                            % use C++ style
92{\lstset{language=C++,escapechar=\$,mathescape=false,moredelim=**[is][\protect\color{red}]{@}{@},#1}}{}
93\lstnewenvironment{uC++}[1][]
94{\lstset{language=uC++,escapechar=\$,mathescape=false,moredelim=**[is][\protect\color{red}]{@}{@},#1}}{}
95
96\newsavebox{\myboxA}
97\newsavebox{\myboxB}
98
99%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100
101% Names used in the document.
102\newcommand{\Version}{\input{build/version}}
103\newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}}
104\newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}}
105\newcommand{\R}[1]{{\color{red}#1}}
106\newcommand{\RB}[1]{\Textbf{#1}}
107\newcommand{\B}[1]{{\Textbf[blue]{#1}}}
108\newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}}
109\newcommand{\Sp}{\R{\textvisiblespace}}
110\newcommand{\KWC}{K-W C\xspace}
111
112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114\setcounter{secnumdepth}{3}                             % number subsubsections
115\setcounter{tocdepth}{3}                                % subsubsections in table of contents
116\makeindex
117
118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119
120\title{\vspace*{-0.5in}
121\uC to \CFA Cheat Sheet}
122%\author{Peter A. Buhr}
123\date{}
124
125%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126
127\begin{document}
128\pagestyle{headings}
129% changed after setting pagestyle
130\renewcommand{\sectionmark}[1]{\markboth{\thesection\quad #1}{\thesection\quad #1}}
131\renewcommand{\subsectionmark}[1]{\markboth{\thesubsection\quad #1}{\thesubsection\quad #1}}
132
133%\linenumbers                                            % comment out to turn off line numbering
134
135\maketitle
136\vspace*{-0.55in}
137
138\section{Introduction}
139
140\CFA is NOT an object-oriented programming-language.
141\CFA uses parametric polymorphism and allows overloading of variables and routines:
142\begin{cfa}
143int i;  char i;  double i;              // overload name i
144int i();  double i();  char i();
145i += 1;                 $\C[1.5in]{// int i}$
146i += 1.0;               $\C{// double i}$
147i += 'a';               $\C{// char i}$
148int j = i();    $\C{// int i()}$
149double j = i(); $\C{// double i();}$
150char j = i();   $\C{// char i()}\CRT$
151\end{cfa}
152\CFA has rebindable references.
153
154\begin{cquote}
155\begin{tabular}{l|l}
156\multicolumn{2}{l}{\lstinline{  int x = 1, y = 2, * p1x = &x, * p1y = &y, ** p2i = &p1x,}} \\
157\multicolumn{2}{l}{\lstinline{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ && r1x = x, & r1y = y, && r2i = r1x;}} \\
158\begin{uC++}
159**p2i = 3;
160p2i = &p1y;
161**p2i = 3;
162p1x = p1y;
163**p2i = 4;
164p1x = @nullptr@;
165\end{uC++}
166&
167\begin{cfa}
168r2i = 3; $\C[1.0in]{// change x}$
169&r2i = &r1y; $\C{// change p2i / r2i}$
170r2i = 3; $\C{// change y}$
171&r1x = &r1y; $\C{// change p1x / r1x}$
172r2i = 4; $\C{// change y}$
173&r1x = @0p@; $\C{// reset}\CRT$
174\end{cfa}
175\end{tabular}
176\end{cquote}
177Non-rebindable reference (\CC reference) is a @const@ reference (@const@ pointer).
178\begin{cfa}
179int & @const@ cr = x; // must initialize, no null pointer
180int & @const@ & @const@ crcr = cr; // generalize
181\end{cfa}
182Aggregate qualification is reduced or eliminated by opening scopes using the @with@ clause.
183\begin{cfa}
184struct S { int i; int j; double m; };  // field i has same type in structures S and T
185struct T { int i; int k; int m; };
186void foo( S s, T t ) @with(s, t)@ {   // open structure scope s and t in parallel
187        j + k;                          $\C[1.6in]{// unambiguous, s.j + t.k}$
188        m = 5.0;                        $\C{// unambiguous, s.m = 5.0}$
189        m = 1;                          $\C{// unambiguous, t.m = 1}$
190        int a = m;                      $\C{// unambiguous, a = t.m}$
191        double b = m;           $\C{// unambiguous, b = s.m}$
192        int c = s.i + t.i;      $\C{// unambiguous with qualification}$
193        (double)m;                      $\C{// unambiguous with cast s.m}\CRT$
194}
195\end{cfa}
196\noindent
197In subsequent code examples, the left example is \uC and the right example is \CFA.
198
199
200\section{Stream I/O}
201
202\CFA output streams automatically separate values and insert a newline at the end of the print.
203
204\begin{cquote}
205\begin{tabular}{l|l}
206\begin{uC++}
207#include <@iostream@>
208using namespace std;
209int i;   double d;   char c;
210cin >> i >> d >> c;
211cout << i << ' ' << d << ' ' << c | endl;
212\end{uC++}
213&
214\begin{cfa}
215#include <@fstream.hfa@>
216
217int i;   double d;   char c;
218sin | i | d | c;
219sout | i | d | c
220\end{cfa}
221\end{tabular}
222\end{cquote}
223
224
225\section{Looping}
226
227\begin{cquote}
228\begin{tabular}{l|l}
229\begin{uC++}
230for ( @;;@ ) { ... }  /  while ( @true@ ) { ... }
231for ( int i = 0; i < @10@; i += 1 ) { ... }
232for ( int i = @5@; i < @15@; i += @2@ ) { ... }
233for ( int i = -1; i <@=@ 10; i += 3 ) { ... }
234for ( int i = 10; i > 0; i @-@= 1 ) { ... }
235\end{uC++}
236&
237\begin{cfa}
238for () { ... }  /  while () { ... }
239for ( @10@ ) { ... }  /  for ( i; @10@ ) { ... }
240for ( i; @5@ ~ @15@ ~ @2@ ) { ... }
241for ( i; -1 ~@=@ 10 ~ 3 ) { ... }
242for ( i; 0 @-@~ 10 ) { ... }
243\end{cfa}
244\\
245\hline
246\begin{uC++}
247int i = 0
248for ( i = 0; i < 10; i += 1 ) { ... }
249@if ( i == 10 )@ { ... }
250\end{uC++}
251&
252\begin{cfa}
253
254for ( i; 10 ) { ... }
255@else@ { ... } // i == 10
256\end{cfa}
257\\
258\hline
259\begin{uC++}
260L1: for ( ;; ) {
261        L2: for ( ;; ) {
262                ... @break L1@; ... @break L2@; ...
263        }
264}
265\end{uC++}
266&
267\begin{cfa}
268L1: for () {
269        L2: for () {
270                ... @break L1@; ... @break L2@; ...
271        }
272}
273\end{cfa}
274\end{tabular}
275\end{cquote}
276
277
278\section{Exception}
279
280Currently, \CFA uses macros @ExceptionDecl@ and @ExceptionInst@ to declare and instantiate an exception.
281\begin{cquote}
282\begin{tabular}{l|ll}
283\begin{uC++}
284
285@_Exception@ E {        // local or global scope
286        ... // exception fields
287};
288try {
289        ...
290        if ( ... ) @_Resume@ E( /* initialization */ );
291        if ( ... ) @_Throw@ E( /* initialization */ );
292                ...
293} @_CatchResume@( E & ) { // should be reference
294        ...
295} catch( E & ) {
296        ...
297}
298\end{uC++}
299&
300\begin{cfa}
301#include <Exception.hfa>
302@ExceptionDecl@( E,             // must be global scope
303        ... // exception fields
304);
305try {
306        ...
307        if ( ... ) @throwResume@ @ExceptionInst@( E, /* intialization */ );
308        if ( ... ) @throw@ @ExceptionInst@( E, /* intialization */ );
309        ...
310} @catchResume@( E * ) { // must be pointer
311        ...
312} catch( E * ) {
313        ...
314}
315\end{cfa}
316\end{tabular}
317\end{cquote}
318
319
320\section{Non-local Exception}
321
322\begin{cquote}
323\begin{tabular}{l|ll}
324\begin{uC++}
325
326
327void main() {
328        try {
329                _Enable {
330                        ... suspend(); ...
331                }
332        } @_CatchResume@( E & ) { // reference
333                ...
334        } catch( E & ) {
335                ...
336        }
337}
338\end{uC++}
339&
340\begin{cfa}
341#define resumePoll( coroutine ) resume( coroutine ); checked_poll()
342#define suspendPoll suspend; checked_poll()
343void main() {
344        try {
345                enable_ehm();
346                ... suspendPoll ...
347                disable_ehm();
348        } @catchResume@( E * ) { // pointer
349                ...
350        } catch( E & ) {
351                ...
352        }
353}
354\end{cfa}
355\end{tabular}
356\end{cquote}
357
358
359\section{Constructor / Destructor}
360
361\begin{cquote}
362\begin{tabular}{l|l}
363\begin{uC++}
364struct S {
365        ... // fields
366        @S@(...) { ... }
367        @~S@(...) { ... }
368};
369\end{uC++}
370&
371\begin{cfa}
372struct S {
373        ... // fields
374};
375@?{}@( @S & s,@ ...) { ... }
376@^?{}@( @S & s@ ) { ... }
377\end{cfa}
378\end{tabular}
379\end{cquote}
380
381
382\section{String}
383
384\begin{cquote}
385\begin{tabular}{l|l}
386\multicolumn{2}{l}{\lstinline{string s1, s2;}} \\
387\begin{uC++}
388s1 = "hi";
389s2 = s1;
390s1 += s2;
391s1 == s2; s1 != s2;
392s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
393s1.length();
394s1[3];
395s1.substr( 2 ); s1.substr( 2, 3 );
396s1.replace( 2, 5, s2 );
397s1.find( s2 ), s1.rfind( s2 );
398s1.find_first_of( s2 ); s1.find_last_of( s2 );
399s1.find_first_not_of(s2 ); s1.find_last_not_of( s2 );
400getline( cin, s1 );
401cout << s1 << endl;
402\end{uC++}
403&
404\begin{cfa}
405s1 = "hi";
406s2 = s1;
407s1 += s2;
408s1 == s2; s1 != s2;
409s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
410size( s1 );
411s1[3];
412s1( 2 ); s1( 2, 3 );
413//s1.replace( 2, 5, s2 );
414find( s1, s2 ), rfind( s1, s2 );
415find_first_of( .substr, s2 ); s1.find_last_of( s2 );
416s1.find_first_not_of(s2 ); s1.find_last_not_of( s2 );
417sin | getline( s1 );
418sout | s1;
419\end{cfa}
420\end{tabular}
421\end{cquote}
422
423
424\section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}}
425
426\begin{cquote}
427\begin{tabular}{l|l}
428\begin{uC++}
429struct S {
430        int i = 0;
431        int setter( int j ) { int t = i; i = j; return t; }
432        int getter() { return i; }
433};
434
435S s;
436@s.@setter( 3 );  // object-oriented call
437int k = @s.@getter();
438\end{uC++}
439&
440\begin{cfa}
441struct S {
442        int i;
443};
444void ?{}( S & s ) { s.i = 0; }
445int setter( @S & s,@ int j ) @with(s)@ { int t = i; i = j; return t; }
446int getter( @S & s@ ) @with(s)@ { return i; }
447S s;
448setter( @s,@ 3 );  // normal routine call
449int k = getter( @s@ );
450\end{cfa}
451\end{tabular}
452\end{cquote}
453
454
455\section{\texorpdfstring{\lstinline{uNoCtor}}{uNoCtor}}
456
457\begin{cquote}
458\begin{tabular}{l|l}
459\begin{uC++}
460struct S {
461        int i;
462        S( int i ) { S::i = i; cout << S::i << endl; }
463};
464@uNoCtor<S>@ s[10];
465int main() {
466        for ( int i = 0; i < 10; i += 1 ) {
467                s[i].ctor( i );
468        }
469        for ( int i = 0; i < 10; i += 1 ) {
470                cout << s[i]@->@i << endl;
471        }
472}
473\end{uC++}
474&
475\begin{cfa}
476struct S {
477        int i;
478};
479void ?{}( S & s, int i ) { s.i = i; sout | s.i; }
480S s[10] @$\color{red}@$= {}@;
481int main() {
482        for ( i; 10 ) {
483                ?{}( s[i], i );  // call constructor
484        }
485        for ( i; 10 ) {
486                sout | s[i]@.@i; // dot not arrow
487        }
488}
489\end{cfa}
490\end{tabular}
491\end{cquote}
492
493
494\section{Coroutines}
495
496\begin{cquote}
497\begin{tabular}{l|ll}
498\begin{uC++}
499
500_Coroutine C {
501        // private coroutine fields
502        void main() {
503                ... suspend(); ...
504                ... _Resume E( ... ) _At partner;
505        }
506  public:
507        void mem( ... ) {
508                ... resume() ...
509        }
510};
511\end{uC++}
512&
513\begin{cfa}
514#include <$coroutine$.hfa>
515coroutine C {
516        // private coroutine fields
517
518};
519void main( C & c ) {
520        ... suspend; ... // keyword not routine
521        ... resumeAt( partner, ExceptionInst( E, ... ) );
522}
523void mem( C & c, ... ) {
524        ... resume(); ...
525}
526\end{cfa}
527\\
528\multicolumn{2}{l}{\lstinline{C c;}}
529\end{tabular}
530\end{cquote}
531
532
533\section{Locks}
534
535\begin{cquote}
536\begin{tabular}{l|ll}
537\begin{uC++}
538
539uOwnerLock m;
540uCondLock s;
541bool avail = true;
542m.acquire();
543if ( ! avail ) s.wait( m );
544else {
545        avail = false;
546        m.release();
547}
548@osacquire( cout )@ << i << endl;
549\end{uC++}
550&
551\begin{cfa}
552#include <locks.hfa>
553owner_lock m;
554condition_variable( owner_lock ) s;
555bool avail = true;
556lock( m );
557if ( ! avail ) wait( s, m );
558else {
559        avail = false;
560        unlock( m );
561}
562@mutex( sout )@ sout | i;  // safe I/O
563\end{cfa}
564\end{tabular}
565\end{cquote}
566
567
568\section{Monitors}
569
570\begin{cquote}
571\begin{tabular}{l|ll}
572\begin{uC++}
573
574@_Monitor@ M {
575        @uCondition@ c;
576        bool avail = true;
577  public:
578
579        void rtn() {
580                if ( ! avail ) c.wait();
581                else avail = false;
582        }
583};
584\end{uC++}
585&
586\begin{cfa}
587#include <$monitor$.hfa>
588@monitor@ M {
589        @condition@ c;
590        bool avail;
591};
592void ?{}( M & m ) { m.avail = true; }
593void rtn( M & m ) with( m ) {
594        if ( ! avail ) wait( c );
595        else avail = false;
596}
597
598\end{cfa}
599\\
600\multicolumn{2}{l}{\lstinline{M m;}}
601\end{tabular}
602\end{cquote}
603
604
605\section{Threads}
606
607\begin{cquote}
608\begin{tabular}{l|ll}
609\begin{uC++}
610
611@_Task@ T {
612        // private task fields
613        void main() {
614                ... _Resume E( ... ) _At partner;
615        }
616  public:
617};
618\end{uC++}
619&
620\begin{cfa}
621#include <$thread$.hfa>
622@thread@ T {
623        // private task fields
624
625};
626void main( @T & t@ ) {
627        ... resumeAt( partner, ExceptionInst( E, ... ) );
628}
629\end{cfa}
630\\
631\multicolumn{2}{l}{\lstinline{T t; // start thread in main routine}}
632\end{tabular}
633\end{cquote}
634
635
636\input{uC++toCFA.ind}
637
638% \bibliographystyle{plain}
639% \bibliography{pl}
640
641\end{document}
642
643% Local Variables: %
644% tab-width: 4 %
645% fill-column: 100 %
646% compile-command: "make" %
647% End: %
Note: See TracBrowser for help on using the repository browser.