Pragmatic directives

From Rosetta Code
Revision as of 23:17, 7 March 2012 by rosettacode>Xenoker (Added Ada)
Pragmatic directives is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Pragmatic directives cause the language to operate in a specific manner, allowing support for operational variances within the program code (possibly by the loading of specific or alternative modules).

The task is to list any pragmatic directives supported by the language, demostrate how to activate and deactivate the pragmatic directives and to describe or demonstate the scope of effect that the pragmatic directives have within a program.

Ada

Some common language defined pragmas:

  • pragma assert(expression, error_message)
  • pragma Import(...) and pragma Export(...) to interface other languages, commonly C
  • pragma Inline(function_name) perform inline expansion of the function
  • pragma Optimize(Time/Space/Off) Implementation defined, attempts to optimize memory usage for speed or time.
  • pragma Pack(type) attempts to minimize memory usage for the type, even if it means slower memory access. A representation clause specifying bit size is usually used instead of this.
  • pragma Suppress(identifier) and pragma Unsuppress(identifier) for enabling/disabling any of the many language checks.

Some pragmas are also implementation defined, the commonly used GNAT provides many, such as:

  • pragma Unreferenced(name) suppresses warnings about unused entities, and raises warnings if they are in fact referenced.

There are far too many pragmas to list here, but a standard informative list can be found in Annex L of the documentation if you have it installed. Or found at:


ALGOL 68

Works with: ALGOL 68 version Revision 1 - pragmas are permitted by the standard, e.g. "portcheck" is recommended for detecting language extensions, other pragma options are implementation specific.
Works with: ALGOL 68G version Any - tested with release algol68g-2.3.5 - most compiler directives are permitted as pragmas options - also ' pr read "filename.a68" pr ' is permitted to "include" a file.

File: Pragmatic_directives.a68<lang algol68>#!/usr/local/bin/a68g --script #

PRAGMAT portcheck PRAGMAT PR portcheck PR

BEGIN PR heap=256M PR # algol68g pragma #

 ~

END;

PROC (REAL)REAL s = sin();

SKIP</lang>Output:

10    PROC (REAL)REAL s = sin();
                             1  
a68g: warning: 1: generic-argument is an extension (detected in particular-program).

BASIC

Some versions of basic support the use of system trace directives that allow the program line or line number to be output.

Works with: BBC BASIC
Works with: GWBASIC

<lang basic>10 TRON: REM activate system trace pragma 20 TROFF: REM deactivate system trace pragma</lang>

D

The -d compiler switch allows deprecated D features in a program. It allows some deprecated features of C language, and user code wrapped inside deprecated{}.

Icon and Unicon

Icon and Unicon have a number of pragmatic modes. Most of these are controlled via keywords (See Special_variables#Icon_and_Unicon)). <lang Icon>&trace # controls execution tracing &error # controls error handling</lang>

Additionally, tracing can be controlled via the environment variable 'TRACE'.

Mathematica

Mathematica makes no formal difference between any normal and "specific" operation of the language. Any possible desired effect can be achieved by calling a function or setting a variable. Function calls are traced using the Trace[] function.

Perl

By convention pragmatic modules are named using lowercase letters.

List of pragmatic modules
  • diagnostics
  • english
  • feature
  • integer
  • lib
  • ops
  • sort
  • strict
  • switch
  • warnings
Utilization

Pragmatic modules have local scope and are utilized using the use directive:

<lang perl>use warnings; # use warnings pragma module use strict; # use strict pragma module</lang>

To disable behaviour of a pragmatic module:

<lang perl>no warnings; # disable warnings pragma module no strict; # disable strict pragma module</lang>

PicoLisp

PicoLisp makes no formal difference between any normal and "specific" operation of the language. Any possible desired effect can be achieved by calling a function or setting a variable. For example, function calls can be traced with the 'trace' function.

Python

Python has the __future__ module which controls certain features:

Python 3.2

<lang python>Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import __future__ >>> __future__.all_feature_names ['nested_scopes', 'generators', 'division', 'absolute_import', 'with_statement', 'print_function', 'unicode_literals', 'barry_as_FLUFL'] >>> </lang>

('barry_as_FLUFL' is an April fools joke)

Python 2.7

<lang python>Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import __future__ >>> __future__.all_feature_names ['nested_scopes', 'generators', 'division', 'absolute_import', 'with_statement', 'print_function', 'unicode_literals'] >>> </lang>

UNIX Shell

List of pragmatic directives
  • -v output the script line before it is executed
  • -x output the command line arguments
Utilization

Pragmatic directives remain effective, until they are deactivated, or the end of the script is reached:

<lang sh>set -vx # Activate both script line output and command line arguments pragma set +vx # Deactivate both pragmatic directives</lang>