Difference between revisions of "Program"

From Free Pascal wiki
Jump to navigationJump to search
(undo last two revisions by Alextp (talk): not an improvement)
 
(9 intermediate revisions by 4 users not shown)
Line 4: Line 4:
 
This is sometimes referred to as the main program.
 
This is sometimes referred to as the main program.
  
== 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" inline>program</syntaxhighlight> is a [[Reserved word|reserved word]] that introduces a classical program source code file:
 +
 
 
<syntaxhighlight lang="pascal" highlight="1">
 
<syntaxhighlight lang="pascal" highlight="1">
program hiWorld(input, output, stderr);
+
program hiWorld(input, output, stdErr);
  
 
begin
 
begin
Line 13: Line 15:
 
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, but in [[Mode iso|<syntaxhighlight lang="pascal" enclose="none">{$mode ISO}</syntaxhighlight>]].
+
Meanwhile [[FPC]] ''discards'' the program header, i.&#8239;e. the first line.
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 in other modes. (cf.: {{Doc|package=RTL|unit=system|identifier=sysinitstdio|text=<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>])
+
The output file name is determined by the source code file’s name.
 +
However, the program name ''does'' become a reserved [[Identifier|identifier]] (except in the <abbr title="International Organization for Standardization">ISO</abbr> modes [since FPC 3.3.1/trunk revision #45757; cf. {{MantisLink|37322}}]).
 +
In the example above, e.g. attempting to define a constant named <syntaxhighlight lang="pascal" inline>hiWorld</syntaxhighlight> would trigger a duplicate identifier [[Compile time|compile time]] error.
 +
The program name will identify the global [[Scope|scope]], thus it can be used to write fully-qualified identifiers.
 +
 
 +
The file descriptor list is completely ignored, except in [[Mode iso|<syntaxhighlight lang="pascal" inline>{$mode ISO}</syntaxhighlight>]].
 +
The [[Text|<syntaxhighlight lang="pascal" inline>text</syntaxhighlight>]] [[Variable|variables]] {{Doc|package=RTL|unit=system|identifier=input|text=<syntaxhighlight lang="pascal" inline>input</syntaxhighlight>}}, {{Doc|package=RTL|unit=system|identifier=output|text=<syntaxhighlight lang="pascal" inline>output</syntaxhighlight>}} and {{Doc|package=RTL|unit=system|identifier=stderr|text=<syntaxhighlight lang="pascal" inline>stderr</syntaxhighlight>}} are always opened and their names can not be changed in other modes. (cf.: {{Doc|package=RTL|unit=system|identifier=sysinitstdio|text=<syntaxhighlight lang="pascal" inline>SysInitStdIO</syntaxhighlight>}} is always called in {{gitlab|tree|FPC|release_3_0_4/rtl/linux/system.pp#L367-L368|<tt>rtl/linux/system.pp</tt>}})
 +
 
 +
Therefore with FPC the following complete source code example compiles identically as does the previous example.
  
Thus with FPC the following complete source code example compiles identically as does the previous example.
 
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
begin
 
begin
Line 28: Line 33:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== program structure ==
+
If the program is syntactically correct, FPC ignores anything that comes after the final <syntaxhighlight lang="pascal" inline>end.</syntaxhighlight>. The following will compile without problems:
A <syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight> file has to follow a certain [[Program Structure|structure]].
+
 
# An (depending on used compiler possibly optional) program header.
+
<syntaxhighlight lang="pascal">
# 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 program header.
+
program awesomeProgram(input, output, stdErr);
# Exactly one [[Block|block]] that concludes with an <syntaxhighlight lang="pascal" enclose="none">end.</syntaxhighlight> (note the [[period#module end|period]]). This block may contain – in contrast to regular blocks – <syntaxhighlight lang="pascal" enclose="none">resourcestring</syntaxhighlight> section(s).
+
begin
The exact order and number of various sections after the (optional) <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.
+
writeLn('Awesome!');
 +
end.I thank my mom, my dad, and everyone who supported me in making this program.
 +
</syntaxhighlight>
 +
This “feature” is primarily used to supply an in-file changelog or copyright notice.
 +
 
 +
FPC does not support multiple modules in one source code file, like some other compilers did or do.
 +
Each module source code has to reside in its own file.
 +
However, the restriction that module names have to match file names does not apply to programs.
 +
This is due to the fact that programs cannot be included by other modules, thus searching them (via their file name) is not necessary.
 +
 
 +
== Program structure ==
 +
 
 +
A <syntaxhighlight lang="pascal" inline>program</syntaxhighlight> file has to follow a certain [[Program Structure|structure]].
 +
# A (depending on used compiler possibly optional) program header.
 +
# There can be at most one [[Uses|<syntaxhighlight lang="pascal" inline>uses</syntaxhighlight>-clause]] and it has to be at the top of the program right after the program header.
 +
# Exactly one [[Block|block]] that concludes with an <syntaxhighlight lang="pascal" inline>end.</syntaxhighlight> (note the [[period#module end|period]]). This block may contain – in contrast to regular blocks – <syntaxhighlight lang="pascal" inline>resourcestring</syntaxhighlight> section(s).
 +
The exact order and number of various sections after the (optional) <syntaxhighlight lang="pascal" inline>uses</syntaxhighlight>-clause until final the compound statement [[Begin|<syntaxhighlight lang="pascal" inline>begin</syntaxhighlight>]]…[[End|<syntaxhighlight lang="pascal" inline>end.</syntaxhighlight>]] is free of choice.
 +
 
 
However, there are some plausible considerations.
 
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|routine]] declarations.
+
* A [[Type|<syntaxhighlight lang="pascal" inline>type</syntaxhighlight>-section]] comes prior any section that can use types, e.g. [[Var|<syntaxhighlight lang="pascal" inline>var</syntaxhighlight>-sections]] or [[Routine|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 statement-frame it is supposed to declare labels for.
+
* Since [[Goto|<syntaxhighlight lang="pascal" inline>goto</syntaxhighlight>]] is known as the devil’s tool, a [[Label|<syntaxhighlight lang="pascal" inline>label</syntaxhighlight>-section]], if any, is as close as possible to the statement-frame 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]].
+
* Generally you go from general into specifics: For example a <syntaxhighlight lang="pascal" inline>var</syntaxhighlight>-section comes in front of a [[Threadvar|<syntaxhighlight lang="delphi" inline>threadVar</syntaxhighlight>-section]]. A [[Const|<syntaxhighlight lang="pascal" inline>const</syntaxhighlight>-section]] comes before a [[Resourcestring|<syntaxhighlight lang="delphi" inline>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.
+
* <syntaxhighlight lang="delphi" inline>resourceString</syntaxhighlight>-sections can be either static or global, that means they should appear relatively soon after the <syntaxhighlight lang="pascal" inline>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>)
+
* 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" inline>var</syntaxhighlight>-(like)-section. (beware: play it safe and set <syntaxhighlight lang="pascal" inline>{$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.
+
* [[global compiler directives|Global compiler directives]], especially such that allow or restrict what can be written (e.g. <syntaxhighlight lang="pascal" inline>{$goto on}</syntaxhighlight> allows the use of <syntaxhighlight lang="pascal" inline>goto</syntaxhighlight>) or implicitly add unit dependencies like [[Mode ObjFPC|<syntaxhighlight lang="pascal" inline>{$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">
+
Taking all considerations into account the rough program structure should look like this (except for <syntaxhighlight lang="pascal" inline>label</syntaxhighlight> and [[sGoto|<syntaxhighlight lang="pascal" inline>{$goto on}</syntaxhighlight>]] which are only mentioned for the sake of completeness):
program sectionDemo(input, output, stderr);
+
 
 +
<syntaxhighlight lang="delphi">
 +
program sectionDemo(input, output, stdErr);
  
// global compiler directives ----------------------------
+
// Global compiler directives ----------------------------
{$mode objfpc}
+
{$mode objFPC}
 
{$goto on}
 
{$goto on}
  
 
uses
 
uses
sysutils;
+
sysUtils;
  
 
const
 
const
 
answer = 42;
 
answer = 42;
  
resourcestring
+
resourceString
 
helloWorld = 'Hello world!';
 
helloWorld = 'Hello world!';
  
Line 69: Line 93:
 
i: longint;
 
i: longint;
  
threadvar
+
threadVar
 
z: longbool;
 
z: longbool;
  
Line 77: Line 101:
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
<small>[The example consciously ignores the possibility of “typed constants”, sticking rather to traditional concepts than unnecessarily confusing beginners.]</small>
 
<small>[The example consciously ignores the possibility of “typed constants”, sticking rather to traditional concepts than unnecessarily confusing beginners.]</small>
  
== see also ==
+
== See also ==
 +
 
 
* [[Unit|unit]]
 
* [[Unit|unit]]
 
* [[Library|library]]
 
* [[Library|library]]
 +
* [[Program Structure|program structure]] in the Object Pascal Introduction series
 +
* [https://en.wikibooks.org/wiki/Pascal_Programming/Beginning § “Beginning” in the ''Pascal Programming'' book on Wikibooks.org]

Latest revision as of 14:59, 18 January 2022

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 (except in the ISO modes [since FPC 3.3.1/trunk revision #45757; cf. Issue #37322]). In the example above, e.g. attempting to define a constant named hiWorld would trigger a duplicate identifier compile time error. The program name will identify the global scope, thus it can be used to write fully-qualified identifiers.

The file descriptor list is completely ignored, except in {$mode ISO}. The text variables input, output and stderr are always opened and their names can not be changed in other modes. (cf.: SysInitStdIO is always called in rtl/linux/system.pp)

Therefore with FPC the following complete source code example compiles identically as does the previous example.

begin
	writeLn('Hi!');
end.

If the program is syntactically correct, FPC ignores anything that comes after the final end.. The following will compile without problems:

program awesomeProgram(input, output, stdErr);
begin
	writeLn('Awesome!');
end.I thank my mom, my dad, and everyone who supported me in making this program.

This “feature” is primarily used to supply an in-file changelog or copyright notice.

FPC does not support multiple modules in one source code file, like some other compilers did or do. Each module source code has to reside in its own file. However, the restriction that module names have to match file names does not apply to programs. This is due to the fact that programs cannot be included by other modules, thus searching them (via their file name) is not necessary.

Program structure

A program file has to follow a certain structure.

  1. A (depending on used compiler possibly optional) program header.
  2. There can be at most one uses-clause and it has to be at the top of the program right after the program header.
  3. Exactly one block that concludes with an end. (note the period). This block may contain – in contrast to regular blocks – resourcestring section(s).

The exact order and number of various sections after the (optional) uses-clause until final the compound statement beginend. 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, a label-section, if any, is as close as possible to the statement-frame it is supposed to declare labels for.
  • Generally you go from general into specifics: For example a var-section comes in front of a threadVar-section. A const-section comes before a resourceString-section.
  • resourceString-sections can be either static or global, that means they should appear relatively soon after the uses-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 of goto) 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.]

See also