Pragmatic directives: Difference between revisions

m
m (syntax highlighting fixup automation)
 
(9 intermediate revisions by 6 users not shown)
Line 196:
 
Above would check for the COCOA feature and if not present, load the library. The library is also expected to later push the feature COCOA to the features list. Reading this code then later would ignore the require statement.
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|}}
Delphi has dozens of directive that change the way the compiler and the language work.
 
Here is a link describing the way directive work in general:
 
[https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Delphi_compiler_directives Using Delphi Directives]
 
Here is a list of all the directive available in current versions of Delphi:
 
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Delphi_Compiler_Directives_(List)_Index
 
<syntaxhighlight lang="Delphi">
// Some examples
 
{$R *.dfm}
 
{$R WorldMaps.res}
 
{$optimization on,hints off}
 
{$B+}
{$R- Turn off range checking}
{$I TYPES.INC}
{$M 32768,40960}
{$DEFINE Debug}
{$IFDEF Debug}
{$ENDIF}
 
 
procedure DivMod(Dividend: Cardinal; Divisor: Word;
var Result, Remainder: Word);
{$IFDEF PUREPASCAL}
begin
Result := Dividend div Divisor;
Remainder := Dividend mod Divisor;
end;
{$ELSE !PUREPASCAL}
{$IFDEF X86ASM}
asm // StackAlignSafe
PUSH EBX
MOV EBX,EDX
MOV EDX,EAX
SHR EDX,16
DIV BX
MOV EBX,Remainder
MOV [ECX],AX
MOV [EBX],DX
POP EBX
end;
{$ENDIF X86ASM}
{$ENDIF !PUREPASCAL}
 
 
 
</syntaxhighlight>
{{out}}
<pre>
 
</pre>
 
 
=={{header|D}}==
Line 242 ⟶ 305:
 
Additionally, parsing words sometimes alter compiler behavior. For example, the compiler uses type information from <code>TYPED:</code> words for optimizations.
=={{header|FreeBASIC}}==
FreeBASIC does not have pragmatic directives in the traditional sense.
However, it has several built-in keywords and functions that can affect program behavior in a similar way to pragmatic directives in other languages.
 
Thus, FreeBASIC has keywords such as <code>Public</code> and <code>Private</code> that can change the visibility of variables and functions within a module.
<syntaxhighlight lang="vbnet">Private Sub i_am_private
End Sub
 
Public Sub i_am_public
End Sub</syntaxhighlight>
 
 
It also has keywords like <code>CDecl</code>, <code>Pascal</code> and <code>StdCall</code> that can change the calling convention of a function.
 
Cdecl is usually the default calling convention for C compilers and is used almost exclusively on Unix, Linux, *BSD, and DOS-like systems.
<syntaxhighlight lang="vbnet">' declaring 'strcpy' from the standard C library
Declare Function strcpy Cdecl Alias "strcpy" (Byval dest As ZString Ptr, Byval src As ZString Ptr) As ZString Ptr</syntaxhighlight>
 
Pascal is the default calling convention for procedures in Microsoft QuickBASIC and is the standard convention used in the Windows 3.1 API.
<syntaxhighlight lang="vbnet">Declare Function MyFunctionPascal Pascal Alias "MyFunction" (param As Integer) As Integer</syntaxhighlight>
 
Stdcall is the default calling convention on Windows and the most common in BASIC languages and the Windows API.
<syntaxhighlight lang="vbnet">Declare Function MyFunc1 Stdcall (param1 As Integer, param2 As Integer) As Integer</syntaxhighlight>
 
=={{header|Go}}==
Line 322 ⟶ 408:
3 :'y.]y=.7' 8
7</syntaxhighlight>
 
=={{header|Java}}==
The closest things which Java has to pragmatic directives are annotations which begin with the @ character.
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class PragmaticDirectives {
public static void main(String[] aArgs) {
/* Take no action */
}
@FunctionalInterface
public interface Adder { // This annotation indicates a functional interface,
abstract int add(int a, int b); // which has exactly one abstract method.
}
@Deprecated
public void Display() {
System.out.println("This author is indicating that this method is deprecated");
}
@SuppressWarnings("unchecked")
public void uncheckedWarning() {
List words = new ArrayList();
words.add("hello");
System.out.println("THe compiler is warning that the generic type declaration is missing.");
System.out.println("The correct syntax is: List<String> words = new ArrayList<String>()");
}
@SafeVarargs
public static <T> List<T> list(final T... items) {
System.out.println("This annotation suppresses unchecked warnings about a non-reifiable variable arity type");
return Arrays.asList(items);
}
}
</syntaxhighlight>
 
