ref.tex 169 KB

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