ref.tex 182 KB

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