ref.tex 160 KB

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