ref.tex 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1997, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. \documentclass{report}
  22. %
  23. % Preamble
  24. %
  25. \usepackage{a4}
  26. \usepackage{makeidx}
  27. \usepackage{multicol}
  28. \usepackage{html}
  29. \usepackage{syntax}
  30. %
  31. % syntax style
  32. %
  33. \input{syntax/diagram.tex}
  34. \latex{\usepackage{fpc}}
  35. \latex{\usepackage{listings}\blankstringtrue%
  36. \selectlisting{fpc}\stringstyle{\ttfamily}\keywordstyle{\bfseries}
  37. \prelisting{\sffamily}}
  38. \html{\input{fpc-html.tex}}
  39. \usepackage{fancyheadings}
  40. \pagestyle{fancy}
  41. \renewcommand{\chaptermark}[1]{\markboth{#1}{}}
  42. \makeindex
  43. %
  44. % start of document.
  45. %
  46. \begin{document}
  47. \title{Free Pascal :\\ Reference guide.}
  48. \docdescription{Reference guide for Free Pascal.}
  49. \docversion{1.4}
  50. \date{March 1998}
  51. \author{Micha\"el Van Canneyt
  52. % \\ Florian Kl\"ampfl
  53. }
  54. \maketitle
  55. \tableofcontents
  56. \newpage
  57. \listoftables
  58. \newpage
  59. \section*{About this guide}
  60. This document describes all constants, types, variables, functions and
  61. procedures as they are declared in the system unit.
  62. Furthermore, it describes all pascal constructs supported by \fpc, and lists
  63. all supported data types. It does not, however, give a detailed explanation
  64. of the pascal language. The aim is to list which Pascal constructs are
  65. supported, and to show where the \fpc implementation differs from the
  66. Turbo Pascal implementation.
  67. Throughout this document, we will refer to functions, types and variables
  68. with \var{typewriter} font. Functions and procedures gave their own
  69. subsections, and for each function or procedure we have the following
  70. topics:
  71. \begin{description}
  72. \item [Declaration] The exact declaration of the function.
  73. \item [Description] What does the procedure exactly do ?
  74. \item [Errors] What errors can occur.
  75. \item [See Also] Cross references to other related functions/commands.
  76. \end{description}
  77. The cross-references come in two flavours:
  78. \begin{itemize}
  79. \item References to other functions in this manual. In the printed copy, a
  80. number will appear after this reference. It refers to the page where this
  81. function is explained. In the on-line help pages, this is a hyperlink, on
  82. which you can click to jump to the declaration.
  83. \item References to Unix manual pages. (For linux related things only) they
  84. are printed in \var{typewriter} font, and the number after it is the Unix
  85. manual section.
  86. \end{itemize}
  87. %
  88. % The Pascal language
  89. %
  90. \chapter{Pascal Tokens}
  91. In this chapter we describe all the pascal reserved words, as well as teh
  92. various ways to denote strings, numbers identifiers etc.
  93. \section{symbols}
  94. Free Pascal allows all characters, digits and some special ASCII symbols
  95. in a Pascal source file.
  96. \input{syntax/symbol.syn}
  97. The following characters have a special meaning:
  98. \begin{verbatim}
  99. + - * / = < > [ ] . , ( ) : ^ @ { } $ #
  100. \end{verbatim}
  101. and the following character pairs too:
  102. \begin{verbatim}
  103. <= >= := (* *) (. .) //
  104. \end{verbatim}
  105. When used in a range specifier, the character pair \var{(.} is equivalent to
  106. the left square bracket \var{[}. Likewise, the character pair \var{.)} is
  107. equivalent to the right square bracket \var{]}.
  108. When used for comment delimiters, the character pair \var{(*} is equivalent
  109. to the left brace \var{\{} and the character pair \var{*)} is equivalent
  110. to the right brace \var{\}}.
  111. These character pairs retain their normal meaning in string expressions.
  112. \section{Comments}
  113. \fpc supports the use of nested comments. The following constructs are valid
  114. comments:
  115. \begin{verbatim}
  116. (* This is an old style comment *)
  117. { This is a Trubo Pascal comment }
  118. // This is a Delphi comment. All is ignored till the end of the line.
  119. \end{verbatim}
  120. The last line would cause problems when attempting to compile with Delphi or
  121. Turbo Pascal. These compiler would consider the first matching brace
  122. \var{\}} as the end of the comment delimiter. If you wish to have this
  123. behaviour, you can use the \var{-So} switch, and the \fpc compiler will
  124. act the same way.
  125. The following are valid ways of nesting comments:
  126. \begin{verbatim}
  127. { Comment 1 (* comment 2 *) }
  128. (* Comment 1 { comment 2 } *)
  129. { comment 1 // Comment 2 }
  130. (* comment 1 // Comment 2 *)
  131. // comment 1 (* comment 2 *)
  132. // comment 1 { comment 2 }
  133. \end{verbatim}
  134. The last two comments {\em must} be on one line. The following two will give
  135. errors:
  136. \begin{verbatim}
  137. // Valid comment { No longer valid comment !!
  138. }
  139. \end{verbatim}
  140. and
  141. \begin{verbatim}
  142. // Valid comment (* No longer valid comment !!
  143. *)
  144. \end{verbatim}
  145. The compiler will react with a 'invalid character' error when it encounters
  146. such constructs, regardless of the \var{-So} switch.
  147. \section{Reserved words}
  148. Reserved words are part of the Pascal language, and cannot be redefined.
  149. They will be denoted as \lit*{this} throughout the syntax diagrams.
  150. Reserved words can be typed regardless of case, i.e. Pascal is case
  151. insensitive.
  152. We make a distinction between Turbo Pascal and Delphi reserved words, since
  153. with the \var{-So} switch, only the Turbo Pascal reserved words are
  154. recognised, and the Delphi ones can be redefined. By default, \fpc
  155. recognises the Delphi reserved words.
  156. \subsection{Turbo Pascal reserved words}
  157. The following keywords exist in Turbo Pascal mode
  158. \latex{\begin{multicols}{4}}
  159. \begin{verbatim}
  160. absolute
  161. and
  162. array
  163. asm
  164. begin
  165. break
  166. case
  167. const
  168. constructor
  169. continue
  170. destructor
  171. dispose
  172. div
  173. do
  174. downto
  175. else
  176. end
  177. exit
  178. false
  179. file
  180. for
  181. function
  182. goto
  183. if
  184. implementation
  185. in
  186. inherited
  187. inline
  188. interface
  189. label
  190. mod
  191. new
  192. nil
  193. not
  194. object
  195. of
  196. on
  197. operator
  198. or
  199. packed
  200. procedure
  201. program
  202. record
  203. repeat
  204. self
  205. set
  206. shl
  207. shr
  208. string
  209. then
  210. to
  211. true
  212. try
  213. type
  214. unit
  215. until
  216. uses
  217. var
  218. while
  219. with
  220. xor
  221. \end{verbatim}
  222. \latex{\end{multicols}}
  223. \subsection{Delphi reserved words}
  224. The Delphi (II) reserved words are the same as the pascal ones, plus the
  225. following ones:
  226. \latex{\begin{multicols}{4}}
  227. \begin{verbatim}
  228. as
  229. class
  230. except
  231. exports
  232. finalization
  233. finally
  234. initialization
  235. is
  236. library
  237. on
  238. property
  239. raise
  240. try
  241. \end{verbatim}
  242. \latex{\end{multicols}}
  243. \subsection{\fpc reserved words}
  244. On top of the Turbo Pascal and Delphi reserved words, \fpc also considers
  245. the following as reserved words:
  246. \latex{\begin{multicols}{4}}
  247. \begin{verbatim}
  248. dispose
  249. exit
  250. export
  251. false
  252. new
  253. popstack
  254. true
  255. \end{verbatim}
  256. \latex{\end{multicols}}
  257. \subsection{Modifiers}
  258. The following is a list of all modifiers. Contrary to Delphi, \fpc doesn't
  259. allow you to redefine these modifiers.
  260. \latex{\begin{multicols}{4}}
  261. \begin{verbatim}
  262. absolute
  263. abstract
  264. alias
  265. assembler
  266. cdecl
  267. default
  268. export
  269. external
  270. far
  271. forward
  272. index
  273. name
  274. near
  275. override
  276. pascal
  277. popstack
  278. private
  279. protected
  280. public
  281. published
  282. read
  283. register
  284. stdcall
  285. virtual
  286. write
  287. \end{verbatim}
  288. \latex{\end{multicols}}
  289. Remark that predefined types such as \var{Byte}, \var{Boolean} and constants
  290. such as \var{maxint} are {\em not} reserved words. They are
  291. identifiers, declared in the system unit. This means that you can redefine
  292. these types. You are, however, not encouraged to do this, as it will cause
  293. a lot of confusion.
  294. \section{Identifiers}
  295. Identifiers denote constants, types, variables, procedures and functions,
  296. units, and programs. All things that you decide to give a name is an
  297. identifier.
  298. An identifier consists of 255 significant characters (letters, digits and
  299. the underscore character), from which the first must be an alphanumeric
  300. character, or an underscore (\var{\_})
  301. \seesy{identifiers} gives the basic syntax for identifiers.
  302. \input{syntax/identifier.syn}
  303. \section{Numbers}
  304. Numbers are denoted in decimal notation. Real (or decimal) numbers are
  305. written using engeneering notation (e.g. \var{0.314E1}).
  306. \fpc supports hexadecimal format the same way as Turbo Pascal does. To
  307. specify a constant value in hexadecimal format, prepend it with a dollar
  308. sign (\var{\$}). Thus, the hexadecimal \var{\$FF} equals 255 decimal.
  309. In addition to the support for hexadecimal notation, \fpc also supports
  310. binary notation. You can specify a binary number by preceding it with a
  311. percent sign (\var{\%}). Thus, \var{255} can be specified in binary notation
  312. as \var{\%11111111}.
  313. The following diagrams show the syntax for numbers.
  314. \input{syntax/numbers.syn}
  315. \section{Labels}
  316. Labels can be digit sequences or identifiers.
  317. \input{syntax/label.syn}
  318. \section{Character strings}
  319. A character string (or string for short) is a sequence of zero or more
  320. characters from the ASCII character set, enclosed by single quotes, and on 1
  321. line of the program source.
  322. A character set with nothing between the quotes (\var{'{}'}) is an empty
  323. string.
  324. \input{syntax/string.syn}
  325. \chapter{Constants}
  326. Just as in Turbo Pascal, \fpc supports both normal and typed constants.
  327. \section{Ordinary constants}
  328. Ordinary constants declarations are no different from the Turbo Pascal or
  329. Delphi implementation.
  330. \input{syntax/const.syn}
  331. Thc ompiler must be able to evaluaye the expression in a constant
  332. declaration at compile time. This means that most of the functions
  333. in the Run-Time library cannot be used in a constant declaration.
  334. Operators such as \var{+, -, *, /, not, and, or, div(), mod(), ord(), chr(),
  335. sizeof} can be used, however.
  336. You can only declare constants of the following types: \var{Ordinal types},
  337. \var{Real types}, \var{Char}, and \var{String}.
  338. The following are all valid constant declarations:
  339. \begin{listing}
  340. Const
  341. e = 2.7182818; { Real type constant. }
  342. a = 2; { Integer type constant. }
  343. c = '4'; { Character type constant. }
  344. s = 'This is a constant string'; {String type constant.}
  345. s = chr(32)
  346. ls = SizeOf(Longint);
  347. \end{listing}
  348. Assigning a value to a constant is not permitted. Thus, given the previous
  349. declaration, the following will result in a compiler error:
  350. \begin{listing}
  351. s:='some other string';
  352. \end{listing}
  353. \section{Typed constants}
  354. Typed constants serve to provide a program with initialized variables.
  355. Contrary to ordinary constants, they may be assigned to at run-time.
  356. The difference with normal variables is that their value is initialised
  357. when the program starts, whereas normal variables must be initialised
  358. explicitly.
  359. \input{syntax/tconst.syn}
  360. Given the declaration:
  361. \begin{listing}
  362. Const
  363. S : String = 'This is a typed constant string';
  364. \end{listing}
  365. The following is a valid assignment:
  366. \begin{listing}
  367. S:='Result : '+Func;
  368. \end{listing}
  369. Where \var{Func} is a function that returns a \var{String}.
  370. Typed constants also allow you to initialize arrays and records. For arrays,
  371. the initial elements must be specified, surrounded by round brackets, and
  372. separated by commas. The number of elements must be exactly the same as
  373. number of elements in the declaration of the type.
  374. As an example:
  375. \begin{listing}
  376. Const
  377. tt : array [1..3] of string[20] = ('ikke','gij', 'hij');
  378. ti : array [1..3] of Longint = (1,2,3);
  379. \end{listing}
  380. For constant records, you should specify each element of the record, in the
  381. form \var{Field : Value}, separated by commas, and surrounded by round
  382. brackets.
  383. As an example:
  384. \begin{listing}
  385. Type
  386. Point = record
  387. X,Y : Real
  388. end;
  389. Const
  390. Origin : Point = (X:0.0 , Y:0.0);
  391. \end{listing}
  392. The order of the fields in a constant record needs to be the same as in the type declaration,
  393. otherwise you'll get a compile-time error.
  394. \chapter{Types}
  395. All variables have a type. \fpc supports the same basic types as Turbo
  396. Pascal, with some extra types from Delphi.
  397. You can declare your own types, which is in essence defining an identifier
  398. that can be used to denote your custom type when declaring variables further
  399. in the source code.
  400. \input{syntax/typedecl.syn}
  401. There are 7 major type classes :
  402. \input{syntax/type.syn}
  403. The last class, \synt{type identifier}, is just a means to give another name to a
  404. type.
  405. \section{Base types}
  406. The base or simple types of \fpc are the Delphi types.
  407. We will discuss each separate.
  408. \input{syntax/typesim.syn}
  409. \subsection{Ordinal types}
  410. With the exception of Real types, all base types are ordinal types.
  411. Ordinal types have the following characteristics:
  412. \begin{enumerate}
  413. \item ordinal types are countable and ordered, i.e. it is, in principle,
  414. possible to start counting them one bye one, in a specified order.
  415. This property allows the operation of functions as \seep{Inc}, seef{Ord}
  416. \seep{Dec}
  417. on ordinal types to be defined.
  418. \item Ordinal values have a smallest possible value. Trying to apply the
  419. \seef{Pred} function on the smallest possible value will generate a range
  420. check error.
  421. \item Ordinal values have a largest possible value. Trying to apply the
  422. \seef{Succ} function on the larglest possible value will generate a range
  423. check error.
  424. \end{enumerate}
  425. \subsubsection{Integers}
  426. A list of pre-defined ordinal types is presented in \seet{ordinals}
  427. \begin{FPCltable}{l}{Predefined ordinal types}{ordinals}
  428. Name\\ \hline
  429. Integer \\
  430. Shortint \\
  431. SmallInt \\
  432. Longint \\
  433. Byte \\
  434. Word \\
  435. Cardinal \\
  436. Boolean \\
  437. ByteBool \\
  438. LongBool \\
  439. Char
  440. \end{FPCltable}
  441. The integer types, and their ranges and sizes, that are predefined in
  442. \fpc are listed in \seet{integers}.
  443. \begin{FPCltable}{lcr}{Predefined integer types}{integers}
  444. Type & Range & Size in bytes \\ \hline
  445. Byte & 0 .. 255 & 1 \\
  446. Shortint & -127 .. 127 & 1\\
  447. Integer & -32768 .. 32767 & 2 \\
  448. Word & 0 .. 65535 & 2 \\
  449. Longint & -2147483648 .. 2147483648 & 4\\
  450. Cardinal\footnote{The cardinal type support is buggy until version 0.99.6} & 0..4294967296 & 4 \\ \hline
  451. \end{FPCltable}
  452. \fpc does automatic type conversion in expressions where different kinds of
  453. integer types are used.
  454. \subsubsection{Boolean types}
  455. \fpc supports the \var{Boolean} type, with its two pre-defined possible
  456. values \var{True} and \var{False}, as well as the \var{ByteBool},
  457. \var{WordBool} and \var{LongBool}. These are the only two values that can be
  458. assigned to a \var{Boolean} type. Of course, any expression that resolves
  459. to a \var{boolean} value, can also be assigned to a boolean type.
  460. \begin{FPCltable}{lll}{Boolean types}{booleans}
  461. Name & Size & Ord(True) \\ hline
  462. Boolean & 1 & 1 \\
  463. ByteBool & 1 & Any nonzero value \\
  464. WordBool & 2 & Any nonzero value \\
  465. LongBool & 4 & Any nonzero value
  466. \end{FPCltable}
  467. Assuming \var{B} to be of type \var{Boolean}, the following are valid
  468. assignments:
  469. \begin{listing}
  470. B:=True;
  471. B:=False;
  472. B:=1<>2; { Results in B:=True }
  473. \end{listing}
  474. Boolean expressions are also used in conditions.
  475. {\em Remark:} In \fpc, boolean expressions are always evaluated in such a
  476. way that when the result is known, the rest of the expression will no longer
  477. be evaluated (Called short-cut evaluation). In the following example, the function \var{Func} will never
  478. be called, which may have strange side-effects.
  479. \begin{listing}
  480. ...
  481. B:=False;
  482. A := B and Func;
  483. \end{listing}
  484. Here \var{Func} is a function which returns a \var{Boolean} type.
  485. {\em Remark:} The wordbool, longbool and bytebool were not supported
  486. by \fpc until version 0.99.6.
  487. \subsubsection{Enumeration types}
  488. Enumeration types are supported in \fpc. On top of the Turbo Pascal
  489. implementation, \fpc allows the following C-style extension of the
  490. enumeration type, where a value is assigned to a particular element of
  491. the enumeration list.
  492. \input{syntax/typeenum.syn}
  493. So the following is a correct enumerated type declaration:
  494. \begin{listing}
  495. Type
  496. Direction = ( North, East, South, West );
  497. \end{listing}
  498. The C style enumeration type looks as follows:
  499. \begin{listing}
  500. Type
  501. EnumType = (one, two, three, forty := 40);
  502. \end{listing}
  503. As a result, the ordinal number of \var{forty} is \var{40}, and not \var{3},
  504. as it would be when the \var{'= 40'} wasn't present.
  505. When specifying such an enumeration type, it is important to keep in mind
  506. that you should keep initialized set elements in ascending order. The
  507. following will produce a compiler error:
  508. \renewcommand{\prelisting}{\sffamily}
  509. \begin{listing}
  510. Type
  511. EnumType = (one, two, three, forty := 40, thirty:=30);
  512. \end{listing}
  513. It is necessary to keep \var{forty} and \var{Thirty} in the correct order.
  514. {\em Remarks :}
  515. \begin{enumerate}
  516. \item You cannot use the \var{Pred} and \var{Succ} functions on
  517. this kind of enumeration types. If you try to do that, you'll get a compiler
  518. error.
  519. \item Enumeration types are by default stored in 4 bytes. You can change
  520. this behaviour with the \var{\{\$PACKENUM \}} compiler directive, which
  521. tells the compiler the minimal number of bytes to be used for enumeration
  522. types.
  523. For instance
  524. \begin{listing}
  525. Type
  526. LargeEnum = ( BigOne, BigTwo, BigThree );
  527. {\$PACKENUM 1}
  528. SmallEnum = ( one, two, three );
  529. Var S : SmallEnum;
  530. L : LargeEnum;
  531. begin
  532. Writeln ('Small enum : ',Sizeof(S));
  533. Writeln ('Large enum : ',SizeOf(L));
  534. end.
  535. \end{listing}
  536. will, when run, print the following:
  537. \begin{listing}
  538. Small enum : 1
  539. Large enum : 4
  540. \end{listing}
  541. \end{enumerate}
  542. More information can be found in the \progref, in the compiler directives
  543. section.
  544. \subsubsection{Subrange types}
  545. A subrange type is a range of values from an ordinal type (the {\em host}
  546. type). To define a subrange type, one must specify it's limiting values: the
  547. highest and lowest value of the type.
  548. \input{syntax/typesubr.syn}
  549. Some of the predefined \var{integer} types are defined as subrange types:
  550. \begin{listing}
  551. Type
  552. Longint = $80000000..$7fffffff;
  553. Integer = -32768..32767;
  554. shortint = -128..127;
  555. byte = 0..255;
  556. Word = 0..65535;
  557. \end{listing}
  558. But you can also define subrange types of enumeration types:
  559. \begin{listing}
  560. Type
  561. Days = (monday,tuesday,wednesday, thursday,friday,
  562. saturday,sunday);
  563. WorkDays = monday .. friday;
  564. WeekEnd = Saturday .. Sunday;
  565. \end{listing}
  566. \subsection{Real types}
  567. \fpc uses the math coprocessor (or an emulation) for all its floating-point
  568. calculations. The Real native type is processor dependant,
  569. but it is either Single or Double. Only the IEEE floating point types are
  570. supported, and these depend on the target processor and emulation options.
  571. The true Turbo Pascal compatible types are listed in
  572. \seet{Reals}.
  573. \begin{FPCltable}{lccr}{Supported Real types}{Reals}
  574. Type & Range & Significant digits & Size\footnote{In Turbo Pascal.} \\ \hline
  575. Single & 1.5E-45 .. 3.4E38 & 7-8 & 4 \\
  576. Real & 5.0E-324 .. 1.7E308 & 15-16 & 8 \\
  577. Double & 5.0E-324 .. 1.7E308 & 15-16 & 8 \\
  578. Extended & 1.9E-4951 .. 1.1E4932 & 19-20 & 10\\
  579. Comp\footnote{\var{Comp} only holds integer values.} & -2E64+1 .. 2E63-1 & 19-20 & 8 \\
  580. \end{FPCltable}
  581. Until version 0.9.1 of the compiler, all the Real types are mapped to type
  582. \var{Double}, meaning that they all have size 8. The \seef{SizeOf} function
  583. is your friend here. The \var{Real} type of turbo pascal is automatically
  584. mapped to Double. The \var{Comp} type is, in effect, a 64-bit integer.
  585. \section{Character types}
  586. \subsection{Char}
  587. \fpc supports the type \var{Char}. A \var{Char} is exactly 1 byte in
  588. size, and contains one character.
  589. You can specify a character constant by enclosing the character in single
  590. quotes, as follows : 'a' or 'A' are both character constants.
  591. You can also specify a character by their ASCII
  592. value, by preceding the ASCII value with the number symbol (\#). For example
  593. specifying \var{\#65} would be the same as \var{'A'}.
  594. Also, the caret character (\verb+^+) can be used in combination with a letter to
  595. specify a character with ASCII value less than 27. Thus \verb+^G+ equals
  596. \var{\#7} (G is the seventh letter in the alphabet.)
  597. If you want to represent the single quote character, type it two times
  598. successively, thus \var{''''} represents the single quote character.
  599. \subsection{Short Strings}
  600. \fpc supports the \var{String} type as it is defined in Turbo Pascal.
  601. To declare a variable as a string, use the following type specification:
  602. \input{syntax/sstring.syn}
  603. The predefined type{ShortString} is defined as a string of length 255.
  604. \fpc reserves \var{Size+1} bytes for the string \var{S}, and in the zeroeth
  605. element of the string (\var{S[0]}) it will store the length of the variable.
  606. If you don't specify the size of the string, \var{255} is taken as a
  607. default.
  608. For example in
  609. \begin{listing}
  610. Type
  611. NameString = String[10];
  612. StreetString = String;
  613. \end{listing}
  614. \var{NameString} can contain maximum 10 characters. While
  615. \var{StreetString} can contain 255 characters. The sizes of these variables
  616. are, respectively, 11 and 256 bytes.
  617. To specify a constant string, you enclose the string in single-quotes, just
  618. as a \var{Char} type, only now you can have more than one character.
  619. Given that \var{S} is of type \var{String}, the following are valid assignments:
  620. \begin{listing}
  621. S:='This is a string.';
  622. S:='One'+', Two'+', Three';
  623. S:='This isn''t difficult !';
  624. S:='This is a weird character : '#145' !';
  625. \end{listing}
  626. As you can see, the single quote character is represented by 2 single-quote
  627. characters next to each other. Strange characters can be specified by their
  628. ASCII value.
  629. The example shows also that you can add two strings. The resulting string is
  630. just the concatenation of the first with the second string, without spaces in
  631. between them. Strings can not be substracted, however.
  632. \subsection{PChar}
  633. \fpc supports the Delphi implementation of the \var{PChar} type. \var{PChar}
  634. is defined as a pointer to a \var{Char} type, but allows additional
  635. operations.
  636. The \var{PChar} type can be understood best as the Pascal equivalent of a
  637. C-style null-terminated string, i.e. a variable of type \var{PChar} is a
  638. pointer that points to an array of type \var{Char}, which is ended by a
  639. null-character (\var{\#0}).
  640. \fpc supports initializing of \var{PChar} typed constants, or a direct
  641. assignment. For example, the following pieces of code are equivalent:
  642. \begin{listing}
  643. program one;
  644. var p : PChar;
  645. begin
  646. P:='This is a null-terminated string.';
  647. writeln (P);
  648. end.
  649. \end{listing}
  650. Results in the same as
  651. \begin{listing}
  652. program two;
  653. const P : PChar = 'This is a null-terminated string.'
  654. begin
  655. Writeln (P);
  656. end.
  657. \end{listing}
  658. These examples also show that it is possible to write {\em the contents} of
  659. the string to a file of type \var{Text}.
  660. The \seestrings\_ unit contains procedures and functions that manipulate the
  661. \var{PChar} type as you can do it in C.
  662. Since it is equivalent to a pointer to a type \var{Char} variable, it is
  663. also possible to do the following:
  664. \begin{listing}
  665. Program three;
  666. Var S : String[30];
  667. P : PChar;
  668. begin
  669. S:='This is a null-terminated string.'#0;
  670. P:=@S[1];
  671. writeln (P);
  672. end.
  673. \end{listing}
  674. This will have the same result as the previous two examples.
  675. You cannot add null-terminated strings as you can do with normal Pascal
  676. strings. If you want to concatenate two \var{PChar} strings, you will need
  677. to use the \seestrings unit.
  678. However, it is possible to do some pointer arithmetic. You can use the
  679. operators \var{+} and \var{-} to do operations on \var{PChar} pointers.
  680. In \seet{PCharMath}, \var{P} and \var{Q} are of type \var{PChar}, and
  681. \var{I} is of type \var{Longint}.
  682. \begin{FPCltable}{lr}{\var{PChar} pointer arithmetic}{PCharMath}
  683. Operation & Result \\ \hline
  684. \var{P + I} & Adds \var{I} to the address pointed to by \var{P}. \\
  685. \var{I + P} & Adds \var{I} to the address pointed to by \var{P}. \\
  686. \var{P - I} & Substracts \var{I} from the address pointed to by \var{P}. \\
  687. \var{P - Q} & Returns, as an integer, the distance between 2 addresses \\
  688. & (or the number of characters between \var{P} and \var{Q}) \\
  689. \hline
  690. \end{FPCltable}
  691. \section{Structured Types}
  692. A structured type is a type that can hold multiple values in one variable.
  693. Stuctured types can be nested to unlimited levels.
  694. \input{syntax/typestru.syn}
  695. Unlike Delphi, \fpc does not support the keyword \var{Packed} for all
  696. structured types, as can be seen in the syntax diagram. It will be mentioned
  697. when a type supports the \var{packed} keyword.
  698. In the following, each of the possible structured types is discussed.
  699. \subsection{Arrays}
  700. \fpc supports arrays as in Turbo Pascal, multi-dimensional arrays
  701. and packed arrays are also supported:
  702. \input{syntax/typearr.syn}
  703. The following is a valid array declaration:
  704. \begin{listing}
  705. Type
  706. RealArray = Array [1..100] of Real;
  707. \end{listing}
  708. As in Turbo Pascal, if the array component type is in itself an array, it is
  709. possible to combine the two arrays into one multi-dimensional array. The
  710. following declaration:
  711. \begin{listing}
  712. Type
  713. APoints = array[1..100] of Array[1..3] of Real;
  714. \end{listing}
  715. is equivalent to the following declaration:
  716. \begin{listing}
  717. Type
  718. APoints = array[1..100,1..3] of Real;
  719. \end{listing}
  720. The functions \seef{High} and \seef{Low} return the high and low bounds of
  721. the leftmost index type of the array. In the above case, this would be 100
  722. and 1.
  723. \subsection{Record types}
  724. \fpc supports fixed records and records with variant parts.
  725. The syntax diagram for a record type is
  726. \input{syntax/typerec.syn}
  727. So the following are valid record types declarations:
  728. \begin{listing}
  729. Type
  730. Point = Record
  731. X,Y,Z : Real;
  732. end;
  733. RPoint = Record
  734. Case Boolean of
  735. False : (X,Y,Z : Real);
  736. True : (R,theta,phi : Real);
  737. end;
  738. BetterRPoint = Record
  739. Case UsePolar : Boolean of
  740. False : (X,Y,Z : Real);
  741. True : (R,theta,phi : Real);
  742. end;
  743. \end{listing}
  744. The variant part must be last in the record. The optional identifier in the
  745. case statement serves to access the tag field value, which otherwise would
  746. be invisible to the programmer. It can be used to see which variant is
  747. active at a certain time. In effect, it introduces a new field in the
  748. record.
  749. Remark that it is possible to nest variant parts, as in:
  750. \begin{listing}
  751. Type
  752. MyRec = Record
  753. X : Longint;
  754. Case byte of
  755. 2 : (Y : Longint;
  756. case byte of
  757. 3 : (Z : Longint);
  758. );
  759. end;
  760. \end{listing}
  761. The size of a record is the sum of the sizes of its fields, each size of a
  762. field is rounded up to two. If the record contains a variant part, the size
  763. of the variant part is the size of the biggest variant, plus the size of the
  764. tag field type {\em if an identifier was declared for it}. Here also, the size of
  765. each part is first rounded up to two. So in the above example,
  766. \seef{SizeOf} would return 24 for \var{Point}, 24 for \var{RPoint} and
  767. 26 for \var{BetterRPoint}. For \var{MyRec}, the value would be 12.
  768. If you want to read a typed file with records, produced by
  769. a Turbo Pascal program, then chances are that you will not succeed in
  770. reading that file correctly.
  771. The reason for this is that by default, elements of a record are aligned at
  772. 2-byte boundaries, for performance reasons. This default behaviour can be
  773. changed with the \var{\{\$PackRecords n\}} switch. Possible values for
  774. \var{n} are 1, 2 and 4. This switch tells the compiler to align elements of
  775. a record or object or class on 1,2 or 4 byte boundaries.
  776. Take a look at the following program:
  777. \begin{listing}
  778. Program PackRecordsDemo;
  779. type {$PackRecords 2}
  780. Trec1 = Record
  781. A : byte;
  782. B : Word;
  783. end;
  784. {$PACKRECORDS 1}
  785. Trec2 = Record
  786. A : Byte;
  787. B : Word;
  788. end;
  789. begin
  790. Writeln ('Size Trec1 : ',SizeOf(Trec1));
  791. Writeln ('Size Trec2 : ',SizeOf(Trec2));
  792. end.
  793. \end{listing}
  794. The output of this program will be :
  795. \begin{listing}
  796. Size Trec1 : 4
  797. Size Trec2 : 3
  798. \end{listing}
  799. And this is as expected. In \var{Trec1}, each of the elements \var{A} and
  800. \var{B} takes 2 bytes of memory, and in \var{Trec1}, \var{A} takes only 1
  801. byte of memory.
  802. As from version 0.9.3, \fpc supports also the 'packed record', this is a
  803. record where all the elements are byte-aligned.
  804. Thus the two following declarations are equivalent:
  805. \begin{listing}
  806. {$PACKRECORDS 1}
  807. Trec2 = Record
  808. A : Byte;
  809. B : Word;
  810. end;
  811. {$PACKRECORDS 2}
  812. \end{listing}
  813. and
  814. \begin{listing}
  815. Trec2 = Packed Record
  816. A : Byte;
  817. B : Word;
  818. end;
  819. \end{listing}
  820. Note the \var{\{\$PACKRECORDS 2\}} after the first declaration !
  821. \subsection{Set types}
  822. \fpc supports the set types as in Turbo Pascal. The prototype of a set
  823. declaration is:
  824. \input{syntax/typeset.syn}
  825. Each of the elements of \var{SetType} must be of type \var{TargetType}.
  826. \var{TargetType} can be any ordinal type with a range between \var{0} and
  827. \var{255}. A set can contain maximally \var{255} elements.
  828. The following are valid set declaration:
  829. \begin{listing}
  830. Type
  831. Junk = Set of Char;
  832. Days = (Mon, Tue, Wed, Thu, Fri, Sqt, Sun);
  833. WorkDays : Set of days;
  834. \end{listing}
  835. Given this set declarations, the following assignment is legal:
  836. \begin{listing}
  837. WorkDays := [ Mon, Tue, Wed, Thu, Fri];
  838. \end{listing}
  839. The operators and functions for manipulations of sets are listed in
  840. \seet{SetOps}.
  841. \begin{FPCltable}{lr}{Set Manipulation operators}{SetOps}
  842. Operation & Operator \\ \hline
  843. Union & + \\
  844. Difference & - \\
  845. Intersection & * \\
  846. Add element & var{include} \\
  847. Delete element & \var{exclude} \\ \hline
  848. \end{FPCltable}
  849. You can compare two sets with the \var{<>} and \var{=} operators, but not
  850. (yet) with the \var{<} and \var{>} operators.
  851. As of compiler version 0.9.5, the compiler stores small sets (less than 32
  852. elements) in a Longint, if the type range allows it. This allows for faster
  853. processing and decreases program size. Otherwise, sets are stored in 32
  854. bytes.
  855. \subsection{File types}
  856. File types are types that store a sequence of some base type, which can be
  857. any type except another file type. It can contain (in principle) an infinite
  858. number of elements.
  859. File types are used commonly to store data on disk. Nothing stops you,
  860. however, from writing a file driver that stores it's data in memory.
  861. Here is the type declaration for a file type:
  862. \input{syntax/typefil.syn}
  863. If no type identifier is given, then the file is an untyped file; it can be
  864. considered as equivalent to a file of bytes. Untyped files require special
  865. commands to act on them (see \seep{BlockRead}, \seep{BlockWrite}).
  866. The following declaration declares a file of records:
  867. \begin{listing}
  868. Type
  869. Point = Record
  870. X,Y,Z : real;
  871. end;
  872. PointFile = File of Point;
  873. \end{listing}
  874. Internally, files are represented by the \var{FileRec} record.
  875. see \seec{refchapter} for it's declaration.
  876. A special file type is the \var{Text} file type, represented by the
  877. \var{TextRec} record. A file of type \var{Text} uses special input-output
  878. routines.
  879. \section{Pointers}
  880. \fpc supports the use of pointers. A variable of the pointer type
  881. contains an address in memory, where the data of another variable may be
  882. stored.
  883. \input{syntax/typepoin.syn}
  884. As can be seen from this diagram, pointers are typed, which means that
  885. they point to a particular kind of data. The type of this data must be
  886. known at compile time.
  887. Dereferencing the pointer (denoted by adding \var{\^{}} after the variable
  888. name) behaves then like a variable. This variable has the type declared in
  889. the pointer declaration, and the variable is stored in the address that is
  890. pointed to by the pointer variable.
  891. Consider the following example:
  892. \begin{listing}
  893. Program pointers;
  894. type
  895. Buffer = String[255];
  896. BufPtr = ^Buffer;
  897. Var B : Buffer;
  898. BP : BufPtr;
  899. PP : Pointer;
  900. etc..
  901. \end{listing}
  902. In this example, \var{BP} {\em is a pointer to} a \var{Buffer} type; while \var{B}
  903. {\em is} a variable of type \var{Buffer}. \var{B} takes 256 bytes memory,
  904. and \var{BP} only takes 4 bytes of memory (enough to keep an adress in
  905. memory).
  906. {\em Remark:} \fpc treats pointers much the same way as C does. This means
  907. that you can treat a pointer to some type as being an array of this type.
  908. The pointer then points to the zeroeth element of this array. Thus the
  909. following pointer declaration
  910. \begin{listing}
  911. Var p : ^Longint;
  912. \end{listing}
  913. Can be considered equivalent to the following array declaration:
  914. \begin{listing}
  915. Var p : array[0..Infinity] of Longint;
  916. \end{listing}
  917. The reference \verb+P^+ is then the same as \var{p[0]}. The following program
  918. illustrates this maybe more clear:
  919. \begin{listing}
  920. program PointerArray;
  921. var i : Longint;
  922. p : ^Longint;
  923. pp : array[0..100] of Longint;
  924. begin
  925. for i:=0 to 100 do pp[i]:=i; { Fill array }
  926. p:=@pp[0]; { Let p point to pp }
  927. for i:=0 to 100 do if p[i]<>pp[i] then writeln ('Ohoh, problem !')
  928. end.
  929. \end{listing}
  930. \fpc supports pointer arithmetic as C does. This means that, if \var{P} is a
  931. typed pointer, the instructions
  932. \begin{listing}
  933. Inc(P);
  934. Dec(P);
  935. \end{listing}
  936. Will increase, respecively descrease the address the pointer points to
  937. with the size of the type \var{P} is a pointer to. For example
  938. \begin{listing}
  939. Var P : ^Longint;
  940. ...
  941. Inc (p);
  942. \end{listing}
  943. will increase \var{P} with 4.
  944. \section{Procedural types}
  945. \fpc has support for procedural types, although it differs a little from
  946. the Turbo Pascal implementation of them. The type declaration remains the
  947. same, as can be seen in the following syntax diagram:
  948. \input{syntax/typeproc.syn}
  949. The two following examples are valid type declarations:
  950. \begin{listing}
  951. Type TOneArg = Procedure (Var X : integer);
  952. TNoArg = Function : Real;
  953. var proc : TOneArg;
  954. func : TNoArg;
  955. \end{listing}
  956. One can assign the following values to a procedural type variable:
  957. \begin{enumerate}
  958. \item \var{Nil}, for both normal procedure pointers and method pointers.
  959. \item A variable reference of a procedural type, i.e. another variable of
  960. the same type.
  961. \item A global procedure or function address, with matching function or
  962. procedure header and calling convention.
  963. \item A method address.
  964. \end{enumerate}
  965. Given these declarations, the following assignments are valid:
  966. \begin{listing}
  967. Procedure printit (Var X : Integer);
  968. begin
  969. writeln (x);
  970. end;
  971. ...
  972. P:=@printit;
  973. Func:=@Pi;
  974. \end{listing}
  975. From this example, the difference with Turbo Pascal is clear: In Turbo
  976. Pascal it isn't necessary to use the address operator (\var{@})
  977. when assigning a procedural type variable, whereas in \fpc it is required
  978. (unless you use the \var{-So} switch, in which case you can drop the address
  979. operator.)
  980. Remark that the modifiers concerning the calling conventions (\var{cdecl},
  981. \var{pascal}, \var{stdcall} and \var{popstack} stick to the declaration;
  982. i.e. the following code would give an error:
  983. \begin{listing}
  984. Type TOneArgCcall = Procedure (Var X : integer);cdecl;
  985. var proc : TOneArgCcall;
  986. Procedure printit (Var X : Integer);
  987. begin
  988. writeln (x);
  989. end;
  990. begin
  991. P:=@printit;
  992. end.
  993. \end{listing}
  994. Because the \var{TOneArgCcall} type is a procedure that uses the cdecl
  995. calling convention.
  996. At the moment, the method procedural pointers (i.e. pointers that point to
  997. methods of objects, distinguished by the \var{of object} keywords in the
  998. declaration) are still in an experimental stage.
  999. \chapter{Objects}
  1000. \section{Declaration}
  1001. \fpc supports object oriented programming. In fact, most of the compiler is
  1002. written using objects. Here we present some technical questions regarding
  1003. object oriented programming in \fpc.
  1004. Objects should be treated as a special kind of record. The record contains
  1005. all the fields that are declared in the objects definition, and pointers
  1006. to the methods that are associated to the objects' type.
  1007. An object is declared just as you would declare a record; except that you
  1008. can now declare procedures and fuctions as of they were part of the record.
  1009. Objects can ''inherit'' fields and methods from ''parent'' objects. This means
  1010. that you can use these fields and methods as if the were included in the
  1011. objects you declared as a ''child'' object.
  1012. Furthermore, you can declare fields, procedures and functions as \var{public}
  1013. or \var{private}. By default, fields and methods are \var{public}, and are
  1014. exported outside the current unit. Fields or methods that are declared
  1015. \var{private} are only accessible in the current unit.
  1016. The prototype declaration of an object is as follows:
  1017. \input{syntax/typeobj.syn}
  1018. As you can see, you can repeat as many \var{private} and \var{public}
  1019. blocks as you want.
  1020. \var{Method definitions} are normal function or procedure declarations.
  1021. You cannot put fields after methods in the same block, i.e. the following
  1022. will generate an error when compiling:
  1023. \begin{listing}
  1024. Type MyObj = Object
  1025. Procedure Doit;
  1026. Field : Longint;
  1027. end;
  1028. \end{listing}
  1029. But the following will be accepted:
  1030. \begin{listing}
  1031. Type MyObj = Object
  1032. Public
  1033. Procedure Doit;
  1034. Private
  1035. Field : Longint;
  1036. end;
  1037. \end{listing}
  1038. because the field is in a different section.
  1039. {\em Remark:}
  1040. \fpc also supports the packed object. This is the same as an object, only
  1041. the elements (fields) of the object are byte-aligned, just as in the packed
  1042. record.
  1043. The declaration of a packed object is similar to the declaration
  1044. of a packed record :
  1045. \begin{listing}
  1046. Type
  1047. TObj = packed object;
  1048. Constructor init;
  1049. ...
  1050. end;
  1051. Pobj = ^TObj;
  1052. Var PP : Pobj;
  1053. \end{listing}
  1054. Similarly, the \var{\{\$PACKRECORDS \}} directive acts on objects as well.
  1055. \section{Fields}
  1056. Object Fields are like record fields. They are accessed in the same way as
  1057. you would access a record field : by using a qualified identifier. Given the
  1058. following declaration:
  1059. \begin{listing}
  1060. Type TAnObject = Object
  1061. AField : Longint;
  1062. Procedure AMethod;
  1063. end;
  1064. Var AnObject : TAnObject;
  1065. \end{listing}
  1066. then the following would be a valid assignment:
  1067. \begin{listing}
  1068. AnObject.AField:=0;
  1069. \end{listing}
  1070. Inside methods, fields can be accessed using the short identifier:
  1071. \begin{listing}
  1072. Procedure TAnObject.AMethod;
  1073. begin
  1074. ...
  1075. AField:=0;
  1076. ...
  1077. end;
  1078. \end{listing}
  1079. Or, one can use the \var{self} identifier. The \var{self} identifier refers
  1080. to the current instance of the object:
  1081. \begin{listing}
  1082. Procedure TAnObject.AMethod;
  1083. begin
  1084. ...
  1085. Self.AField:=0;
  1086. ...
  1087. end;
  1088. \end{listing}
  1089. You cannot access fields that are in a private section of an object from
  1090. outside the objects' methods. If you do, the compiler will complain about
  1091. an unknown identifier.
  1092. It is also possible to use the \var{with} statement with an object instance:
  1093. \begin{listing}
  1094. With AnObject do
  1095. begin
  1096. Afield:=12
  1097. AMethod;
  1098. end;
  1099. \end{listing}
  1100. In this example, between the \var{begin} and \var{end}, it is as if
  1101. \var{AnObject} was prepended to the \var{Afield} and \var{Amethod}
  1102. identifiers. More about this in \sees{With}
  1103. \section{Constructors and destructors }
  1104. \label{se:constructdestruct}
  1105. As can be seen in the syntax diagram for an object declaration, \fpc supports
  1106. constructors and destructors. You are responsible for calling the
  1107. destructor and constructor explicitly when using objects.
  1108. The declaration of a constructor or destructor is as follows:
  1109. \input{syntax/construct.syn}
  1110. A constructor is {\em required} if you use virtual methods.
  1111. \fpc supports also the extended syntax of the \var{New} and \var{Dispose}
  1112. procedures. In case you want to allocate a dynamic varible of an object
  1113. type, you can specify the constructor's name in the call to \var{New}.
  1114. The \var{New} is implemented as a function which returns a pointer to the
  1115. instantiated object. Given the following declarations :
  1116. \begin{listing}
  1117. Type
  1118. TObj = object;
  1119. Constructor init;
  1120. ...
  1121. end;
  1122. Pobj = ^TObj;
  1123. Var PP : Pobj;
  1124. \end{listing}
  1125. Then the following 3 calls are equivalent :
  1126. \begin{listing}
  1127. pp:=new (Pobj,Init);
  1128. \end{listing}
  1129. and
  1130. \begin{listing}
  1131. new(pp,init);
  1132. \end{listing}
  1133. and also
  1134. \begin{listing}
  1135. new (pp);
  1136. pp^.init;
  1137. \end{listing}
  1138. In the last case, the compiler will issue a warning that you should use the
  1139. extended syntax of \var{new} and \var{dispose} to generate instances of an
  1140. object. You can ignore this warning, but it's better programming practice to
  1141. use the extended syntax to create instances of an object.
  1142. Similarly, the \var{Dispose} procedure accepts the name of a destructor. The
  1143. destructor will then be called, before removing the object from the heap.
  1144. In view of the compiler warning remark, the now following Delphi approach may
  1145. be considered a more natural way of object-oriented programming.
  1146. \section{Methods}
  1147. Object methods are just like ordinary procedures or functions, only they
  1148. have an implicit extra parameter : \var{self}. Self points to the object
  1149. with which the method was invoked.
  1150. When implementing methods, the fully qualified identifier must be given
  1151. in the function header. When declaring methods, a normal identifier must be
  1152. given.
  1153. \section{Method invocation}
  1154. Methods are called just as normal procedures are called, only they have a
  1155. object instance identifier prepended to them
  1156. \seec{statements}.
  1157. To determine which method is called, it is necessary to know the type of
  1158. method:
  1159. \subsubsection{Static methods}
  1160. Static methods are methods that have been declared without a \var{abstract}
  1161. or \var{virtual} keyword. When calling a static method, the declared (i.e.
  1162. compile time) method of the object is used.
  1163. For example, consider the following declarations:
  1164. \begin{listing}
  1165. Type
  1166. TParent = Object
  1167. ...
  1168. procedure Method;
  1169. ...
  1170. end;
  1171. PPArent = ^TParent;
  1172. TChild = Object(TParent)
  1173. ...
  1174. procedure Method;
  1175. ...
  1176. end;
  1177. PChild = ^TChild;
  1178. \end{listing}
  1179. As it is visible, both the parent and child objects have a method called
  1180. \var{Draw}. Consider now the following declarations and calls :
  1181. \begin{listing}
  1182. Var ParentA,ParentB : PParent;
  1183. Child : PChild;
  1184. ParentA:=New(PParent,Init);
  1185. ParentB:=New(PChild,Init);
  1186. Child:=New(PChild,Init);
  1187. ParentA^.Method;
  1188. ParentB^.Method;
  1189. Child^.Method;
  1190. \end{listing}
  1191. Of the three invocations of \var{Method}, only the last one will call
  1192. \var{TChild.Method}, the other two calls will call \var{TParent.Method}
  1193. This is because for static methods, the compiler determines at compile
  1194. time which method should be called. Since \var{ParentB} is of type
  1195. \var{TPArent}, the compiler decides that it must be called with
  1196. \var{TParent.Method}, even though it will be created as a \var{TChild}.
  1197. There may be times when you want the method that is actually called to
  1198. depend on the actual type of the object at run-time. If so, the method
  1199. cannot be a static method, but must be a virtual method.
  1200. \subsubsection{Virtual methods}
  1201. To remedy the situation in the previous section, \var{virtual} methods are
  1202. created. This is simply done by appending the method declaration with the
  1203. \var{virtual} modifier.
  1204. Going back to the provious example, consider the following alterbative
  1205. declaration:
  1206. \begin{listing}
  1207. Type
  1208. TParent = Object
  1209. ...
  1210. procedure Method;virtual;
  1211. ...
  1212. end;
  1213. TChild = Object(TParent)
  1214. ...
  1215. procedure Method;virtual;
  1216. ...
  1217. end;
  1218. PChild = ^TChild;
  1219. \end{listing}
  1220. As it is visible, both the parent and child objects have a method called
  1221. \var{Draw}. Consider now the following declarations and calls :
  1222. \begin{listing}
  1223. Var ParentA,ParentB : PParent;
  1224. Child : PChild;
  1225. ParentA:=New(PParent,Init);
  1226. ParentB:=New(PChild,Init);
  1227. Child:=New(PChild,Init);
  1228. ParentA^.Method;
  1229. ParentB^.Method;
  1230. Child^.Method;
  1231. \end{listing}
  1232. Now, different methods will be called, depending on the actual run-time type
  1233. of the object. For \var{ParentA}, nothing changes, since it is created as
  1234. a \var{TPArent} instance. For \var{Child}, the situation also doesn't
  1235. change: it is again created as an instance of \var{TChild}.
  1236. For \var{ParentB} however, the situation does change: Even though it was
  1237. declared as a var{TPArent}, it is created as an instance of \var{TChild}.
  1238. Now, when the program runs, before calling the \var{Method}, the program
  1239. checks what the actual type of \var{ParentB} is, and only then decides which
  1240. method must be called. Seeing that \var{ParentB} is of type \var{TChild},
  1241. \var{TChild.Method} will be called.
  1242. The code for this run-time checking of the actual type of an object is
  1243. inserted by the compiler at compile time.
  1244. The \var{TChild.Method} is said to {\em override} the \var{TParent.Method}.
  1245. It is possible to acces the \var{TPArent.Method} from within the
  1246. var{TChild.Method}, with the \var{inherited} keyword:
  1247. \begin{listing}
  1248. TChild.Method;
  1249. begin
  1250. inherited Method;
  1251. ...
  1252. end;
  1253. \end{listing}
  1254. In the above example, when var{TChild.Method} is called, the first thing it
  1255. does is call \var{TPArent.Method}. You cannot use the inherited keyword on
  1256. static methods, only on virtual methods.
  1257. \subsubsection{Abstract methods}
  1258. An abstract method is a special kind of virtual method. A method can not be
  1259. abstract if it is not virtual. You cannot create an instance of an object
  1260. that has an abstract method. The reason is obvious: there is no method where
  1261. the compiler could jump to !
  1262. A method that is declared \var{abstract} does not have an implementation for
  1263. this method. It is up to inherited objects to override and implement this
  1264. method. Continuing our example, take a look at this:
  1265. \begin{listing}
  1266. Type
  1267. TParent = Object
  1268. ...
  1269. procedure Method;virtual;
  1270. ...
  1271. end;
  1272. PParent=^TParent;
  1273. TChild = Object(TParent)
  1274. ...
  1275. procedure Method;virtual;
  1276. ...
  1277. end;
  1278. PChild = ^TChild;
  1279. \end{listing}
  1280. As it is visible, both the parent and child objects have a method called
  1281. \var{Draw}. Consider now the following declarations and calls :
  1282. \begin{listing}
  1283. Var ParentA,ParentB : PParent;
  1284. Child : PChild;
  1285. ParentA:=New(PParent,Init);
  1286. ParentB:=New(PChild,Init);
  1287. Child:=New(PChild,Init);
  1288. ParentA^.Method;
  1289. ParentB^.Method;
  1290. Child^.Method;
  1291. \end{listing}
  1292. First of all, Line 4 will generate a compiler error, stating that you cannot
  1293. generate instances of objects with abstract methods: The compiler has
  1294. detected that \var{PParent} points to an object which has an abstract
  1295. method. Commenting line 4 would allow compilation of the program.
  1296. Remark that if you override an abstract method, you cannot call the parent
  1297. method with \var{inherited}, since there is no parent method; The compiler
  1298. will detect this, and complain about it, like this:
  1299. \begin{verbatim}
  1300. testo.pp(32,3) Error: Abstract methods can't be called directly
  1301. \end{verbatim}
  1302. If, through some mechanism, an abstract method is called at run-time,
  1303. then a run-time error will occur. (run-time error 211, to be precise)
  1304. \section{Visibility}
  1305. For objects, only 2 visibility specifiers exist : \var{private} and
  1306. \var{public}. If you don't specify a visibility specifier, \var{public}
  1307. is assumed.
  1308. both methods and fields can be hidden from a programmer by putting them
  1309. in a \var{private} section. The exact visibility rule is as follows:
  1310. \begin{description}
  1311. \item [Private\ ] All fields and methods that are in a \var{private} block,
  1312. can only be accessed in the module (i.e. unit or program) that contains
  1313. the object definition.
  1314. They can be accessed from inside the object's methods or from outside them
  1315. e.g. from other objects' methods, or global functions.
  1316. \item [Public\ ] sections are always accessible, from everywhere.
  1317. Fields and metods in a \var{public} section behave as though they were part
  1318. of an ordinary \var{record} type.
  1319. \end{description}
  1320. \chapter{Classes}
  1321. In the Delphi approach to Object Oriented Programming, everything revolves
  1322. around the concept of 'Classes'. A class can be seen as a pointer to an
  1323. object, or a pointer to a record.
  1324. In order to use classes, it is necessary to put the \file{objpas} unit in the
  1325. uses clause of your unit or program. This unit contains the basic
  1326. definitions of \var{TObject} and \var{TClass}, as well as some auxiliary
  1327. methods for using classes.
  1328. \section{Class definitions}
  1329. The prototype declaration of a class is as follows :
  1330. \input{syntax/typeclas.syn}
  1331. Again, You can repeat as many \var{private} and \var{public} blocks as you
  1332. want. Methods are normal function or procedure declarations.
  1333. As you can see, the declaration of a class is almost identical to the
  1334. declaration of an object. The real difference between objects and classes
  1335. is in the way they are created (see further in this chapter).
  1336. The visibility of the different sections is as follows:
  1337. \begin{description}
  1338. \item [Private\ ] All fields and methods that are in a \var{private} block, can
  1339. only be accessed in the module (i.e. unit) that contains the class definition.
  1340. They can be accessed from inside the classes' methods or from outside them
  1341. (e.g. from other classes' methods)
  1342. \item [Protected\ ] Is the same as \var{Private}, except that the members of
  1343. a \var{Protected} section are also accessible to descendent types, even if
  1344. they are implemented in other modules.
  1345. \item [Public\ ] sections are always accessible.
  1346. \item [Published\ ] Is the same as a \var{Public} section, but the compiler
  1347. generates also type information that is needed for automatic streaming of
  1348. these classes. Fields defined in a \var{published} section must be of class type.
  1349. Array peroperties cannot be in a \var{published} section.
  1350. \end{description}
  1351. \section{Class instantiation}
  1352. Classes must be created using their constructor. Remember that a class is a
  1353. pointer to an object, so when you declare a variable of some class, the
  1354. compiler just allocates a pointer, not the entire object. The constructor of
  1355. a class returns a pointer to an initialized instance of the object.
  1356. So, to initialize an instance of some class, you would do the following :
  1357. \begin{listing}
  1358. ClassVar:=ClassType.ConstructorName;
  1359. \end{listing}
  1360. You cannot use the extended syntax of \var{new} and \var{dispose} to
  1361. instantiate and destroy class instances.
  1362. That construct is reserved for use with objects only.
  1363. Calling the constructor will provoke a call to \var{getmem}, to allocate
  1364. enough space to hold the class instance data.
  1365. After that, the constuctor's code is executed.
  1366. The constructor has a pointer to it's data, in \var{self}.
  1367. {\em Remark :}
  1368. \begin{itemize}
  1369. \item The \var{\{\$Packrecords \}} directive also affects classes.
  1370. i.e. the alignment in memory of the different fields depends on the
  1371. value of the \var{\{\$Packrecords \}} directive.
  1372. \item Just as for objects and records, you can declare a packed class.
  1373. This has the same effect as on an object, or record, namely that the
  1374. elements are aligned on 1-byte boundaries. i.e. as close as possible.
  1375. \item \var{SizeOf(class)} will return 4, since a class is but a pointer to
  1376. an object. To get the size of the class instance data, use the
  1377. \var{TObject.InstanceSize} method.
  1378. \end{itemize}
  1379. \section{Methods}
  1380. Method invocation for classes is no different than for objects. The
  1381. following is a valid method invocation:
  1382. \begin{listing}
  1383. Var AnObject : TAnObject;
  1384. begin
  1385. AnObject:=TAnObject.Create;
  1386. ANobject.AMethod;
  1387. \end{listing}
  1388. \subsection{Properties}
  1389. Classes can contain properties as part of their fields list. A property
  1390. acts like a normal field, i.e. you can get or set it's value, but
  1391. allows to redirect the access of the field through functions and
  1392. procedures. They provide a means to assiciate an action with an assignment
  1393. of or a reading from a class 'field'. This allows for e.g. checking that a
  1394. value is valid when assigning, or, when reading, it allows to constuct the
  1395. value on the fly. Moreover, properties can be read-only or write only.
  1396. The prototype declaration of a property is as follows:
  1397. \input{syntax/property.syn}
  1398. From the diagram, it is cler that \fpc doesn't support the \var{index}
  1399. specifier yet.
  1400. A \var{read specifier} is either the name of a field that contains the
  1401. property, or the name of a method function that has the same return type as
  1402. the property type. In the case of a simple type, this
  1403. function must not accept an argument. A read specifier is optional, making
  1404. the property write-only.
  1405. A \var{write specifier} is optional: If there is no write specifier, the
  1406. property is read-only. A write specifier is either the name of a field, or
  1407. the name of a method procedure that accepts as a sole argument a variable of
  1408. the same type as the property.
  1409. The section (\var{private}, \var{published} in which the specified function or
  1410. procedure resides is irrelevant. Usually, however, this will be a protected
  1411. or private method.
  1412. Example:
  1413. Given the following declaration:
  1414. \begin{listing}
  1415. Type
  1416. MyClass = Class
  1417. Private
  1418. Field1 : Longint;
  1419. Field2 : Longint;
  1420. Field3 : Longint;
  1421. Procedure Sety (value : Longint);
  1422. Function Gety : Longint;
  1423. Function Getz : Longint;
  1424. Public
  1425. Property X : Longint Read Field1 write Field2;
  1426. Property Y : Longint Read GetY Write Sety;
  1427. Property Z : Longint Read GetZ;
  1428. end;
  1429. Var MyClass : TMyClass;
  1430. \end{listing}
  1431. The following are valid statements:
  1432. \begin{listing}
  1433. Writeln ('X : ',MyClass.X);
  1434. Writeln ('Y : ',MyClass.Y);
  1435. Writeln ('Z : ',MyClass.Z);
  1436. MyClass.X:=0;
  1437. MyClass.Y:=0;
  1438. \end{listing}
  1439. But the following would generate an error:
  1440. \begin{listing}
  1441. MyClass.Z:=0;
  1442. \end{listing}
  1443. because Z is a read-only property.
  1444. What happens in the above statements is that when a value needs to be read,
  1445. the compiler inserts a call to the various \var{getNNN} methods of the
  1446. object, and the result of this call is used. When an assignment is made,
  1447. the compiler passes the value that must be assigned as a paramater to
  1448. the various \var{setNNN} methods.
  1449. Because of this mechanism, properties cannot be passed as var arguments to a
  1450. function or procedure, since there is no known address of the property (at
  1451. least, not always).
  1452. You can also have array properties. These are properties that accept an
  1453. index, just as an array does. Only now the index doesn't have to be an
  1454. ordinal type, but can be any type.
  1455. A \var{read specifier} for an array property is the name method function
  1456. that has the same return type as the property type.
  1457. The function must accept as a sole arguent a variable of the same type as
  1458. the index type. For an array property, you cannot specify fields as read
  1459. specifiers.
  1460. A \var{write specifier} for an array property is the name of a method
  1461. procedure that accepts two arguments: The first argument has the same
  1462. type as the index, and the second argument is a parameter of the same
  1463. type as the property type.
  1464. As an example, see the following declaration:
  1465. \begin{listing}
  1466. Type TIntList = Class
  1467. Private
  1468. Function GetInt (I : Longint);
  1469. Function GetAsString (A : String) : String;
  1470. Procedure SetInt (I : Longint; Value : Longint;);
  1471. Procedure SetAsString (A : String; Value : String);
  1472. Public
  1473. Property Items [i : Longint] : Longint Read GetInt
  1474. Write SetInt;
  1475. Property StrItems [S : String] : String Read GetAsString
  1476. Write SetAsstring;
  1477. end;
  1478. Var AIntList : TIntList;
  1479. \end{listing}
  1480. Then the following statements would be valid:
  1481. \begin{listing}
  1482. AIntList.Items[26]:=1;
  1483. AIntList.StrItems['twenty-five']:='zero';
  1484. Writeln ('Item 26 : ',AIntList.Items[26]);
  1485. Writeln ('Item 25 : ',AIntList.StrItems['twenty-five']);
  1486. \end{listing}
  1487. While the following statements would generate errors:
  1488. \begin{listing}
  1489. AIntList.Items['twenty-five']:=1;
  1490. AIntList.StrItems[26]:='zero';
  1491. \end{listing}
  1492. Because the index types are wrong.
  1493. Array properties can be declared as \var{default} properties. This means that
  1494. it is not necessary to specifiy the property name when assigning or readin
  1495. it. If, in the previous example, the definition of the items property would
  1496. have been
  1497. \begin{listing}
  1498. Property Items[i : Longint]: Longint Read GetInt
  1499. Write SetInt; Default;
  1500. \end{listing}
  1501. Then the assignment
  1502. \begin{listing}
  1503. AIntList.Items[26]:=1;
  1504. \end{listing}
  1505. Would be equivalent to the following abbreviation.
  1506. \begin{listing}
  1507. AIntList[26]:=1;
  1508. \end{listing}
  1509. You can have only one default property per class, and descendent classes
  1510. cannot redeclare the default property.
  1511. \chapter{Expressions}
  1512. Expressions occur in assignments or in tests. Expressions produce a value,
  1513. of a certain type.
  1514. Expressions are built with two ocmponents: Operators and their Operands.
  1515. Usually an operator is binary, i.e. it requires 2 operands. Binary operators
  1516. occur always between the operands (as in \var{X/Y}). Sometimes an
  1517. operator is unary, i.e. it requires only one argument. A unary operator
  1518. occurs always before the operand, as in \var{-X}.
  1519. When using multiple operands in an expression, the precedence rules of
  1520. \seet{OperatorPrecedence} are used.
  1521. \begin{FPCltable}{lll}{Precedence of operators}{OperatorPrecedence}
  1522. Operator & Precedence & Category \\ \hline
  1523. \var{Not, @} & Highest & Unary operators\\
  1524. \var{* / div mod and shl shr as} & Second & Multiplying operators\\
  1525. \var{+ - or xor} & Third & Adding operators \\
  1526. \var{< <> < > <= >= in is} & Lowest (Fourth) & relational operators \\
  1527. \hline
  1528. \end{FPCltable}
  1529. When determining the precedence, te compiler uses the following rules:
  1530. \begin{enumerate}
  1531. \item Operations with equal precedence are executed from left to right.
  1532. \item In operations with unequal precedence the operands belong to the
  1533. operater with the highest precedence. For example, in \var{5*3+7}, the
  1534. multiplication is higher in precedence than the addition, so it is
  1535. executed first. The result would be 22.
  1536. \item If parentheses are used in an epression, their contents is evaluated
  1537. first. Thus, \var {5*(3+7)} would result in 50.
  1538. \end{enumerate}
  1539. An expression is a sequence of terms and factors. A factor is an operand of
  1540. a multiplication operator. A term is an operand of an adding operator.
  1541. \section{Expression syntax}
  1542. An expression applies relational operators to simple expressions. Simple
  1543. expressions are a series of terms, joined by adding operators.
  1544. \input{syntax/expsimpl.syn}
  1545. The following are valid expressions:
  1546. \begin{listing}
  1547. GraphResult<>grError
  1548. (DoItToday=Yes) and (DoItTomorrow=No);
  1549. Day in Weekend
  1550. \end{listing}
  1551. And here are some simple expressions:
  1552. \begin{listing}
  1553. A + B
  1554. -Pi
  1555. ToBe or Not ToBe
  1556. \end{listing}
  1557. Terms consist of factors, connected by multiplication operators.
  1558. \input{syntax/expterm.syn}
  1559. Here are some valid terms:
  1560. \begin{listing}
  1561. 2 * Pi
  1562. A Div B
  1563. (DoItToday=Yes) and (DoItTomorrow=No);
  1564. \end{listing}
  1565. Factors are all other constructions:
  1566. \input{syntax/expfact.syn}
  1567. \section{function calls}
  1568. Function calls are part of expressions (although, using extended syntax,
  1569. they can be statements too). They are constructed as follows:
  1570. \input{syntax/fcall.syn}
  1571. The \synt{variable reference} must be a procedural type variable referce.
  1572. A method designator can only be used in side the method of an object. A
  1573. qualified method designator can be used outside object methods too.
  1574. The function that will get called is the function with a declared parameter
  1575. list that matches the actual parameter list. This means that
  1576. \begin{enumerate}
  1577. \item The number of actual parameters must equal the number of declared
  1578. parameters.
  1579. \item The types of the parameters must be compatible. For varriable
  1580. reference parameters, the parameter types must be exactly the same.
  1581. \end{enumerate}
  1582. If no matching function is found, then the compiler will generate an error.
  1583. Depending on the fact of the function is overloaded (i.e. multiple functions
  1584. with the same name, but different parameter lists) the error will be
  1585. different.
  1586. Thera are cases when the compiler will not execute the function call in an
  1587. expression. This is the case when you are assigning a value to a procedural
  1588. type variable, as in the following example:
  1589. \begin{listing}
  1590. Type
  1591. FuncType = Function: Integer;
  1592. Var A : Integer;
  1593. Function AddOne : Integer;
  1594. begin
  1595. A:=A+1;
  1596. AddOne:=A;
  1597. end;
  1598. Var F : FuncType;
  1599. N : Integer;
  1600. begin
  1601. A:=0;
  1602. F:=AddOne; { Assign AddOne to F, Don't call AddOne}
  1603. N:=AddOne; { N:=1 !!}
  1604. end.
  1605. \end{listing}
  1606. In the above listing, the assigment to F will not cause the function AddOne
  1607. to be called. The assignment to N, however, will call AddOne.
  1608. A problem with this syntax is the following construction:
  1609. \begin{listing}
  1610. If F = AddOne Then
  1611. DoSomethingHorrible;
  1612. \end{listing}
  1613. Should the compiler compare the addresses of \var{F} and \var{AddOne},
  1614. or should it call both functions, and compare the result ? \fpc solves this
  1615. by deciding that a procedural variable is equivalent to a pointer. Thus the
  1616. compiler will give a type mismatch error, since AddOne is considered a
  1617. call to a function with integer result, and F is a pointer, Hence a type
  1618. mismatch occurs.
  1619. How then, should one compare whether \var{F} points to the function
  1620. \var{AddOne} ? To do this, one should use the address operator \var{@}:
  1621. \begin{listing}
  1622. If F = @AddOne Then
  1623. Writeln ('Functions are equal');
  1624. \end{listing}
  1625. The left hand side of the boolean expression is an address. The right and
  1626. side also, and so the compiler compares 2 addresses.
  1627. How to compare the values that both functions return ? By adding an empty
  1628. parameter list:
  1629. \begin{listing}
  1630. If F()=Addone then
  1631. Writeln ('Functions return same values ');
  1632. \end{listing}
  1633. Remark that this behaviour is not compatible with Delphi syntax.
  1634. \section{Set constructors}
  1635. When you want to enter a set-type constant in an expression, you must give a
  1636. set constructor. In essence this is the same thing as when you define a set
  1637. type, only you have no identifier to identify the set with.
  1638. A set constructor is a comma separated list of expressions, enclosed in
  1639. square brackets.
  1640. \input{syntax/setconst.syn}
  1641. All set groups and set elements must be of the same ordinal type.
  1642. The empty set is denoted by \var{[]}, and it can be assigned to any type of
  1643. set. A set group with a range \var{[A..Z]} makes all values in the range a
  1644. set element. If the first range specifier has a bigger ordinal value than
  1645. the second the set is empty, e.g., \var{[Z..A]} denotes an empty set.
  1646. The following are valid set constructors:
  1647. \begin{listing}
  1648. [today,tomorrow]
  1649. [Monfay..Friday,Sunday]
  1650. [ 2, 3*2, 6*2, 9*2 ]
  1651. ['A'..'Z','a'..'z','0'..'9']
  1652. \end{listing}
  1653. \section{Value typecasts}
  1654. Sometimes it is necessary to change the type of an expression, or a part of
  1655. the expression, to be able to be assignment compatible. This is done through
  1656. a value typecast. The syntax diagram for a value typecast is as follows:
  1657. \input{syntax/tcast.syn}
  1658. Value typecasts cannot be used on the left side of assignments, as variable
  1659. typecasts.
  1660. Here are some valid typecasts:
  1661. \begin{listing}
  1662. Byte('A')
  1663. Char(48)
  1664. boolean(1)
  1665. longint(@Buffer)
  1666. \end{listing}
  1667. The type size of the expression and the size of the type cast must be the
  1668. same. That is, the following doesn't work:
  1669. \begin{listing}
  1670. Integer('A')
  1671. Char(4875)
  1672. boolean(100)
  1673. Word(@Buffer)
  1674. \end{listing}
  1675. \section{The @ operator}
  1676. The address operator \var{@} returns the address of a variable or function.
  1677. It is used as follows:
  1678. \input{syntax/address.syn}
  1679. The \var{@} operator returns a typed pointer if the \var{\$T} switch is on.
  1680. If the \var{\$T} switch is off then the address operator returns an untyped
  1681. pointer, which is assignent compatible with all pointer types. The type of
  1682. the pointer is \var{\^{}T}, where \var{T} is the type of the variable
  1683. reference.
  1684. For example, the following will compile
  1685. \begin{listing}
  1686. Program tcast;
  1687. {$T-} { @ returns untyped pointer }
  1688. Type art = Array[1..100] of byte;
  1689. Var Buffer : longint;
  1690. PLargeBuffer : ^art;
  1691. begin
  1692. PLargeBuffer:=@Buffer;
  1693. end.
  1694. \end{listing}
  1695. Changing the \var{\{\$T-\}} to \var{\{\$T+\}} will prevent the compiler from
  1696. compiling this. It will give a type mismatch error.
  1697. By default, the address operator returns an untyped pointer.
  1698. Applying the address operator to a function, method, or procedure identifier
  1699. will give a pointer to the entry point of that function. The result is an
  1700. untyped pointer.
  1701. By default, you must use the address operator if you want to assign a value
  1702. to a procedural type variable. This behaviour can be avoided by using the
  1703. \var{-So} or \var{-S2} switches, which result in a more compatible Delphi or
  1704. Turbo Pascal syntax.
  1705. \section{Operators}
  1706. Operators can be classified according to the type of expression they
  1707. operate on. We will discuss them type by type.
  1708. \subsection{Arithmetic operators}
  1709. Arithmetic operators occur in arithmetic operations, i.e. in expressions
  1710. that contain integers or reals. There are 2 kinds of operators : Binary and
  1711. unary arithmetic operators.
  1712. Binary operators are listed in \seet{binaroperators}, unary operators are
  1713. listed in \seet{unaroperator}.
  1714. \begin{FPCltable}{ll}{Binary arithmetic operators}{binaroperators.}
  1715. Operator & Operation \\ \hline
  1716. \var{+} & Addition\\
  1717. \var{-} & Subtraction\\
  1718. \var{*} & Multiplication \\
  1719. \var{/} & Division \\
  1720. \var{Div} & Integer division \\
  1721. \var{Mod} & Remainder \\ \hline
  1722. \end{FPCltable}
  1723. With the exception of \var{Div} and \var{Mod}, which accept only integer
  1724. expressions as operands, all operators accept real and integer expressions as
  1725. operands.
  1726. For binary operators, the result type will be integer if both operands are
  1727. integer type expressions. If one of the operands is a real type expression,
  1728. then the result is real.
  1729. As an exception : division \var{/} results always in real values.
  1730. \begin{FPCltable}{ll}{Unary arithmetic operators}{unnaroperators.}
  1731. Operator & Operation \\ \hline
  1732. \var{+} & Sign identity\\
  1733. \var{-} & Sign inversion \\ \hline
  1734. \end{FPCltable}
  1735. For unary operators, the result type is always equal to th expression type.
  1736. The division (\var{/}) and \var{Mod} operator will cause run-time errors if
  1737. the second argument is zero.
  1738. The sign of the result of a \var{Mod} operator is the same as the sign of
  1739. the left side operand of the \var{Mod} operator. In fact, the \var{Mod}
  1740. operator is equivalent to the following operation :
  1741. \begin{listing}
  1742. I mod J = I - (I div J) * J
  1743. \end{listing}
  1744. but it executes faster than the right hand side expression.
  1745. \subsection{Logical operators}
  1746. Logical operators act on the individual bits of ordinal expressions.
  1747. Logical operators require operands that are of an integer type, and produce
  1748. an integer type result. The possible logical operators are listed in
  1749. \seet{logicoperations}.
  1750. \begin{FPCltable}{ll}{Logical operators}{logicoperations}
  1751. Operator & Operation \\ \hline
  1752. \var{not} & Bitwise negation (unary) \\
  1753. \var{and} & Bitwise and \\
  1754. \var{or} & Bitwise or \\
  1755. \var{xor} & Bitwise xor \\
  1756. \var{shl} & Bitwise shift to the left \\
  1757. \var{shr} & Bitwise shift to the right \\ \hline
  1758. \end{FPCltable}
  1759. The following are valid logical expressions:
  1760. \begin{listing}
  1761. A shr 1 { same as A div 2, but faster}
  1762. Not 1 { equals -2 }
  1763. Not 0 { equals -1 }
  1764. Not -1 { equals 0 }
  1765. B shl 2 { same as B * 2 for integers }
  1766. 1 or 2 { equals 3 }
  1767. 3 xor 1 { equals 2 }
  1768. \end{listing}
  1769. \subsection{Boolean operators}
  1770. Boolean operators can be considered logical operations on a type with 1 bit
  1771. size. Therefore the \var{shl} and \var{shr} operations have little sense.
  1772. Boolean operators can only have boolean type operands, and the resulting
  1773. type is always boolean. The possible operators are listed in \seet{booleans}
  1774. \begin{FPCltable}{ll}{Boolean operators}{booleans}
  1775. Operator & Operation \\ \hline
  1776. \var{not} & logical negation (unary) \\
  1777. \var{and} & logical and \\
  1778. \var{or} & logical or \\
  1779. \var{xor} & logical xor \\ \hline
  1780. \end{FPCltable}
  1781. Remark that boolean expressions are ALWAYS evaluated with short-circuit
  1782. evaluation. This means that from the moment the result of the complete
  1783. expression is known, evaluation is stopped and the result is returned.
  1784. For instance, in the following expression:
  1785. \begin{listing}
  1786. B := True or MaybeTrue;
  1787. \end{listing}
  1788. The compiler will never look at the value of \var{MaybeTrue}, since it is
  1789. obvious that the expression will always be true. As a result of this
  1790. strategy, if \var{MaybeTrue} is a function, it will not get called !
  1791. (This can have surprising effects when used in conjunction with properties)
  1792. \subsection{String operators}
  1793. There is only one string operator : \var{+}. It's action is to concatenate
  1794. the contents of the two strings (or characters) it stands between.
  1795. You cannot use \var{+} to concatenate null-terminated (\var{PChar}) strings.
  1796. The following are valid string operations:
  1797. \begin{listing}
  1798. 'This is ' + 'VERY ' + 'easy !'
  1799. Dirname+'\'
  1800. \end{listing}
  1801. The folowwing is not:
  1802. \begin{listing}
  1803. Var Dirname = Pchar;
  1804. ...
  1805. Dirname:=Dirname+'\';
  1806. \end{listing}
  1807. Because \var{Dirname} is a null-terminated string.
  1808. \subsection{Set operations}
  1809. The following operations on sets can be performed with operators:
  1810. Union, difference and intersection. The operators needed for this are listed
  1811. in \seet{setoperators}.
  1812. \begin{FPCltable}{ll}{Set operators}{setoperators}
  1813. Operator & Action \\ \hline
  1814. \var{+} & Union \\
  1815. \var{-} & Difference \\
  1816. \var{*} & Intersection \\ \hline
  1817. \end{FPCltable}
  1818. The set typed of the operands must be the same, or an error will be
  1819. generated by the compiler.
  1820. \subsection{Relational operators}
  1821. The relational operators are listed in \seet{relationoperators}
  1822. \begin{FPCltable}{ll}{Relational operators}{relationoperators}
  1823. Operator & Action \\ \hline
  1824. \var{=} & Equal \\
  1825. \var{<>} & Not equal \\
  1826. \var{<} & Stricty less than\\
  1827. \var{>} & Strictly greater than\\
  1828. \var{<=} & Less than or equal \\
  1829. \var{>=} & Greater than or equal \\
  1830. \var{in} & Element of \\ \hline
  1831. \end{FPCltable}
  1832. Left and right operands must be of the same type. You can only mix integer
  1833. and real types in relational expressions.
  1834. Comparing strings is done on the basis of their ASCII code representation.
  1835. When comparing pointers, the addresses to which they point are compared.
  1836. This also is troe for \var{PChar} type pointers. If you want to compare the
  1837. strings the \var{Pchar} points to, you must use the \var{StrComp} function
  1838. from the \file{strings} unit.
  1839. The \var{in} returns \var{True} if the left operand (which must have the same
  1840. ordinal type as the set type) is an element of the set which is the right
  1841. operand, otherwise it returns \var{False}
  1842. \chapter{Statements}
  1843. The heart of each algorithm are the actions it takes. These actions are
  1844. contained in the statements of your program or unit. You can label your
  1845. statements, and jump to them (within certain limits) with var{Goto}
  1846. statements.
  1847. This can be seen in the following syntax diagram:
  1848. \input{syntax/statement.syn}
  1849. A label can be an identifier or an integer digit.
  1850. \section{Simple statements}
  1851. A simple statement cannot be decomposed in separate statements. There are
  1852. basically 3 kinds of simple statements:
  1853. \input{syntax/simstate.syn}
  1854. \subsection{Assignments}
  1855. Assignments give a value to a variable, replacing any previous value the
  1856. observable might have had:
  1857. \input{syntax/assign.syn}
  1858. In addition to the standard Pascal assignment operator (\var{:=}), which
  1859. simply replaces the value of the varable with the value resulting from the
  1860. expression on the right of the {:=} operator, \fpc
  1861. supports some c-style constructions. All available constructs are listed in
  1862. \seet{assignments}.
  1863. \begin{FPCltable}{lr}{Allowed C constructs in \fpc}{assignments}
  1864. Assignment & Result \\ \hline
  1865. a += b & Adds \var{b} to \var{a}, and stores the result in \var{a}.\\
  1866. a -= b & Substracts \var{b} from \var{a}, and stores the result in
  1867. \var{a}. \\
  1868. a *= b & Multiplies \var{a} with \var{b}, and stores the result in
  1869. \var{a}. \\
  1870. a /= b & Divides \var{a} through \var{b}, and stores the result in
  1871. \var{a}. \\ \hline
  1872. \end{FPCltable}
  1873. For these constructs to work, you should specify the \var{-Sc}
  1874. command-line switch.
  1875. {\em Remark:} These constructions are just for typing convenience, they
  1876. don't generate different code.
  1877. Here are some examples of valid assignment statements:
  1878. \begin{listing}
  1879. X:=X+Y;
  1880. X+=Y; { Same as X:=X+Y, needs -Sc command line switch}
  1881. X/=2; { Same as X:=X/2, needs -Sc command line switch}
  1882. Done:=False;
  1883. Weather:=Good;
  1884. MyPi:= 4* Tan(1);
  1885. \end{listing}
  1886. \subsection{Procedure statements}
  1887. Procedure statements are calls to subroutines. There are
  1888. different possibilities for procedure calls: A normal procedure call, an
  1889. object method call (qualified or not) , or even a call to a procedural
  1890. type variable. All types are present in the following diagram.
  1891. \input{syntax/procedure.syn}
  1892. The \fpc compiler will look for a procedure with the same name as given in
  1893. the procedure statement, and with a declared parameter list that matches the
  1894. actual parameter list.
  1895. The following are valid procedure statements:
  1896. \begin{listing}
  1897. Usage;
  1898. Writeln('Pascal is an easy language !');
  1899. Doit();
  1900. \end{listing}
  1901. \subsection{Goto statements}
  1902. \fpc supports the \var{goto} jump statement. Its prototype syntax is
  1903. \input{syntax/goto.syn}
  1904. When using \var{goto} statements, you must keep the following in mind:
  1905. \begin{enumerate}
  1906. \item The jump label must be defined in the same block as the \var{Goto}
  1907. statement.
  1908. \item Jumping from outside a loop to the inside of a loop or vice versa can
  1909. have strange effects.
  1910. \item To be able to use the \var{Goto} statement, you need to specify the
  1911. \var{-Sg} compiler switch.
  1912. \end{enumerate}
  1913. \var{Goto} statements are considered bad practice and should be avoided as
  1914. much as possible. It is always possible to replace a \var{goto} statement by a
  1915. construction that doesn't need a \var{goto}, although this construction may
  1916. not be as clear as a goto statement.
  1917. For instance, the following is an allowed goto statement:
  1918. \begin{listing}
  1919. var
  1920. jumpto : label
  1921. ...
  1922. Jumpto :
  1923. Statement;
  1924. ...
  1925. Goto jumpto;
  1926. ...
  1927. \end{listing}
  1928. \section{Structured statements}
  1929. Structured statements can be broken into smaller simple statements, which
  1930. should be executed repeatedly, conditionally or sequentially:
  1931. \input{syntax/struct.syn}
  1932. Conditional statements come in 2 flavours :
  1933. \input{syntax/conditio.syn}
  1934. Repetitive statements come in 3 flavours:
  1935. \input{syntax/repetitive}
  1936. The following sections deal with each of these statements.
  1937. \subsection{Compound statements}
  1938. Compound statements are a group of statements, separated by semicolons,
  1939. that are surrounded by the keywords \var{Begin} and \var{End}. The
  1940. Last statement doesn't need to be followed by a semicolon, although it is
  1941. allowed. A compound statement is a way of grouping statements together,
  1942. executing the statements sequentially. They are treated as one statement
  1943. in cases where Pascal syntax expects 1 statement, such as in
  1944. \var{if ... then} statements.
  1945. \input{syntax/compound.syn}
  1946. \subsection{The \var{Case} statement}
  1947. \fpc supports the \var{case} statement. Its syntax diagram is
  1948. \input{syntax/case.syn}
  1949. The constants appearing in the various case parts must be known at
  1950. compile-time, and can be of the following types : enumeration types,
  1951. Ordinal types (except boolean), and chars. The expression must be also of
  1952. this type, or an compiler error will occur. All case constants must
  1953. have the same type.
  1954. The compiler will evaluate the expression. If one of the case constants
  1955. values matches the value of the expression, the statement that containing
  1956. this constant is executed. After that, the program continues after the final
  1957. \var{end}.
  1958. If none of the case constants match the expression value, the statement
  1959. after the \var{else} keyword is executed. This can be an empty statement.
  1960. If no else part is present, and no case constant matches the expression
  1961. value, program flow continues after the final \var{end}.
  1962. The case statements can be compound statements
  1963. (i.e. a \var{begin..End} block).
  1964. {\em Remark:} Contrary to Turbo Pascal, duplicate case labels are not
  1965. allowed in \fpc, so the following code will generate an error when
  1966. compiling:
  1967. \begin{listing}
  1968. Var i : integer;
  1969. ...
  1970. Case i of
  1971. 3 : DoSomething;
  1972. 1..5 : DoSomethingElse;
  1973. end;
  1974. \end{listing}
  1975. The compiler will generate a \var{Duplicate case label} error when compiling
  1976. this, because the 3 also appears (implicitly) in the range \var{1..5}. This
  1977. is similar to Delhpi syntax.
  1978. The following are valid case statements:
  1979. 'b' : writeln ('B pressed');
  1980. \begin{listing}
  1981. Case C of
  1982. 'a' : writeln ('A pressed');
  1983. 'c' : writeln ('C pressed');
  1984. else
  1985. writeln ('unknown letter pressed : ',C);
  1986. end;
  1987. \end{listing}
  1988. Or
  1989. 'b' : writeln ('B pressed');
  1990. \begin{listing}
  1991. Case C of
  1992. 'a','e','i','o','u' : writeln ('vowel pressed');
  1993. 'y' : writeln ('This one depends on the language');
  1994. else
  1995. writeln ('Consonant pressed');
  1996. end;
  1997. \end{listing}
  1998. \begin{listing}
  1999. Case Number of
  2000. 1..10 : writeln ('Small number');
  2001. 11..100 : writeln ('Normal, medium number');
  2002. else
  2003. writeln ('HUGE number');
  2004. end;
  2005. \end{listing}
  2006. \subsection{The \var{If..then..else} statement}
  2007. The \var{If .. then .. else..} protottype syntax is
  2008. \input{syntax/ifthen.syn}
  2009. The expression between the \var{if} and \var{then} keywords must have a
  2010. boolean return type. If the expression evaluates to \var{True} then the
  2011. statement following{then} is executed. If the expression evaluates to
  2012. \var{False}, then the statement following \var{else} is executed, if it is
  2013. present.
  2014. Be aware of the fact that the boolean expression will be short-cut evaluated.
  2015. (Meaning that the evaluation will be stopped at the point where the
  2016. outcome is known with certainty)
  2017. Also, before the \var {else} keyword, no semicolon (\var{;}) is allowed,
  2018. but all statements can be compound statements.
  2019. In nested \var{If.. then .. else} constructs, some ambiguity may araise as
  2020. to which \var{else} statement paits with which \var{if} statement. The rule
  2021. is that the \var{else } keyword matches the first \var{if} keyword not
  2022. already matched by an \var{else} keyword.
  2023. For example:
  2024. \begin{listing}
  2025. If exp1 Then
  2026. If exp2 then
  2027. Stat1
  2028. else
  2029. stat2;
  2030. \end{listing}
  2031. Despite it's appreance, the statement is syntactically equivalent to
  2032. \begin{listing}
  2033. If exp1 Then
  2034. begin
  2035. If exp2 then
  2036. Stat1
  2037. else
  2038. stat2
  2039. end;
  2040. \end{listing}
  2041. and not to
  2042. \begin{listing}
  2043. { NOT EQUIVALENT }
  2044. If exp1 Then
  2045. begin
  2046. If exp2 then
  2047. Stat1
  2048. end
  2049. else
  2050. stat2
  2051. \end{listing}
  2052. If it is this latter construct you want, you must explicitly put the
  2053. \var{begin} and \ver{end} keywords. When in doubt, add them, they don't
  2054. hurt.
  2055. The following is a valid statement:
  2056. \begin{listing}
  2057. If Today in [Monday..Friday] then
  2058. Writeln ('Must work harder')
  2059. else
  2060. Writeln ('Take a day off.');
  2061. \end{listing}
  2062. \subsection{The \var{For..to/downto..do} statement}
  2063. \fpc supports the \var{For} loop construction. The prototype syntax is as
  2064. follows:
  2065. \input{syntax/for.syn}
  2066. \var{Statement} can be a compound statement. In the first case, if
  2067. \var{Lowerbound} is larger than \var{Upperbound} then \var{Statement} will
  2068. never be executed. \var{Counter} must be an ordinal type, no other types can
  2069. be used as counters in a loop.
  2070. {\em Remark:} Contrary to ANSI pascal specifications, \fpc first initializes
  2071. the counter variable, and only then calculates the upper bound.
  2072. \subsection{The \var{Repeat..until} statement}
  2073. The prototype of the \var{Repeat..until} statement is
  2074. \begin{listing}
  2075. Repeat
  2076. Statement1;
  2077. Statement2;
  2078. Until Expression;
  2079. \end{listing}
  2080. This will execute \var{Statement1} etc. until \var{Expression} evaluates to
  2081. \var{True}. Since \var{Expression} is evaluated {\em after} the execution of the
  2082. statements, they are executed at least once.
  2083. Be aware of the fact that the boolean expressions \var{Expression1} and
  2084. \var{Expression2} will be short-cut evaluated. (Meaning that the evaluation
  2085. will be stopped at the point where the outcome is known with certainty)
  2086. \subsection{The \var{While..do} statement}
  2087. The prototype of the \var{While..do} statement is
  2088. \begin{listing}
  2089. While Expression Do
  2090. Statement;
  2091. \end{listing}
  2092. This will execute \var{Statement} as long as \var{Expression} evaluates to
  2093. \var{True}. Since \var{Expression} is evaluated {\em before} the execution
  2094. of \var{Statement}, it is possible that \var{Statement} isn't executed at
  2095. all.
  2096. \var{Statement} can be a compound statement.
  2097. Be aware of the fact that the boolean expressions \var{Expression1} and
  2098. \var{Expression2} will be short-cut evaluated. (Meaning that the evaluation
  2099. will be stopped at the point where the outcome is known with certainty)
  2100. \subsection{The \var{With} statement}
  2101. The with statement serves to access the elements of a record\footnote{
  2102. The \var{with} statement does not work correctly when used with
  2103. objects or classes until version 0.99.6}
  2104. , without
  2105. having to specify the name of the record. Given the declaration:
  2106. \begin{listing}
  2107. Type Passenger = Record
  2108. Name : String[30];
  2109. Flight : String[10];
  2110. end;
  2111. Var TheCustomer : Passenger;
  2112. \end{listing}
  2113. The following statements are completely equivalent:
  2114. \begin{listing}
  2115. TheCustomer.Name:='Michael';
  2116. TheCustomer.Flight:='PS901';
  2117. \end{listing}
  2118. and
  2119. \begin{listing}
  2120. With TheCustomer do
  2121. begin
  2122. Name:='Michael';
  2123. Flight:='PS901';
  2124. end;
  2125. \end{listing}
  2126. \subsection{Compound statements}
  2127. Compound statements are a group of statements, separated by semicolons,
  2128. that are surrounded by the keywords \var{Begin} and \var{End}. The
  2129. Last statement doesn't need to be followed by a semicolon, although it is
  2130. allowed.
  2131. \subsection{Exceptions}
  2132. As of version 0.99.7, \fpc supports exceptions. Exceptions provide a
  2133. convenient way to program error and error-recovery mechanisms, and are
  2134. closely related to classes.
  2135. Exception support is based on 3 constructs:
  2136. \begin{description}
  2137. \item [Raise\ ] statements. To raise an exeption. This is usually done to signal an
  2138. error condition.
  2139. \item [Try ... Except\ ] blocks. These block serve to catch exceptions
  2140. raised within the scope of the block, and to provide exception-recovery
  2141. code.
  2142. \item [Try ... Finally\ ] blocks. These block serve to force code to be
  2143. executed irrespective of an exception occurrence or not. They generally
  2144. serve to clean up memory or close files in case an exception occurs.
  2145. code.
  2146. \end{description}
  2147. The \var{raise} statement is as follows:
  2148. \begin{listing}
  2149. Raise [ExceptionInstance [at Address]];
  2150. \end{listing}
  2151. This statement will raise an exception. If specified, \var{ExceptionInstance}
  2152. must be an initialized instance of a class, which is the raise type. If
  2153. specified, \var{Address} must be an expression that returns an address.
  2154. If \var{ExceptionInstance} is omitted, then the Current exception is
  2155. re-raised. This construct can only be used in an exception handling
  2156. block.
  2157. As an example: The following division checks whether the denominator is
  2158. zero, and if so, raises an exception of type \var{EDivException}
  2159. \begin{listing}
  2160. Type EDivException = Class(Exception);
  2161. Function DoDiv (X,Y : Longint) : Integer;
  2162. begin
  2163. If Y=0 then
  2164. Raise EDivException.Create ('Division by Zero would occur');
  2165. Result:=X Div Y;
  2166. end;
  2167. \end{listing}
  2168. The class \var{Exception} is defined in the \file{Sysutils} unit of the rtl.
  2169. An exception handling block is of the following form :
  2170. \begin{listing}
  2171. Try
  2172. ...Statement List...
  2173. Except
  2174. [On [E:] ExceptionClass do CompoundStatement;]
  2175. [ Default exception handler]
  2176. end;
  2177. \end{listing}
  2178. If an exception occurs during the execution of the \var{statement list}, the
  2179. program flow fill be transferred to the except block. There, the type of the
  2180. exception is checked, and if there is a \var{On ExcType} statement where
  2181. \var{ExcType} matches the exception object type, or is a parent type of
  2182. the exception object type, then the statements follwing the corresponding
  2183. \var{Do} will be executed. The first matching type is used. After the
  2184. \var{Do} block was executed, the program continues after the \var{End}
  2185. statement.
  2186. The identifier \var{E} is optional, and declares an exception object. It
  2187. can be used to manipulate the exception object in the exception handling
  2188. code. The scope of this declaration is the statement block foillowing the
  2189. \var{Do} keyword.
  2190. If none of the \var{On} handlers matches the exception object type, then the
  2191. \var{Default exception handler} is executed. If no such default handler is
  2192. found, then the exception is automatically re-raised. This process allows
  2193. to nest \var{try...except} blocks.
  2194. As an example, given the previous declaration of the \var{DoDiv} function,
  2195. consider the following
  2196. \begin{listing}
  2197. Try
  2198. Z:=DoDiv (X,Y);
  2199. Except
  2200. On EDivException do Z:=0;
  2201. end;
  2202. \end{listing}
  2203. If \var{Y} happens to be zero, then the DoDiv function code will raise an
  2204. exception. When this happens, program flow is transferred to the except
  2205. statement, where the Exception handler will set the value of \var{Z} to
  2206. zero. If no exception is raised, then program flow continues past the last
  2207. \var{end} statement.
  2208. To allow error recovery, the \var{Try ... Finally} block is supported.
  2209. A \var{Try...Finally} block ensures that the statements following the
  2210. \var{Finally} keyword are guaranteed to be executed, even if an exception
  2211. occurs.
  2212. A \var{Try..Finally} block has the following form:
  2213. \begin{listing}
  2214. Try
  2215. ...Statement List...
  2216. Finally
  2217. [ Finally Statements ]
  2218. end;
  2219. \end{listing}
  2220. If no exception occurs inside the \var{Statement List}, then the program
  2221. runs as if the \var{Try}, \var{Finally} and \var{End} keywords were not
  2222. present.
  2223. If, however, an exception occurs, the program flow is immediatly
  2224. transferred to the first statement of the \var{Finally statements}.
  2225. All statements of the \var{Finally Statements} will be executed, and then
  2226. the exception will be automatically re-raised. Any statements between the
  2227. place where the exception was raised and the first statement of the
  2228. \var{Finally Statements} are skipped.
  2229. As an example consider the following routine:
  2230. \begin{listing}
  2231. Procedure Doit (Name : string);
  2232. Var F : Text;
  2233. begin
  2234. Try
  2235. Assign (F,Name);
  2236. Rewrite (name);
  2237. ... File handling ...
  2238. Finally
  2239. Close(F);
  2240. end;
  2241. \end{listing}
  2242. If during the execution of the file handling an excption occurs, then
  2243. program flow will continue at the \var{close(F)} statement, skipping any
  2244. file operations that might follow between the place where the exception
  2245. was raised, and the \var{Close} statement.
  2246. If no exception occurred, all file operations will be executed, and the file
  2247. will be closed at the end.
  2248. It is possible to nest \var{Try...Except} blocks with \var{Try...Finally}
  2249. blocks. Program flow will be done according to a \var{lifo} (last in, first
  2250. out) principle: The code of the last encountered \var{Try...Except} or
  2251. \var{Try...Finally} block will be executed first. If the exception is not
  2252. caught, or it was a finally statement, program flow will we transferred to
  2253. the last but-one block, {\em ad infinitum}.
  2254. If an exception occurs, and there is no exception handler present, then a
  2255. runerror 217 will be generated. If you use the \file{sysutils} unit, a default
  2256. handler is installed which ioll show the exception object message, and the
  2257. address where the exception occurred, after which the program will exit with
  2258. a \var{Halt} instruction.
  2259. \section{Using functions and procedures}
  2260. \fpc supports the use of functions and procedures, but with some extras:
  2261. Function overloading is supported, as well as \var{Const} parameters and
  2262. open arrays.
  2263. {\em remark:} In the subsequent paragraph the word \var{procedure} and
  2264. \var{function} will be used interchangeably. The statements made are
  2265. valid for both.
  2266. \subsection{Function overloading}
  2267. Function overloading simply means that you can define the same function more
  2268. than once, but each time with a different set of arguments.
  2269. When the compiler encounters a function call, it will look at the function
  2270. parameters to decide which od the defined function
  2271. This can be useful if you want to define the same function for different
  2272. types. For example, if the RTL, the \var{Dec} procedure is
  2273. is defined as:
  2274. \begin{listing}
  2275. ...
  2276. Dec(Var I : Longint;decrement : Longint);
  2277. Dec(Var I : Longint);
  2278. Dec(Var I : Byte;decrement : Longint);
  2279. Dec(Var I : Byte);
  2280. ...
  2281. \end{listing}
  2282. When the compiler encounters a call to the dec function, it wil first search
  2283. which function it should use. It therefore checks the parameters in your
  2284. function call, and looks if there is a function definition which maches the
  2285. specified parameter list. If the compiler finds such a function, a call is
  2286. inserted to that function. If no such function is found, a compiler error is
  2287. generated.
  2288. \subsection{\var{Const} parameters}
  2289. In addition to \var{var} parameters and normal parameters (call by value,
  2290. call by reference), \fpc also supports \var{Const} parameters. You can
  2291. specify a \var{Const} parameter as follows:
  2292. \begin{listing}
  2293. Function Name (Const S: Type_Of_S) : ResultType
  2294. \end{listing}
  2295. A constant argument is passed by refenence
  2296. (i.e. the function or procedure receives a pointer to the passed ,
  2297. but you are not allowed to assign to it, this will result in a compiler error.
  2298. The main use for this is reducing the stack size, hence improving
  2299. performance.
  2300. \subsection{Open array parameters}
  2301. \fpc supports the passing of open arrays, i.e. You can declare a procedure
  2302. with an array of unspecified length as a parameter, as in Delphi.
  2303. The prototype declaration for open array parameters is:
  2304. \begin{listing}
  2305. Function Func ( ... [Var|Const] Ident : Array of Type ...) : ReturnType;
  2306. ProcedureFunction Func (... [Var|Const] Ident : Array of Type ...);
  2307. \end{listing}
  2308. The \var{[Var|Const]} means that open parameters can be passed by reference
  2309. or as a constant parameter.
  2310. In a function or procedure, you can pass open arrays only to functions which
  2311. are also declared with open arrays as parameters, {\em not} to functions or
  2312. procedures which accept arrays of fixed length.
  2313. \section{Using assembler in your code}
  2314. \fpc supports the use of assembler in your code, but not inline
  2315. assembler macros. To have more information on the processor
  2316. specific assembler syntax and its limitations, see the \progref.
  2317. \subsection{ Assembler statements }
  2318. The following is an example of assembler inclusion in your code.
  2319. \begin{listing}
  2320. ...
  2321. Statements;
  2322. ...
  2323. Asm
  2324. your asm code here
  2325. ...
  2326. end;
  2327. ...
  2328. Statements;
  2329. \end{listing}
  2330. The assembler instructions between the \var{Asm} and \var{end} keywords will
  2331. be inserted in the assembler generated by the compiler.
  2332. You can still use conditionals in your assembler, the compiler will
  2333. recognise it, and treat it as any other conditionals.
  2334. \emph{ Remark: } Before version 0.99.1, \fpc did not support
  2335. reference to variables by their names in the assembler parts of your code.
  2336. \subsection{ Assembler procedures and functions }
  2337. Assembler procedures and functions are declared using the
  2338. \var{Assembler} directive. The \var{Assembler} keyword is supported
  2339. as of version 0.9.7. This permits the code generator to make a number
  2340. of code generation optimizations.
  2341. The code generator does not generate any stack frame (entry and exit
  2342. code for the routine) if it contains no local variables and no
  2343. parameters. In the case of functions, ordinal values must be returned
  2344. in the accumulator. In the case of floating point values, these depend
  2345. on the target processor and emulation options.
  2346. \emph{ Remark: } Before version 0.99.1, \fpc did not support
  2347. reference to variables by their names in the assembler parts of your code.
  2348. \emph{ Remark: } From version 0.99.1 to 0.99.5 (\emph{excluding}
  2349. FPC 0.99.5a), the \var{Assembler} directive did not have the
  2350. same effect as in Turbo Pascal, so beware! The stack frame would be
  2351. omitted if there were no local variables, in this case if the assembly
  2352. routine had any parameters, they would be referenced directly via the stack
  2353. pointer. This was \emph{ NOT} like Turbo Pascal where the stack frame is only
  2354. omitted if there are no parameters \emph{ and } no local variables. As
  2355. stated earlier, starting from version 0.99.5a, \fpc now has the same
  2356. behaviour as Turbo Pascal.
  2357. \section{Modifiers}
  2358. \fpc doesn't support all Turbo Pascal modifiers, but
  2359. does support a number of additional modifiers. They are used mainly for assembler and
  2360. reference to C object files.
  2361. \subsection{Public}
  2362. The \var{Public} keyword is used to declare a function globally in a unit.
  2363. This is useful if you don't want a function to be accessible from the unit
  2364. file, but you do want the function to be accessible from the object file.
  2365. as an example:
  2366. \begin{listing}
  2367. Unit someunit;
  2368. interface
  2369. Function First : Real;
  2370. Implementation
  2371. Function First : Real;
  2372. begin
  2373. First:=0;
  2374. end;
  2375. Function Second : Real; [Public];
  2376. begin
  2377. Second:=1;
  2378. end;
  2379. end.
  2380. \end{listing}
  2381. If another program or unit uses this unit, it will not be able to use the
  2382. function \var{Second}, since it isn't declared in the interface part.
  2383. However, it will be possible to access the function \var{Second} at the
  2384. assembly-language level, by using it's mangled name (\progref).
  2385. \subsection{cdecl}
  2386. \label{se:cdecl}
  2387. The \var{cdecl} modifier can be used to declare a function that uses a C
  2388. type calling convention. This must be used if you wish to acces functions in
  2389. an object file generated by a C compiler. It allows you to use the function in
  2390. your code, and at linking time, you must link the object file containing the
  2391. \var{C} implementation of the function or procedure.
  2392. As an example:
  2393. \begin{listing}
  2394. program CmodDemo;
  2395. {$LINKLIB c}
  2396. Const P : PChar = 'This is fun !';
  2397. Function strlen (P : PChar) : Longint; cdecl; external;
  2398. begin
  2399. Writeln ('Length of (',p,') : ',strlen(p))
  2400. end.
  2401. \end{listing}
  2402. When compiling this, and linking to the C-library, you will be able to call
  2403. the \var{strlen} function throughout your program. The \var{external}
  2404. directive tells the compiler that the function resides in an external
  2405. object filebrary (see \ref{se:external}).
  2406. {\em Remark} The parameters in our declaration of the \var{C} function should
  2407. match exactly the ones in the declaration in \var{C}. Since \var{C} is case
  2408. sensitive, this means also that the name of the
  2409. function must be exactly the same. the \fpc compiler will use the name {\em
  2410. exactly} as it is typed in the declaration.
  2411. \subsection{popstack}
  2412. \label{se:popstack}
  2413. Popstack does the same as \var{cdecl}, namely it tells the \fpc compiler
  2414. that a function uses the C calling convention. In difference with the
  2415. \var{cdecl} modifier, it still mangles the name of the function as it would
  2416. for a normal pascal function.
  2417. With \var{popstack} you could access functions by their pascal names in a
  2418. library.
  2419. \subsection{external}
  2420. \label{se:external}
  2421. The \var{external} modifier can be used to declare a function that resides in
  2422. an external object file. It allows you to use the function in
  2423. your code, and at linking time, you must link the object file containing the
  2424. implementation of the function or procedure.
  2425. It replaces, in effect, the function or procedure code block. As such, it
  2426. can be present only in an implementation block of a unit, or in a program.
  2427. As an example:
  2428. \begin{listing}
  2429. program CmodDemo;
  2430. {$Linklib c}
  2431. Const P : PChar = 'This is fun !';
  2432. Function strlen (P : PChar) : Longint; cdecl; external;
  2433. begin
  2434. Writeln ('Length of (',p,') : ',strlen(p))
  2435. end.
  2436. \end{listing}
  2437. {\em Remark} The parameters in our declaration of the \var{external} function
  2438. should match exactly the ones in the declaration in the object file.
  2439. Since \var{C} is case sensitive, this means also that the name of the
  2440. function must be exactly the same.
  2441. The \var{external} modifier has also an extended syntax:
  2442. \begin{enumerate}
  2443. \item The first form specifies the name of the library
  2444. \begin{listing}
  2445. external 'lname';
  2446. \end{listing}
  2447. Tells the compiler that the function resides in library 'lname'. The
  2448. compiler will the automatically link this library to your program.
  2449. \item The second form specifies the library name and the name of the
  2450. function:
  2451. \begin{listing}
  2452. external 'lname' name Fname;
  2453. \end{listing}
  2454. Tells the compiler that the function resides in library 'lname', but with
  2455. name 'Fname'. The compiler will the automatically link this library to your
  2456. program, and use the correct name for the function.
  2457. \item \windows and \ostwo only:
  2458. \begin{listing}
  2459. external 'lname' Index Ind;
  2460. \end{listing}
  2461. Tells the compiler that the function resides in library 'lname', but with
  2462. indexname \var{Ind}. The compiler will the automatically link this library to your
  2463. program, and use the correct index for the function.
  2464. \end{enumerate}
  2465. \subsection{Export}
  2466. Sometimes you must provide a callback function for a C library, or you want
  2467. your routines to be callable from a C program. Since \fpc and C use
  2468. different calling schemes for functions and procedures\footnote{More
  2469. techically: In C the calling procedure must clear the stack. In \fpc, the
  2470. subroutine clears the stack.}, the compiler must be told to generate code
  2471. that can be called from a C routine. This is where the \var{Export} modifier
  2472. comes in. Contrary to the other modifiers, it must be specified separately,
  2473. as follows:
  2474. \begin{listing}
  2475. function DoSquare (X : Longint) : Longint; export;
  2476. begin
  2477. ...
  2478. end;
  2479. \end{listing}
  2480. The square brackets around the modifier are not allowed in this case.
  2481. {\em Remark:} You cannot call an exported function from within \fpc programs.
  2482. If you try to do so, the compiler will complain when compiling your source
  2483. code.
  2484. If you do want to call an exported procedure from pascal, you must use a
  2485. dummy function:
  2486. \begin{listing}
  2487. Procedure RealDoSomething;
  2488. begin
  2489. ...
  2490. end;
  2491. Procedure DoSomething; export;
  2492. begin
  2493. RealDoSomething;
  2494. end;
  2495. \end{listing}
  2496. In this example, from your \fpc code, you can call the \var{RealDoSomething}
  2497. procedure. If someone wants to link to your code from a C program, he can
  2498. call the \var{DoSomething} procedure. Both calls will have the same effect.
  2499. {\em Remark:}
  2500. as of version 0.9.8, \fpc supports the Delphi \var{cdecl} modifier.
  2501. This modifier works in the same way as the \var{export} modifier.
  2502. More information about these modifiers can be found in the \progref, in the
  2503. section on the calling mechanism and the chapter on linking.
  2504. \subsection{StdCall}
  2505. As of version 0.9.8, \fpc supports the Delphi \var{stdcall} modifier.
  2506. This modifier does actually nothing, since the \fpc compiler by default
  2507. pushes parameters from right to left on the stack, which is what the
  2508. modifier does under Delphi (which pushes parameters on the stack from left to
  2509. right).
  2510. More information about this modifier can be found in the \progref, in the
  2511. section on the calling mechanism and the chapter on linking.
  2512. \subsection{Alias}
  2513. The \var{Alias} modifier allows you to specify a different name for a
  2514. procedure or function. This is mostly useful for referring to this procedure
  2515. from assembly language constructs. As an example, consider the following
  2516. program:
  2517. \begin{listing}
  2518. Program Aliases;
  2519. Procedure Printit; [Alias : 'DOIT'];
  2520. begin
  2521. Writeln ('In Printit (alias : "DOIT")');
  2522. end;
  2523. begin
  2524. asm
  2525. call DOIT
  2526. end;
  2527. end.
  2528. \end{listing}
  2529. {\rm Remark:} the specified alias is inserted straight into the assembly
  2530. code, thus it is case sensitive.
  2531. The \var{Alias} modifier, combined with the \var{Public} modifier, make a
  2532. powerful tool for making externally accessible object files.
  2533. \subsection{[RegisterList]}
  2534. This modifier list is used to indicate the registers that are modified by an
  2535. assembler block in your code. The compiler stores certain results in the
  2536. registers. If you modify the registers in an assembly block, the compiler
  2537. should, sometimes, be told about it.
  2538. The prototype syntax of the \var{Registerlist} modifier is:
  2539. \begin{listing}
  2540. asm
  2541. statements
  2542. end; ['register1','register2',...,'registern'];
  2543. \end{listing}
  2544. Where is register one of any of the available processor registers.
  2545. \subsection{Unsupported Turbo Pascal modifiers}
  2546. The modifiers that exist in Turbo pascal, but aren't supported by \fpc, are
  2547. listed in \seet{Modifs}.
  2548. \begin{FPCltable}{lr}{Unsupported modifiers}{Modifs}
  2549. Modifier & Why not supported ? \\ \hline
  2550. Near & \fpc is a 32-bit compiler.\\
  2551. Far & \fpc is a 32-bit compiler. \\
  2552. %External & Replaced by \var{C} modifier. \\ \hline
  2553. \end{FPCltable}
  2554. %
  2555. % System unit reference guide.
  2556. %
  2557. \end{document}
  2558. \chapter{Reference : The system unit}
  2559. \label{ch:refchapter}
  2560. The system unit contains the standard supported functions of \fpc. It is the
  2561. same for all platforms. Basically it is the same as the system unit provided
  2562. with Borland or Turbo Pascal.
  2563. Functions are listed in alphabetical order.
  2564. Arguments to functions or procedures that are optional are put between
  2565. square brackets.
  2566. The pre-defined constants and variables are listed in the first section. The
  2567. second section contains the supported functions and procedures.
  2568. \section{Types, Constants and Variables}
  2569. \subsection{Types}
  2570. The following integer types are defined in the System unit:
  2571. \begin{listing}
  2572. shortint = -128..127;
  2573. Longint = $80000000..$7fffffff;
  2574. integer = -32768..32767;
  2575. byte = 0..255;
  2576. word = 0..65535;
  2577. \end{listing}
  2578. And the following pointer types:
  2579. \begin{listing}
  2580. PChar = ^char;
  2581. pPChar = ^PChar;
  2582. \end{listing}
  2583. For the \seef{SetJmp} and \seep{LongJmp} calls, the following jump bufer
  2584. type is defined (for the I386 processor):
  2585. \begin{listing}
  2586. jmp_buf = record
  2587. ebx,esi,edi : Longint;
  2588. bp,sp,pc : Pointer;
  2589. end;
  2590. PJmp_buf = ^jmp_buf;
  2591. \end{listing}
  2592. \subsection{Constants}
  2593. The following constants for file-handling are defined in the system unit:
  2594. \begin{listing}
  2595. Const
  2596. fmclosed = $D7B0;
  2597. fminput = $D7B1;
  2598. fmoutput = $D7B2;
  2599. fminout = $D7B3;
  2600. fmappend = $D7B4;
  2601. filemode : byte = 2;
  2602. \end{listing}
  2603. Further, the following non processor specific general-purpose constants
  2604. are also defined:
  2605. \begin{listing}
  2606. const
  2607. erroraddr : pointer = nil;
  2608. errorcode : word = 0;
  2609. { max level in dumping on error }
  2610. max_frame_dump : word = 20;
  2611. \end{listing}
  2612. \emph{ Remark: } Processor specific global constants are named Testxxxx
  2613. where xxxx represents the processor number (such as Test8086, Test68000),
  2614. and are used to determine on what generation of processor the program
  2615. is running on.
  2616. \subsection{Variables}
  2617. The following variables are defined and initialized in the system unit:
  2618. \begin{listing}
  2619. var
  2620. output,input,stderr : text;
  2621. exitproc : pointer;
  2622. exitcode : word;
  2623. stackbottom : Longint;
  2624. loweststack : Longint;
  2625. \end{listing}
  2626. The variables \var{ExitProc}, \var{exitcode} are used in the \fpc exit
  2627. scheme. It works similarly to the on in Turbo Pascal:
  2628. When a program halts (be it through the call of the \var{Halt} function or
  2629. \var{Exit} or through a run-time error), the exit mechanism checks the value
  2630. of \var{ExitProc}. If this one is non-\var{Nil}, it is set to \var{Nil}, and
  2631. the procedure is called. If the exit procedure exits, the value of ExitProc
  2632. is checked again. If it is non-\var{Nil} then the above steps are repeated.
  2633. So if you want to install your exit procedure, you should save the old value
  2634. of \var{ExitProc} (may be non-\var{Nil}, since other units could have set it before
  2635. you did). In your exit procedure you then restore the value of
  2636. \var{ExitProc}, such that if it was non-\var{Nil} the exit-procedure can be
  2637. called.
  2638. The \var{ErrorAddr} and \var{ExitCode} can be used to check for
  2639. error-conditions. If \var{ErrorAddr} is non-\var{Nil}, a run-time error has
  2640. occurred. If so, \var{ExitCode} contains the error code. If \var{ErrorAddr} is
  2641. \var{Nil}, then {ExitCode} contains the argument to \var{Halt} or 0 if the
  2642. program terminated normally.
  2643. \var{ExitCode} is always passed to the operating system as the exit-code of
  2644. your process.
  2645. Under \file{GO32}, the following constants are also defined :
  2646. \begin{listing}
  2647. const
  2648. seg0040 = $0040;
  2649. segA000 = $A000;
  2650. segB000 = $B000;
  2651. segB800 = $B800;
  2652. \end{listing}
  2653. These constants allow easy access to the bios/screen segment via mem/absolute.
  2654. \section{Functions and Procedures}
  2655. \function{Abs}{(X : Every numerical type)}{Every numerical type}
  2656. {\var{Abs} returns the absolute value of a variable. The result of the
  2657. function has the same type as its argument, which can be any numerical
  2658. type.}
  2659. {None.}
  2660. {\seef{Round}}
  2661. \latex{\inputlisting{refex/ex1.pp}}
  2662. \html{\input{refex/ex1.tex}}
  2663. \function{Addr}{(X : Any type)}{Pointer}
  2664. {\var{Addr} returns a pointer to its argument, which can be any type, or a
  2665. function or procedure name. The returned pointer isn't typed.
  2666. The same result can be obtained by the \var{@} operator, which can return a
  2667. typed pointer (\progref). }
  2668. {None}
  2669. {\seef{SizeOf}}
  2670. \latex{\inputlisting{refex/ex2.pp}}
  2671. \html{\input{refex/ex2.tex}}
  2672. \procedure{Append}{(Var F : Text)}
  2673. {\var{Append} opens an existing file in append mode. Any data written to
  2674. \var{F} will be appended to the file. If the file didn't exist, it will be
  2675. created, contrary to the Turbo Pascal implementation of \var{Append}, where
  2676. a file needed to exist in order to be opened by
  2677. append.
  2678. Only text files can be opened in append mode.
  2679. }
  2680. {If the file can't be created, a run-time error will be generated.}
  2681. {\seep{Rewrite},\seep{Append}, \seep{Reset}}
  2682. \latex{\inputlisting{refex/ex3.pp}}
  2683. \html{\input{refex/ex3.tex}}
  2684. \function{Arctan}{(X : Real)}{Real}
  2685. {\var{Arctan} returns the Arctangent of \var{X}, which can be any Real type.
  2686. The resulting angle is in radial units.}{None}{\seef{Sin}, \seef{Cos}}
  2687. \latex{\inputlisting{refex/ex4.pp}}
  2688. \html{\input{refex/ex4.tex}}
  2689. \procedure{Assign}{(Var F; Name : String)}
  2690. {\var{Assign} assigns a name to \var{F}, which can be any file type.
  2691. This call doesn't open the file, it just assigns a name to a file variable,
  2692. and marks the file as closed.}
  2693. {None.}
  2694. {\seep{Reset}, \seep{Rewrite}, \seep{Append}}
  2695. \latex{\inputlisting{refex/ex5.pp}}
  2696. \html{\input{refex/ex5.tex}}
  2697. \procedure{Blockread}{(Var F : File; Var Buffer; Var Count : Longint [; var
  2698. Result : Longint])}
  2699. {\var{Blockread} reads \var{count} or less records from file \var{F}. The
  2700. result is placed in \var{Buffer}, which must contain enough room for
  2701. \var{Count} records. The function cannot read partial records.
  2702. If \var{Result} is specified, it contains the number of records actually
  2703. read. If \var{Result} isn't specified, and less than \var{Count} records were
  2704. read, a run-time error is generated. This behavior can be controlled by the
  2705. \var{\{\$i\}} switch. }
  2706. {If \var{Result} isn't specified, then a run-time error is generated if less
  2707. than \var{count} records were read.}
  2708. {\seep{Blockwrite}, \seep{Close}, \seep{Reset}, \seep{Assign}}
  2709. \latex{\inputlisting{refex/ex6.pp}}
  2710. \html{\input{refex/ex6.tex}}
  2711. \procedure{Blockwrite}{(Var F : File; Var Buffer; Var Count : Longint)}
  2712. {\var{BlockWrite} writes \var{count} records from \var{buffer} to the file
  2713. \var{F}.
  2714. If the records couldn't be written to disk, a run-time error is generated.
  2715. This behavior can be controlled by the \var{\{\$i\}} switch.
  2716. }
  2717. {A run-time error is generated if, for some reason, the records couldn't be
  2718. written to disk.}
  2719. {\seep{Blockread},\seep{Close}, \seep{Rewrite}, \seep{Assign}}
  2720. For the example, see \seep{Blockread}.
  2721. \procedure{Chdir}{(const S : string)}
  2722. {\var{Chdir} changes the working directory of the process to \var{S}.}
  2723. {If the directory \var{S} doesn't exist, a run-time error is generated.}
  2724. {\seep{Mkdir}, \seep{Rmdir}}
  2725. \latex{\inputlisting{refex/ex7.pp}}
  2726. \html{\input{refex/ex7.tex}}
  2727. \function{Chr}{(X : byte)}{Char}
  2728. {\var{Chr} returns the character which has ASCII value \var{X}.}
  2729. {None.}
  2730. {\seef{Ord},\seep{Str}}
  2731. \latex{\inputlisting{refex/ex8.pp}}
  2732. \html{\input{refex/ex8.tex}}
  2733. \procedure{Close}{(Var F : Anyfiletype)}
  2734. {\var{Close} flushes the buffer of the file \var{F} and closes \var{F}.
  2735. After a call to \var{Close}, data can no longer be read from or written to
  2736. \var{F}.
  2737. To reopen a file closed with \var{Close}, it isn't necessary to assign the
  2738. file again. A call to \seep{Reset} or \seep{Rewrite} is sufficient.}
  2739. {None.}{\seep{Assign}, \seep{Reset}, \seep{Rewrite}}
  2740. \latex{\inputlisting{refex/ex9.pp}}
  2741. \html{\input{refex/ex9.tex}}
  2742. \function{Concat}{(S1,S2 [,S3, ... ,Sn])}{String}
  2743. {\var{Concat} concatenates the strings \var{S1},\var{S2} etc. to one long
  2744. string. The resulting string is truncated at a length of 255 bytes.
  2745. The same operation can be performed with the \var{+} operation.}
  2746. {None.}
  2747. {\seef{Copy}, \seep{Delete}, \seep{Insert}, \seef{Pos}, \seef{Length}}
  2748. \latex{\inputlisting{refex/ex10.pp}}
  2749. \html{\input{refex/ex10.tex}}
  2750. \function{Copy}{(Const S : String;Index : Integer;Count : Byte)}{String}
  2751. {\var{Copy} returns a string which is a copy if the \var{Count} characters
  2752. in \var{S}, starting at position \var{Index}. If \var{Count} is larger than
  2753. the length of the string \var{S}, the result is truncated.
  2754. If \var{Index} is larger than the length of the string \var{S}, then an
  2755. empty string is returned.}
  2756. {None.}
  2757. {\seep{Delete}, \seep{Insert}, \seef{Pos}}
  2758. \latex{\inputlisting{refex/ex11.pp}}
  2759. \html{\input{refex/ex11.tex}}
  2760. \function{Cos}{(X : Real)}{Real}
  2761. {\var{Cos} returns the cosine of \var{X}, where X is an angle, in radians.}
  2762. {None.}
  2763. {\seef{Arctan}, \seef{Sin}}
  2764. \latex{\inputlisting{refex/ex12.pp}}
  2765. \html{\input{refex/ex12.tex}}
  2766. \Function{CSeg}{Word}
  2767. {\var{CSeg} returns the Code segment register. In \fpc, it returns always a
  2768. zero, since \fpc is a 32 bit compiler.}
  2769. {None.}
  2770. {\seef{DSeg}, \seef{Seg}, \seef{Ofs}, \seef{Ptr}}
  2771. \latex{\inputlisting{refex/ex13.pp}}
  2772. \html{\input{refex/ex13.tex}}
  2773. \procedure{Dec}{(Var X : Any ordinal type[; Decrement : Longint])}
  2774. {\var{Dec} decreases the value of \var{X} with \var{Decrement}.
  2775. If \var{Decrement} isn't specified, then 1 is taken as a default.}
  2776. {A range check can occur, or an underflow error, if you try to decrease \var{X}
  2777. below its minimum value.}
  2778. {\seep{Inc}}
  2779. \latex{\inputlisting{refex/ex14.pp}}
  2780. \html{\input{refex/ex14.tex}}
  2781. \procedure{Delete}{(var S : string;Index : Integer;Count : Integer)}
  2782. {\var{Delete} removes \var{Count} characters from string \var{S}, starting
  2783. at position \var{Index}. All remaining characters are shifted \var{Count}
  2784. positions to the left, and the length of the string is adjusted.
  2785. }
  2786. {None.}
  2787. {\seef{Copy},\seef{Pos},\seep{Insert}}
  2788. \latex{\inputlisting{refex/ex15.pp}}
  2789. \html{\input{refex/ex15.tex}}
  2790. \procedure{Dispose}{(P : pointer)}
  2791. {\var{Dispose} releases the memory allocated with a call to \seep{New}.
  2792. The pointer \var{P} must be typed. The released memory is returned to the
  2793. heap.}
  2794. {An error will occur if the pointer doesn't point to a location in the
  2795. heap.}
  2796. {\seep{New}, \seep{Getmem}, \seep{Freemem}}
  2797. \latex{\inputlisting{refex/ex16.pp}}
  2798. \html{\input{refex/ex16.tex}}
  2799. \Function{DSeg}{Word}
  2800. {\var{DSeg} returns the data segment register. In \fpc, it returns always a
  2801. zero, since \fpc is a 32 bit compiler.}
  2802. {None.}
  2803. {\seef{CSeg}, \seef{Seg}, \seef{Ofs}, \seef{Ptr}}
  2804. \latex{\inputlisting{refex/ex17.pp}}
  2805. \html{\input{refex/ex17.tex}}
  2806. \function{Eof}{[(F : Any file type)]}{Boolean}
  2807. {\var{Eof} returns \var{True} if the file-pointer has reached the end of the
  2808. file, or if the file is empty. In all other cases \var{Eof} returns
  2809. \var{False}.
  2810. If no file \var{F} is specified, standard input is assumed.}
  2811. {None.}
  2812. {\seef{Eoln}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}}
  2813. \latex{\inputlisting{refex/ex18.pp}}
  2814. \html{\input{refex/ex18.tex}}
  2815. \function{Eoln}{[(F : Text)]}{Boolean}
  2816. {\var{Eof} returns \var{True} if the file pointer has reached the end of a
  2817. line, which is demarcated by a line-feed character (ASCII value 10), or if
  2818. the end of the file is reached.
  2819. In all other cases \var{Eof} returns \var{False}.
  2820. If no file \var{F} is specified, standard input is assumed.
  2821. It can only be used on files of type \var{Text}.}
  2822. {None.}
  2823. {\seef{Eof}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}}
  2824. \latex{\inputlisting{refex/ex19.pp}}
  2825. \html{\input{refex/ex19.tex}}
  2826. \procedure{Erase}{(Var F : Any file type)}
  2827. {\var{Erase} removes an unopened file from disk. The file should be
  2828. assigned with \var{Assign}, but not opened with \var{Reset} or \var{Rewrite}}
  2829. {A run-time error will be generated if the specified file doesn't exist.}
  2830. {\seep{Assign}}
  2831. \latex{\inputlisting{refex/ex20.pp}}
  2832. \html{\input{refex/ex20.tex}}
  2833. \procedure{Exit}{([Var X : return type )]}
  2834. {\var{Exit} exits the current subroutine, and returns control to the calling
  2835. routine. If invoked in the main program routine, exit stops the program.
  2836. The optional argument \var{X} allows to specify a return value, in the case
  2837. \var{Exit} is invoked in a function. The function result will then be
  2838. equal to \var{X}.}
  2839. {None.}
  2840. {\seep{Halt}}
  2841. \latex{\inputlisting{refex/ex21.pp}}
  2842. \html{\input{refex/ex21.tex}}
  2843. \function{Exp}{(Var X : Real)}{Real}
  2844. {\var{Exp} returns the exponent of \var{X}, i.e. the number \var{e} to the
  2845. power \var{X}.}
  2846. {None.}{\seef{Ln}, \seef{Power}}
  2847. \latex{\inputlisting{refex/ex22.pp}}
  2848. \html{\input{refex/ex22.tex}}
  2849. \function{Filepos}{(Var F : Any file type)}{Longint}
  2850. {\var{Filepos} returns the current record position of the file-pointer in file
  2851. \var{F}. It cannot be invoked with a file of type \var{Text}.}
  2852. {None.}
  2853. {\seef{Filesize}}
  2854. \latex{\inputlisting{refex/ex23.pp}}
  2855. \html{\input{refex/ex23.tex}}
  2856. \function{Filesize}{(Var F : Any file type)}{Longint}
  2857. {\var{Filepos} returns the total number of records in file \var{F}.
  2858. It cannot be invoked with a file of type \var{Text}. (under \linux, this
  2859. also means that it cannot be invoked on pipes.)
  2860. If \var{F} is empty, 0 is returned.
  2861. }
  2862. {None.}
  2863. {\seef{Filepos}}
  2864. \latex{\inputlisting{refex/ex24.pp}}
  2865. \html{\input{refex/ex24.tex}}
  2866. \procedure{Fillchar}{(Var X;Count : Longint;Value : char or byte);}
  2867. {\var{Fillchar} fills the memory starting at \var{X} with \var{Count} bytes
  2868. or characters with value equal to \var{Value}.
  2869. }
  2870. {No checking on the size of \var{X} is done.}
  2871. {\seep{Fillword}, \seep{Move}}
  2872. \latex{\inputlisting{refex/ex25.pp}}
  2873. \html{\input{refex/ex25.tex}}
  2874. \procedure{Fillword}{(Var X;Count : Longint;Value : Word);}
  2875. {\var{Fillword} fills the memory starting at \var{X} with \var{Count} words
  2876. with value equal to \var{Value}.
  2877. }
  2878. {No checking on the size of \var{X} is done.}
  2879. {\seep{Fillword}, \seep{Move}}
  2880. \latex{\inputlisting{refex/ex76.pp}}
  2881. \html{\input{refex/ex76.tex}}
  2882. \procedure{Flush}{(Var F : Text)}
  2883. {\var{Flush} empties the internal buffer of file \var{F} and writes the
  2884. contents to disk. The file is \textit{not} closed as a result of this call.}
  2885. {If the disk is full, a run-time error will be generated.}
  2886. {\seep{Close}}
  2887. \latex{\inputlisting{refex/ex26.pp}}
  2888. \html{\input{refex/ex26.tex}}
  2889. \function{Frac}{(X : Real)}{Real}
  2890. {\var{Frac} returns the non-integer part of \var{X}.}
  2891. {None.}
  2892. {\seef{Round}, \seef{Int}}
  2893. \latex{\inputlisting{refex/ex27.pp}}
  2894. \html{\input{refex/ex27.tex}}
  2895. \procedure{Freemem}{(Var P : pointer; Count : Longint)}
  2896. {\var{Freemem} releases the memory occupied by the pointer \var{P}, of size
  2897. \var{Count}, and returns it to the heap. \var{P} should point to the memory
  2898. allocated to a dynamical variable.}
  2899. {An error will occur when \var{P} doesn't point to the heap.}
  2900. {\seep{Getmem}, \seep{New}, \seep{Dispose}}
  2901. \latex{\inputlisting{refex/ex28.pp}}
  2902. \html{\input{refex/ex28.tex}}
  2903. \procedure{Getdir}{(drivenr : byte;var dir : string)}
  2904. {\var{Getdir} returns in \var{dir} the current directory on the drive
  2905. \var{drivenr}, where {drivenr} is 1 for the first floppy drive, 3 for the
  2906. first hard disk etc. A value of 0 returns the directory on the current disk.
  2907. On \linux, \var{drivenr} is ignored, as there is only one directory tree.}
  2908. {An error is returned under \dos, if the drive requested isn't ready.}
  2909. {\seep{Chdir}}
  2910. \latex{\inputlisting{refex/ex29.pp}}
  2911. \html{\input{refex/ex29.tex}}
  2912. \procedure{Getmem}{(var p : pointer;size : Longint)}
  2913. {\var{Getmem} reserves \var{Size} bytes memory on the heap, and returns a
  2914. pointer to this memory in \var{p}. If no more memory is available, nil is
  2915. returned.}
  2916. {None.}
  2917. {\seep{Freemem}, \seep{Dispose}, \seep{New}}
  2918. For an example, see \seep{Freemem}.
  2919. \procedure{Halt}{[(Errnum : byte]}
  2920. {\var{Halt} stops program execution and returns control to the calling
  2921. program. The optional argument \var{Errnum} specifies an exit value. If
  2922. omitted, zero is returned.}
  2923. {None.}
  2924. {\seep{Exit}}
  2925. \latex{\inputlisting{refex/ex30.pp}}
  2926. \html{\input{refex/ex30.tex}}
  2927. \function{Hi}{(X : Ordinal type)}{Word or byte}
  2928. {\var{Hi} returns the high byte or word from \var{X}, depending on the size
  2929. of X. If the size of X is 4, then the high word is returned. If the size is
  2930. 2 then the high byte is retuned.
  2931. \var{hi} cannot be invoked on types of size 1, such as byte or char.}
  2932. {None}
  2933. {\seef{Lo}}
  2934. \latex{\inputlisting{refex/ex31.pp}}
  2935. \html{\input{refex/ex31.tex}}
  2936. \procedure{Inc}{(Var X : Any ordinal type[; Increment : Longint])}
  2937. {\var{Inc} increases the value of \var{X} with \var{Increment}.
  2938. If \var{Increment} isn't specified, then 1 is taken as a default.}
  2939. {A range check can occur, or an overflow error, if you try to increase \var{X}
  2940. over its maximum value.}
  2941. {\seep{Dec}}
  2942. \latex{\inputlisting{refex/ex32.pp}}
  2943. \html{\input{refex/ex32.tex}}
  2944. \procedure{Insert}{(Var Source : String;var S : String;Index : integer)}
  2945. {\var{Insert} inserts string \var{S} in string \var{Source}, at position
  2946. \var{Index}, shifting all characters after \var{Index} to the right. The
  2947. resulting string is truncated at 255 characters, if needed.}
  2948. {None.}
  2949. {\seep{Delete}, \seef{Copy}, \seef{Pos}}
  2950. \latex{\inputlisting{refex/ex33.pp}}
  2951. \html{\input{refex/ex33.tex}}
  2952. \function{Int}{(X : Real)}{Real}
  2953. {\var{Int} returns the integer part of any Real \var{X}, as a Real.}
  2954. {None.}
  2955. {\seef{Frac}, \seef{Round}}
  2956. \latex{\inputlisting{refex/ex34.pp}}
  2957. \html{\input{refex/ex34.tex}}
  2958. \Function{IOresult}{Word}
  2959. {IOresult contains the result of any input/output call, when the
  2960. \var{\{\$i-\}} compiler directive is active, and IO checking is disabled. When the
  2961. flag is read, it is reset to zero.
  2962. If \var{IOresult} is zero, the operation completed successfully. If
  2963. non-zero, an error occurred. The following errors can occur:
  2964. \dos errors :
  2965. \begin{description}
  2966. \item [2\ ] File not found.
  2967. \item [3\ ] Path not found.
  2968. \item [4\ ] Too many open files.
  2969. \item [5\ ] Access denied.
  2970. \item [6\ ] Invalid file handle.
  2971. \item [12\ ] Invalid file-access mode.
  2972. \item [15\ ] Invalid disk number.
  2973. \item [16\ ] Cannot remove current directory.
  2974. \item [17\ ] Cannot rename across volumes.
  2975. \end{description}
  2976. I/O errors :
  2977. \begin{description}
  2978. \item [100\ ] Error when reading from disk.
  2979. \item [101\ ] Error when writing to disk.
  2980. \item [102\ ] File not assigned.
  2981. \item [103\ ] File not open.
  2982. \item [104\ ] File not opened for input.
  2983. \item [105\ ] File not opened for output.
  2984. \item [106\ ] Invalid number.
  2985. \end{description}
  2986. Fatal errors :
  2987. \begin{description}
  2988. \item [150\ ] Disk is write protected.
  2989. \item [151\ ] Unknown device.
  2990. \item [152\ ] Drive not ready.
  2991. \item [153\ ] Unknown command.
  2992. \item [154\ ] CRC check failed.
  2993. \item [155\ ] Invalid drive specified..
  2994. \item [156\ ] Seek error on disk.
  2995. \item [157\ ] Invalid media type.
  2996. \item [158\ ] Sector not found.
  2997. \item [159\ ] Printer out of paper.
  2998. \item [160\ ] Error when writing to device.
  2999. \item [161\ ] Error when reading from device.
  3000. \item [162\ ] Hardware failure.
  3001. \end{description}
  3002. }
  3003. {None.}
  3004. {All I/O functions.}
  3005. \latex{\inputlisting{refex/ex35.pp}}
  3006. \html{\input{refex/ex35.tex}}
  3007. \function{Length}{(S : String)}{Byte}
  3008. {\var{Length} returns the length of the string \var{S},
  3009. which is limited to 255. If the strings \var{S} is empty, 0 is returned.
  3010. {\em Note:} The length of the string \var{S} is stored in \var{S[0]}.
  3011. }
  3012. {None.}
  3013. {\seef{Pos}}
  3014. \latex{\inputlisting{refex/ex36.pp}}
  3015. \html{\input{refex/ex36.tex}}
  3016. \function{Ln}{(X : Real)}{Real}
  3017. {
  3018. \var{Ln} returns the natural logarithm of the Real parameter \var{X}.
  3019. \var{X} must be positive.
  3020. }
  3021. {An run-time error will occur when \var{X} is negative.}
  3022. {\seef{Exp}, \seef{Power}}
  3023. \latex{\inputlisting{refex/ex37.pp}}
  3024. \html{\input{refex/ex37.tex}}
  3025. \function{Lo}{(O : Word or Longint)}{Byte or Word}
  3026. {\var{Lo} returns the low byte of its argument if this is of type
  3027. \var{Integer} or
  3028. \var{Word}. It returns the low word of its argument if this is of type
  3029. \var{Longint} or \var{Cardinal}.}
  3030. {None.}
  3031. {\seef{Ord}, \seef{Chr}}
  3032. \latex{\inputlisting{refex/ex38.pp}}
  3033. \html{\input{refex/ex38.tex}}
  3034. \procedure{LongJmp}{(Var env : Jmp\_Buf; Value : Longint)}
  3035. {
  3036. \var{LongJmp} jumps to the adress in the \var{env} \var{jmp\_buf},
  3037. and resores the registers that were stored in it at the corresponding
  3038. \seef{SetJmp} call.
  3039. In effect, program flow will continue at the \var{SetJmp} call, which will
  3040. return \var{value} instead of 0. If you pas a \var{value} equal to zero, it will be
  3041. converted to 1 before passing it on. The call will not return, so it must be
  3042. used with extreme care.
  3043. This can be used for error recovery, for instance when a segmentation fault
  3044. occurred.}{None.}{\seef{SetJmp}}
  3045. For an example, see \seef{SetJmp}
  3046. \function{Lowercase}{(C : Char or String)}{Char or String}
  3047. {\var{Lowercase} returns the lowercase version of its argument \var{C}.
  3048. If its argument is a string, then the complete string is converted to
  3049. lowercase. The type of the returned value is the same as the type of the
  3050. argument.}
  3051. {None.}
  3052. {\seef{Upcase}}
  3053. \latex{\inputlisting{refex/ex73.pp}}
  3054. \html{\input{refex/ex73.tex}}
  3055. \procedure{Mark}{(Var P : Pointer)}
  3056. {\var{Mark} copies the current heap-pointer to \var{P}.}
  3057. {None.}
  3058. {\seep{Getmem}, \seep{Freemem}, \seep{New}, \seep{Dispose}, \seef{Maxavail}}
  3059. \latex{\inputlisting{refex/ex39.pp}}
  3060. \html{\input{refex/ex39.tex}}
  3061. \Function{Maxavail}{Longint}
  3062. {\var{Maxavail} returns the size, in bytes, of the biggest free memory block in
  3063. the heap.
  3064. {\em Remark:} The heap grows dynamically if more memory is needed than is
  3065. available.}
  3066. {None.}
  3067. {\seep{Release}, \seef{Memavail},\seep{Freemem}, \seep{Getmem}}
  3068. \latex{\inputlisting{refex/ex40.pp}}
  3069. \html{\input{refex/ex40.tex}}
  3070. \Function{Memavail}{Longint}
  3071. {\var{Memavail} returns the size, in bytes, of the free heap memory.
  3072. {\em Remark:} The heap grows dynamically if more memory is needed than is
  3073. available.}
  3074. {None.}
  3075. {\seef{Maxavail},\seep{Freemem}, \seep{Getmem}}
  3076. \latex{\inputlisting{refex/ex41.pp}}
  3077. \html{\input{refex/ex41.tex}}
  3078. \procedure{Mkdir}{(const S : string)}
  3079. {\var{Chdir} creates a new directory \var{S}.}
  3080. {If a parent-directory of directory \var{S} doesn't exist, a run-time error is generated.}
  3081. {\seep{Chdir}, \seep{Rmdir}}
  3082. For an example, see \seep{Rmdir}.
  3083. \procedure{Move}{(var Source,Dest;Count : Longint)}
  3084. {\var{Move} moves \var{Count} bytes from \var{Source} to \var{Dest}.}
  3085. {If either \var{Dest} or \var{Source} is outside the accessible memory for
  3086. the process, then a run-time error will be generated. With older versions of
  3087. the compiler, a segmentation-fault will occur. }
  3088. {\seep{Fillword}, \seep{Fillchar}}
  3089. \latex{\inputlisting{refex/ex42.pp}}
  3090. \html{\input{refex/ex42.tex}}
  3091. \procedure{New}{(Var P : Pointer[, Constructor])}
  3092. {\var{New} allocates a new instance of the type pointed to by \var{P}, and
  3093. puts the address in \var{P}.
  3094. If P is an object, then it is possible to
  3095. specify the name of the constructor with which the instance will be created.}
  3096. {If not enough memory is available, \var{Nil} will be returned.}
  3097. {\seep{Dispose}, \seep{Freemem}, \seep{Getmem}, \seef{Memavail},
  3098. \seef{Maxavail}}
  3099. For an example, see \seep{Dispose}.
  3100. \function{Odd}{(X : Longint)}{Boolean}
  3101. {\var{Odd} returns \var{True} if \var{X} is odd, or \var{False} otherwise.}
  3102. {None.}
  3103. {\seef{Abs}, \seef{Ord}}
  3104. \latex{\inputlisting{refex/ex43.pp}}
  3105. \html{\input{refex/ex43.tex}}
  3106. \function{Ofs}{Var X}{Longint}
  3107. {\var{Ofs} returns the offset of the address of a variable.
  3108. This function is only supported for compatibility. In \fpc, it
  3109. returns always the complete address of the variable, since \fpc is a 32 bit
  3110. compiler.
  3111. }
  3112. {None.}
  3113. {\seef{DSeg}, \seef{CSeg}, \seef{Seg}, \seef{Ptr}}
  3114. \latex{\inputlisting{refex/ex44.pp}}
  3115. \html{\input{refex/ex44.tex}}
  3116. \function{Ord}{(X : Ordinal type)}{Byte}
  3117. {\var{Ord} returns the Ordinal value of a ordinal-type variable \var{X}.}
  3118. {None.}
  3119. {\seef{Chr}}
  3120. \latex{\inputlisting{refex/ex45.pp}}
  3121. \html{\input{refex/ex45.tex}}
  3122. \Function{Paramcount}{Longint}
  3123. {\var{Paramcount} returns the number of command-line arguments. If no
  3124. arguments were given to the running program, \var{0} is returned.
  3125. }
  3126. {None.}
  3127. {\seef{Paramstr}}
  3128. \latex{\inputlisting{refex/ex46.pp}}
  3129. \html{\input{refex/ex46.tex}}
  3130. \function{Paramstr}{(L : Longint)}{String}
  3131. {\var{Paramstr} returns the \var{L}-th command-line argument. \var{L} must
  3132. be between \var{0} and \var{Paramcount}, these values included.
  3133. The zeroth argument is the name with which the program was started.
  3134. }
  3135. { In all cases, the command-line will be truncated to a length of 255,
  3136. even though the operating system may support bigger command-lines. If you
  3137. want to access the complete command-line, you must use the \var{argv} pointer
  3138. to access the Real values of the command-line parameters.}
  3139. {\seef{Paramcount}}
  3140. For an example, see \seef{Paramcount}.
  3141. \Function{Pi}{Real}
  3142. {\var{Pi} returns the value of Pi (3.1415926535897932385).}
  3143. {None.}
  3144. {\seef{Cos}, \seef{Sin}}
  3145. \latex{\inputlisting{refex/ex47.pp}}
  3146. \html{\input{refex/ex47.tex}}
  3147. \function{Pos}{(Const Substr : String;Const S : String)}{Byte}
  3148. {\var{Pos} returns the index of \var{Substr} in \var{S}, if \var{S} contains
  3149. \var{Substr}. In case \var{Substr} isn't found, \var{0} is returned.
  3150. The search is case-sensitive.
  3151. }
  3152. {None}
  3153. {\seef{Length}, \seef{Copy}, \seep{Delete}, \seep{Insert}}
  3154. \latex{\inputlisting{refex/ex48.pp}}
  3155. \html{\input{refex/ex48.tex}}
  3156. \function{Power}{(base,expon : Real)}{Real}
  3157. {
  3158. \var{Power} returns the value of \var{base} to the power \var{expon}.
  3159. \var{Base} and \var{expon} can be of type Longint, in which case the
  3160. result will also be a Longint.
  3161. The function actually returns \var{Exp(expon*Ln(base))}
  3162. }{None.}{\seef{Exp}, \seef{Ln}}
  3163. \latex{\inputlisting{refex/ex78.pp}}
  3164. \html{\input{refex/ex78.tex}}
  3165. \function{Ptr}{(Sel,Off : Longint)}{Pointer}
  3166. {
  3167. \var{Ptr} returns a pointer, pointing to the address specified by
  3168. segment \var{Sel} and offset \var{Off}.
  3169. {\em Remark 1:} In the 32-bit flat-memory model supported by \fpc, this
  3170. function is obsolete.
  3171. {\em Remark 2:} The returned address is simply the offset. If you recompile
  3172. the RTL with \var{-dDoMapping} defined, then the compiler returns the
  3173. following : \var{ptr:=pointer(\$e0000000+sel shl 4+off)} under \dos, or
  3174. \var{ptr:=pointer(sel shl 4+off)} on other OSes.
  3175. }
  3176. {None.}
  3177. {\seef{Addr}}
  3178. \latex{\inputlisting{refex/ex59.pp}}
  3179. \html{\input{refex/ex59.tex}}
  3180. \function{Random}{[(L : Longint)]}{Longint or Real}
  3181. {\var{Random} returns a random number larger or equal to \var{0} and
  3182. strictly less than \var{L}.
  3183. If the argument \var{L} is omitted, a Real number between 0 and 1 is returned.
  3184. (0 included, 1 excluded)}
  3185. {None.}
  3186. {\seep{Randomize}}
  3187. \latex{\inputlisting{refex/ex49.pp}}
  3188. \html{\input{refex/ex49.tex}}
  3189. \Procedure{Randomize}
  3190. {\var{Randomize} initializes the random number generator of \fpc, by giving
  3191. a value to \var{Randseed}, calculated with the system clock.
  3192. }
  3193. {None.}
  3194. {\seef{Random}}
  3195. For an example, see \seef{Random}.
  3196. \procedure{Read}{([Var F : Any file type], V1 [, V2, ... , Vn])}
  3197. {\var{Read} reads one or more values from a file \var{F}, and stores the
  3198. result in \var{V1}, \var{V2}, etc.; If no file \var{F} is specified, then
  3199. standard input is read.
  3200. If \var{F} is of type \var{Text}, then the variables \var{V1, V2} etc. must be
  3201. of type \var{Char}, \var{Integer}, \var{Real} or \var{String}.
  3202. If \var{F} is a typed file, then each of the variables must be of the type
  3203. specified in the declaration of \var{F}. Untyped files are not allowed as an
  3204. argument.}
  3205. {If no data is available, a run-time error is generated. This behavior can
  3206. be controlled with the \var{\{\$i\}} compiler switch.}
  3207. {\seep{Readln}, \seep{Blockread}, \seep{Write}, \seep{Blockwrite}}
  3208. \latex{\inputlisting{refex/ex50.pp}}
  3209. \html{\input{refex/ex50.tex}}
  3210. \procedure{Readln}{[Var F : Text], V1 [, V2, ... , Vn])}
  3211. {\var{Read} reads one or more values from a file \var{F}, and stores the
  3212. result in \var{V1}, \var{V2}, etc. After that it goes to the next line in
  3213. the file (defined by the \var{LineFeed (\#10)} character).
  3214. If no file \var{F} is specified, then standard input is read.
  3215. The variables \var{V1, V2} etc. must be of type \var{Char}, \var{Integer},
  3216. \var{Real}, \var{String} or \var{PChar}.
  3217. }
  3218. {If no data is available, a run-time error is generated. This behavior can
  3219. be controlled with the \var{\{\$i\}} compiler switch.}
  3220. {\seep{Read}, \seep{Blockread}, \seep{Write}, \seep{Blockwrite}}
  3221. For an example, see \seep{Read}.
  3222. \procedure{Release}{(Var P : pointer)}
  3223. {\var{Release} sets the top of the Heap to the location pointed to by
  3224. \var{P}. All memory at a location higher than \var{P} is marked empty.}
  3225. {A run-time error will be generated if \var{P} points to memory outside the
  3226. heap.}
  3227. {\seep{Mark}, \seef{Memavail}, \seef{Maxavail}, \seep{Getmem}, \seep{Freemem}
  3228. \seep{New}, \seep{Dispose}}
  3229. For an example, see \seep{Mark}.
  3230. \procedure{Rename}{(Var F : Any Filetype; Const S : String)}
  3231. {\var{Rename} changes the name of the assigned file \var{F} to \var{S}.
  3232. \var{F}
  3233. must be assigned, but not opened.}
  3234. {A run-time error will be generated if \var{F} isn't assigned,
  3235. or doesn't exist.}
  3236. {\seep{Erase}}
  3237. \latex{\inputlisting{refex/ex77.pp}}
  3238. \html{\input{refex/ex77.tex}}
  3239. \procedure{Reset}{(Var F : Any File Type[; L : Longint])}
  3240. {\var{Reset} opens a file \var{F} for reading. \var{F} can be any file type.
  3241. If \var{F} is an untyped or typed file, then it is opened for reading and
  3242. writing. If \var{F} is an untyped file, the record size can be specified in
  3243. the optional parameter \var{L}. Default a value of 128 is used.}
  3244. {If the file cannot be opened for reading, then a run-time error is
  3245. generated. This behavior can be changed by the \var{\{\$i\} } compiler switch.}
  3246. {\seep{Rewrite}, \seep{Assign}, \seep{Close}}
  3247. \latex{\inputlisting{refex/ex51.pp}}
  3248. \html{\input{refex/ex51.tex}}
  3249. \procedure{Rewrite}{(Var F : Any File Type[; L : Longint])}
  3250. {\var{Rewrite} opens a file \var{F} for writing. \var{F} can be any file type.
  3251. If \var{F} is an untyped or typed file, then it is opened for reading and
  3252. writing. If \var{F} is an untyped file, the record size can be specified in
  3253. the optional parameter \var{L}. Default a value of 128 is used.
  3254. if \var{Rewrite} finds a file with the same name as \var{F}, this file is
  3255. truncated to length \var{0}. If it doesn't find such a file, a new file is
  3256. created.
  3257. }
  3258. {If the file cannot be opened for writing, then a run-time error is
  3259. generated. This behavior can be changed by the \var{\{\$i\} } compiler switch.}
  3260. {\seep{Reset}, \seep{Assign}, \seep{Close}}
  3261. \latex{\inputlisting{refex/ex52.pp}}
  3262. \html{\input{refex/ex52.tex}}
  3263. \procedure{Rmdir}{(const S : string)}
  3264. {\var{Rmdir} removes the directory \var{S}.}
  3265. {If \var{S} doesn't exist, or isn't empty, a run-time error is generated.
  3266. }
  3267. {\seep{Chdir}, \seep{Rmdir}}
  3268. \latex{\inputlisting{refex/ex53.pp}}
  3269. \html{\input{refex/ex53.tex}}
  3270. \function{Round}{(X : Real)}{Longint}
  3271. {\var{Round} rounds \var{X} to the closest integer, which may be bigger or
  3272. smaller than \var{X}.}
  3273. {None.}
  3274. {\seef{Frac}, \seef{Int}, \seef{Trunc}}
  3275. \latex{\inputlisting{refex/ex54.pp}}
  3276. \html{\input{refex/ex54.tex}}
  3277. \procedure{Runerror}{(ErrorCode : Word)}
  3278. {\var{Runerror} stops the execution of the program, and generates a
  3279. run-time error \var{ErrorCode}.}
  3280. {None.}
  3281. {\seep{Exit}, \seep{Halt}}
  3282. \latex{\inputlisting{refex/ex55.pp}}
  3283. \html{\input{refex/ex55.tex}}
  3284. \procedure{Seek}{(Var F; Count : Longint)}
  3285. {\var{Seek} sets the file-pointer for file \var{F} to record Nr. \var{Count}.
  3286. The first record in a file has \var{Count=0}. F can be any file type, except
  3287. \var{Text}. If \var{F} is an untyped file, with no specified record size, 128
  3288. is assumed.}
  3289. {A run-time error is generated if \var{Count} points to a position outside
  3290. the file, or the file isn't opened.}
  3291. {\seef{Eof}, \seef{SeekEof}, \seef{SeekEoln}}
  3292. \latex{\inputlisting{refex/ex56.pp}}
  3293. \html{\input{refex/ex56.tex}}
  3294. \function{SeekEof}{[(Var F : text)]}{Boolean}
  3295. {\var{SeekEof} returns \var{True} is the file-pointer is at the end of the
  3296. file. It ignores all whitespace.
  3297. Calling this function has the effect that the file-position is advanced
  3298. until the first non-whitespace character or the end-of-file marker is
  3299. reached.
  3300. If the end-of-file marker is reached, \var{True} is returned. Otherwise,
  3301. False is returned.
  3302. If the parameter \var{F} is omitted, standard \var{Input} is assumed.
  3303. }
  3304. {A run-time error is generated if the file \var{F} isn't opened.}
  3305. {\seef{Eof}, \seef{SeekEoln}, \seep{Seek}}
  3306. \latex{\inputlisting{refex/ex57.pp}}
  3307. \html{\input{refex/ex57.tex}}
  3308. \function{SeekEoln}{[(Var F : text)]}{Boolean}
  3309. {\var{SeekEoln} returns \var{True} is the file-pointer is at the end of the
  3310. current line. It ignores all whitespace.
  3311. Calling this function has the effect that the file-position is advanced
  3312. until the first non-whitespace character or the end-of-line marker is
  3313. reached.
  3314. If the end-of-line marker is reached, \var{True} is returned. Otherwise,
  3315. False is returned.
  3316. The end-of-line marker is defined as \var{\#10}, the LineFeed character.
  3317. If the parameter \var{F} is omitted, standard \var{Input} is assumed.}
  3318. {A run-time error is generated if the file \var{F} isn't opened.}
  3319. {\seef{Eof}, \seef{SeekEof}, \seep{Seek}}
  3320. \latex{\inputlisting{refex/ex58.pp}}
  3321. \html{\input{refex/ex58.tex}}
  3322. \function{Seg}{Var X}{Longint}
  3323. {\var{Seg} returns the segment of the address of a variable.
  3324. This function is only supported for compatibility. In \fpc, it
  3325. returns always 0, since \fpc is a 32 bit compiler, segments have no meaning.
  3326. }
  3327. {None.}
  3328. {\seef{DSeg}, \seef{CSeg}, \seef{Ofs}, \seef{Ptr}}
  3329. \latex{\inputlisting{refex/ex60.pp}}
  3330. \html{\input{refex/ex60.tex}}
  3331. \function{SetJmp}{(Var Env : Jmp\_Buf)}{Longint}
  3332. {
  3333. \var{SetJmp} fills \var{env} with the necessary data for a jump back to the
  3334. point where it was called. It returns zero if called in this way.
  3335. If the function returns nonzero, then it means that a call to \seep{LongJmp}
  3336. with \var{env} as an argument was made somewhere in the program.
  3337. }{None.}{\seep{LongJmp}}
  3338. \latex{\inputlisting{refex/ex79.pp}}
  3339. \html{\input{refex/ex79.tex}}
  3340. \procedure{SetTextBuf}{(Var f : Text; Var Buf[; Size : Word])}
  3341. {\var{SetTextBuf} assigns an I/O buffer to a text file. The new buffer is
  3342. located at \var{Buf} and is \var{Size} bytes long. If \var{Size} is omitted,
  3343. then \var{SizeOf(Buf)} is assumed.
  3344. The standard buffer of any text file is 128 bytes long. For heavy I/0
  3345. operations this may prove too slow. The \var{SetTextBuf} procedure allows
  3346. you to set a bigger buffer for your application, thus reducing the number of
  3347. system calls, and thus reducing the load on the system resources.
  3348. The maximum size of the newly assigned buffer is 65355 bytes.
  3349. {\em Remark 1:} Never assign a new buffer to an opened file. You can assign a
  3350. new buffer immediately after a call to \seep{Rewrite}, \seep{Reset} or
  3351. \var{Append}, but not after you read from/wrote to the file. This may cause
  3352. loss of data. If you still want to assign a new buffer after read/write
  3353. operations have been performed, flush the file first. This will ensure that
  3354. the current buffer is emptied.
  3355. {\em Remark 2:} Take care that the buffer you assign is always valid. If you
  3356. assign a local variable as a buffer, then after your program exits the local
  3357. program block, the buffer will no longer be valid, and stack problems may
  3358. occur.
  3359. }
  3360. {No checking on \var{Size} is done.}
  3361. {\seep{Assign}, \seep{Reset}, \seep{Rewrite}, \seep{Append}}
  3362. \latex{\inputlisting{refex/ex61.pp}}
  3363. \html{\input{refex/ex61.tex}}
  3364. \function{Sin}{(X : Real)}{Real}
  3365. {\var{Sin} returns the sine of its argument \var{X}, where \var{X} is an
  3366. angle in radians.}
  3367. {None.}
  3368. {\seef{Cos}, \seef{Pi}, \seef{Exp}}
  3369. \latex{\inputlisting{refex/ex62.pp}}
  3370. \html{\input{refex/ex62.tex}}
  3371. \function{SizeOf}{(X : Any Type)}{Longint}
  3372. {\var{SizeOf} Returns the size, in bytes, of any variable or type-identifier.
  3373. {\em Remark:} this isn't Really a RTL function. Its result is calculated at
  3374. compile-time, and hard-coded in your executable.}
  3375. {None.}
  3376. {\seef{Addr}}
  3377. \latex{\inputlisting{refex/ex63.pp}}
  3378. \html{\input{refex/ex63.tex}}
  3379. \Function{Sptr}{Pointer}
  3380. {\var{Sptr} returns the current stack pointer.
  3381. }{None.}{}
  3382. \latex{\inputlisting{refex/ex64.pp}}
  3383. \html{\input{refex/ex64.tex}}
  3384. \function{Sqr}{(X : Real)}{Real}
  3385. {\var{Sqr} returns the square of its argument \var{X}.}
  3386. {None.}
  3387. {\seef{Sqrt}, \seef{Ln}, \seef{Exp}}
  3388. \latex{\inputlisting{refex/ex65.pp}}
  3389. \html{\input{refex/ex65.tex}}
  3390. \function{Sqrt}{(X : Real)}{Real}
  3391. {\var{Sqrt} returns the square root of its argument \var{X}, which must be
  3392. positive.}
  3393. {If \var{X} is negative, then a run-time error is generated.}
  3394. {\seef{Sqr}, \seef{Ln}, \seef{Exp}}
  3395. \latex{\inputlisting{refex/ex66.pp}}
  3396. \html{\input{refex/ex66.tex}}
  3397. \Function{SSeg}{Longint}
  3398. { \var{SSeg} returns the Stack Segment. This function is only
  3399. supported for compatibolity reasons, as \var{Sptr} returns the
  3400. correct contents of the stackpointer.}
  3401. {None.}{\seef{Sptr}}
  3402. \latex{\inputlisting{refex/ex67.pp}}
  3403. \html{\input{refex/ex67.tex}}
  3404. \procedure{Str}{(Var X[:NumPlaces[:Decimals]]; Var S : String)}
  3405. {\var{Str} returns a string which represents the value of X. X can be any
  3406. numerical type.
  3407. The optional \var{NumPLaces} and \var{Decimals} specifiers control the
  3408. formatting of the string.}
  3409. {None.}
  3410. {\seep{Val}}
  3411. \latex{\inputlisting{refex/ex68.pp}}
  3412. \html{\input{refex/ex68.tex}}
  3413. \function{Swap}{(X)}{Type of X}
  3414. {\var{Swap} swaps the high and low order bytes of \var{X} if \var{X} is of
  3415. type \var{Word} or \var{Integer}, or swaps the high and low order words of
  3416. \var{X} if \var{X} is of type \var{Longint} or \var{Cardinal}.
  3417. The return type is the type of \var{X}}
  3418. {None.}{\seef{Lo}, \seef{Hi}}
  3419. \latex{\inputlisting{refex/ex69.pp}}
  3420. \html{\input{refex/ex69.tex}}
  3421. \function{Trunc}{(X : Real)}{Longint}
  3422. {\var{Trunc} returns the integer part of \var{X},
  3423. which is always smaller than (or equal to) \var{X}.}
  3424. {None.}
  3425. {\seef{Frac}, \seef{Int}, \seef{Trunc}}
  3426. \latex{\inputlisting{refex/ex70.pp}}
  3427. \html{\input{refex/ex70.tex}}
  3428. \procedure{Truncate}{(Var F : file)}
  3429. {\var{Truncate} truncates the (opened) file \var{F} at the current file
  3430. position.
  3431. }{Errors are reported by IOresult.}{\seep{Append}, \seef{Filepos},
  3432. \seep{Seek}}
  3433. \latex{\inputlisting{refex/ex71.pp}}
  3434. \html{\input{refex/ex71.tex}}
  3435. \function{Upcase}{(C : Char or string)}{Char or String}
  3436. {\var{Upcase} returns the uppercase version of its argument \var{C}.
  3437. If its argument is a string, then the complete string is converted to
  3438. uppercase. The type of the returned value is the same as the type of the
  3439. argument.}
  3440. {None.}
  3441. {\seef{Lowercase}}
  3442. \latex{\inputlisting{refex/ex72.pp}}
  3443. \html{\input{refex/ex72.tex}}
  3444. \procedure{Val}{(const S : string;var V;var Code : word)}
  3445. {\var{Val} converts the value represented in the string \var{S} to a numerical
  3446. value, and stores this value in the variable \var{V}, which
  3447. can be of type \var{Longint}, \var{Real} and \var{Byte}.
  3448. If the conversion isn't succesfull, then the parameter \var{Code} contains
  3449. the index of the character in \var{S} which prevented the conversion.
  3450. The string \var{S} isn't allow to contain spaces.}
  3451. {If the conversion doesn't succeed, the value of \var{Code} indicates the
  3452. position where the conversion went wrong.}
  3453. {\seep{Str}}
  3454. \latex{\inputlisting{refex/ex74.pp}}
  3455. \html{\input{refex/ex74.tex}}
  3456. \procedure{Write}{([Var F : Any filetype;] V1 [; V2; ... , Vn)]}
  3457. {\var{Write} writes the contents of the variables \var{V1}, \var{V2} etc. to
  3458. the file \var{F}. \var{F} can be a typed file, or a \var{Text} file.
  3459. If \var{F} is a typed file, then the variables \var{V1}, \var{V2} etc. must
  3460. be of the same type as the type in the declaration of \var{F}. Untyped files
  3461. are not allowed.
  3462. If the parameter \var{F} is omitted, standard output is assumed.
  3463. If \var{F} is of type \var{Text}, then the necessary conversions are done
  3464. such that the output of the variables is in human-readable format.
  3465. This conversion is done for all numerical types. Strings are printed exactly
  3466. as they are in memory, as well as \var{PChar} types.
  3467. The format of the numerical conversions can be influenced through
  3468. the following modifiers:
  3469. \var{ OutputVariable : NumChars [: Decimals ] }
  3470. This will print the value of \var{OutputVariable} with a minimum of
  3471. \var{NumChars} characters, from which \var{Decimals} are reserved for the
  3472. decimals. If the number cannot be represented with \var{NumChars} characters,
  3473. \var{NumChars} will be increased, until the representation fits. If the
  3474. representation requires less than \var{NumChars} characters then the output
  3475. is filled up with spaces, to the left of the generated string, thus
  3476. resulting in a right-aligned representation.
  3477. If no formatting is specified, then the number is written using its natural
  3478. length, with a space in front of it if it's positive, and a minus sign if
  3479. it's negative.
  3480. Real numbers are, by default, written in scientific notation.
  3481. }
  3482. {If an error occurs, a run-time error is generated. This behavior can be
  3483. controlled with the \var{\{\$i\}} switch. }
  3484. {\seep{Writeln}, \seep{Read}, \seep{Readln}, \seep{Blockwrite} }
  3485. \procedure{Writeln}{[([Var F : Text;] [V1 [; V2; ... , Vn)]]}
  3486. {\var{Writeln} does the same as \seep{Write} for text files, and emits a
  3487. Carriage Return - LineFeed character pair after that.
  3488. If the parameter \var{F} is omitted, standard output is assumed.
  3489. If no variables are specified, a Carriage Return - LineFeed character pair
  3490. is emitted, resulting in a new line in the file \var{F}.
  3491. {\em Remark:} Under \linux, the Carriage Return character is omitted, as
  3492. customary in Unix environments.
  3493. }
  3494. {If an error occurs, a run-time error is generated. This behavior can be
  3495. controlled with the \var{\{\$i\}} switch. }
  3496. {\seep{Write}, \seep{Read}, \seep{Readln}, \seep{Blockwrite}}
  3497. \latex{\inputlisting{refex/ex75.pp}}
  3498. \html{\input{refex/ex75.tex}}
  3499. %
  3500. % The index.
  3501. %
  3502. \printindex
  3503. \end{document}