Sophie

Sophie

distrib > Fedora > 13 > i386 > media > os > by-pkgid > 52a37fb77746ef557a2ec666070d732e > files > 31

bigloo-doc-3.2b-3.fc12.i686.rpm

<!-- 95% W3C COMPLIANT, 95% CSS FREE, RAW HTML -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title>BiglooA ``practical Scheme compiler''User manual for version 3.2bJune 2009</title>
 <style type="text/css">
  <!--
  pre { font-family: monospace }
  tt { font-family: monospace }
  code { font-family: monospace }
  p.flushright { text-align: right }
  p.flushleft { text-align: left }
  span.sc { font-variant: small-caps }
  span.sf { font-family: sans-serif }
  span.skribetitle { font-family: sans-serif; font-weight: bolder; font-size: x-large; }
  span.refscreen { }
  span.refprint { display: none; }
  -->
 </style>
</head>

<body class="chapter" bgcolor="#ffffff">
<table width="100%" class="skribetitle" cellspacing="0" cellpadding="0"><tbody>
<tr><td align="center" bgcolor="#8381de"><div class="skribetitle"><strong><big><big><big>25. Bigloo<br/>A ``practical Scheme compiler''<br/>User manual for version 3.2b<br/>June 2009 -- The C interface</big></big></big></strong></div><center>
</center>
</td></tr></tbody></table>
<table cellpadding="3" cellspacing="0" width="100%" class="skribe-margins"><tr>
<td align="left" valign="top" class="skribe-left-margin" width="20%" bgcolor="#dedeff"><div class="skribe-left-margin">
<br/><center id='center30218'
><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody>
<tr bgcolor="#8381de"><th id="tc30208" align="center" colspan="1"><font color="#ffffff"><strong id='bold30206'
>main page</strong></font></th></tr>
<tr bgcolor="#ffffff"><td id="tc30215" align="center" colspan="1"><table width="100%" border="0" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc30211" align="left" valign="top" colspan="1"><strong id='bold30210'
>top:</strong></td><td id="tc30212" align="right" valign="top" colspan="1"><a href="bigloo.html#Bigloo-A-``practical-Scheme-compiler''-User-manual-for-version-3.2b-June-2009" class="inbound">Bigloo<br/>A ``practical Scheme compiler''<br/>User manual for version 3.2b<br/>June 2009</a></td></tr>
</tbody></table>
</td></tr>
</tbody></table>
</center>
<br/><br/><center id='center30228'
><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody>
<tr bgcolor="#8381de"><th id="tc30222" align="center" colspan="1"><font color="#ffffff"><strong id='bold30220'
>The C interface</strong></font></th></tr>
<tr bgcolor="#ffffff"><td id="tc30225" align="center" colspan="1"><table cellspacing="1" cellpadding="1" width="100%" class="toc">
<tbody>
 <tr><td valign="top" align="left">25.1</td><td colspan="4" width="100%"><a href="bigloo-26.html#The-syntax-of-the-foreign-declarations">The syntax of the foreign declarations</a></td></tr>
 <tr><td></td><td valign="top" align="left">25.1.1</td><td colspan="3" width="100%"><a href="bigloo-26.html#Automatic-extern-clauses-generation">Automatic extern clauses generation</a></td></tr>
 <tr><td></td><td valign="top" align="left">25.1.2</td><td colspan="3" width="100%"><a href="bigloo-26.html#Importing-an-extern-variable">Importing an extern variable</a></td></tr>
 <tr><td></td><td valign="top" align="left">25.1.3</td><td colspan="3" width="100%"><a href="bigloo-26.html#Importing-an-extern-function">Importing an extern function</a></td></tr>
 <tr><td></td><td valign="top" align="left">25.1.4</td><td colspan="3" width="100%"><a href="bigloo-26.html#Including-an-extern-file">Including an extern file</a></td></tr>
 <tr><td></td><td valign="top" align="left">25.1.5</td><td colspan="3" width="100%"><a href="bigloo-26.html#Exporting-a-Scheme-variable">Exporting a Scheme variable</a></td></tr>
 <tr><td></td><td valign="top" align="left">25.1.6</td><td colspan="3" width="100%"><a href="bigloo-26.html#Defining-an-extern-type">Defining an extern type</a></td></tr>
 <tr><td valign="top" align="left">25.2</td><td colspan="4" width="100%"><a href="bigloo-26.html#The-very-dangerous-``pragma''-Bigloo-special-forms">The very dangerous ``pragma'' Bigloo special forms</a></td></tr>
 <tr><td valign="top" align="left">25.3</td><td colspan="4" width="100%"><a href="bigloo-26.html#Name-mangling">Name mangling</a></td></tr>
 <tr><td valign="top" align="left">25.4</td><td colspan="4" width="100%"><a href="bigloo-26.html#Embedded-Bigloo-applications">Embedded Bigloo applications</a></td></tr>
 <tr><td valign="top" align="left">25.5</td><td colspan="4" width="100%"><a href="bigloo-26.html#Using-C-bindings-within-the-interpreter">Using C bindings within the interpreter</a></td></tr>
</tbody>
</table>
</td></tr>
</tbody></table>
</center>
<br/><br/><center id='center30238'
><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody>
<tr bgcolor="#8381de"><th id="tc30232" align="center" colspan="1"><font color="#ffffff"><strong id='bold30230'
>Chapters</strong></font></th></tr>
<tr bgcolor="#ffffff"><td id="tc30235" align="center" colspan="1"><table cellspacing="1" cellpadding="1" width="100%" class="toc">
<tbody>
 <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="bigloo-1.html#Acknowledgements">Acknowledgements</a></td></tr>
 <tr><td valign="top" align="left">1</td><td colspan="4" width="100%"><a href="bigloo-2.html#Table-of-contents">Table of contents</a></td></tr>
 <tr><td valign="top" align="left">2</td><td colspan="4" width="100%"><a href="bigloo-3.html#Overview-of-Bigloo">Overview of Bigloo</a></td></tr>
 <tr><td valign="top" align="left">3</td><td colspan="4" width="100%"><a href="bigloo-4.html#Modules">Modules</a></td></tr>
 <tr><td valign="top" align="left">4</td><td colspan="4" width="100%"><a href="bigloo-5.html#Core-Language">Core Language</a></td></tr>
 <tr><td valign="top" align="left">5</td><td colspan="4" width="100%"><a href="bigloo-6.html#DSSSL-support">DSSSL support</a></td></tr>
 <tr><td valign="top" align="left">6</td><td colspan="4" width="100%"><a href="bigloo-7.html#Standard-Library">Standard Library</a></td></tr>
 <tr><td valign="top" align="left">7</td><td colspan="4" width="100%"><a href="bigloo-8.html#Pattern-Matching">Pattern Matching</a></td></tr>
 <tr><td valign="top" align="left">8</td><td colspan="4" width="100%"><a href="bigloo-9.html#Fast-search">Fast search</a></td></tr>
 <tr><td valign="top" align="left">9</td><td colspan="4" width="100%"><a href="bigloo-10.html#Structures-and-Records">Structures and Records</a></td></tr>
 <tr><td valign="top" align="left">10</td><td colspan="4" width="100%"><a href="bigloo-11.html#Object-System">Object System</a></td></tr>
 <tr><td valign="top" align="left">11</td><td colspan="4" width="100%"><a href="bigloo-12.html#Regular-parsing">Regular parsing</a></td></tr>
 <tr><td valign="top" align="left">12</td><td colspan="4" width="100%"><a href="bigloo-13.html#Lalr(1)-parsing">Lalr(1) parsing</a></td></tr>
 <tr><td valign="top" align="left">13</td><td colspan="4" width="100%"><a href="bigloo-14.html#Posix-Regular-Expressions">Posix Regular Expressions</a></td></tr>
 <tr><td valign="top" align="left">14</td><td colspan="4" width="100%"><a href="bigloo-15.html#Command-Line-Parsing">Command Line Parsing</a></td></tr>
 <tr><td valign="top" align="left">15</td><td colspan="4" width="100%"><a href="bigloo-16.html#Cryptography">Cryptography</a></td></tr>
 <tr><td valign="top" align="left">16</td><td colspan="4" width="100%"><a href="bigloo-17.html#Errors-Assertions-and-Traces">Errors, Assertions, and Traces</a></td></tr>
 <tr><td valign="top" align="left">17</td><td colspan="4" width="100%"><a href="bigloo-18.html#Threads">Threads</a></td></tr>
 <tr><td valign="top" align="left">18</td><td colspan="4" width="100%"><a href="bigloo-19.html#Database-library">Database library</a></td></tr>
 <tr><td valign="top" align="left">19</td><td colspan="4" width="100%"><a href="bigloo-20.html#Multimedia-library">Multimedia library</a></td></tr>
 <tr><td valign="top" align="left">20</td><td colspan="4" width="100%"><a href="bigloo-21.html#Mail-library">Mail library</a></td></tr>
 <tr><td valign="top" align="left">21</td><td colspan="4" width="100%"><a href="bigloo-22.html#Eval-and-code-interpretation">Eval and code interpretation</a></td></tr>
 <tr><td valign="top" align="left">22</td><td colspan="4" width="100%"><a href="bigloo-23.html#Macro-expansion">Macro expansion</a></td></tr>
 <tr><td valign="top" align="left">23</td><td colspan="4" width="100%"><a href="bigloo-24.html#Parameters">Parameters</a></td></tr>
 <tr><td valign="top" align="left">24</td><td colspan="4" width="100%"><a href="bigloo-25.html#Explicit-typing">Explicit typing</a></td></tr>
 <tr><td valign="top" align="left">25</td><td colspan="4" width="100%"><a href="bigloo-26.html#The-C-interface">The C interface</a></td></tr>
 <tr><td valign="top" align="left">26</td><td colspan="4" width="100%"><a href="bigloo-27.html#The-Java-interface">The Java interface</a></td></tr>
 <tr><td valign="top" align="left">27</td><td colspan="4" width="100%"><a href="bigloo-28.html#Bigloo-Libraries">Bigloo Libraries</a></td></tr>
 <tr><td valign="top" align="left">28</td><td colspan="4" width="100%"><a href="bigloo-29.html#Extending-the-Runtime-System">Extending the Runtime System</a></td></tr>
 <tr><td valign="top" align="left">29</td><td colspan="4" width="100%"><a href="bigloo-30.html#SRFIs">SRFIs</a></td></tr>
 <tr><td valign="top" align="left">30</td><td colspan="4" width="100%"><a href="bigloo-31.html#Compiler-description">Compiler description</a></td></tr>
 <tr><td valign="top" align="left">31</td><td colspan="4" width="100%"><a href="bigloo-32.html#User-Extensions">User Extensions</a></td></tr>
 <tr><td valign="top" align="left">32</td><td colspan="4" width="100%"><a href="bigloo-33.html#Bigloo-Development-Environment">Bigloo Development Environment</a></td></tr>
 <tr><td valign="top" align="left">33</td><td colspan="4" width="100%"><a href="bigloo-34.html#Global-Index">Global Index</a></td></tr>
 <tr><td valign="top" align="left">34</td><td colspan="4" width="100%"><a href="bigloo-35.html#Library-Index">Library Index</a></td></tr>
 <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="bigloo-36.html#Bibliography">Bibliography</a></td></tr>
