Pragmatic directives

From Rosetta Code
Revision as of 13:55, 11 August 2012 by Rdm (talk | contribs) (→‎{{header|J}})
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{}.

Go

Go has a feature called build constraints that work on the level of whole files. A comment line reading

<lang go>// +build <expression></lang>

will cause the entire file to be excluded from a build unless <expression> is true. The elements, called tags, in the expression are typically things like the target operating system or hardware architecture. For example

<lang go>// +build linux</lang>

will include the file if the target OS is linux, but will exclude the file otherwise. Arbitrary tags can be passed on the command line of the go command. A file could begin

<lang go>// +build Tuesday</lang>

and the build command

<lang bash>go install -tags `date +%A`</lang>

would only include the file on Tuesdays.

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'.

J

J's foreign global parameters possibly qualify as pragmatic directives.

They are analogous to setting (or reading) variables.

Here's a list of the settings:

9!:1 random seed (incomplete specification of state -- see 9!:45)
9!:3 default display for non-nouns
9!:7 box drawing characters
9!:9 error messages
9!:11 print precision
9!:17 centering (or not) when box contents are smaller than boxes 
9!:19 comparison tolerance
9!:21 memory limit
9!:25 security level
9!:27 text of immediate execution phrase
9!:29 enable immediate execution phrase
9!:33 execution time limit
9!:35 disable (or re-enable) assertions
9!:37 output control
9!:39 locales' hash table size
9!:41 retain (or not) whitespace and comments in explicit definitions
9!:43 which random number generator to use?
9!:45 what is the current state of that rng?
9!:49 enable reserved words for argument names 

For example,

<lang j> 9!:25]1</lang>

disables access to the file system (and disables some other features, including the ability to exit the program, because the system exit mechanism is a system feature and thus not trusted). You cannot turn this off after it has been turned on (so you will need to shut down J and start it again if you want to use those features).

Or, for example, y is the usual name for the right argument of a verb.

<lang j> 3 :'y' 8 8</lang>

But y is also a regular variable. So J also offers a reserved word y. which serves the same role. But this is disabled by default (because mostly it's just an unnecessary complication).

<lang j> 3 :'y.' 8 |spelling error</lang>

But you can enable the reserved word mechanism:

<lang j> 9!:49]1

  3 :'y.' 8

8

  3 :'y.[y=.7' 8

8

  3 :'y.]y=.7' 8

7</lang>

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>

Tcl

Mostly Tcl avoids anything like pragmas as they are a source of portability trouble. However, certain global variables can be set to induce non-standard behaviors:

tcl_precision
This is used to control how Tcl converts floating point numbers to strings, and represents the number of significant figures to use. From Tcl 8.5 onwards this should be left at its default (which uses the minimum number of digits required to represent the number exactly on conversion back to a double-precision float) and the format command used where an exact number of digits is required.
tcl_traceCompile
This is used (provided it is enabled at library-build time) to enable printing out information about the compilation of Tcl scripts to bytecodes. (Not useful in wish on Windows due to system issues.)
tcl_traceExec
This is used (provided it is enabled at library-build time) to enable printing out information about the execution of Tcl's bytecodes and when different procedures are called. (Not useful in wish on Windows due to system issues.)
tcl_interactive
This marks whether the “convenience” features of the top-level REPL should be enabled; in non-interactive mode, Tcl scripts typically don't prompt for commands to execute, never allow abbreviation of command names, never automatically run external commands, etc.

Under normal circumstances, all of these variables should be left alone; their default values are virtually always correct for programs.

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>