Difference between revisions of "Program"
m (→Example) |
(review) |
||
Line 1: | Line 1: | ||
{{Program}} | {{Program}} | ||
− | A '''program''' is either an [[Executable program|executable program]], that is, the complete and runnable [[Application|application]], or it is that portion of a [[Pascal]] [[Source code]] [[File|file]] or files that can be compiled and is not declared to be a [[Unit|unit]]. | + | A '''program''' is either an [[Executable program|executable program]], that is, the complete and runnable [[Application|application]], or it is that portion of a [[Pascal]] [[Source code]] [[File|file]] or files that can be compiled and is not declared to be a [[Unit|unit]] or [[Library|library]]. |
+ | This is sometimes referred to as the main program. | ||
− | [[ | + | == main program == |
− | = | + | <syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight> is a [[Reserved word|reserved word]] that introduces a classical program source code file: |
+ | <syntaxhighlight lang="pascal" highlight="1"> | ||
+ | program hiWorld(input, output, stderr); | ||
− | |||
− | |||
begin | begin | ||
− | + | writeLn('Hi!'); | |
end. | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | Meanwhile [[FPC]] ''discards'' the program header, i.e. the first line. | ||
+ | The output file name is determined by the source code file's name. | ||
+ | However, the program name ''does'' become a reserved identifier. | ||
+ | In the example above, e.g. attempting to define a constant named <syntaxhighlight lang="pascal" enclose="none">hiWorld</syntaxhighlight> would trigger a duplicate identifier compile-time error. | ||
+ | |||
+ | The file descriptor list is completely ignored. | ||
+ | The [[Text|<syntaxhighlight lang="pascal" enclose="none">text</syntaxhighlight>]] [[Variable|variables]] {{Doc|package=RTL|unit=system|identifier=input|text=<syntaxhighlight lang="pascal" enclose="none">input</syntaxhighlight>}}, {{Doc|package=RTL|unit=system|identifier=output|text=<syntaxhighlight lang="pascal" enclose="none">output</syntaxhighlight>}} and {{Doc|package=RTL|unit=system|identifier=stderr|text=<syntaxhighlight lang="pascal" enclose="none">stderr</syntaxhighlight>}} are always opened and their names can not be changed. (cf.: <syntaxhighlight lang="pascal" enclose="none">SysInitStdIO</syntaxhighlight> is always called in [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/rtl/linux/system.pp?view=markup#l367 <tt>rtl/linux/system.pp</tt>]) | ||
+ | |||
+ | Thus with FPC the following complete source code example compiles identically as does the previous example. | ||
+ | <syntaxhighlight lang="pascal"> | ||
+ | begin | ||
+ | writeLn('Hi!'); | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == program structure == | ||
+ | A <syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight> file has to follow a certain structure. | ||
+ | There can be at most one [[Uses|<syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight>-clause]] and it has to be at the top of the program right after the (depending on used compiler possibly optional) program header. | ||
+ | <syntaxhighlight lang="pascal" highlight="3-6"> | ||
+ | program hiWorld(input, output, stderr); | ||
+ | |||
+ | uses | ||
+ | // sysutils for applicationName | ||
+ | // gettext for internationalization routines | ||
+ | sysutils, gettext; | ||
+ | |||
+ | resourcestring | ||
+ | helloWorld = 'Hi!'; | ||
+ | |||
+ | begin | ||
+ | translateResourcestrings(applicationName() + '.%s.mo'); | ||
+ | writeLn(helloWorld); | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | What comes thereafter, the exact order and number of various sections after the <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight>-clause until final the compound statement [[Begin|<syntaxhighlight lang="pascal" enclose="none">begin</syntaxhighlight>]]…[[End|<syntaxhighlight lang="pascal" enclose="none">end.</syntaxhighlight>]] is free of choice. | ||
+ | However, there are some plausible considerations. | ||
+ | * A [[Type|<syntaxhighlight lang="pascal" enclose="none">type</syntaxhighlight>-section]] comes prior any section that can use types, e.g. [[Var|<syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight>-sections]] or routine declarations. | ||
+ | * Since [[Goto|<syntaxhighlight lang="pascal" enclose="none">goto</syntaxhighlight>]] is known as the devil's tool, a [[Label|<syntaxhighlight lang="pascal" enclose="none">label</syntaxhighlight>-section]], if any, is as close as possible to the block it is supposed to declare labels for. | ||
+ | * Generally you go from general into specifics: For example a <syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight>-section comes in front of a [[Threadvar|<syntaxhighlight lang="pascal" enclose="none">threadvar</syntaxhighlight>-section]]. A [[Const|<syntaxhighlight lang="pascal" enclose="none">const</syntaxhighlight>-section]] comes before a [[Resourcestring|<syntaxhighlight lang="pascal" enclose="none">resourcestring</syntaxhighlight>-section]]. | ||
+ | * <syntaxhighlight lang="pascal" enclose="none">resourcestring</syntaxhighlight>-sections can be either static or global, that means they should appear relatively soon after the <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight>-clause. | ||
+ | * Direct usage of [[Global variables|global variables]] in routines (or even the mere possibility) is considered as bad style. Instead, declare/define your routines prior any <syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight>-(like)-section. (beware: play it safe and set <syntaxhighlight lang="pascal" enclose="none">{$writeableConst off}</syntaxhighlight>) | ||
+ | * Global compiler directives, especially such that allow or restrict what can be written (e.g. <syntaxhighlight lang="pascal" enclose="none">{$goto on}</syntaxhighlight> allows the use of <syntaxhighlight lang="pascal" enclose="none">goto</syntaxhighlight>) or implicitly add unit dependencies like [[Mode ObjFPC|<syntaxhighlight lang="pascal" enclose="none">{$mode objfpc}</syntaxhighlight>]] should appear soon after the program header. | ||
+ | Taking all considerations into account the rough program structure should look like this (except for <syntaxhighlight lang="pascal" enclose="none">label</syntaxhighlight> and [[sGoto|<syntaxhighlight lang="pascal" enclose="none">{$goto on}</syntaxhighlight>]] which are only mentioned for the sake of completeness): | ||
+ | <syntaxhighlight lang="pascal"> | ||
+ | program sectionDemo(input, output, stderr); | ||
+ | |||
+ | // global compiler directives ---------------------------- | ||
+ | {$mode objfpc} | ||
+ | {$goto on} | ||
+ | |||
+ | uses | ||
+ | sysutils; | ||
+ | |||
+ | const | ||
+ | answer = 42; | ||
+ | |||
+ | resourcestring | ||
+ | helloWorld = 'Hello world!'; | ||
+ | |||
+ | type | ||
+ | primaryColor = (red, green, blue); | ||
+ | |||
+ | procedure doSomething(const color: primaryColor); | ||
+ | begin | ||
+ | end; | ||
+ | |||
+ | // M A I N ----------------------------------------------- | ||
+ | var | ||
+ | i: longint; | ||
+ | |||
+ | threadvar | ||
+ | z: longbool; | ||
+ | |||
+ | label | ||
+ | 42; | ||
+ | begin | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | <small>[The example consciously ignores the possibility of “typed constants”, sticking rather to traditional concepts than unnecessarily confusing beginners.]</small> | ||
+ | |||
+ | == see also == | ||
+ | * [[Unit|unit]] | ||
+ | * [[Library|library]] |
Revision as of 11:39, 3 May 2018
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
Bahasa Indonesia (id) │
italiano (it) │
português (pt) │
русский (ru) │
A program is either an executable program, that is, the complete and runnable application, or it is that portion of a Pascal Source code file or files that can be compiled and is not declared to be a unit or library. This is sometimes referred to as the main program.
main program
program
is a reserved word that introduces a classical program source code file:
program hiWorld(input, output, stderr);
begin
writeLn('Hi!');
end.
Meanwhile FPC discards the program header, i.e. the first line.
The output file name is determined by the source code file's name.
However, the program name does become a reserved identifier.
In the example above, e.g. attempting to define a constant named hiWorld
would trigger a duplicate identifier compile-time error.
The file descriptor list is completely ignored.
The text
variables input
, output
and stderr
are always opened and their names can not be changed. (cf.: SysInitStdIO
is always called in rtl/linux/system.pp)
Thus with FPC the following complete source code example compiles identically as does the previous example.
begin
writeLn('Hi!');
end.
program structure
A program
file has to follow a certain structure.
There can be at most one uses
-clause and it has to be at the top of the program right after the (depending on used compiler possibly optional) program header.
program hiWorld(input, output, stderr);
uses
// sysutils for applicationName
// gettext for internationalization routines
sysutils, gettext;
resourcestring
helloWorld = 'Hi!';
begin
translateResourcestrings(applicationName() + '.%s.mo');
writeLn(helloWorld);
end.
What comes thereafter, the exact order and number of various sections after the uses
-clause until final the compound statement begin
…end.
is free of choice.
However, there are some plausible considerations.
- A
type
-section comes prior any section that can use types, e.g.var
-sections or routine declarations. - Since
goto
is known as the devil's tool, alabel
-section, if any, is as close as possible to the block it is supposed to declare labels for. - Generally you go from general into specifics: For example a
var
-section comes in front of athreadvar
-section. Aconst
-section comes before aresourcestring
-section. resourcestring
-sections can be either static or global, that means they should appear relatively soon after theuses
-clause.- Direct usage of global variables in routines (or even the mere possibility) is considered as bad style. Instead, declare/define your routines prior any
var
-(like)-section. (beware: play it safe and set{$writeableConst off}
) - Global compiler directives, especially such that allow or restrict what can be written (e.g.
{$goto on}
allows the use ofgoto
) or implicitly add unit dependencies like{$mode objfpc}
should appear soon after the program header.
Taking all considerations into account the rough program structure should look like this (except for label
and {$goto on}
which are only mentioned for the sake of completeness):
program sectionDemo(input, output, stderr);
// global compiler directives ----------------------------
{$mode objfpc}
{$goto on}
uses
sysutils;
const
answer = 42;
resourcestring
helloWorld = 'Hello world!';
type
primaryColor = (red, green, blue);
procedure doSomething(const color: primaryColor);
begin
end;
// M A I N -----------------------------------------------
var
i: longint;
threadvar
z: longbool;
label
42;
begin
end.
[The example consciously ignores the possibility of “typed constants”, sticking rather to traditional concepts than unnecessarily confusing beginners.]