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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
|
\documentstyle[makeidx]{mybook}
%\input{slovak.tex}
%
%\vsize 29.7cm
%\hsize 21.0cm
%\textheight 22cm
%\textwidth 15.5cm
%\topmargin -0.5cm
%\oddsidemargin 0.5cm
%\evensidemargin 0.5cm
%\renewcommand{\chaptername}{Kapitola}
%\renewcommand{\appendixname}{Dodatok}
%\renewcommand{\bibname}{Preh"lad literat`ury}
%\renewcommand{\contentsname}{Obsah}
%\renewcommand{\figurename}{Obr}
%\renewcommand{\listfigurename}{Zoznam obr`azkov}
%% doktorat
%\addtolength{\topmargin}{-68pt}
%\addtolength{\textheight}{102pt}
%\renewcommand{\baselinestretch}{1.3}
%\textheight 20.0cm
%\textwidth 12.cm
%\topmargin -1.5cm
%\oddsidemargin 0.45cm
%\evensidemargin 0cm
%%%%%%%%%%%%%%%%%%%%%%%%
\textheight 24.7cm
\textwidth 17cm
%
%
\oddsidemargin -0.40cm
\topmargin -1.90cm
\newcommand{\CiF}{\sf Clif}
\newcommand{\mybasel}{\baselineskip 9pt}
\newcommand{\myver}{0.8.1.beta
}
\makeindex
%%
\title{The Interpreter \CiF \\ User's Guide}
\author{\v{L}. Kore\v{n}%\thanks{
%\protect \input{adr0.tex}}
\\
For version \myver
}
\date{Last updated 30 July 1996}
%%
\begin{document}
\maketitle
\pagenumbering{roman}
\chapter{Preface}
The syntax of the \CiF\ is based on the C language. The handbook is
sequenced very close to the \cite{ritchie} or other computer
programming language books.
The syntax structures are described in the following chapters. These
structures are described using examples. All examples are simply
stated programs and therefore they should be understandable for users
knowing a C-language.
\tableofcontents
\listoffigures
\chapter{Introduction to the interpreter \CiF}
Our goal is to show main structures of the language via
examples. We want to explain all structures in the way of
writing simply stated programs.
%This kind of explanation has disadvantages. The most
%important are the explanation of a feature is not placed at
%one part and simplicity could be strange.
\section{Start of the interpreter}
The interpreter\index{interpreter!session} session can begin by
writing just \CiF\ on the command interpreter line. The interpreter
framework is started (another way of starting the interpreter session
is explained later). On the standard output is written:
\begin{verbatim}
This is interpreter Clif
\end{verbatim}
The simplest command is:\index{function!cprintf@{\sf cprintf()}}
{\mybasel
\begin{verbatim}
export_type extern int cprintf();
cprintf(1,"%s\n","hello");
\end{verbatim}}
On the standard output is written:
\begin{verbatim}
hello
\end{verbatim}
If you make a mistake in the statement, the interpreter announces
it. The argument 1 means {\it stdout}\index{stdout@{\it stdout}}.
\section{Variables and arithmetic operations}
The following program adds two numbers:\index{function!cprintf@{\sf
cprintf()}}\index{function!cscanf@{\sf cscanf()}}
{\mybasel
\begin{verbatim}
/* program for input and output of the text */
export_type extern int cprintf();
extern int cscanf();
int a,b,c;
cprintf(1,"%s\n","input value a");
cscanf(0,"%d",a);
cprintf(1,"%s\n","input value b");
cscanf(0,"%d",b);
c=a+b;
cprintf(1,"the sum is %d\n",c);
exit;
\end{verbatim}}
The comment\index{comment} lines are embedded between the $\slash
\ast$ and $\ast \slash$. They are unimportant during the run-time.
Variables\index{variable!declaration} have to be declared before they
are used first time. If you forget the declaration statement the
interpreter announces the mistake. The declaration contents of a name
of the type and list of variables separated by commas:\\
\begin{verbatim}
int a,b,c;
\end{verbatim}
The argument 0 in {\sf cscanf} statement means {\it
stdin}\index{stdin@{\it stdin}}. The rest of the statements is
described in the following sections.
\pagenumbering{arabic}
\chapter{Data types, operators and expressions}
\section{Names of the variables}
Names of the variables\index{variable!name} are sequences of
characters. The first character must be a letter. The character "\_"
is assumed like a letter as well. The names of the variables are
restricted. The names can not be keywords (e.g. {\sf if, else,
int}). The keywords are dedicated.
\section{Data types}
The interpreter \CiF\ as well as the C-language, has only few
fundamental data types: \label{`types'}
\begin{tabular}{ll}
{\sf char} & one byte, it is possible to store one
character from the set \\
{\sf int} & integer, it is depending on a size of the
integer of the host \\
{\sf float} & single precision real number \\
{\sf double} & double precision real number \\
\end{tabular}
\section{Constants}\index{constant}
There are different types of constants. The
integer\index{constant!int@{\sf int}} constant has usual notation for
example 134 e.g. it is any sequence of digits. The {\sf
float}\index{constant!float@{\sf float}} and {\sf
double}\index{constant!double@{\sf double}} constants have the
following notations: 134.435 or 12.56e-3 or 0.2345E3
The character\index{constant!char@{\sf char}} constant is a character
cited in single quotas\index{quotas!single}, for example 'a'. The
value of the character constant is integer value from the set of
characters of the host computer (for example ASCII).
There are special character constant\index{constant!special!char@{\sf
char}} comprised of sequence of characters, for example: $\backslash$n
(newline), $\backslash$t (tab), $\backslash \backslash$ (backslash),
etc.
The string constant\index{constant!string} is embedded by double
quotas\index{quotas!double}, e.g. "I am a string". The quotas are not
included in string constants. The string is an array\index{array} of
characters. At the end of the string constant an empty character
$\backslash$0 is appended. Therefore it is necessary to bear into mind
that 'x' is different from "x".
\section{Declaration}\index{variable!declaration}
All variables must have been declared before they are used. The
declaration contents of type specifier followed by a list of one or
more variables of the type. For example:\\
{\mybasel
\begin{verbatim}
int a,b,c;
double d,e,f;
int g;
double h;
\end{verbatim}}
The declaration is a little bit larger but it can be completed by a
comment.
\section{Arithmetic operators}
The arithmetic operators\index{operator!arithmetic} are
$+,-,\ast,\slash$ and the modulus operator \%. There is a
operator\index{operator!unary} of unary $-$ but not unary $+$. The
rest is truncated in the integer division. The expression $x$\%$y$ is
the operation $x$ divided by $y$. The result is zero, if $y$ is the
divisor of the $x$.
The operators $+$ and $-$ have the same
precedence\index{operator!precedence}. The precedence is lower then
the (equal) precedence of the operators $\ast, \slash$ and \%. The
arithmetic operators are left
associative\index{operator!associativity}.
%The order of evaluation of the
%associative and commutative operators (e.g. $\ast$ and
%$+$) is left associative too.
\section{Relational and logical
operators}\index{operator!relational}\index{operator!logical}
The relational operators are\\
\hspace{0.5cm}$>$\hspace{0.5cm}$>\,=$\hspace{0.5cm}$<$\hspace{0.5cm}$<\,=$\\
Each of them has the same precedence\index{operator!precedence}. The
operators of equality are behind them in the precedence\\
\hspace{0.5cm}$==$\hspace{0.5cm}$!=$\\ (both have the same
precedence). The relational operators have the lower precedence than
the arithmetic ones.
The logical operators\index{operator!logical} \&\& and $||$ are left
associative\index{operator!associativity}. The logical expressions
are evaluated from left to right.
%These behaviors are important for the writing good programs.
The precedence of the operator \&\& is higher than the operator
$||$. Both of them have the lower precedence than the relational and
equality operators.
\section{Type conversion}
In an expression, the operands can differ in types. The
coercions\index{coercion} take place in the expressions which have a
sense. For example, using a variable which has type double as an array
subscript has no sense; therefore the coercion is not processed. In
this case the interpreter announces an error.
If one of the operands is a double, all operands become
double. Always, types of both processed operands are compared. Then
the coercion is made to the type of operand which is "wider". The
order of the implemented types from the "narrowest" to the "widest" is
as follows: {\it char, int, float, double}. Another type of
coercion\index{coercion} is in an assignment. The right side value of
the expression is converted to the type of the variable on the left
side. The type of result is the same as type of the variable on the
left side of the expression.
The type of the result can be forced by cast operator which
causes an explicit conversion. In a statement\\ {\em
(name\_of\_type) expression}\\ the {\em expression} is
converted to the {\em name\_of\_type}.
\section{Increment and decrement
operators}\index{operator!increment}\index{operator!decrement}
The language of the interpreter has operators for the increment and
decrement of the
variables\index{variable!increment}\index{variable!decrement}. The
increment operator ($++$) adds one to the operand. The decrement
operator ($--$) subtracts one from the operand. The operators can be
used only in the prefix notation\index{notation!prefix}. E.g. let n
is 5, the expression\\
\begin{verbatim}
x = ++n;
\end{verbatim}
assigns to the variable n the value 6. Then the value 6
is assigned to the variable x.
\section{Bitwise logical operators}\index{operator!bitwise logical}
The \CiF\ has operators for bit manipulating\\
\begin{tabbing}
\&\hspace{3cm} \=the bitwise AND operator\\
$|$ \>the bitwise inclusive OR operator\\
$\hat{}$ \>the bitwise exclusive OR operator\\
$<<$ \>the left shift operator\\
$>>$ \>the right shift operator\\
$\tilde{}$ \>the unary one's complement
operator\\\index{operator!one's complement}\index{operator!unary}
\end{tabbing}
The bitwise AND operator\index{operator!AND} (\&) is often used as a
mask of the bits. The bitwise inclusive OR operator\index{operator!OR}
($|$) is used in a setting of bits.
An user should bear in mind the big difference between the bitwise
logical operators \&, $|$ and logical operators \&\&, $||$. For
example if $x$ is 1 and $y$ is 2 then expression $x$\&$y$ is equal
zero, while $x$\&\&$y$ is equal one. The result of bitwise logical AND
and bitwise logical OR is always {\it int}.
The shift operators\index{operator!shift} $<<$ and $>>$ cause the
shift of the left operand to the left or right. The number of shifted
bits is in the right operand. The result of these operations is
undefined if the right operand is negative or greater than or equal to
the length of object in this bits.
The unary one's complement operator\index{operator!unary} $\tilde{}$
changes each bit, ones to zeros and vice versa. The type of the
operand must be integer.
\section{Conditional expression}
There is a short form for the conditional statement using only
expressions instead of statements, i.e.:
{\mybasel
\begin{tabbing}
\sf expr1 ? expr2 : expr3;
\end{tabbing}}
The {\sf expr1} is evaluated. If it is true, the {\sf expr2} is
executed. If the {\sf expr1} is false, {\sf expr3} is executed.
\section{Assignment operators}
\label{sec:ao}
There are the following compound assign operators:\\ \newline
{\mybasel
\begin{center}
\begin{tabular}{|c|}\hline
$*=$ \\ \hline
$/=$ \\ \hline
$\%=$ \\ \hline
$+=$ \\ \hline
$-=$ \\ \hline
$<<=$ \\ \hline
$>>=$ \\ \hline
$\&=$ \\ \hline
$\hat{}=$ \\ \hline
$|=$ \\ \hline
\end{tabular}
\end{center}}
\section{The precedence and the order of the
evaluation}\index{operator!precedence}
The following table offers the survey of the operators. The operators
in the same line have the same precedence. The precedence increases
line by line.\\ \newline
{\mybasel
\begin{center}
\begin{tabular}{|l|c|} \hline
{\bf Operator} & {\bf Associativity}\\ \hline \hline
$=$ $+=$ $-=$ $*=$ $/=$ $\%=$ $\&=$ $\hat{}=$ $|=$ $<<=$ $>>=$ & right
to left\\ \hline
$?:$ & right to left \\ \hline
$||$ & left to right\\ \hline
$\&\&$ & left to right\\ \hline
$|$ & left to right\\ \hline
$\hat{}$ & left to right\\ \hline
$\&$ & left to right\\ \hline
$==$ $!=$ & left to right\\ \hline
$<$ $<=$ $>$ $>=$& left to right\\ \hline
$<<$ $>>$ & left to right\\ \hline
$+$ $-$ & left to right\\ \hline
$*$ $/$ $\%$ & left to right\\ \hline
$\tilde{}$ $!$ $++$ $--$ $-$ ({\it name\_of\_type})& right to left\\
\hline $($ $)$ $[$ $]$ & left to right\\ \hline
\end{tabular}
\end{center}}
\vspace{1cm}
The table must be born in mind to write correct program without
complications.
\chapter{Control flow}
\section{Conditional statement}
The conditional statement\index{statement!if@{\sf if}} {\sf if-else}
has the following syntax:
{\mybasel
\begin{tabbing}
\sf if \sf ( \sf expr \sf ) \= \\
\> \sf \{ \sf stat1 \sf \} \\
\sf else \\
\> \sf \{ \sf stat2 \sf \} \\
\end{tabbing}}
The {\sf else }part is not compulsory. The {\sf expr} is evaluated and
if it is nonzero, the first substatement is executed. If the {\sf
expr} is equal zero and if the {\sf else} part exists, the second
substatement is executed ({\sf stat2}).
\section{Statement {\sf switch-case}}
If more conditional statements have to be used in a sequence, a {\sf
switch}\index{statement!switch@{\sf switch}} can be used. Then the
different branches can be followed using {\sf
case}\index{statement!case@{\sf case}} statement. I.e.,
{\mybasel
\begin{tabbing}
\sf switch \sf ( \sf expr \sf ) \= \\
\> \sf \{ \\
\> \sf case \sf expr1 : \sf stat1 \\
\> \sf case \sf expr2 : \sf stat2 \\
\> \sf default : \sf stat3 \\
\> \} \\
\end{tabbing}}
The {\sf expr} is evaluated. Then it is compared to {\sf expr1}. If
they are equal then the {\sf stat1} is executed. If they are not equal
the execution continues at the next label ({\sf expr2} in this case).
The expressions in labels have to be constant expressions. {\sf
default} is executed only if no expression in label statements is
equal to the {\sf expr}. The {\sf default} statement can be
omitted. A usage is in the following
example:\index{function!cprintf@{\sf
cprintf()}}\index{function!cscanf@{\sf cscanf()}}
{\mybasel
\begin{verbatim}
export_type extern int cprintf();
extern int cscanf();
int a;
cprintf(1,"%s\n","input a");
cscanf(0,"%d",a);
switch (a)
{
default:
cprintf(1,"%s\n","default");
case 1:
cprintf(1,"%s\n","1");
break;
case 2|5:
cprintf(1,"%s\n","2|5");
break;
case 3:
cprintf(1,"%s\n","3");
cprintf(1,"%s\n","no break");
case 4:
cprintf(1,"%s\n","4");
break;
}
exit;
\end{verbatim}}
\section{{\sf while}, {\sf do while} and {\sf for}
loops}\index{statement!while@{\sf while}}\index{statement!for@{\sf for}}\index{statement!do@{\sf do}}
In
{\mybasel
\begin{tabbing}
\sf while \sf ( \sf expr \sf )\= \\
\> \sf \{ \sf stat \sf \} \\
\end{tabbing}}
the {\sf expr} is evaluated. If its value is nonzero, the {\sf stat}
is executed and the {\sf expr} is evaluated again. The
loop\index{loop} continues until the {\sf expr} becomes 0. After loop
finishing, it is proceeded below the {\sf stat}.
The sequence of statements\index{statement!do@{\sf do}}
{\mybasel
\begin{tabbing}
\sf do \sf \= \\
\> \sf \{ \sf stat \sf \} \\
\> \sf while \sf ( \sf expr \sf ) \\
\end{tabbing}}
is executed at least once. The {\sf while expr} is evaluated at the
end of the loop sequence. If the {\sf expr} is true the sequence of
statements in the loop is repeated.
The statement {\sf for}\\
{\mybasel
\begin{tabbing}
\sf for \sf ( \sf expr1; \sf expr2; \sf expr3) \= \\
\> \sf \{ \sf stat \sf \} \\
\end{tabbing}}
is equal with the sequence of the following commands\\
{\mybasel
\begin{tabbing}
\sf expr1;\\
\sf while \sf ( \sf expr2 \sf ) \= \\
\> \sf \{ \sf stat \\
\> \sf expr3; \sf \}\\
\end{tabbing}}
All three parts of the statement {\sf for} are
expressions\index{expression}. The expressions {\sf expr1} and {\sf
expr3} are usually assignments. The {\sf expr2} is a relational
expression. Thus the first expression specifies initialization for
the loop\index{loop!initialization}; the second specifies a test, made
before each iteration, such that the loop is exited when expression
becomes 0. The third expression often specifies an
incrementing\index{operation!increment} that is performed after each
iteration.
If all three parts are dropped, the {\sf expr2} is
considered true and\\
{\mybasel
\begin{tabbing}
\sf for \sf( \sf ; \sf ; \sf ) \sf \{ \= \\
\> $\dots$ \\
\> \sf \} \\
\end{tabbing}}
is an infinite loop\index{loop!infinite}. It is assumed another
termination of the loop. For example by the
statements\index{statement!break@{\sf break}} {\sf break} or {\sf
return}\index{statement!return@{\sf return}}.
Everyone should consider which loop statement\index{loop!statement} to
use. If we need not to initialize or to reinitialize the loop
variable\index{loop!initialize}, we can take an advantage of the
statement {\sf while}.
The loop statement\index{statement!for@{\sf for}} {\sf for} is useful
if we need to initialize and reinitialize the loop variable. The
control statements are concentrated at the beginning of the loop.
\section{Statement {\sf break}}\index{statement!break@{\sf break}}
It is useful to break processing of the loop at arbitrary place. The
statement\index{statement!break@{\sf break}} {\sf break} causes
termination of the most inner {\sf while}\index{statement!while@{\sf while}} or {\sf for}\index{statement!for@{\sf for}}. The
following program shows function of the {\sf
break}\index{statement!break@{\sf break}}:\index{function!cprintf@{\sf cprintf()}}
{\mybasel
\begin{verbatim}
export_type extern int cprintf();
int x;
x=1;
while (x<=10)
{
cprintf(1,"%s\n","now in while");
if(x == 5)
{
cprintf(1,"%s\n","now in IF");
cprintf(1,"%s\n","*********************************");
x++;
break;
}
cprintf(1,"x=%d\n",x);
x++;
}
exit;
\end{verbatim}}
Control passes to the statement following the terminated compound
statement.
\section{Statement {\sf continue}}\index{statement!continue@{\sf continue}}
Statement {\sf continue} is related to the statement {\sf break}. It
causes control to pass to the loop-continuation part of the most inner
loop ({\sf for, while})\index{statement!while@{\sf while}}\index{statement!for@{\sf for}}, i.e. to the end of the loop.
The {\sf while} loop proceeds at the expression. The {\sf for} loop
proceeds at the reinitialization. The program with the statement {\sf
continue} follows:\index{function!cprintf@{\sf cprintf()}}
{\mybasel
\begin{verbatim}
export_type extern int cprintf();
int x;
for (x=1;x<=10;x++)
{
cprintf(1,"%s\n","now in for");
if(x == 5)
{
cprintf(1,"%s\n","now in IF");
print(1,"%s\n","*********************************");
continue;
}
cprintf(1,"x=%d\n",x);
}
exit;
\end{verbatim}}
and more complex example:\index{function!cprintf@{\sf cprintf()}}
{\mybasel
\begin{verbatim}
/* test program for nested loop */
export_type extern int cprintf();
int x,y;
y=1;
for(x=1;x<15;x++)
{
cprintf(1,"%s\n","now in for");
cprintf(1,"x=%d\n",x);
while(x>5 && x<10)
{
cprintf(1,"%s\n","now in while");
cprintf(1,"x=%d\n",x);
if(x==9)
{
cprintf(1,"%s\n","now in the body of the first condition");
cprintf(1,"x=%d\n",x);
continue;
}
if(x==8)
{
cprintf(1,"%s\n","now in the body of the second condition");
break;
}
x=x+y;
cprintf(1,"x=%d\n",x);
}
if(x==14)
{
cprintf(1,"%s\n","now in the body of the third condition");
cprintf(1,"x=%d\n",x);
break;
}
}
exit;
\end{verbatim}}
\section{Statement {\sf goto}}\index{statement!goto@{\sf goto}}
Although the {\sf goto} statement is a part of unstructured
programming the \CiF environment supports this syntactic construction
as well. The {\sf goto} statement is used for unconditional branching
of programs. The jump is possible only in the level one. There is no
possibility to jump with {\sf goto} statement across
functions. Typical usage is in the following example:
\index{function!cprintf@{\sf cprintf()}}
{\mybasel
\begin{verbatim}
export_type extern int cprintf();
int i;
cprintf(1,"%s\n","in front");
{
a: cprintf(1,"%s ","a");
if (10 == i)
{
cprintf(1,"%s ","in if"); ++i;
goto b;
}
else
{
++i;
}
goto a;
b:
cprintf(1,"%s ","b");
}
cprintf(1,"%s\n","after");
exit;
\end{verbatim}
}
\chapter{Functions and program structure}
Bigger tasks can be split by functions to the parts.
Programmer can use the parts which were designed by other
authors.
\section{Fundamentals}
Each function looks like
{\mybasel
\begin{tabbing}
\sf type \sf name \sf(type \sf argument, \sf \ldots \sf )\= \\
\> \sf \{ \\
\> \sf declarations \sf a \sf statements \\
\> \sf \} \\
\end{tabbing}}
(The types are mentioned on the page~\pageref{`types'}. In addition,
the type {\sf void}\index{type!void@{\sf void}} can be in a function
declaration). The {\sf void} type denotes an nonexistent return
value. Some parts of functions can be dropped out; minimal function
is\\ {\sf type dummy() \{ \}\\}. (The function can be useful in
filling place in program design).
{\sf return} statement\index{statement!return@{\sf return}} returns
value of the called function to the calling function. Syntax of the
statement {\sf return} is as follows:\\ {\sf return ( expr );\\}\\ The
calling function can ignore the return value.
\section{Scope rules}
Global variables\index{variable!global} must have been declared before
they are used. Functions\index{function!declaration} must be declared
before the formal call as well. The functions must be
defined\index{function!definition} before the real call. The part of
program where the name is declared is a scope of the
name\index{variable!scope}. The global
variables\index{variable!global} are valid during whole interpreter
session\index{interpreter!session}. The local
variables\index{variable!local} are only valid during the processing
of the function. The interpreter searches the table of local variables
for the variable. If it is not successful, it proceeds searching the
table of global variables for the variable. If it is not successful
again it announces an error.
\section{Recursion}
Functions\index{function!recursive} can be written in recursive form
(e.g. a function can call {\em itself}). The recursion can be direct
or indirect. An example of the recursion is n!
(factorial):\index{function!cprintf@{\sf
cprintf()}}\index{function!cscanf@{\sf cscanf()}}
{\mybasel
\begin{verbatim}
export_type extern int cprintf();
extern int cscanf();
int n;
int fak(int n)
{
if(n==1)
{return(1);}
return(fak(n-1)*n);
}
cprintf(1,"%s\n","input n");
cscanf(0,"%d",n);
n=fak(n);
cprintf(1,"factorial =%d\n",n);
exit;
\end{verbatim}}
Other examples follow:\\ Recursive function for Fibonacci
sequence\index{function!cprintf@{\sf
cprintf()}}\index{function!cscanf@{\sf cscanf()}}
{\mybasel
\begin{verbatim}
export_type extern int cprintf();
extern int cscanf();
int n;
int fib(int n)
{
if(n<=1)
{
return(1);
}
else
{
return(fib(n-1)+fib(n-2));
}
}
cprintf(1,"%s\n","input n");
cscanf(0,"%d",n);
n=fib(n);
cprintf(1,"n=%d\n",n);
exit;
\end{verbatim}}
or for $n \choose k$\index{function!cprintf@{\sf
cprintf()}}\index{function!cscanf@{\sf cscanf()}}
{\mybasel
\begin{verbatim}
export_type extern int cprintf();
extern int cscanf();
int n,k;
int komb(int n,int k)
{
if((n==k) || (k==0))
{
return(1);
}
else
{
return(komb(n-1,k)+komb(n-1,k-1));
}
}
cprintf(1,"%s\n","input n");
cscanf(0,"%d",n);
cprintf(1,"%s\n","input k");
cscanf(0,"%d",k);
n=komb(n,k);
cprintf(1,"n=%d\n",n);
exit;
\end{verbatim}}
and for ${n \choose k} \cdot \sum \limits_{i=1}^{l}i
$\index{function!cprintf@{\sf cprintf()}}\index{function!cscanf@{\sf
cscanf()}}
{\mybasel
\begin{verbatim}
export_type extern int cprintf();
extern int cscanf();
int n,k,l;
int f(int n,int k)
{
if((k==0) || (n==k))
{
return(1);
}
return(f(n-1,k)+f(n-1,k-1));
}
int komb(int n,int k,int l)
{
if((k==0) || (n==k))
{
return(1);
}
if(l==1)
{
return(f(n,k));
}
return(komb(n-1,k,l-1)+f(n-1,k)*l+komb(n-1,k-1,l-1)+f(n-1,k-1)*l);
}
cprintf(1,"%s\n","input n");
cscanf(0,"%d",n);
cprintf(1,"%s\n","input k");
cscanf(0,"%d",k);
cprintf(1,"%s\n","input l");
cscanf(0,"%d",l);
n=komb(n,k,l);
cprintf(1,"n=%d\n",n);
exit;
\end{verbatim}}
\section{Including files for interpretation during the
session}\index{interpreter!session}
Users can include a file into the interpreter by
statement\index{statement!load@{\sf load}}\\
{\sf load ( name\_of\_file);\\}
The file will be interpreted automatically.
\chapter{Derived variable types}\index{variable!type}
\section{Arrays}\index{array}
One and multidimensional type of
arrays\index{array!onedimensional}\index{array!multidimensional} are
available in the interpreter. They are used to group the sets of the
like variables. Example of one-dimensional array follows:
\begin{verbatim}
type name_of_array[number_of_elements];
\end{verbatim}
The size of an array\index{array!size of} must be an integer
constant\index{constant!int@{\sf int}}. An element of the
array\index{array!subscript} can be accessed through subscripting.
For example:\\
\begin{verbatim}
a[7]=56;
\end{verbatim}
In the interpreter, subscripting\index{array!subscript} begins with
zero. An integer expression\index{expression} can be between the
brackets; i.e. a subscripting variable\index{variable!subscripting},
an expression\index{expression}, or return value from a function call.
\subsection{Internal representation of arrays}\index{array!internal
representation of}
Arrays are containing certain data type. They are stored in a
contiguous set of these data types. See figure \ref{pole}
\begin{figure}
\begin{center}
\unitlength=1.00mm
%\special{em:linewidth 0.4pt}
\linethickness{0.4pt}
\begin{picture}(73.00,128.33)
\put(50.00,46.00){\framebox(23.00,81.00)[cc]{}}
\put(50.00,122.67){\line(1,0){23.00}}
\put(50.00,120.67){\line(1,0){23.00}}
\put(61.00,118.67){\makebox(0,0)[cc]{.}}
\put(61.00,114.67){\makebox(0,0)[cc]{.}}
\put(61.00,108.67){\makebox(0,0)[cc]{.}}
\put(50.00,99.00){\line(1,0){23.00}}
\put(46.00,109.00){\makebox(0,0)[cc]{0}}
\put(46.00,84.67){\makebox(0,0)[cc]{1}}
\put(61.00,36.00){\makebox(0,0)[cc]{.}}
\put(61.00,40.00){\makebox(0,0)[cc]{.}}
\put(61.00,44.00){\makebox(0,0)[cc]{.}}
\put(50.00,4.67){\framebox(23.00,18.67)[cc]{}}
\put(60.67,101.67){\makebox(0,0)[cc]{n}}
\put(25.67,127.00){\line(0,-1){122.33}}
\put(50.00,74.33){\line(1,0){23.00}}
\put(46.00,56.33){\makebox(0,0)[cc]{n}}
\put(0.67,41.00){\line(1,0){52.67}}
\put(16.34,91.00){\makebox(0,0)[cc]{0}}
\put(16.00,21.67){\makebox(0,0)[cc]{1}}
\put(36.33,127.00){\vector(1,0){13.67}}
\put(42.33,128.33){\makebox(0,0)[cc]{A}}
\put(45.67,18.00){\makebox(0,0)[cc]{.}}
\put(45.67,22.00){\makebox(0,0)[cc]{.}}
\put(45.67,26.00){\makebox(0,0)[cc]{.}}
\put(16.00,11.00){\makebox(0,0)[cc]{.}}
\put(16.00,15.00){\makebox(0,0)[cc]{.}}
\put(16.00,19.00){\makebox(0,0)[cc]{.}}
\end{picture}
\caption[Internal representation of arrays]{Internal
representation of arrays}\label{pole}
\end{center}
\end{figure}
\section{Multidimensional arrays}\index{array!multidimensional}
In the interpreter, arrays of arbitrary dimensions can also be
declared. The two-dimensional array with $m\times n$ elements is
declared as follows:
\begin{verbatim}
type name_of_array[m][n];
\end{verbatim}
An element of the array can again be accessed through
subscripting\index{array!subscripting}, e.g.
\begin{verbatim}
a[7][9]
\end{verbatim}
This will access the element in the 8-th row (row number 7)
and in the 10-th column (column number 9).
\section{Array names in expressions}
The element of the array\index{array!element of} in an expression can
only be accessed through subscripting\index{array!subscript}, but
there is an exception; the array can be passed as a parameter to a
function (without subscripting). An example of the parameter
passing\index{parameter!passing} mechanism using an array as a
parameter\index{array!as parameter}
follows:\index{function!cprintf@{\sf cprintf()}}
{\mybasel
\begin{verbatim}
export_type extern int cprintf();
int a[5][5][5][5][5];
a[0][0][0][0][0]=1;
a[1][1][1][1][1]=1;
a[2][2][2][2][2]=1;
a[3][3][3][3][3]=1;
a[4][4][4][4][4]=a[3][3][3][3][3];
int z(int b[][5][5][5][5])
{
cprintf(1,"%d\n",b[0][0][0][0][0]);
cprintf(1,"%d\n",b[1][1][1][1][1]);
cprintf(1,"%d\n",b[2][2][2][2][2]);
cprintf(1,"%d\n",b[3][3][3][3][3]);
cprintf(1,"%d\n",b[4][4][4][4][4]);
}
z(a);
exit;
\end{verbatim}}
\chapter{Termination of the \CiF\ session}
Statement {\sf exit;}\index{statement!exit@{\sf exit}} terminates the
interpreter session\index{interpreter!session}. This statement
terminates correctly work of the interpreter environment.
\chapter{Run-string handling}
So far, the input was always standard input. But the interpreter can
have specified parameters\index{parameter!run-string}\index{command
line} in the run-string (command line) which are names of
programs. The input is redirected and a file\index{file@open} is
opened instead of the standard input.\index{input!standard}
Other parameters can appear in the command line. One of the
parameters is {\sf $\slash$help} evoking brief help. Another
parameter is for resetting of the size of main memory\index{memory}
areas of the interpreter\index{interpreter!memory area of}. The size
is specified relatively. This parameter can be specified in the
run-string\index{parameter!run-string} as follows:
\begin{verbatim}
/bc=<number>
\end{verbatim}
As the {\sf $<$number$>$} can be specified a positive integer. The
default value is 10.
\chapter{The standard I/O routines}
So far, all our sample programs have written data to the standard
output\index{output!standard}\index{stdout@{\it stdout}} and read data
from standard input\index{input!standard}\index{stdin@{\it stdin}} (in
both cases the terminal). As long as we are
accessing\index{data!access} not large portions of data, thus data in
the simple form, this type of I/O routines is sufficient. However,
large amounts of data are generally stored in files\index{file}. To
perform I/O from and to files\index{file} I/O functions are included
in the interpreter.
Access to files generally requires four basic functions:
\begin{description}
\item[open] This enables access to a file and establishes
the file handle.
\item[close] This terminates access to the file. When the
access to a file is complete, it should be closed. The
number of files that the interpreter can simultaneously
manage is limited. Therefore the files should be properly
closed.
\item[read] This function gets data from the file.
\item[write] This function adds information to the file or
replaces information already in the file.
\end{description}
\section{Opening a file}\index{file!open}
The standard I/O function used to open a file is named copen(). The
function returns a handle\index{handle} to the opened
file\index{file!open}. The function has to be declared as
follows:\index{function!copen@{\sf copen()}}
\begin{verbatim}
extern int copen();
\end{verbatim}
The function has two arguments\index{function!argument}:
\begin{itemize}
\item {\sf file\_name} is the starting address of a character string
describing the name of the file. This can be a string constant or an
array name\index{array!as parameter}.
\item {\sf mode} which describes the action that should be
performing on the file. The modes are:
\begin{itemize}
\item {\sf r} The file\index{file!open} is opened for reading started
at the beginning of the file.
\item {\sf w} The file\index{file!open} is opened for writing; it is
assumed that the file is to be created. If the file does not exist, it
is created; if it exists, it is truncated (size of the file is reduced
to zero) and positioned at the beginning of the file.
\item {\sf a} The file\index{file!open} is opened for writing. The
mode is the same as mode for {\sf w} except that the initial position
is at the end of the file, i.e. the file is not truncated.
\item {\sf r+} The file\index{file!open} is opened for update,
i.e. the file is opened for reading and writing and is not
truncated. The initial position is at the beginning of the file.
\item {\sf w+} The file\index{file!open} is opened for update,
i.e. the file is opened for reading and writing and is truncated, if
the file already exists. If it does not exist, it is created. The
initial position is at the beginning of the file.
\item {\sf a+} The file\index{file!open} is opened for update,
i.e. the file is opened for reading and writing and is not truncated
and the initial position is at the end of the file.
\end{itemize}
\end{itemize}
The function returns\index{function!return} an handle\index{handle}
(integer number) if opening is successful. Otherwise the function
returns\index{function!return} {\it EOF}\index{EOF@{\it EOF}}.
\section{Closing a file}\index{file!close}
If the access to a file was completed, the file\index{file!close}
should be closed. The declaration\index{function!declaration} of the
function is as follows:\index{function!cclose@{\sf cclose()}}
\begin{verbatim}
extern int cclose();
\end{verbatim}
The argument of the {\sf cclose()}\index{function!cclose@{\sf
cclose()}} function\index{function!argument} is the return
value\index{function!return} from the {\sf
copen()}\index{function!copen@{\sf copen()}} function (the
handle\index{handle}). The handle is released. If the program exits
without closing a file\index{file!close}, the system closes
automatically the opened file.
The return\index{function!return} values are 0 that means a successful
file\index{file!close} closing or {\it EOF}\index{EOF@{\it EOF}} that
means an error was encountered during the closing of the file.
\section{Reading from a file}
As far, we have only used the {\sf
cscanf()}\index{function!cscanf@{\sf cscanf()}} function. Another
function that can be used is {\sf cgetc()}\index{function!cgetc@{\sf
cgetc()}} function. The parameter of the
function\index{function!parameter} is an integer number
(handle\index{handle}), which was returned by a {\sf
copen()}\index{function!copen@{\sf copen()}} function. The {\sf
cgetc()}\index{function!cgetc@{\sf cgetc()}} function reads a
character from the file\index{file} which is pointed by the
handle. The function {\sf cgetc()}\index{function!cgetc@{\sf cgetc()}}
is declared as follows:\index{function!cgetc@{\sf cgetc()}}
\begin{verbatim}
extern int cgetc();
\end{verbatim}
This function returns\index{function!return} an {\it
EOF}\index{EOF@{\it EOF}} character when an error occurred or at the
end-of-file.
\section{Writing to a file}\index{file!write}
In the previous sections we only write to the {\it
stdout}\index{stdout@{\it stdout}} via the {\sf cprintf()}
function. Analogous function to the {\sf
cgetc()}\index{function!cgetc@{\sf cgetc()}} is a function {\sf
cputc()}\index{function!cputc@{\sf cputc()}}. The function is
declared\index{function!declaration}:\index{function!cputc@{\sf
cputc()}}
\begin{verbatim}
extern int cputc();
\end{verbatim}
The function writes a character into the file\index{file!write}. On
failure, the function\index{function!return} returns an {\it
EOF}\index{EOF@{\it EOF}}.
\section{Functions {\sf cscanf()} and {\sf
cprintf()}}\index{function!cscanf@{\sf
cscanf()}}\index{function!cprintf@{\sf cprintf()}}
The functions used in previous section read from standard input and
wrote to standard output. Default handles for {\it
stdin}\index{stdin@{\it stdin}}, {\it stdout}\index{stdout@{\it
stdout}} and {\it stderr}\index{stderr@{\it stderr}} are 0,1,2
respectively. These functions can read\index{file!read} from or
write\index{file!write} to a file. As the first parameter of the
functions\index{function!parameter} is used a handle\index{handle} of
the file, for example:\index{function!cscanf@{\sf
cscanf()}}\index{function!cprintf@{\sf cprintf()}}
{\mybasel
\begin{verbatim}
float f;
int a,b;
extern int cscanf ();
export_type extern int cprintf ();
extern int cclose ();
extern int copen ();
cscanf(0,"%f",f);
b=copen("test_file","w");
cprintf(1,"handle = %d\n",b);
cprintf(b," f=%f\n",f);
cscanf(0,"%d",a);
cprintf(b,"integer value = %d\n",a);
cclose(b);
cprintf(1,"%s\n","I am a string");
exit;
\end{verbatim}}
The second parameter in the {\sf cscanf()} function is a format string
and the third parameter is a variable name for storing the input.
The second parameter of the {\sf cprintf()} function is a format
string and/or string constant and the third parameter is a variable
name for output.
The following list describes valid format strings\index{string!format}:
\begin{description}
\item[d] the input/output field is a decimal integer; the
corresponding variable name must point to the integer
\item[u] the input/output field is a decimal integer; the
corresponding variable name must point to the integer; value is
unsigned
\item[o] the input/output field is a octal integer; the corresponding
variable name must point to the integer
\item[x] the input/output field is a hexadecimal integer; the
corresponding variable name must point to the integer
\item[e,f,g] the input/output field is an optionally signed string of
digits. The field may contain a radix character and an exponent field
begins with a letter E or e, followed by an optional sign or space and
an integer. The variable must point to the floating point variable. If
you specify l, the variable must point to the double precision
variable.
\item[s] the input/output field is a character string. The variable
must point to an array of characters large enough to contain the
string and a termination character ($\backslash 0$). The {\sf
cscanf()} function adds the termination character automatically. A
white-space character terminates the input string, so the input string
cannot contain spaces.
\item[c] the input/output field is a character or character
string. The variable must point to either a character variable or a
character array.
\end{description}
\chapter{Graphics interface}
So far, the sample programs have written data to the standard
output\index{output!standard} in the numerical form. The interpreter
also provides graphics outputs\index{output!graphics}. Certain type
of graphics functions\index{function!graphics} is included in the
interpreter. A call to the graphics functions results in establishment
of a channel\index{channel}. Access to channels generally requires
four basic operations: \\
\begin{description}
\item[open] First channel\index{channel!open} should be opened which
allows access to the other graphics
functions\index{function!graphics}.
\item[close] This terminates access to the
channel\index{channel!close}. When the access to the channel is
complete, it should be closed. The number of channels that can
simultaneously be opened is limited.
\item[write] This adds data to the channel\index{channel!write}.
\item[flush] This flushes all data from the
channel\index{channel!flush} to the output device.
\end{description}
\section{Opening a channel}
The graphics interface function used to open a
channel\index{channel!open} is named {\sf
chopen()}\index{function!chopen@{\sf chopen()}}. It returns a integer
number - handle\index{handle}, if opening succeeds; otherwise it
returns -1. The handle must be saved; all other graphics functions
require the handle as an argument. The {\sf
chopen()}\index{function!chopen@{\sf chopen()}} function should be
declared as follows:
\begin{verbatim}
extern int chopen("arguments");
\end{verbatim}
This function creates a window for a graphics
output\index{output!graphics}. This must be done before attempts are
made to write in to the channel\index{channel!write}.
The {\sf chopen()}\index{function!chopen@{\sf chopen()}} has the
following arguments (arguments are in the form of
sublanguage):\index{drawn object}
\begin{description}
\item[fields]\label{`fields'}\index{fields} This record is compulsive.
This is an integer number which specifies number of records written to
the channel\index{channel!record}. The other arguments have their
default\index{default} values.
\item[style] This specifies the type and path (attributes) of the
drawn object. The {\sf style}\index{style} is a positive integer number.
\item[lower]\index{lower} This denotes lower boundary of the drawn
object.
\item[upper]\index{upper} This denotes upper boundary of the drawn
object.
\item[print\_format]\index{print\_format} The available values are
{\sf point}\index{point@{\sf point}} and {\sf line}\index{line@{\sf
line}}. Default\index{default} is {\sf line}.
\item[start\_time]\index{start\_time} Initial setup of the time. The
available values are {\sf automatic}\index{automatic@{\sf automatic}}
and an arbitrary real number. The {\sf automatic}\index{automatic@{\sf
automatic}} means that the window is cleared when the output reaches
the right end of the window. New initial time continues at the end
value of the time in previous window. Default\index{default} value is
0.0.
\item[duration\_time]\index{duration\_time} This means a length of the
window, i.e. the number of the records contained in the window in the
vertical direction. The available values are {\sf
automatic}\index{automatic@{\sf automatic}} and an arbitrary real
number. The {\sf automatic}\index{automatic@{\sf automatic}} means if
the output reaches the right end of the window then output is redrawn,
i. e. the {\sf duration\_time} size is changed. Default\index{default}
value is 1000.0.
\item[w\_resolution]\index{w\_resolution} This specifies the size of
the window. The default \index{default}value is 256 by 256 pixels.
\item[type]\index{type} This specifies if the alphanumeric window
should be created as well. The available values are {\sf
alpha}\index{alpha} and {\sf
graphic}\index{graphic}. Default\index{default} value is the {\sf
graphic}.
\item[OnLeaveW]\index{OnLeaveW} The parameter specifies if the
interpreter should be stopped temporarily when enriches the right end
of the window. The available values are {\sf noevent}\index{noevent}
and {\sf suspend}\index{suspend}. The default\index{default} value is
the {\sf noevent}.
\end{description}
The records\index{channel!record} are separated by a backslash
immediately followed by a newline. The first record should be the {\sf
fields}\index{fields}. The other records can be arbitrarily ordered.
\section{Closing a channel}\index{channel!close}
When the program has completed its use of a channel, the channel
should be closed using {\sf chclose()}\index{function!chclose@{\sf
chclose()}} function. This function is declared as follows:
\begin{verbatim}
extern void chclose();
\end{verbatim}
This function closes the specified channel and releases the {\sf
handle}\index{handle}. The {\sf handle} is the only parameter of the
{\sf chclose} function. If program exits without closing a channel,
the system will automatically close the channel.
\section{Writing to a channel}\index{channel!write}
The graphics interface function used for writing to a channel is
named {\sf chwrite}\index{function!chwrite@{\sf chwrite()}}. This
function is declared as follows:
\begin{verbatim}
extern void chwrite();
\end{verbatim}
The function has the following arguments: The first argument is the
{\sf handle}\index{handle} of the channel in to which data should be
written. The second argument is a datum. The data are collected in the
specified channel\index{data!collected in channel}. When number of
records is equal to the number specified in the record {\sf
fields}\index{fields} (see page~\pageref{`fields'} for more details),
the data are put into the window.
\section{Flushing a channel}\index{channel!flush}
This function flushes a channel in which were collected data. The
function is declared:\index{function!chflush@{\sf chflush()}}
\begin{verbatim}
extern void chflush();
\end{verbatim}
The function has no arguments. It flushes all opened graphics
channels. This function is necessary at the end of putting image to
the window, when data are in the channel but in the window.
The following program shows a use of graphics interface
functions:\index{function!sin@{\sf sin()}}
{\mybasel
\begin{verbatim}
double x,y;
extern double sin();
/* declaration of graphics functions */
extern int chopen();
extern void chwrite();
extern void chclose();
extern void chflush();
int handle;
handle=chopen("fields=6\
lower(0)=-0.5\
upper (0) = 0.5\
style (0) = 10\
lower(1)=-1.5\
upper (1) = 1.5\
style (1) = 20\
lower(2)=-3.\
upper (2) = 3.\
style (2) = 15\
lower(3)=-3.5\
upper (3) = 3.5\
style (3) = 5\
lower(4)=-4.\
upper (4) = 4.\
style (4) = 17\
lower(5)=-4.5\
upper (5) = 4.5\
style (5) = 7\
start_time=automatic\
duration_time=256.\
w_resolution=600 400\
print_format=point\
type=alpha\
OnLeaveW=suspend");
for(x=0;x<6.5;x=x+0.01)
{
y=sin(x);
chwrite(handle,y);
chwrite(handle,y);
chwrite(handle,y);
chwrite(handle,y);
chwrite(handle,y);
chwrite(handle,y);
}
chclose(handle);
exit;
\end{verbatim}}
\chapter{Interrupt handling}\index{interrupt!handling}
The run of the virtual machine of the interpreter can be
interrupted. The interpreter provides two kinds of the interrupts;
synchronous\index{interrupt!synchronous} and
asynchronous\index{interrupt!asynchronous}.
\section{Synchronous interrupt}
The synchronous interrupt is specified in the form of
statements\index{statement!csuspend@{\sf csuspend}} in a program. The
following statement can be placed where it is useful:
\begin{verbatim}
csuspend;
\end{verbatim}
When the interpreter reaches the statement it is stopped
and echoed:
\begin{verbatim}
clif interrupt level <number>
\end{verbatim}
Where the {\sf $<$number$>$} is a positive number. A user, at any
interrupt level\index{interrupt!level}, can do anything what is
available in the interpreter. To resume the run of the interpreter a
user should write the following statement\index{statement!resume@{\sf
resume}}:
\begin{verbatim}
resume;
\end{verbatim}
or press the key DC4 (Ctrl-t)\index{Ctrl-t}. The interpreter is then
echoed:
\begin{verbatim}
clif interrupt level <number>-1
\end{verbatim}
which means that the interrupt level is decreased. The lowest
interrupt level\index{interrupt!level} is zero. An example of use of
the synchronous interrupt\index{interrupt!synchronous}
follows:\index{function!cprintf@{\sf
cprintf()}}\index{function!cscanf@{\sf cscanf()}}
{\mybasel
\begin{verbatim}
int a,n;
extern int cscanf();
export_type extern int cprintf();
int fakt(int n)
{
csuspend;
if(n==1)
{
return(1);
}
else
{
return(fakt(n-1)*n);
}
}
cprintf(1,"input n %d\n",n);
cscanf(0,"%d",n);
csuspend;
a=fakt(n);
cprintf(1,"factorial = %d\n",a);
exit;
\end{verbatim}}
\section{Asynchronous interrupt}\index{interrupt!asynchronous}
An asynchronous interrupt can be invoked by the user with pressing the
key {\sf Ctrl-t}\index{Ctrl-t} while the virtual machine\index{virtual
machine} of the interpreter is running. A dummy program in which a
user can follow the use of asynchronous interrupt
is:\index{function!cprintf@{\sf cprintf()}}
{\mybasel
\begin{verbatim}
extern int cscanf();
export_type extern int cprintf();
int i;
for(i=0;i<100000;++i)
{
cprintf(1,"%d",i);
}
cprintf(1,"%s\n","loop is over");
exit;
\end{verbatim}}
After making statements requested by the user, the further processing
can be invoked as by the synchronous
interrupt\index{interrupt!synchronous} with pressing {\sf Ctrl-t} or
typing {\sf resume}\index{statement!resume@{\sf resume}}.
%\input{gu10a.tex}
\chapter{Errors}
\section{\CiF\ error messages}
In this chapter is a list of \CiF\ error messages.
\subsection{Syntax error messages}
{\mybasel
\begin{verbatim}
case 1000:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: variable `%s' isn't declared\n",
err_no, line_counter, text);
break;
case 1001:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: variable `%s' was already declared\n",
err_no, line_counter, text);
break;
case 1002:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: remote procedure %s is not declared\n",
err_no, line_counter, proc_name_text[proc]);
break;
case 1003:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: local variable `%s' was already declared\n", err_no, text);
break;
case 1004:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: at the %d-th char, near the \"%s\"\n", err_no, line_counter, char_counter, yytext);
break;
case 1005:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: Invalid type of the operand, %d-th character\n", err_no, line_counter, char_counter);
break;
case 1006:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: Remote function %s already declared\n", err_no, text);
break;
case 1007:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: Remote function isn't declared\n", err_no);
break;
case 1008:
print_source_line ();
fprintfx (stderr, "Error %d: Remote functions are not in the load table\n", err_no);
break;
case 1009:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: Void type in expression at line\n", err_no, line_counter);
break;
case 1010:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: Void type assigned to l_value\n", err_no, line_counter);
break;
case 1011:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: load can't open file `%s'\n", err_no, yytext);
break;
case 1012:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: variable or field `%s' declared void\n", err_no, line_counter, text);
break;
case 1013:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: switch quantity not an integer\n", err_no, line_counter);
break;
case 1014:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: case label does not reduce to an integer constant\n", err_no, line_counter);
break;
case 1015:
print_file_name ();
fprintfx (stderr, "Error %d: line %d: duplicate case value",
err_no, tmp_c->line_number);
print_file_name ();
fprintfx (stderr, "Error %d: line %d: this is the first entry for that value\n", err_no, tmp_m->line_number);
break;
case 1016:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: case label not within a switch statement\n", err_no, line_counter);
break;
case 1017:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: struct tag `%s' was already declared\n",
err_no, line_counter, text);
break;
case 1018:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: union tag `%s' was already declared\n",
err_no, line_counter, text);
break;
case 1019:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: enum tag `%s' was already declared\n",
err_no, line_counter, text);
break;
\end{verbatim}
}
\subsection{\CiF\ compilation error messages}
{\mybasel
\begin{verbatim}
case 2000:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: invalid number of subscripts\n", err_no, line_counter);
break;
case 2001:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: `%s' is not an array variable\n", err_no, line_counter, text);
break;
case 2002:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: invalid type of array subscript\n", err_no, line_counter);
break;
case 2003:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: type of formal parameter does not match previous declaration\n", err_no, line_counter);
break;
case 2004:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: number of formal parameters does not match previous declaration\n", err_no, line_counter);
break;
case 2005:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: name of formal paramter does not match previous declaration\n", err_no, line_counter);
break;
case 2006:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: size of array subscript of formal parameter does not match previous declaration\n", err_no, line_counter);
break;
case 2007:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: number of array subscripts of formal parameter does not match previous declaration\n", err_no, line_counter);
break;
\end{verbatim}
}
\subsection{\CiF\ }
{\mybasel
\begin{verbatim}
case 3000:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: \"break\" outside loop or switch\n", err_no, line_counter);
break;
case 3001:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: bad used continue\n",
err_no, line_counter);
break;
case 3002:
print_source_line ();
print_file_name ();
fprintfx (stderr, "Error %d: line %d: default label not within a switch statement\n", err_no, line_counter);
break;
case 3003:
print_file_name ();
fprintfx (stderr, "Error %d: line %d: multiple default labels in one switch", err_no, line_counter);
print_file_name ();
fprintfx (stderr, "Error %d: line %d: this is the first default label\n",
err_no, fixp->switch1.def_use.line_number);
break;
case 3004:
print_file_name ();
fprintfx (stderr, "Error %d: line %d: duplicate label `%s'\n",
err_no, line_counter, text);
break;
case 3005:
print_file_name ();
fprintfx (stderr, "Error %d: line %d: label `%s' used but not defined\n",
err_no, error_line_number, text);
break;
\end{verbatim}
}
\subsection{\CiF\ run-time error messages}
{\mybasel
\begin{verbatim}
case 4000:
fprintfx (stderr, "Error %d: interpreter: full memory\n", err_no);
break;
case 4001:
fprintfx (stderr, "Error %d: interpreter: stack overflow\n", err_no);
break;
case 4002:
fprintfx (stderr, "Error %d: Operating system out of memory\n", err_no);
break;
\end{verbatim}
}
\subsection{\CiF\ fatal error messages}
{\mybasel
\begin{verbatim}
case 5000:
fprintfx (stderr, "Compile fatal error %d:\nInterpreter Internal Error\n(unknown operand type)\nin line %d\ne-mail: %s\n", err_no, line_counter, EMAIL);
break;
case 5001:
fprintfx (stderr, "Run-time fatal error %d:\nInternal Interpreter Error\n(unknown instruction)\ne-mail: %s\n", err_no, EMAIL);
break;
case 5002:
fprintfx (stderr, "Compile fatal error %d:\nInterpreter Internal Error\n(error in book-keeping)\nin line %d\ne-mail: %s\n", err_no, line_counter, EMAIL);
break;
\end{verbatim}
}
\subsection{\CiF\ warning messages}
{\mybasel
\begin{verbatim}
case 6000:
if (warning_yes)
{
fprintfx (stderr, "Warning %d: line %d: remote function %s already declared\n", err_no, line_counter, text);
}
return;
break;
case 6001:
if (warning_yes)
{
print_file_name ();
fprintfx (stderr, "Warning %d: line %d: `return' with no value, in function returning non-void\n", err_no, line_counter);
}
return;
break;
case 6002:
if (warning_yes)
{
print_file_name ();
fprintfx (stderr, "Warning %d: line %d: `return' with a value, in function returning void\n", err_no, line_counter);
}
return;
break;
case 6003:
if (warning_yes)
{
if (proc)
{
fprintfx (stderr,
"%s: In function `%s':\n%s: Warning %d: unused variable `%s'\n",
argvv[argc_counter], proc_name_text[proc],
argvv[argc_counter], err_no, text);
}
else
{
fprintfx (stderr,
"%s: In block finishing at line %d:\n%s: Warning %d: unused variable `%s'\n",
argvv[argc_counter], line_counter,
argvv[argc_counter], err_no, text);
}
}
return;
break;
case 6004:
if (warning_yes)
{
fprintfx (stderr, "Warning %d: line %d: label `%s' defined but not used\n",
err_no, error_line_number, text);
}
return;
break;
case 6005:
if (warning_yes)
{
fprintfx (stderr, "Warning %d: `/*' within comment\n",
err_no);
}
return;
break;
case 6006:
if (warning_yes)
{
if (proc)
{
fprintfx (stderr,
"%s: In function `%s':\n%s: Warning %d: `%s' might be used uninitialized in this function\n",
argvv[argc_counter], proc_name_text[proc],
argvv[argc_counter], err_no, text);
}
else
{
fprintfx (stderr,
"%s: In block finishing at line %d:\n%s: Warning %d: `%s' might be used uninitialized in the block\n",
argvv[argc_counter], line_counter,
argvv[argc_counter], err_no, text);
}
}
return;
break;
case 6007:
if (warning_yes)
{
print_file_name ();
fprintfx (stderr,
"In function `%s':\n%s: Warning %d: number of locals is greater than the ANSI allows\n",
proc_name_text[proc], argvv[argc_counter],
err_no);
}
return;
break;
case 6008:
if (warning_yes)
{
print_file_name ();
fprintfx (stderr,
"In function `%s':\n%s: Warning %d: number of params is greater than the ANSI allows\n",
proc_name_text[proc], argvv[argc_counter],
err_no);
}
return;
break;
\end{verbatim}
}
\subsection{\CiF\ initialization error messages}
{\mybasel
\begin{verbatim}
case 7000:
fprintfx (stderr, "Error %d: in run-string and/or in `clif.ini' file\n", err_no);
break;
case 7001:
fprintfx (stderr, "Error %d: interpreter: can't open file %s\n\n", err_no, argvv[argc_counter]);
break;
default:
fprintfx (stderr, "Fatal error invalid error number (%d)\ne-mail: %s\n", err_no, EMAIL);
break;
\end{verbatim}
}
\bibliographystyle{plain}
\bibliography{knia}
\printindex
\end{document}
|