Difference between revisions of "Frame"

From Free Pascal wiki
Jump to navigationJump to search
(create more complete list of frames)
 
(→‎available types: remove accidental mention of `with` which works for a single statement only)
Line 11: Line 11:
 
*; <syntaxhighlight lang="pascal" inline>repeat … until</syntaxhighlight>
 
*; <syntaxhighlight lang="pascal" inline>repeat … until</syntaxhighlight>
 
*: <syntaxhighlight lang="pascal" inline>repeat</syntaxhighlight> in conjunction with <syntaxhighlight lang="pascal" inline>until</syntaxhighlight> is used to surround the loop body of a tail-controlled [[Loops|loop]]. It is the only frame type not ending with an <syntaxhighlight lang="pascal" inline>end</syntaxhighlight>.
 
*: <syntaxhighlight lang="pascal" inline>repeat</syntaxhighlight> in conjunction with <syntaxhighlight lang="pascal" inline>until</syntaxhighlight> is used to surround the loop body of a tail-controlled [[Loops|loop]]. It is the only frame type not ending with an <syntaxhighlight lang="pascal" inline>end</syntaxhighlight>.
*; [[With|<syntaxhighlight lang="pascal" inline>with</syntaxhighlight>]]
 
*: This frame allows to temporarily modify the scope lookup routing.
 
 
*; exception treatment
 
*; exception treatment
 
*: If [[Exceptions|exceptions]] are supported in the current compiler mode, the following frames are available as well. These frames are in fact “double”-frames: They group ''two'' sequences at once. Neither of them can be used independently (e. g. writing <syntaxhighlight lang="delphi" inline>finally … end;</syntaxhighlight> ''without'' a proper <syntaxhighlight lang="delphi" inline>try</syntaxhighlight> is illegal).
 
*: If [[Exceptions|exceptions]] are supported in the current compiler mode, the following frames are available as well. These frames are in fact “double”-frames: They group ''two'' sequences at once. Neither of them can be used independently (e. g. writing <syntaxhighlight lang="delphi" inline>finally … end;</syntaxhighlight> ''without'' a proper <syntaxhighlight lang="delphi" inline>try</syntaxhighlight> is illegal).

Revision as of 16:43, 5 February 2021

A frame (frequently also referred to as block) is a language construct grouping a (possibly empty) sequence of statements (or instructions in the case of asm frames).

available types

All frames but repeat  until are terminated by the word end. Frame types are distinguished by their corresponding opening words.

  • Pascal: These frames expect Pascal statements or may contain other frames.
    begin
    This frame begins a (possibly empty) sequence of statements. In the context of routine definitions or a program it can delimit a scope.
    else/otherwise
    This frame surrounds a “catch-all”-alternative as part of a case statement.
    repeat  until
    repeat in conjunction with until is used to surround the loop body of a tail-controlled loop. It is the only frame type not ending with an end.
    exception treatment
    If exceptions are supported in the current compiler mode, the following frames are available as well. These frames are in fact “double”-frames: They group two sequences at once. Neither of them can be used independently (e. g. writing finally  end; without a proper try is illegal).
    tryexcept
    Use this to install exception handlers.
    tryfinally
    Use this to ensure a certain code fragment is executed despite any thrown exceptions.
    unit overhead
    initializationfinalization
    This double-frame designates code being executed when the corresponding unit is loaded or unloaded. Either part of this frame is optional. This frame may also delimit a scope.
    begin
    If there is no need for a finalization part, initialization can be replaced by begin.
  • Assembly language: Frames beginning with asm expect assembly language. In pure assembly routines, this kind of frame may delimit a scope, too. Note, you cannot nest other frames in asm frames.

style

Although not mandatory, it is customary to indent all code surrounded by frame markers by one level.

try
	openJar;
except
	throwATantrum;
end;

Some styles add another indentation level for nested or subordinate frame markers per se.

if apples = oranges then
  begin
    protest;
    halt(123);
  end;

technical background

Frames frequently, but not always, turn up to be (conditional) jmp targets. Some compile-time optimizations require code to be structured in a certain way, frames setting boundaries for that.