</tbody>
</table>
</td></tr>
</tbody></table>
</center>
</div></td>
<td align="left" valign="top" class="skribe-body"><div class="skribe-body">
<a name="C-Interface" class="mark"></a><a name="g22946" class="mark"></a>
We call all the pieces of program devoted to the interactions between Scheme
and another language a <em id='emph22948'
>foreign interface</em>. In Bigloo, the foreign 
interface allows Scheme's functions and variables to be exported to a 
foreign language and foreign functions and variables to be imported into 
the Scheme code. Using the foreign interface requires two kind of operations.<br/><br/><ul class="itemize" id='itemize22952'
><li>Declarations --- type declarations, 
import declarations or export declarations.
</li>
<li>Foreign reference in the Scheme code.
</li>
</ul>
Declarations take place in a special module clause, see 
<a href="bigloo-4.html#Module-Declaration" class="inbound">Module Declaration</a>, and reference to foreign variables within 
Scheme code requires no special construction. The current release 
of Bigloo includes a C and a Java interface. The Java connection is
specified by the means of a <code id='code22953'
>java</code> clause (see <a href="bigloo-27.html#Java-Interface" class="inbound">Java Interface</a>).
The C interface is active (that is the <code id='code22954'
>extern</code> module clauses are
read) only when compiling to C. So, when compiling to Jvm the binding
declared in an <code id='code22955'
>extern</code> clause are not bound.<br/><br/>
<!-- The syntax of the foreign declarations -->
<a name="The-syntax-of-the-foreign-declarations"></a>
<div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">25.1 The syntax of the foreign declarations</font>
</h3></td></tr></table>
</div><div class="section">
<a name="The-syntax-of-the-foreign-declarations" class="mark"></a><a name="g22957" class="mark"></a>The syntax of <em id='emph22959'
>foreign</em> clauses is defined by:<br/><br/><center id='center22963'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog22961'
>&lt;extern&gt; ==&gt; &lt;variable-clause&gt;
     | &lt;function-clause&gt;
     | &lt;include-clause&gt;
     | &lt;export-clause&gt;
     | &lt;type-clause&gt;
</pre>
</td></tr>
</tbody></table></center>

Foreign clauses are automatically ``transmitted'' by the importation
process.  That is, if module <code id='code22964'
>module1</code> imports a module
<code id='code22965'
>module2</code>, <code id='code22966'
>module</code> treats the <code id='code22967'
>extern</code> clauses of
<code id='code22968'
>module2</code> as though they were included in its own module
declaration. Redefinition of a variable or a function already defined in
an foreign clause is an error.<br/><br/>
<!-- Automatic extern clauses generation -->
<a name="Automatic-extern-clauses-generation"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">25.1.1 Automatic extern clauses generation</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Automatic-extern-clauses-generation" class="mark"></a><a name="g22970" class="mark"></a><a name="g22973" class="mark"></a>
Extern clauses can be automatically generated using the Cigloo program
which is distributed in the same package as Bigloo. Using Cigloo may be
a good way to understand how C prototypes (and types) have to be
declared in Bigloo. Cigloo reads C files and generates the Bigloo
extern clauses for that files.<br/><br/></div>
<!-- Importing an extern variable -->
<a name="Importing-an-extern-variable"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">25.1.2 Importing an extern variable</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Importing-an-extern-variable" class="mark"></a><a name="g22976" class="mark"></a>
The &lt;variable-clause&gt; denotes importation of variables.
<center id='center22992'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog22990'
>&lt;variable-clause&gt; ==&gt; <code id='code22978'
>(</code> &lt;typed-ident&gt; &lt;c-name&gt;<code id='code22979'
>)</code>
     | <code id='code22980'
>(macro</code> &lt;typed-ident&gt; &lt;string&gt;<code id='code22981'
>)</code>
     | <code id='code22982'
>(macro</code> &lt;typed-ident&gt; <code id='code22983'
>(</code>&lt;typed-ident&gt;+<code id='code22984'
>)</code> &lt;string&gt;<code id='code22985'
>)</code>
     | <code id='code22986'
>(infix macro</code> &lt;typed-ident&gt; <code id='code22987'
>(</code>&lt;typed-ident&gt;+<code id='code22988'
>)</code> &lt;string&gt;<code id='code22989'
>)</code>
</pre>
</td></tr>
</tbody></table></center>

Only extern ``non-<code id='code22993'
>macro</code>'' variables are mutable (that is
mutable using the <code id='code22994'
>set!</code> construction). Bigloo does not emit
``extern C prototype'' for variables introduced by a <code id='code22995'
>macro</code>
clause. &lt;string&gt; is the C name of variable. The Scheme name
of that variable is extracted from the &lt;typed-ident&gt;.<br/><br/>Here is an example of variable importations:
<center id='center23011'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23009'
>(<font color="#1919af"><strong id='bold30239'
>module</strong></font> <font color="#1919af"><strong id='bold30241'
>example</strong></font>
   (<strong id='bold30243'
>extern</strong> (c-var<font color="#00cf00"><strong id='bold30244'
>::double</strong></font> <font color="red">&quot;c_var&quot;</font>)
           (macro bufsiz<font color="#00cf00"><strong id='bold30247'
>::long</strong></font> <font color="red">&quot;BUFSIZ&quot;</font>)))<br/><br/>(print <font color="red">&quot;c-var: &quot;</font> c-var)
(<strong id='bold30251'
>set!</strong> c-var (+ 1.0 c-var))
(print <font color="red">&quot;c-var: &quot;</font> c-var)
(print <font color="red">&quot;bufsize: &quot;</font> BUFSIZ)
</pre>
</td></tr>
</tbody></table></center>

</div>
<!-- Importing an extern function -->
<a name="Importing-an-extern-function"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">25.1.3 Importing an extern function</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Importing-an-extern-function" class="mark"></a><a name="g23012" class="mark"></a>
Function are imported using the &lt;function-clause&gt;.<br/><br/><center id='center23033'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog23031'
>&lt;function-clause&gt; ==&gt; <code id='code23015'
>(</code>&lt;typed-ident&gt; <code id='code23016'
>(</code>&lt;typed-ident&gt;*<code id='code23017'
>)</code> &lt;string&gt;<code id='code23018'
>)</code>
     | <code id='code23019'
>(</code>&lt;typed-ident&gt; <code id='code23020'
>(</code>&lt;typed-ident&gt;+ . &lt;typed-ident&gt;<code id='code23021'
>)</code> &lt;string&gt;<code id='code23022'
>)</code>
     | <code id='code23023'
>(macro</code> &lt;typed-ident&gt; <code id='code23024'
>(</code>&lt;typed-ident&gt;*<code id='code23025'
>)</code> &lt;string&gt;<code id='code23026'
>)</code>
     | <code id='code23027'
>(macro</code> &lt;typed-ident&gt; <code id='code23028'
>(</code>&lt;typed-ident&gt;+ . &lt;typed-ident&gt;<code id='code23029'
>)</code> &lt;string&gt;<code id='code23030'
>)</code>
</pre>
</td></tr>
</tbody></table></center>

