Pragmatic directives: Difference between revisions

m
(New post.)
 
(7 intermediate revisions by 5 users not shown)
Line 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 407 ⟶ 430:
public void Display() {
System.out.println("This author is indicating that this method is deprecated");
System.out.println("Note that the IDE has converted the method name to 'strikethrough' text");
}
Line 616 ⟶ 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 702 ⟶ 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 902 ⟶ 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 941 ⟶ 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 948 ⟶ 1,028:
}</syntaxhighlight>
 
<syntaxhighlight lang="ecmascriptwren">/* linux.wren */
 
class Linux {
Line 955 ⟶ 1,035:
}</syntaxhighlight>
 
<syntaxhighlight lang="ecmascriptwren">/*pragmatic_directives Pragmatic_directives.wren */
 
import "os" for Platform
Line 961 ⟶ 1,041:
var os
if (Platform.isWindows) {
import "./windows" for Windows
os = Windows
} else {
import "./linux" for Linux
os = Linux
}
Line 972 ⟶ 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