Native Code - brombres/Rogue GitHub Wiki
class XYZ
NATIVE
nativeHeader "#include <stdio.h>"
nativeHeader @|#include <this>
|#include <that>
nativeHeader "void c_function();"
nativeHeader File("XYZ.h")
nativeCode ...
@|void c_function()
|{
| ...
nativeCode File("XYZ.c")
A NATIVE class section allows native code to be defined.
nativeHeader "content string"
nativeHeader File("Filename.h")
nativeHeader defines content that will be appended to the .h file generated by the compiler.
If native header files are specified with relative paths, the path should be relative to folder containing the .rogue file being compiled.
nativeCode "content string"
nativeCode File("Filename.c")
nativeCode defines content that will be appended to the .c file generated by the compiler.
If native code files are specified with relative paths, the path should be relative to folder containing the .rogue file being compiled.
native "FILE* fp;"
# Inlines a native property in a PROPERTIES section.
native @|printf( "Rogue\n" );
# Inlines a native statement or series of statements.
local result = 2 * native( "cos($rads)" )->Real64
# Inlines a native expression
which (value)
case native("EnumType::Alpha")->LiteralInt32
# 'LiteralInt32' is a special case that designates the native code as resolving
# to a literal Int32, thereby allowing the 'which' to generate a more efficient
# 'switch' rather than a general 'if/elseIf' block.
Inline native commands can be used to create native properties, statements, and expressions. In this way Rogue code can easily interoperate with C code.
Content strings can be expressed using any of the various String notations: "double-quoted", 'single quote', ''two quote'', and @|verbatim.
The following markers (or escape sequences) can be used in inline native code.
| Marker | nativeC Replacement | Description |
|---|---|---|
| $this | THIS | Object self-reference. |
| $property_name | THIS->property_name | Native property name. |
| $local_name | local_name_3 | Native local name. |
| $TemplateParam | Template Arg | Inserts a template parameter. For instance, if template ABC<<$Type>> is instanced with ABC<<int>>, then a method writing native "$Type value;" generates native "int value;". |
| $<<var_name>> | RogueTypeName | Evaluates to the C type name of the specified variable |
| $<<TypeName>> | RogueTypeName | Evaluates to the C type name of the specified Rogue type |
class XYZ
PROPERTIES
...
nativeC "FILE* fp;"
METHODS
method atan2( y:Real64, x:Real64 )->Real64
return native( "atan2($y,$x)" )->Real64
method fopen( filename:String )->Logical
nativeC @|$this->fp = fopen( $filename->utf8, "rb" );
return native( "!!($this->fp)" )->Logical
method printf( value:String )
if (value) native @|printf( $value->utf8 );
else native @|printf( "null" );