=={{header|Julia}}==
Line 512 ⟶ 638:
trace -- allow or disallow debugging (source code line-by-line tracing) <br>
debug -- turn debugging info generation on or off <br>
nested_globals -- fine-grained scope control, nested_locals is the complementary option <br>
 
The last two are related: without debug completely removes all tracing and diagnostics for a specific file (primarily intended for use in well-tested system routines), whereas (under with debug, which is the default) with/without trace can make debugging less tedious by not stopping on every line of irrelevant (user-selected) code, although dumps still contain a full call stack. Some other options, specifically indirect_includes, inline, and digital stamps, exist solely for compatability and are completely ignored.
Line 598 ⟶ 725:
Get-Help about_Requires
</syntaxhighlight>
 
=={{header|PureBasic}}==
PureBasic supports several pragmatic directives, also known as '''[https://www.purebasic.com/documentation/reference/compilerdirectives.html compiler directives]'''.
 
<code>CompilerIf…CompilerElseIf…CompilerElse…CompilerEndIf</code>: allows certain parts of the code to be compiled depending on the evaluation of a constant expression.
<syntaxhighlight lang="vb">CompilerIf #PB_Compiler_OS = #PB_OS_Linux
; some Linux specific code
CompilerElse
; some code for other operating systems
CompilerEndIf</syntaxhighlight>
 
<code>CompilerSelect…CompilerCase…CompilerDefault…CompilerEndSelect</code>: Allows you to select which code to compile based on a constant numeric value.
<syntaxhighlight lang="vb">CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_MacOS
; some Mac OS
CompilerCase #PB_OS_Linux
; some Linux specific code
CompilerEndSelect</syntaxhighlight>
 
<code>CompilerError</code> and <code>CompilerWarning</code>: generate an error or a warning, respectively.
<syntaxhighlight lang="vb">CompilerIf #PB_Compiler_OS = #PB_OS_Linux
CompilerError "Linux is not supported, sorry."
CompilerElse
CompilerWarning "OS supported, you can now comment me."
CompilerEndIf</syntaxhighlight>
 
<code>EnableExplicit</code> and <code>DisableExplicit1</code>: Enable or disable explicit mode. When enabled, all variables not explicitly declared with Define, Global, Protected, or Static will not be accepted and the compiler will generate an error.
<syntaxhighlight lang="vb">EnableExplicit
Defines
a = 20 ; Ok, as declared with 'Define'
b = 10 ; Will raise an error here</syntaxhighlight>
 
<code>EnableASM</code> and <code>DisableASM</code>: Enable or disable the inline assembler. When enabled, all assembler keywords are available directly in the source code.
<syntaxhighlight lang="vb">; x86 assembly example
Test = 10
EnableASM
MOV dword [v_Test],20
DisableASM
DebugTest ; Will be 20</syntaxhighlight>
 
It is important to note that the scope of these directives is global, that is, they affect all code after them until a directive is found that disables them or until the end of the code file.
 
=={{header|Python}}==
Line 798 ⟶ 966:
 
For external routines, the defaults are used. <br><br>
 
=={{header|RPL}}==
Pragmatic directives are given through system flags that can bet with the <code>SF</code> and <code>CF</code> commands anywhere in a program.
System flags are a legacy of programmable calculators and unfortunately, which flag to set or reset for a specific directive depends on the calculator model you are using.
As an example, here are the directives controlled by flags on HP-28 models:
* LAST variable activated
* Immediate evaluation of constants (e, pi...) and functions
* length of binary words
* display and print formats
* decimal sign and object separator
* real number display format
* sound on/off
* underflow/overflow error handling
 
 
Fortunately, some of them can also be operated through language commands, such as <code>STWS</code> (defines the length of binary words) or <code>RAD</code> (defines radian as the trigonometric unit).
 
=={{header|Scala}}==
Line 837 ⟶ 1,021:
Import statements can appear anywhere a variable declaration is allowed and in particular within conditional structures. This enables us to do things such as the following:
 
<syntaxhighlight lang="ecmascriptwren">/* windows.wren */
 
class Windows {
Line 844 ⟶ 1,028:
}</syntaxhighlight>
 
<syntaxhighlight lang="ecmascriptwren">/* linux.wren */
 
class Linux {
Line 851 ⟶ 1,035:
}</syntaxhighlight>
 
<syntaxhighlight lang="ecmascriptwren">/*pragmatic_directives Pragmatic_directives.wren */
 
import "os" for Platform
Line 857 ⟶ 1,041:
var os
if (Platform.isWindows) {
import "./windows" for Windows
os = Windows
} else {
import "./linux" for Linux
os = Linux
}
Line 868 ⟶ 1,052:
From the last script, when run on a Linux system.
<pre>
$ wren_cli pragmatic_directivesPragmatic_directives.wren
Using Linux which has a "\n" line separator.
</pre>
7,794

edits