The function result type and Scheme name are extracted from the
&lt;typed-ident&gt;; the &lt;typed-ident&gt; denotes the type of the function
arguments and &lt;string&gt; is the C name of the function. Bigloo
does not produce ``C extern prototype'' for macro functions
(those introduced by <code id='code23034'
>macro</code> clauses). If the typed identifier
of the function does not contain any type information. Bigloo will 
emit a warning message when compiling and it will use a default C type 
(e.g. the <code id='code23035'
>int</code> C type) as the return type of the function.<br/><br/><center id='center23049'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23047'
>(<font color="#1919af"><strong id='bold30254'
>module</strong></font> <font color="#1919af"><strong id='bold30256'
>example</strong></font>
   (<strong id='bold30258'
>extern</strong> (macro prn<font color="#00cf00"><strong id='bold30259'
>::int</strong></font> (<font color="#00cf00"><strong id='bold30261'
>::string</strong></font> . <font color="#00cf00"><strong id='bold30263'
>::long</strong></font>) <font color="red">&quot;printf&quot;</font>)))<br/><br/>(<strong id='bold30266'
>let</strong> ((n (read)))
   (prn <font color="red">#&quot;fib(%d): %d\n&quot;</font> n (fib n)))
</pre>
</td></tr>
</tbody></table></center>
<br/><br/></div>
<!-- Including an extern file -->
<a name="Including-an-extern-file"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">25.1.4 Including an extern file</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Including-an-extern-file" class="mark"></a><a name="g23051" class="mark"></a>
C files can be included in the C code produced by using
&lt;include-clause&gt;.<br/><br/><center id='center23058'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog23056'
>&lt;include-clause&gt; ==&gt; <code id='code23054'
>(include</code> &lt;string&gt;<code id='code23055'
>)</code>
</pre>
</td></tr>
</tbody></table></center>

</div>
<!-- Exporting a Scheme variable -->
<a name="Exporting-a-Scheme-variable"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">25.1.5 Exporting a Scheme variable</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Exporting-a-Scheme-variable" class="mark"></a><a name="g23059" class="mark"></a>
A Scheme variable (or function) can be exported to the
foreign world if and only if it is also exported using an <code id='code23061'
>export</code> 
clause. Type information is given in the Scheme
exportation, thus, the only requirement for a variable
to be extern exported is to be given a foreign name. The
foreign &lt;export-clause&gt; does this:<br/><br/><center id='center23067'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog23065'
>&lt;export-clause&gt; ==&gt; <code id='code23063'
>(export</code> &lt;ident&gt; &lt;string&gt;<code id='code23064'
>)</code>
</pre>
</td></tr>
</tbody></table></center>
 
Here is an example of exportation:<br/><br/><center id='center23084'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23082'
>(<font color="#1919af"><strong id='bold30268'
>module</strong></font> <font color="#1919af"><strong id='bold30270'
>example</strong></font>
   (<font color="#1919af"><strong id='bold30272'
>export</strong></font> (<font color="#1919af"><strong id='bold30274'
>fib</strong></font><font color="#00cf00"><strong id='bold30276'
>::long</strong></font> <font color="#00cf00"><strong id='bold30278'
>::long</strong></font>))
   (<strong id='bold30280'
>extern</strong> (<font color="#1919af"><strong id='bold30281'
>export</strong></font> <font color="#1919af"><strong id='bold30283'
>fib</strong></font> <font color="red">&quot;scheme_fib&quot;</font>)))<br/><br/>(<font color="#6959cf"><strong id='bold30286'
>define</strong></font> (<font color="#6959cf"><strong id='bold30288'
>fib</strong></font> x) (<strong id='bold30290'
>if</strong> (&lt; x 2) 1 ...))
</pre>
</td></tr>
</tbody></table></center>

</div>
<!-- Defining an extern type -->
<a name="Defining-an-extern-type"></a>
<div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">25.1.6 Defining an extern type</font>
</h3></td></tr></table>
</div><div class="subsection">
<a name="Defining-an-extern-type" class="mark"></a><a name="g23085" class="mark"></a>
New Bigloo types can be defined using extern
&lt;type-clause&gt;. These newly introduced types can be used in any
declaration (that is in any extern <em id='emph23087'
>or</em> Scheme module clause
and in any Scheme variable or function definition). The syntax of
&lt;type-clause&gt; is:<br/><br/><center id='center23093'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog23091'
>&lt;type-clause&gt; ==&gt; <code id='code23089'
>(type</code> &lt;ident&gt; &lt;type-def&gt; &lt;string&gt;<code id='code23090'
>)</code>
&lt;type-def&gt; ==&gt; &lt;atomic-type&gt;
     | &lt;ident&gt;
     | &lt;struct-type&gt;
     | &lt;union-type&gt;
     | &lt;function-type&gt;
     | &lt;array-type&gt;
     | &lt;pointer-type&gt;
     | &lt;enum-type&gt;
     | &lt;opaque-type&gt;
</pre>
</td></tr>
</tbody></table></center>

The symbol &lt;ident&gt; is the Scheme name of the introduced type and
&lt;string&gt; is the C name of the type. When Bigloo produces the definition
of a variable v of type s, it produces the following C code: s v<code id='code23094'
>;</code>. 
This rules applies unless s is a pointer or an array and then, to 
produce a C definition, the name of the elements of the array or the elements
pointed by the pointer type are used. Hence, if v is for instance <code id='code23095'
>foo</code>
and s is <code id='code23096'
>(array int)</code> the produced C code will be: <code id='code23097'
>int *foo</code>.<br/><br/>
<!-- Atomic types -->
<a name="Atomic-types"></a>
<div class="subsubsection-atitle"><h4><font color="#8381de">25.1.6.1 Atomic types</font>
</h4></div><div class="subsubsection">
<a name="Atomic-types" class="mark"></a><a name="g23099" class="mark"></a>The atomic types are the pre-existing ones, defined in the standard Bigloo's
library. 
<center id='center23142'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog23140'
>&lt;atomic-type&gt; ==&gt; &lt;bigloo-type&gt; 
     | &lt;c-type&gt;                                   
&lt;bigloo-type&gt; ==&gt; <code id='code23101'
>obj</code> <code id='code23102'
>procedure</code> 
     | <code id='code23103'
>pair</code> | <code id='code23104'
>nil</code> | <code id='code23105'
>pair-nil</code> | <code id='code23106'
>bint</code>          
     | <code id='code23107'
>blong</code> | <code id='code23108'
>bbool</code> | <code id='code23109'
>cnst</code>          
     | <code id='code23110'
>bstring</code> | <code id='code23111'
>bchar</code> | <code id='code23112'
>real</code>          
     | <code id='code23113'
>vector</code> | <code id='code23114'
>tvector</code> | <code id='code23115'
>struct</code>        
     | <code id='code23116'
>tstruct</code> | <code id='code23117'
>output-port</code> | <code id='code23118'
>input-port</code>    
     | <code id='code23119'
>binary-port</code> | <code id='code23120'
>unspecified</code> | <code id='code23121'
>symbol</code> 
     | <code id='code23122'
>foreign</code>       
&lt;c-type&gt; ==&gt; <code id='code23123'
>cobj</code> <code id='code23124'
>char</code> 
     | <code id='code23125'
>uchar</code> | <code id='code23126'
>short</code>    
     | <code id='code23127'
>ushort</code> | <code id='code23128'
>int</code> | <code id='code23129'
>uint</code> | <code id='code23130'
>long</code>
     | <code id='code23131'
>ulong</code> | <code id='code23132'
>slong</code> | <code id='code23133'
>bool</code> | <code id='code23134'
>string</code> 
     | <code id='code23135'
>file</code> | <code id='code23136'
>double</code> | <code id='code23137'
>float</code> | <code id='code23138'
>void</code> 
     | <code id='code23139'
>function</code>
</pre>
</td></tr>
</tbody></table></center>

<a name="g23144" class="mark"></a><a name="g23147" class="mark"></a><a name="g23150" class="mark"></a>
The type <code id='code23152'
>obj</code> denotes the super type of all Bigloo types (i.e.,
all Bigloo types, such as <code id='code23153'
>procedure</code>, <code id='code23154'
>pair</code>, ...) is an
<code id='code23155'
>obj</code>.  The type <code id='code23156'
>cobj</code> denotes the super of all C types
(i.e., all preexisting C types such as <code id='code23157'
>char</code>, <code id='code23158'
>uchar</code>,
<code id='code23159'
>schar</code>, <code id='code23160'
>short</code>, ...). The type <code id='code23161'
>pair-nil</code> denotes
values that are either pairs or the <code id='code23162'
>()</code> value.<br/><br/></div>
<!-- Struct and Union types -->
<a name="Struct-and-Union-types"></a>
<div class="subsubsection-atitle"><h4><font color="#8381de">25.1.6.2 Struct and Union types</font>
</h4></div><div class="subsubsection">
<a name="C-structures-and-unions" class="mark"></a><a name="g23164" class="mark"></a>C struct and Union types can be declared in Bigloo using
&lt;struct-type&gt; clauses:<br/><br/><center id='center23177'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog23175'
>&lt;struct-type&gt; ==&gt; <code id='code23167'
>(struct</code> <code id='code23168'
>(</code>&lt;typed-ident&gt; &lt;string&gt;<code id='code23169'
>)^+</code><code id='code23170'
>)</code>   
&lt;union-type&gt; ==&gt; <code id='code23171'
>(union</code> <code id='code23172'
>(</code>&lt;typed-ident&gt; &lt;string&gt;<code id='code23173'
>)^+</code><code id='code23174'
>)</code>
</pre>
</td></tr>
</tbody></table></center>

This clause declared a C struct but C structure values <em id='emph23178'
>cannot</em> 
be handled by Bigloo. Instead Bigloo is able to handle
<em id='emph23179'
>pointers to</em> C structure. Thus, in order to help the
definition of extern types, when a struct named <code id='code23181'
><em id='it23180'
>struct</em></code> is
defined, if it does not exists yet, Bigloo automatically defines
a type <em id='emph23182'
>pointer to the structure</em>. This type is named 
<code id='code23184'
><em id='it23183'
>struct</em></code><code id='code23185'
>*</code>.<br/><br/>When a pointer to a structure type is defined, Bigloo
automatically produces functions to manipulate objects of this
type. Let us suppose the type definition of <code id='code23188'
><em id='it23187'
>struct</em></code><code id='code23189'
>*</code>: <br/><br/><center id='center23208'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23206'
>(<strong id='bold30291'
>type</strong> <code id='code23192'
><em id='it23191'
>struct</em></code>
   (struct (<code id='code23194'
><em id='it23193'
>id1</em></code>::<code id='code23196'
><em id='it23195'
>type1</em></code> <code id='code23198'
><em id='it23197'
>name1</em></code>)
           ...
           (<code id='code23200'
><em id='it23199'
>idn</em></code>::<code id='code23202'
><em id='it23201'
>typen</em></code> <code id='code23204'
><em id='it23203'
>namen</em></code>))
</pre>
</td></tr>
</tbody></table></center>

The following functions are created:<br/><br/><ul class="itemize" id='itemize23302'
><li>A creator: 
<center id='center23220'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23218'
>(<code id='code23211'
><em id='it23210'
>struct</em></code>*::<code id='code23213'
><em id='it23212'
>struct</em></code>* ::<code id='code23215'
><em id='it23214'
>type_1</em></code> ... ::<code id='code23217'
><em id='it23216'
>type_n</em></code>)
</pre>
</td></tr>
</tbody></table></center>

This function allocates a fresh <code id='code23222'
><em id='it23221'
>struct</em></code><code id='code23223'
>*</code> (in the same heap as
any Scheme value) and fills the fields of the C structure with the proper
values provided in the call.<br/><br/></li>
<li>A type checker: 
<center id='center23234'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23232'
>(<code id='code23227'
><em id='it23226'
>struct</em></code>*?<font color="#00cf00"><strong id='bold30292'
>::bool</strong></font> <code id='code23229'
><em id='it23228'
>obj</em></code><font color="#00cf00"><strong id='bold30294'
>::obj</strong></font>)
</pre>
</td></tr>
</tbody></table></center>

This function returns <code id='code23235'
>#t</code> if and only if the argument <code id='code23237'
><em id='it23236'
>obj</em></code> 
is of type <code id='code23239'
><em id='it23238'
>struct</em></code><code id='code23240'
>*</code>.<br/><br/></li>
<li>A null checker: 
<center id='center23250'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23248'
>(<code id='code23244'
><em id='it23243'
>struct</em></code>*-null?<font color="#00cf00"><strong id='bold30296'
>::bool</strong></font> ::<code id='code23246'
><em id='it23245'
>struct</em></code>*)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23251'
>#t</code> if and only if its argument is <code id='code23252'
>Null</code>.<br/><br/></li>
<li>A null creator: 
<center id='center23261'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23259'
>(make-null-<code id='code23256'
><em id='it23255'
>struct</em></code>::<code id='code23258'
><em id='it23257'
>struct</em></code>*)
</pre>
</td></tr>
</tbody></table></center>
This function creates a <code id='code23262'
>NULL</code> value of type <code id='code23264'
><em id='it23263'
>struct</em></code>*.<br/><br/></li>
<li>An equality checker: 
<center id='center23276'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23274'
>(=<code id='code23268'
><em id='it23267'
>struct</em></code>*?<font color="#00cf00"><strong id='bold30298'
>::bool</strong></font> ::<code id='code23270'
><em id='it23269'
>struct</em></code>* ::<code id='code23272'
><em id='it23271'
>struct</em></code>*)
</pre>
</td></tr>
</tbody></table></center>

This function returns <code id='code23277'
>#t</code> if and only if its arguments are equal.<br/><br/></li>
<li>Accessors and mutators: 
<center id='center23299'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23297'
>(<code id='code23281'
><em id='it23280'
>struct</em></code>*-<code id='code23283'
><em id='it23282'
>id_1</em></code>::<code id='code23285'
><em id='it23284'
>type_1</em></code> ::<code id='code23287'
><em id='it23286'
>struct</em></code>*)
(<code id='code23289'
><em id='it23288'
>struct</em></code>*-<code id='code23291'
><em id='it23290'
>id_1</em></code>-set!<font color="#00cf00"><strong id='bold30300'
>::obj</strong></font> ::<code id='code23293'
><em id='it23292'
>struct</em></code>* ::<code id='code23295'
><em id='it23294'
>type_1</em></code>)
...
</pre>
</td></tr>
</tbody></table></center>
These functions read and store field values.<br/><br/></li>
</ul>
Here is an example of structure usage:<br/><br/><center id='center23333'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23331'
>(<font color="#1919af"><strong id='bold30302'
>module</strong></font> <font color="#1919af"><strong id='bold30304'
>foo</strong></font> 
   (<strong id='bold30306'
>extern</strong> 
     (include <font color="red">&quot;named_point_declaration.h&quot;</font>)
     (<strong id='bold30308'
>type</strong> named-point 
           (struct (x<font color="#00cf00"><strong id='bold30309'
>::double</strong></font> <font color="red">&quot;x&quot;</font>)
                   (y<font color="#00cf00"><strong id='bold30312'
>::double</strong></font> <font color="red">&quot;y&quot;</font>)
                   (name<font color="#00cf00"><strong id='bold30315'
>::string</strong></font> <font color="red">&quot;name&quot;</font>))
           <font color="red">&quot;struct named_point&quot;</font>)
     (c-print-point<font color="#00cf00"><strong id='bold30319'
>::int</strong></font> (named-point*) <font color="red">&quot;ppoint&quot;</font>)))<br/><br/>(<font color="#6959cf"><strong id='bold30322'
>define</strong></font> (<font color="#6959cf"><strong id='bold30324'
>scheme-print-point</strong></font> point)
   (print <font color="red">&quot;point*-name: &quot;</font> point
          <font color="red">&quot; x: &quot;</font> (named-point*-x point)
          <font color="red">&quot; y: &quot;</font> (named-point*-y point)))<br/><br/>(<strong id='bold30329'
>let</strong> ((orig (named-point* 0.0 0.0 <font color="red">&quot;orig&quot;</font>)))
   (<strong id='bold30331'
>if</strong> (named-point*-null? orig)
       (<font color="red"><strong id='bold30332'
>error</strong></font> <font color="red">&quot;bigloo&quot;</font> <font color="red">&quot;cannot allocate point&quot;</font> orig)
       (<strong id='bold30336'
>begin</strong>
          (c-print-point orig)
          (scheme-print-point orig))))
</pre>
</td></tr>
</tbody></table></center>

</div>
<!-- C pointers -->
<a name="C-pointers"></a>
<div class="subsubsection-atitle"><h4><font color="#8381de">25.1.6.3 C pointers</font>
</h4></div><div class="subsubsection">
<a name="C-pointers" class="mark"></a><a name="g23334" class="mark"></a>C pointers are defined by the &lt;pointer-type&gt;<br/><br/><center id='center23341'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog23339'
>&lt;pointer-type&gt; ==&gt; <code id='code23337'
>(pointer</code> &lt;ident&gt;<code id='code23338'
>)</code>
</pre>
</td></tr>
</tbody></table></center>

&lt;ident&gt; is the name of a previously defined type. 
Let us suppose the pointer type declaration:
<center id='center23349'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23347'
>(<strong id='bold30337'
>type</strong> <code id='code23343'
><em id='it23342'
>ptr</em></code> (pointer <code id='code23345'
><em id='it23344'
>ident</em></code>) ...)
</pre>
</td></tr>
</tbody></table></center>

If <code id='code23351'
><em id='it23350'
>ident</em></code> is the name of a structure type, Bigloo automatically
creates structure accessors (see <a href="bigloo-26.html#C-structures-and-unions" class="inbound">C structures and unions</a>). 
Otherwise, it creates the following functions:<br/><br/><ul class="itemize" id='itemize23448'
><li>A creator: 
<center id='center23362'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23360'
>(make-<code id='code23354'
><em id='it23353'
>ptr</em></code>::<code id='code23356'
><em id='it23355'
>ptr</em></code> <code id='code23358'
><em id='it23357'
>nb</em></code><font color="#00cf00"><strong id='bold30338'
>::long</strong></font>)
</pre>
</td></tr>
</tbody></table></center>

This function allocates memory for <code id='code23364'
><em id='it23363'
>nb</em></code> elements of type <code id='code23366'
><em id='it23365'
>ident</em></code>
and returns a <code id='code23368'
><em id='it23367'
>ptr</em></code> to this zone. The memory is filled with
the C <code id='code23369'
>Null</code> value.<br/><br/></li>
<li>A type checker: 
<center id='center23380'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23378'
>(<code id='code23373'
><em id='it23372'
>ptr</em></code>?<font color="#00cf00"><strong id='bold30340'
>::bool</strong></font> <code id='code23375'
><em id='it23374'
>obj</em></code><font color="#00cf00"><strong id='bold30342'
>::obj</strong></font>)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23381'
>#t</code> the argument <code id='code23383'
><em id='it23382'
>obj</em></code> is of type
<code id='code23385'
><em id='it23384'
>ptr</em></code> and <code id='code23386'
>#f</code> otherwise.<br/><br/></li>
<li>A null checker: 
<center id='center23396'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23394'
>(<code id='code23390'
><em id='it23389'
>ptr</em></code>-null?<font color="#00cf00"><strong id='bold30344'
>::bool</strong></font> ::<code id='code23392'
><em id='it23391'
>ptr</em></code>)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23397'
>#t</code> if its argument is <code id='code23398'
>Null</code> and <code id='code23399'
>#f</code>
otherwise.<br/><br/></li>
<li>A null creator: 
<center id='center23408'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23406'
>(make-null-<code id='code23403'
><em id='it23402'
>ptr</em></code>::<code id='code23405'
><em id='it23404'
>ptr</em></code>*)
</pre>
</td></tr>
</tbody></table></center>
This function creates a <code id='code23409'
>NULL</code> value of type <code id='code23411'
><em id='it23410'
>ptr</em></code>*.<br/><br/></li>
<li>An equality checker: 
<center id='center23423'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23421'
>(=<code id='code23415'
><em id='it23414'
>ptr</em></code>*?<font color="#00cf00"><strong id='bold30346'
>::bool</strong></font> ::<code id='code23417'
><em id='it23416'
>ptr</em></code>* ::<code id='code23419'
><em id='it23418'
>ptr</em></code>*)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23424'
>#t</code> if its arguments are equal and <code id='code23425'
>#f</code> otherwise.<br/><br/></li>
<li>Accessors and mutators: 
<center id='center23445'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23443'
>(<code id='code23429'
><em id='it23428'
>ptr</em></code>-ref::<code id='code23431'
><em id='it23430'
>ident</em></code> ::<code id='code23433'
><em id='it23432'
>ptr</em></code> <font color="#00cf00"><strong id='bold30348'
>::long</strong></font>)
(<code id='code23435'
><em id='it23434'
>ptr</em></code>-set!<font color="#00cf00"><strong id='bold30350'
>::obj</strong></font> ::<code id='code23437'
><em id='it23436'
>ptr</em></code> <font color="#00cf00"><strong id='bold30352'
>::long</strong></font> ::<code id='code23439'
><em id='it23438'
>ident</em></code>)
</pre>
</td></tr>
</tbody></table></center>
These functions read and store field values.<br/><br/></li>
</ul>
Here is an example of a program using pointer types:<br/><br/><center id='center23465'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23463'
>(<font color="#1919af"><strong id='bold30354'
>module</strong></font> <font color="#1919af"><strong id='bold30356'
>foo</strong></font> 
   (<strong id='bold30358'
>extern</strong> 
    (<strong id='bold30359'
>type</strong> double* (pointer double) <font color="red">&quot;double *&quot;</font>)))<br/><br/>(<font color="#6959cf"><strong id='bold30361'
>define</strong></font> (<font color="#6959cf"><strong id='bold30363'
>make-vect</strong></font><font color="#00cf00"><strong id='bold30365'
>::double*</strong></font> x y z)
   (<strong id='bold30367'
>let</strong> ((vect (make-double* 3)))
      (double*-set! vect 0 x)
      (double*-set! vect 1 y)
      (double*-set! vect 2 z)
      vect))<br/><br/>(<font color="#6959cf"><strong id='bold30368'
>define</strong></font> (<font color="#6959cf"><strong id='bold30370'
>vect-norm</strong></font> vect<font color="#00cf00"><strong id='bold30372'
>::double*</strong></font>)
   (sqrt (+ (expt (double*-ref vect 0) 2)
	    (expt (double*-ref vect 1) 2)
	    (expt (double*-ref vect 2) 2))))<br/><br/>(print (vect-norm (make-vect 1.2 4.5 -4.5)))
</pre>
</td></tr>
</tbody></table></center>

</div>
<!-- C null pointers -->
<a name="C-null-pointers"></a>
<div class="subsubsection-atitle"><h4><font color="#8381de">25.1.6.4 C null pointers</font>
</h4></div><div class="subsubsection">
<a name="C-null-pointers" class="mark"></a><a name="g23466" class="mark"></a>
It may be convenient to build C null pointers. Several means can be
used.  In particular, foreign structures and pointers are provided with
Null creators. For other foreign types, the easiest one is likely to be
a <code id='code23468'
>pragma</code> form. For instance, in order to create a null pointer to
a <code id='code23469'
>double*</code> type, one may use:<br/><br/><center id='center23476'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23474'
>(<strong id='bold30374'
>pragma</strong><font color="#00cf00"><strong id='bold30375'
>::double*</strong></font> <font color="red">&quot;((double *)0L)&quot;</font>)
</pre>
</td></tr>
</tbody></table></center>

<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g23478" class="mark"></a><a name="string-ptr-null?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc23482" align="left" colspan="1"><strong id='bold23480'
>string-ptr-null?</strong><em id='it23481'
> string</em></td><td id="tc23483" align="right" colspan="1">bigloo procedure</td></tr>
</tbody></table>
<a name="g23487" class="mark"></a><a name="void*-null?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc23491" align="left" colspan="1"><strong id='bold23489'
>void*-null?</strong><em id='it23490'
> void*</em></td><td id="tc23492" align="right" colspan="1">bigloo procedure</td></tr>
</tbody></table>
These two predicates checks if there argument is the C <code id='code23495'
>NULL</code> value.
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g23499" class="mark"></a><a name="make-string-ptr-null" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc23503" align="left" colspan="1"><strong id='bold23501'
>make-string-ptr-null</strong><em id='it23502'
></em></td><td id="tc23504" align="right" colspan="1">bigloo procedure</td></tr>
</tbody></table>
<a name="g23508" class="mark"></a><a name="make-void*-null" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc23512" align="left" colspan="1"><strong id='bold23510'
>make-void*-null</strong><em id='it23511'
></em></td><td id="tc23513" align="right" colspan="1">bigloo procedure</td></tr>
</tbody></table>
These two constructors creates <em id='emph23516'
>null</em> foreign values.
</td></tr>
</tbody></table><br/>
</div>
<!-- C arrays -->
<a name="C-arrays"></a>
<div class="subsubsection-atitle"><h4><font color="#8381de">25.1.6.5 C arrays</font>
</h4></div><div class="subsubsection">
<a name="C-arrays" class="mark"></a><a name="g23519" class="mark"></a>C arrays are defined by the &lt;array-type&gt;<br/><br/><center id='center23526'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog23524'
>&lt;array-type&gt; ==&gt; <code id='code23522'
>(array</code> &lt;ident&gt;<code id='code23523'
>)</code>
</pre>
</td></tr>
</tbody></table></center>

&lt;ident&gt; is the name of a previously defined type. Array types are 
similar to pointer types except that they include their size in their type
definition string. Let us suppose the array type declaration: 
<center id='center23534'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23532'
>(<strong id='bold30378'
>type</strong> <code id='code23528'
><em id='it23527'
>array</em></code> (array <code id='code23530'
><em id='it23529'
>ident</em></code>) ...)
</pre>
</td></tr>
</tbody></table></center>

If <code id='code23536'
><em id='it23535'
>ident</em></code> is the name of a structure type, Bigloo automatically
creates structures accessors (see <a href="bigloo-26.html#C-structures-and-unions" class="inbound">C structures and unions</a>).
Otherwise, it creates the following functions:<br/><br/><ul class="itemize" id='itemize23615'
><li>A creator: 
<center id='center23544'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23542'
>(make-<code id='code23539'
><em id='it23538'
>array</em></code>::<code id='code23541'
><em id='it23540'
>array</em></code>)
</pre>
</td></tr>
</tbody></table></center>
This function allocates memory for the array <code id='code23546'
><em id='it23545'
>array</em></code>.
The memory is filled with the C <code id='code23547'
>Null</code> value.<br/><br/></li>
<li>A type checker: 
<center id='center23558'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23556'
>(<code id='code23551'
><em id='it23550'
>array</em></code>?<font color="#00cf00"><strong id='bold30379'
>::bool</strong></font> <code id='code23553'
><em id='it23552'
>obj</em></code><font color="#00cf00"><strong id='bold30381'
>::obj</strong></font>)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23559'
>#t</code> if the argument <code id='code23561'
><em id='it23560'
>obj</em></code> is of type 
<em id='emph23562'
>array</em> and <code id='code23563'
>#f</code> otherwise.<br/><br/></li>
<li>A null checker: 
<center id='center23573'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23571'
>(null-<code id='code23567'
><em id='it23566'
>array</em></code>?<font color="#00cf00"><strong id='bold30383'
>::bool</strong></font> ::<code id='code23569'
><em id='it23568'
>array</em></code>)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23574'
>#t</code> if the argument <code id='code23576'
><em id='it23575'
>obj</em></code> is <code id='code23577'
>Null</code>
and <code id='code23578'
>#f</code> otherwise.<br/><br/></li>
<li>An equality checker: 
<center id='center23590'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23588'
>(=<code id='code23582'
><em id='it23581'
>array</em></code>*?<font color="#00cf00"><strong id='bold30385'
>::bool</strong></font> ::<code id='code23584'
><em id='it23583'
>array</em></code>* ::<code id='code23586'
><em id='it23585'
>array</em></code>*)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23591'
>#t</code> if its arguments are equal and <code id='code23592'
>#f</code> 
otherwise.<br/><br/></li>
<li>Accessors and mutators: 
<center id='center23612'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23610'
>(<code id='code23596'
><em id='it23595'
>array</em></code>-ref::<code id='code23598'
><em id='it23597'
>ident</em></code> ::<code id='code23600'
><em id='it23599'
>array</em></code> <font color="#00cf00"><strong id='bold30387'
>::long</strong></font>)
(<code id='code23602'
><em id='it23601'
>array</em></code>-set!<font color="#00cf00"><strong id='bold30389'
>::obj</strong></font> ::<code id='code23604'
><em id='it23603'
>array</em></code> <font color="#00cf00"><strong id='bold30391'
>::long</strong></font> ::<code id='code23606'
><em id='it23605'
>ident</em></code>)
</pre>
</td></tr>
</tbody></table></center>
These functions read and store field values.<br/><br/></li>
</ul>
Here is an example of a program using array types:<br/><br/><center id='center23632'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23630'
>(<font color="#1919af"><strong id='bold30393'
>module</strong></font> <font color="#1919af"><strong id='bold30395'
>foo</strong></font> 
   (<strong id='bold30397'
>extern</strong> 
    (<strong id='bold30398'
>type</strong> double* (array double) <font color="red">&quot;double [ 10 ]&quot;</font>)))<br/><br/>(<font color="#6959cf"><strong id='bold30400'
>define</strong></font> (<font color="#6959cf"><strong id='bold30402'
>make-vect</strong></font><font color="#00cf00"><strong id='bold30404'
>::double*</strong></font> x y z)
   (<strong id='bold30406'
>let</strong> ((vect (make-double*)))
      (double*-set! vect 0 x)
      (double*-set! vect 1 y)
      (double*-set! vect 2 z)
      vect))<br/><br/>(<font color="#6959cf"><strong id='bold30407'
>define</strong></font> (<font color="#6959cf"><strong id='bold30409'
>vect-norm</strong></font> vect<font color="#00cf00"><strong id='bold30411'
>::double*</strong></font>)
   (sqrt (+ (expt (double*-ref vect 0) 2)
	    (expt (double*-ref vect 1) 2)
	    (expt (double*-ref vect 2) 2))))<br/><br/><br/>(print (vect-norm (make-vect 1.2 4.5 -4.5)))
</pre>
</td></tr>
</tbody></table></center>

</div>
<!-- C functions -->
<a name="C-functions"></a>
<div class="subsubsection-atitle"><h4><font color="#8381de">25.1.6.6 C functions</font>
</h4></div><div class="subsubsection">
<a name="C-functions" class="mark"></a><a name="g23633" class="mark"></a>C function types are introduced by the &lt;function-type&gt; clause:<br/><br/><center id='center23641'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog23639'
>&lt;function-type&gt; ==&gt; <code id='code23636'
>(function</code> &lt;ident&gt; <code id='code23637'
>(</code>&lt;ident&gt;*<code id='code23638'
>))</code>
</pre>
</td></tr>
</tbody></table></center>

Let us suppose the array type declaration: 
<center id='center23651'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23649'
>(<strong id='bold30413'
>type</strong> <code id='code23643'
><em id='it23642'
>fun</em></code> (function <code id='code23645'
><em id='it23644'
>res</em></code> (<code id='code23647'
><em id='it23646'
>arg</em></code>*)) ...)
</pre>
</td></tr>
</tbody></table></center>

Bigloo creates the following functions:<br/><br/><ul class="itemize" id='itemize23706'
><li>A type checker: 
<center id='center23661'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23659'
>(<code id='code23654'
><em id='it23653'
>fun</em></code>?<font color="#00cf00"><strong id='bold30414'
>::bool</strong></font> <code id='code23656'
><em id='it23655'
>obj</em></code><font color="#00cf00"><strong id='bold30416'
>::obj</strong></font>)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23662'
>#t</code> if the argument <code id='code23664'
><em id='it23663'
>obj</em></code> is of type
<code id='code23666'
><em id='it23665'
>fun</em></code> and <code id='code23667'
>#f</code> otherwise.<br/><br/></li>
<li>An equality checker: 
<center id='center23679'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23677'
>(=<code id='code23671'
><em id='it23670'
>fun</em></code>*?<font color="#00cf00"><strong id='bold30418'
>::bool</strong></font> ::<code id='code23673'
><em id='it23672'
>fun</em></code>* ::<code id='code23675'
><em id='it23674'
>fun</em></code>*)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23680'
>#t</code> if and only if its arguments are equal.<br/><br/></li>
<li>Caller: 
<center id='center23697'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23695'
>(<code id='code23684'
><em id='it23683'
>fun</em></code>-call::<code id='code23686'
><em id='it23685'
>res</em></code> <code id='code23688'
><em id='it23687'
>f</em></code>::<code id='code23690'
><em id='it23689'
>fun</em></code> <code id='code23692'
><em id='it23691'
>a</em></code>::<code id='code23694'
><em id='it23693'
>ta</em></code> ...)
</pre>
</td></tr>
</tbody></table></center>
This function invokes <code id='code23699'
><em id='it23698'
>f</em></code> with the arguments <code id='code23701'
><em id='it23700'
>a</em></code> ...
<code id='code23703'
><em id='it23702'
>an</em></code>.<br/><br/></li>
</ul>
Suppose we have to use in Scheme the following C variable:<br/><br/><center id='center23710'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23708'
>double (*convert)(char *);
</pre>
</td></tr>
</tbody></table></center>

It can be done as in:<br/><br/><center id='center23723'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23721'
>(<font color="#1919af"><strong id='bold30420'
>module</strong></font> <font color="#1919af"><strong id='bold30422'
>foo</strong></font> 
   (<strong id='bold30424'
>extern</strong>
    (<strong id='bold30425'
>type</strong> *string-&gt;double
          (function double (string)) 
                    <font color="red">&quot;double (*)(char *)&quot;</font>)
    (macro cv<font color="#00cf00"><strong id='bold30427'
>::*string-&gt;double</strong></font> <font color="red">&quot;convert&quot;</font>)))<br/><br/>(print (*string-&gt;double-call cv <font color="red">&quot;3.14&quot;</font>))
</pre>
</td></tr>
</tbody></table></center>

</div>
<!-- C enums -->
<a name="C-enums"></a>
<div class="subsubsection-atitle"><h4><font color="#8381de">25.1.6.7 C enums</font>
</h4></div><div class="subsubsection">
<a name="C-enums" class="mark"></a><a name="g23724" class="mark"></a>This form defines <code id='code23726'
>enum</code> types.<br/><br/><center id='center23733'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog23731'
>&lt;enum-type&gt; ==&gt; <code id='code23728'
>(enum (</code>&lt;ident&gt; &lt;string&gt;<code id='code23729'
>)...</code> ...<code id='code23730'
>)</code>
</pre>
</td></tr>
</tbody></table></center>

Let us suppose the type: 
<center id='center23747'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23745'
>(<strong id='bold30431'
>type</strong> <code id='code23735'
><em id='it23734'
>enum</em></code>
   (enum (<code id='code23737'
><em id='it23736'
>id_1</em></code> <code id='code23739'
><em id='it23738'
>name_1</em></code>)
         ...
         (<code id='code23741'
><em id='it23740'
>id_n</em></code> <code id='code23743'
><em id='it23742'
>name_n</em></code>)))
</pre>
</td></tr>
</tbody></table></center>

Bigloo creates the following functions:<br/><br/><ul class="itemize" id='itemize23798'
><li>Creators: 
<center id='center23763'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23761'
>(<code id='code23750'
><em id='it23749'
>enum</em></code>-<code id='code23752'
><em id='it23751'
>id_1</em></code>::<code id='code23754'
><em id='it23753'
>enum</em></code>)
...
(<code id='code23756'
><em id='it23755'
>enum</em></code>-<code id='code23758'
><em id='it23757'
>id_n</em></code>::<code id='code23760'
><em id='it23759'
>enum</em></code>)
</pre>
</td></tr>
</tbody></table></center>
These functions create <code id='code23765'
><em id='it23764'
>enum</em></code> values.<br/><br/></li>
<li>A type checker: 
<center id='center23776'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23774'
>(<code id='code23769'
><em id='it23768'
>enum</em></code>?<font color="#00cf00"><strong id='bold30432'
>::bool</strong></font> <code id='code23771'
><em id='it23770'
>obj</em></code><font color="#00cf00"><strong id='bold30434'
>::obj</strong></font>)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23777'
>#t</code> if the argument <code id='code23779'
><em id='it23778'
>obj</em></code> is of type 
<code id='code23781'
><em id='it23780'
>enum</em></code> and <code id='code23782'
>#f</code> otherwise.<br/><br/></li>
<li>An equality checker: 
<center id='center23794'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23792'
>(=<code id='code23786'
><em id='it23785'
>enum</em></code>?<font color="#00cf00"><strong id='bold30436'
>::bool</strong></font> ::<code id='code23788'
><em id='it23787'
>enum</em></code> ::<code id='code23790'
><em id='it23789'
>enum</em></code>)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23795'
>#t</code> if the arguments are equal and <code id='code23796'
>#f</code> otherwise.
</li>
</ul>
Here is an example of Scheme code using <code id='code23800'
><em id='it23799'
>enum</em></code> type.<br/><br/><center id='center23814'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23812'
>(<font color="#1919af"><strong id='bold30438'
>module</strong></font> <font color="#1919af"><strong id='bold30440'
>foo</strong></font>
   (<strong id='bold30442'
>extern</strong>
    (<strong id='bold30443'
>type</strong> gizmo
          (enum (titi <font color="red">&quot;titi&quot;</font>)
                (tutu <font color="red">&quot;tutu&quot;</font>)
                (tata <font color="red">&quot;tata&quot;</font>))
          <font color="red">&quot;enum toto&quot;</font>)))<br/><br/>(<strong id='bold30448'
>let</strong> ((v1 (gizmo-titi))
      (v2 (gizmo-tutu)))
   (print (=gizmo? v1 v2)))
</pre>
</td></tr>
</tbody></table></center>

</div>
<!-- C opaques -->
<a name="C-opaques"></a>
<div class="subsubsection-atitle"><h4><font color="#8381de">25.1.6.8 C opaques</font>
</h4></div><div class="subsubsection">
<a name="C-opaques" class="mark"></a><a name="g23815" class="mark"></a>This form defines <code id='code23817'
>opaque</code> types.<br/><br/><center id='center23822'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog23820'
>&lt;opaque-type&gt; ==&gt; <code id='code23819'
>(opaque)</code>
</pre>
</td></tr>
</tbody></table></center>

Let us suppose the type:
<center id='center23828'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23826'
>(<strong id='bold30449'
>type</strong> <code id='code23824'
><em id='it23823'
>opa</em></code> (opaque) ...)
</pre>
</td></tr>
</tbody></table></center>

Bigloo creates the following functions:
<ul class="itemize" id='itemize23859'
><li>A type checker: 
<center id='center23837'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23835'
>(<code id='code23830'
><em id='it23829'
>opa</em></code>?<font color="#00cf00"><strong id='bold30450'
>::bool</strong></font> <code id='code23832'
><em id='it23831'
>obj</em></code><font color="#00cf00"><strong id='bold30452'
>::obj</strong></font>)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23838'
>#t</code> if the argument <code id='code23840'
><em id='it23839'
>obj</em></code> is of type 
<code id='code23842'
><em id='it23841'
>opa</em></code> and <code id='code23843'
>#f</code> otherwise.<br/><br/></li>
<li>An equality checker: 
<center id='center23855'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23853'
>(=<code id='code23847'
><em id='it23846'
>opa</em></code>?<font color="#00cf00"><strong id='bold30454'
>::bool</strong></font> ::<code id='code23849'
><em id='it23848'
>opa</em></code> ::<code id='code23851'
><em id='it23850'
>opa</em></code>)
</pre>
</td></tr>
</tbody></table></center>
This function returns <code id='code23856'
>#t</code> if the arguments 
are equal and <code id='code23857'
>#f</code> otherwise.
</li>
</ul>
Opaque types are relevant when a C value must transit via a Scheme function
from a C function to another C function. The value can't be used in Scheme
because no accessors are defined over that type it can only be send back
to a C function.<br/><br/>Here is an example of Scheme code using <code id='code23862'
><em id='it23861'
>opaque</em></code> type.<br/><br/><center id='center23894'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23892'
>(<font color="#1919af"><strong id='bold30456'
>module</strong></font> <font color="#1919af"><strong id='bold30458'
>foo</strong></font>
   (<strong id='bold30460'
>extern</strong> (<strong id='bold30461'
>type</strong> filedes (opaque) <font color="red">&quot;FILE *&quot;</font>)
           (macro _fopen<font color="#00cf00"><strong id='bold30463'
>::filedes</strong></font> (<font color="#00cf00"><strong id='bold30465'
>::string</strong></font> <font color="#00cf00"><strong id='bold30467'
>::string</strong></font>) <font color="red">&quot;fopen&quot;</font>)
           (_fgetc<font color="#00cf00"><strong id='bold30470'
>::int</strong></font> (<font color="#00cf00"><strong id='bold30472'
>::filedes</strong></font>) <font color="red">&quot;fgetc&quot;</font>)
           (_fclose (<font color="#00cf00"><strong id='bold30475'
>::filedes</strong></font>) <font color="red">&quot;fclose&quot;</font>))
   (<font color="#1919af"><strong id='bold30478'
>export</strong></font> (<font color="#1919af"><strong id='bold30480'
>fopen</strong></font><font color="#00cf00"><strong id='bold30482'
>::filedes</strong></font> <font color="#00cf00"><strong id='bold30484'
>::bstring</strong></font> <font color="#00cf00"><strong id='bold30486'
>::bstring</strong></font>)
           (fclose <font color="#00cf00"><strong id='bold30488'
>::filedes</strong></font>)
           (fgetc<font color="#00cf00"><strong id='bold30490'
>::char</strong></font> <font color="#00cf00"><strong id='bold30492'
>::filedes</strong></font>)))<br/><br/>(<font color="#6959cf"><strong id='bold30494'
>define</strong></font> (<font color="#6959cf"><strong id='bold30496'
>fopen</strong></font> fname mode)
   (_fopen fname mode))<br/><br/>(<font color="#6959cf"><strong id='bold30498'
>define</strong></font> (<font color="#6959cf"><strong id='bold30500'
>fclose</strong></font> filedes)
   (_fclose filedes))<br/><br/>(<font color="#6959cf"><strong id='bold30502'
>define</strong></font> (<font color="#6959cf"><strong id='bold30504'
>fgetc</strong></font> filedes)
   (integer-&gt;char (_fgetc filedes)))
</pre>
</td></tr>
</tbody></table></center>

<em id='emph23895'
>Note: </em> To illustrate the default type compilation of extern function,
we have voluntary introduced an incomplete declaration for the <code id='code23896'
>fclose</code>
function. This will make Bigloo to produce a warning when compiling that
module.<br/><br/></div>
</div>
</div><br>
<!-- The very dangerous ``pragma'' Bigloo special forms -->
<a name="The-very-dangerous-``pragma''-Bigloo-special-forms"></a>
<div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">25.2 The very dangerous ``pragma'' Bigloo special forms</font>
</h3></td></tr></table>
</div><div class="section">
<a name="The-very-dangerous-pragma-Bigloo-special-forms" class="mark"></a><a name="g23898" class="mark"></a>
Bigloo has a special form which allows the inclusion of C text
into the produced code. It is <em id='emph23900'
>only</em> applicable to the C back-end. 
In particular, the JVM back-end (see Chapter <a href="bigloo-27.html#Java-Interface" class="inbound">Java Interface</a>) does
not support it.<br/><br/><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g23903" class="mark"></a><a name="pragma::" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc23909" align="left" colspan="1"><strong id='bold23905'
>pragma::</strong><em id='it23908'
><code id='code23907'
><em id='it23906'
>ident</em></code> string [args]</em></td><td id="tc23910" align="right" colspan="1">bigloo syntax</td></tr>
</tbody></table>
<a name="g23914" class="mark"></a><a name="free-pragma::" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc23920" align="left" colspan="1"><strong id='bold23916'
>free-pragma::</strong><em id='it23919'
><code id='code23918'
><em id='it23917'
>ident</em></code> string [args]</em></td><td id="tc23921" align="right" colspan="1">bigloo syntax</td></tr>
</tbody></table>
This force Bigloo to include <code id='code23925'
><em id='it23924'
>string</em></code> in the produced C code as a
regular C fragment of code. This form must not be used without an in depth
understanding of Bigloo C code production; with unskilled use, the
produced C file may be unacceptable to the C compiler.<br/><br/>Values can be passed to a <code id='code23927'
>pragma</code> form, being
referenced in <code id='code23929'
><em id='it23928'
>string</em></code> by expressions of the form <code id='code23932'
>$<code id='code23931'
><em id='it23930'
>number</em></code></code>.
Such expression are replaced by the corresponding
values, the number of referenced values in <code id='code23934'
><em id='it23933'
>string</em></code>
being exactly the number of values provided. Here is an example
of <code id='code23935'
>pragma</code> usage:<br/><br/><center id='center23943'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog23941'
>(<font color="#6959cf"><strong id='bold30506'
>define</strong></font> (<font color="#6959cf"><strong id='bold30508'
>fibo</strong></font> x<font color="#00cf00"><strong id='bold30510'
>::long</strong></font>)
   (<strong id='bold30512'
>pragma</strong> <font color="red">&quot;printf( \&quot;fib(%d):%d\\n\&quot;, $1, $2 );&quot;</font> 
           x 
           (fib x)))
</pre>
</td></tr>
</tbody></table></center>

Arguments provided to a pragma form are not converted during compilation.
Hence, pragma arguments can be of any types, including, foreign types.<br/><br/>A pragma result type can be specified using the notation 
<code id='code23947'
>pragma::<code id='code23946'
><em id='it23945'
>name</em></code></code> where the default type is <code id='code23948'
>unspecified</code>. Then,
for instance, the expression <code id='code23949'
>(pragma::bool &quot;$1 == 0&quot; x)</code> will
be considered to be returning a object of type <code id='code23950'
>bool</code> (C boolean) while
the expression <code id='code23951'
>(pragma &quot;$1 == 0&quot; x)</code> will be considered by
Bigloo to be returning the <code id='code23952'
>unspecified</code> typed object.<br/><br/>The compiler assumes that a <code id='code23954'
>pragma</code> forms operates a side effects
and that it writes into its parameters. This assumption no long holds
with <code id='code23955'
>free-pragma</code>. This is the only difference between the two
forms.
</td></tr>
</tbody></table><br/>
</div><br>
<!-- Name mangling -->
<a name="Name-mangling"></a>
<div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">25.3 Name mangling</font>
</h3></td></tr></table>
</div><div class="section">
<a name="Name-mangling" class="mark"></a><a name="g23958" class="mark"></a>
In order to avoid name clashes, Bigloo uses name mangling when compiling to
C or to Jvm. The name mangling for a Scheme identifier may be overridden
by the means of an extern <code id='code23960'
>export</code> clause 
(see Section <a href="bigloo-26.html#Exporting-a-Scheme-variable" class="inbound">Exporting a Scheme variable</a>).<br/><br/>Four public functions may be used to mangle and to demangle
Scheme identifiers:<br/><br/><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g23964" class="mark"></a><a name="bigloo-mangle" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc23968" align="left" colspan="1"><strong id='bold23966'
>bigloo-mangle</strong><em id='it23967'
> string</em></td><td id="tc23969" align="right" colspan="1">bigloo procedure</td></tr>
</tbody></table>
Mangle the identifier <code id='code23973'
><em id='it23972'
>string</em></code>.
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g23977" class="mark"></a><a name="bigloo-module-mangle" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc23981" align="left" colspan="1"><strong id='bold23979'
>bigloo-module-mangle</strong><em id='it23980'
> string1 string2</em></td><td id="tc23982" align="right" colspan="1">bigloo procedure</td></tr>
</tbody></table>
Mangle the identifier <code id='code23986'
><em id='it23985'
>string1</em></code> that belongs to module <code id='code23988'
><em id='it23987'
>string2</em></code>.
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g23992" class="mark"></a><a name="bigloo-mangled?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc23996" align="left" colspan="1"><strong id='bold23994'
>bigloo-mangled?</strong><em id='it23995'
> string</em></td><td id="tc23997" align="right" colspan="1">bigloo procedure</td></tr>
</tbody></table>
Returns <code id='code24000'
>#t</code> if <code id='code24002'
><em id='it24001'
>string</em></code> has been computed by the <code id='code24003'
>bigloo-mangle</code>
or <code id='code24004'
>bigloo-module-mangle</code> function.
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g24008" class="mark"></a><a name="bigloo-class-mangled?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc24012" align="left" colspan="1"><strong id='bold24010'
>bigloo-class-mangled?</strong><em id='it24011'
> string</em></td><td id="tc24013" align="right" colspan="1">bigloo procedure</td></tr>
</tbody></table>
Returns <code id='code24016'
>#t</code> if <code id='code24018'
><em id='it24017'
>string</em></code> is a mangled name of a Bigloo class.
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g24022" class="mark"></a><a name="bigloo-need-mangling" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc24026" align="left" colspan="1"><strong id='bold24024'
>bigloo-need-mangling</strong><em id='it24025'
> string</em></td><td id="tc24027" align="right" colspan="1">bigloo procedure</td></tr>
</tbody></table>
Returns <code id='code24030'
>#t</code> if <code id='code24032'
><em id='it24031'
>string</em></code> requires name mangling because it
is not a C or Jvm valid identifier.
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g24036" class="mark"></a><a name="bigloo-demangle" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc24040" align="left" colspan="1"><strong id='bold24038'
>bigloo-demangle</strong><em id='it24039'
> string</em></td><td id="tc24041" align="right" colspan="1">bigloo procedure</td></tr>
</tbody></table>
Demangle previously mangled identifiers:<br/><br/><center id='center24053'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog24051'
>(<strong id='bold30514'
>let</strong> ((id <font color="red">&quot;foo!&quot;</font>)
      (<font color="#1919af"><strong id='bold30516'
>module</strong></font> <font color="red">&quot;a-module&quot;</font><font color="#1919af"><strong id='bold30519'
>)</strong></font>)
   (<strong id='bold30521'
>let</strong> ((mangled (bigloo-module-mangle id module)))
      (multiple-value-bind (new-id new-module)
         (bigloo-demangle mangled)
         (and (string=? id new-id) (string=? module new-module)))))
   =&gt; #t
</pre>
</td></tr>
</tbody></table></center>
</td></tr>
</tbody></table><br/>
<table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody>
<tr><td><a name="g24057" class="mark"></a><a name="bigloo-class-demangle" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody>
<tr><td id="tc24061" align="left" colspan="1"><strong id='bold24059'
>bigloo-class-demangle</strong><em id='it24060'
> string</em></td><td id="tc24062" align="right" colspan="1">bigloo procedure</td></tr>
</tbody></table>
Demangle previously mangled class identifier.
</td></tr>
</tbody></table><br/>
</div><br>
<!-- Embedded Bigloo applications -->
<a name="Embedded-Bigloo-applications"></a>
<div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">25.4 Embedded Bigloo applications</font>
</h3></td></tr></table>
</div><div class="section">
<a name="Embedded-Bigloo-applications" class="mark"></a><a name="g24067" class="mark"></a>
It is possible to design and realize embedded Bigloo
applications. This facility is useful for adding a new Scheme part
to an already existing C program. The C part of the program has only
to enter the Bigloo initialization, hence, it can call any
Bigloo function.<br/><br/>Normally, Bigloo creates an initialization function called <code id='code24070'
>
main</code> when it reads a <code id='code24071'
>main</code> module clause. To use an
embedded Bigloo program, such an initialization function would have to
be created but with a different name. Changing the name can be
be done using the following Bigloo option: 
<code id='code24074'
>-copt &quot;-DBIGLOO_MAIN=&lt;<code id='code24073'
><em id='it24072'
>new-name</em></code>&gt;&quot;</code>. To prevent exit from the
program after &lt;<code id='code24076'
><em id='it24075'
>new-name</em></code>&gt; is executed, the
following Bigloo option must be used: 
<code id='code24077'
>-copt &quot;-DBIGLOO_EXIT='BUNSPEC,'&quot;</code>.<br/><br/>A very important part of designing embedded Bigloo programs is
being sure that all used Bigloo modules are correctly
initialized and the normal way to initialize them is to use 
<code id='code24079'
>with</code> clauses in the module which contains the <code id='code24080'
>main</code>
clause.<br/><br/>An example of an embedded program can be found in the distribution's
examples directory.<br/><br/></div><br>
<!-- Using C bindings within the interpreter -->
<a name="Using-C-bindings-within-the-interpreter"></a>
<div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">25.5 Using C bindings within the interpreter</font>
</h3></td></tr></table>
</div><div class="section">
<a name="Using-C-bindings-within-the-interpreter" class="mark"></a><a name="g24083" class="mark"></a>
To be able to get access to foreign functions within the Bigloo
interpreter, some extra measurements have to be taken. The foreign
functions have to be present in the interpreter binary, which means
you have to compile a custom interpreter. Fortunately, this is easy.
What has to be done is to wrap the foreign functions within Scheme and
make an interpreter module.<br/><br/>Let us consider an example where a C function <code id='code24086'
>get_system_time</code>
returning and <code id='code24087'
>int</code> is used in an interpreter. (When linking, be 
sure to add the <code id='code24088'
>.o</code> file containing the <code id='code24089'
>get_system_time</code>.)<br/><br/>The <code id='code24091'
>ffi-interpreter.scm</code> file:<br/><br/><center id='center24102'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog24100'
>(<font color="#1919af"><strong id='bold30522'
>module</strong></font> <font color="#1919af"><strong id='bold30524'
>ExtendendInterpreter</strong></font>
   (<font color="#1919af"><strong id='bold30526'
>import</strong></font> (<font color="#1919af"><strong id='bold30528'
>wrapper</strong></font> <font color="red">&quot;wrapper.scm&quot;</font>))
   (main main))<br/><br/>(<font color="#6959cf"><strong id='bold30531'
>define</strong></font> (<font color="#6959cf"><strong id='bold30533'
>main</strong></font> argv)
   (repl))
</pre>
</td></tr>
</tbody></table></center>

The <code id='code24103'
>wrapper.scm</code> file:<br/><br/><center id='center24116'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ffffcc"><pre class="prog" id='prog24114'
>(<font color="#1919af"><strong id='bold30535'
>module</strong></font> <font color="#1919af"><strong id='bold30537'
>wrapper</strong></font>
   (<strong id='bold30539'
>extern</strong> (macro %get-system-time<font color="#00cf00"><strong id='bold30540'
>::int</strong></font> () <font color="red">&quot;get_system_time&quot;</font>))
   (<font color="#1919af"><strong id='bold30543'
>export</strong></font> (<font color="#1919af"><strong id='bold30545'
>get-system-time</strong></font>))
   (eval (export-exports))<br/><br/>(<font color="#6959cf"><strong id='bold30547'
>define</strong></font> (<font color="#6959cf"><strong id='bold30549'
>get-system-time</strong></font>)
  (%get-system-time))
</pre>
</td></tr>
</tbody></table></center>

Compile and link your application with something like:<br/><br/><center id='center24120'
><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody>
<tr><td bgcolor="#ccccff"><pre class="prog" id='prog24118'
>cc gettime.c -c gettime.o
bigloo wrapper.scm -c
bigloo ffi-interpreter.scm wrapper.o gettime.o
</pre>
</td></tr>
</tbody></table></center>


</div><br>
</div></td>
</tr></table><div class="skribe-ending">
<hr> 
<p class="ending" id='paragraph30556'
><font size="-1">
This <span class="sc">Html</span> page has been produced by 
<a href="http://www.inria.fr/mimosa/fp/Skribe" class="http">Skribe</a>.
<br/>
Last update <em id='it30554'
>Tue Jun  2 11:43:27 2009</em>.</font></p></div>
</body>
</html>