raz / site / public / tour / c.html

156 lines · 10.8 KiB · raw · history · last change de634aad

1<!doctype html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<meta name="viewport" content="width=device-width, initial-scale=1">
6<title>Talking to C - Raz, Set, Go</title>
7<meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux.">
8<meta name="theme-color" content="#E07A5F">
9<meta property="og:title" content="Talking to C - Raz, Set, Go">
10<meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report.">
11<meta property="og:image" content="https://raz.lucela.org/social.png">
12<meta name="twitter:card" content="summary_large_image">
13<link rel="icon" href="../favicon.svg" type="image/svg+xml">
14<link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32">
15<link rel="apple-touch-icon" href="../apple-touch-icon.png">
16<link rel="stylesheet" href="../style.css">
17</head>
18<body>
19<header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header>
20<main>
21<div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
22<li><a href="hello.html">Hello, world</a></li>
23<li><a href="modules.html">Modules</a></li>
24<li><a href="integers.html">Integers</a></li>
25<li><a href="values.html">Values and conversions</a></li>
26<li><a href="procedures.html">Procedures and several results</a></li>
27</ol>
28<h3>Set</h3><ol start='6'>
29<li><a href="slices.html">Arrays, slices and strings</a></li>
30<li><a href="records.html">Records and pointers</a></li>
31<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
32<li><a href="memory.html">Memory and allocators</a></li>
33</ol>
34<h3>Go</h3><ol start='10'>
35<li><a href="errors.html">Errors</a></li>
36<li><a href="defer.html">Defer</a></li>
37<li><a href="checks.html">When it goes wrong</a></li>
38<li><a href="generics.html">Generic modules</a></li>
39<li><a href="c.html" aria-current="page">Talking to C</a></li>
40<li><a href="threads.html">Threads</a></li>
41</ol></aside><article><p class="kicker">Go &middot; lesson 14 of 15</p><h1>Talking to C</h1>
42<div class="file"><span>Qsort.raz</span></div><pre class="code"><code><span class="kw">module</span> Qsort;
43<span class="kw">import</span> Out, Fmt, SYSTEM;
44
45<span class="kw">link</span> <span class="string">&quot;libm.so.6&quot;</span>;
46
47<span class="kw">extern</span> <span class="kw">proc</span> qsort(base: ^<span class="ty">U8</span>; n, size: <span class="ty">UINT</span>;
48 less: <span class="kw">proc</span>(a, b: ^<span class="ty">U8</span>): <span class="ty">I32</span>);
49<span class="kw">extern</span> <span class="kw">proc</span> cbrt(x: <span class="ty">F64</span>): <span class="ty">F64</span>;
50
51<span class="comment">(* the order of two INTs, as C&#x27;s qsort wants it *)</span>
52<span class="kw">proc</span> order(a, b: ^<span class="ty">U8</span>): <span class="ty">I32</span>;
53<span class="kw">var</span> x, y: ^<span class="ty">INT</span>;
54<span class="kw">begin</span>
55 x := ^<span class="ty">INT</span>(a);
56 y := ^<span class="ty">INT</span>(b);
57 <span class="kw">if</span> x^ &lt; y^ <span class="kw">then</span> <span class="kw">return</span> -<span class="number">1</span>; <span class="kw">elsif</span> x^ &gt; y^ <span class="kw">then</span> <span class="kw">return</span> <span class="number">1</span>; <span class="kw">end</span>;
58 <span class="kw">return</span> <span class="number">0</span>;
59<span class="kw">end</span>;
60
61<span class="kw">proc</span> main*;
62<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>;
63<span class="kw">begin</span>
64 a := [<span class="number">30</span>, <span class="number">10</span>, <span class="number">50</span>, <span class="number">20</span>, <span class="number">40</span>];
65 qsort(^<span class="ty">U8</span>(SYSTEM.<span class="ty">ADR</span>(a)), <span class="ty">LEN</span>(a), <span class="ty">SIZE</span>(<span class="ty">INT</span>), order);
66 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4</span> <span class="kw">do</span> Out.int(a[i]); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="kw">end</span>;
67 Out.ln;
68 Fmt.print(<span class="string">&quot;{}\n&quot;</span>, [Fmt.real(cbrt(<span class="number">27.0</span>), <span class="number">3</span>)]);
69<span class="kw">end</span>;
70
71<span class="kw">end</span> Qsort.</code></pre>
72<pre class="output"><code>10 20 30 40 50
733.000
74</code></pre>
75<p>An <code>extern</code> procedure is found by its name in the C library, or in a library a <code>link</code> line names. Its parameters are integers, reals, pointers and procedures, as C declares them; a raz procedure of such types can be given to C to call back, as <code>order</code> is. A record goes to C by pointer, never by value.</p>
76<h2>What using C costs</h2>
77<p>A program that uses C is linked against it dynamically and starts through it, so that C's own set-up and clean-up run; one that doesn't is a static executable with nothing in it but its own code. A program uses C if any module it uses has an <code>extern</code> or a <code>link</code> of a library.</p>
78<h2>Varargs</h2>
79<p><code>...</code> after an <code>extern</code>'s parameters takes any number of further arguments, as <code>printf</code> wants. A string literal goes as a C string, an integer constant as an <code>INT</code>, a real constant as an <code>F64</code>. An <code>F32</code> is an error there, since C promotes it; write <code>F64(x)</code>.</p>
80<div class="file"><span>Say.raz</span></div><pre class="code"><code><span class="kw">module</span> Say;
81<span class="kw">import</span> Out;
82<span class="kw">import</span> SYSTEM;
83
84<span class="kw">extern</span> <span class="kw">proc</span> printf(format: ^<span class="ty">U8</span>; ...): <span class="ty">I32</span>;
85<span class="kw">extern</span> <span class="kw">proc</span> fflush(f: ^<span class="ty">U8</span>): <span class="ty">I32</span>;
86<span class="kw">extern</span> <span class="kw">proc</span> puts(s: ^<span class="ty">U8</span>): <span class="ty">I32</span>;
87
88<span class="kw">proc</span> main*;
89<span class="kw">var</span> n: <span class="ty">INT</span>; x: <span class="ty">F32</span>;
90<span class="kw">begin</span>
91 n := <span class="number">42</span>;
92 x := <span class="number">2.5</span>;
93 Out.str(<span class="string">&quot;raz first\n&quot;</span>);
94 Out.flush; <span class="comment">(* Out has a buffer; so does C *)</span>
95 _ := printf(<span class="string">&quot;C says %ld and %.2f and %s\n&quot;</span>, n, <span class="ty">F64</span>(x), <span class="string">&quot;hello&quot;</span>);
96 _ := puts(<span class="string">&quot;and puts&quot;</span>);
97 _ := fflush(<span class="ty">nil</span>); <span class="comment">(* nil: every C stream *)</span>
98 Out.str(<span class="string">&quot;raz again\n&quot;</span>);
99<span class="kw">end</span>;
100
101<span class="kw">end</span> Say.</code></pre>
102<pre class="output"><code>raz first
103C says 42 and 2.50 and hello
104and puts
105raz again
106</code></pre>
107<p><code>Out</code> and C's <code>printf</code> keep two buffers, so a program that writes with both flushes one before it writes with the other, or the text comes out of order.</p>
108<h2>Libraries, and C's variables</h2>
109<p><code>link "libm.so.6";</code> names a shared library to load at run time. An <code>extern var</code> is C's variable itself: reading it reads C's, assigning it writes C's.</p>
110<div class="file"><span>Env.raz</span></div><pre class="code"><code><span class="kw">module</span> Env;
111<span class="kw">import</span> Out;
112<span class="kw">import</span> SYSTEM;
113
114<span class="kw">extern</span> <span class="kw">var</span> environ: ^^<span class="ty">U8</span>;
115<span class="kw">extern</span> <span class="kw">proc</span> strlen(s: ^<span class="ty">U8</span>): <span class="ty">UINT</span>;
116<span class="kw">extern</span> <span class="kw">proc</span> getenv(name: ^<span class="ty">U8</span>): ^<span class="ty">U8</span>;
117
118<span class="kw">proc</span> main*;
119<span class="kw">var</span> p: ^<span class="ty">U8</span>; n: <span class="ty">INT</span>;
120<span class="kw">begin</span>
121 n := <span class="number">0</span>;
122 <span class="kw">while</span> environ[n] # <span class="ty">nil</span> <span class="kw">do</span> <span class="ty">INC</span>(n); <span class="kw">end</span>;
123 <span class="kw">if</span> n &gt; <span class="number">0</span> <span class="kw">then</span> Out.str(<span class="string">&quot;the environment is not empty\n&quot;</span>); <span class="kw">end</span>;
124 p := getenv(<span class="string">&quot;RAZ_NO_SUCH_VARIABLE&quot;</span>);
125 <span class="kw">if</span> p = <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="string">&quot;not set\n&quot;</span>); <span class="kw">end</span>;
126 Out.int(<span class="ty">INT</span>(strlen(<span class="string">&quot;six&quot;</span>))); Out.ln;
127<span class="kw">end</span>;
128
129<span class="kw">end</span> Env.</code></pre>
130<pre class="output"><code>the environment is not empty
131not set
1323
133</code></pre>
134<h2>Callbacks</h2>
135<p>Every raz procedure uses the System V calling convention, so any with parameters and a result of C's types may be given to C to call, as <code>qsort</code> is given <code>order</code> above. The procedure may not be a closure: it has only its arguments, so state travels through the pointer C hands back or a module variable.</p>
136<div class="file"><span>Roots.raz</span></div><pre class="code"><code><span class="kw">module</span> Roots;
137<span class="kw">import</span> Fmt;
138<span class="kw">import</span> SYSTEM;
139
140<span class="kw">link</span> <span class="string">&quot;libm.so.6&quot;</span>;
141
142<span class="kw">extern</span> <span class="kw">proc</span> cbrt(x: <span class="ty">F64</span>): <span class="ty">F64</span>;
143<span class="kw">extern</span> <span class="kw">proc</span> hypot(x, y: <span class="ty">F64</span>): <span class="ty">F64</span>;
144
145<span class="kw">proc</span> main*;
146<span class="kw">begin</span>
147 Fmt.print(<span class="string">&quot;{} {}\n&quot;</span>, [Fmt.real(cbrt(<span class="number">27.0</span>), <span class="number">3</span>), Fmt.real(hypot(<span class="number">3.0</span>, <span class="number">4.0</span>), <span class="number">3</span>)]);
148<span class="kw">end</span>;
149
150<span class="kw">end</span> Roots.</code></pre>
151<pre class="output"><code>3.000 5.000
152</code></pre><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="generics.html">&larr; Generic modules</a><a href="index.html">Contents</a><a class="next" href="threads.html">Threads &rarr;</a></div></article></div>
153</main>
154<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
155</body>
156</html>