| 1 | <h2>Comments</h2> |
| 2 | <pre><code>#! shebang |
| 3 | // Single line comment |
| 4 | /* Multi-line |
| 5 | comment */</code></pre> |
| 6 | |
| 7 | <h2>Strings</h2> |
| 8 | <pre><code>"This is a string." |
| 9 | "This is a string with \"quotes\" in it."</code></pre> |
| 10 | |
| 11 | <h2>Numbers</h2> |
| 12 | <pre><code>4711 |
| 13 | 4711L |
| 14 | 1.2e-3 |
| 15 | .14 |
| 16 | 1000 |
| 17 | 0x3e8 |
| 18 | 01750 |
| 19 | 0b1111101000 |
| 20 | inf |
| 21 | nan</code></pre> |
| 22 | |
| 23 | <h2>Inline code</h2> |
| 24 | <p>Inline code requires the desired language to be loaded. |
| 25 | On this page, check C, C++ and Fortran <strong>before</strong> checking Pure should make |
| 26 | the examples below work properly.</p> |
| 27 | <pre><code>%< |
| 28 | int mygcd(int x, int y) |
| 29 | { |
| 30 | if (y == 0) |
| 31 | return x; |
| 32 | else |
| 33 | return mygcd(y, x%y); |
| 34 | } |
| 35 | %> |
| 36 | |
| 37 | %< -*- Fortran90 -*- |
| 38 | function fact(n) result(p) |
| 39 | integer n, p |
| 40 | p = 1 |
| 41 | do i = 1, n |
| 42 | p = p*i |
| 43 | end do |
| 44 | end function fact |
| 45 | %> |
| 46 | |
| 47 | %< -*- C++ -*- |
| 48 | |
| 49 | #include <pure/runtime.h> |
| 50 | #include <string> |
| 51 | #include <map> |
| 52 | |
| 53 | // An STL map mapping strings to Pure expressions. |
| 54 | |
| 55 | using namespace std; |
| 56 | typedef map<string,pure_expr*> exprmap; |
| 57 | |
| 58 | // Since we can't directly deal with C++ classes in Pure, provide some C |
| 59 | // functions to create, destroy and manipulate these objects. |
| 60 | |
| 61 | extern "C" exprmap *map_create() |
| 62 | { |
| 63 | return new exprmap; |
| 64 | } |
| 65 | |
| 66 | extern "C" void map_add(exprmap *m, const char *key, pure_expr *x) |
| 67 | { |
| 68 | exprmap::iterator it = m->find(string(key)); |
| 69 | if (it != m->end()) pure_free(it->second); |
| 70 | (*m)[key] = pure_new(x); |
| 71 | } |
| 72 | |
| 73 | extern "C" void map_del(exprmap *m, const char *key) |
| 74 | { |
| 75 | exprmap::iterator it = m->find(key); |
| 76 | if (it != m->end()) { |
| 77 | pure_free(it->second); |
| 78 | m->erase(it); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | extern "C" pure_expr *map_get(exprmap *m, const char *key) |
| 83 | { |
| 84 | exprmap::iterator it = m->find(key); |
| 85 | return (it != m->end())?it->second:0; |
| 86 | } |
| 87 | |
| 88 | extern "C" pure_expr *map_keys(exprmap *m) |
| 89 | { |
| 90 | size_t i = 0, n = m->size(); |
| 91 | pure_expr **xs = new pure_expr*[n]; |
| 92 | for (exprmap::iterator it = m->begin(); it != m->end(); ++it) |
| 93 | xs[i++] = pure_string_dup(it->first.c_str()); |
| 94 | pure_expr *x = pure_listv(n, xs); |
| 95 | delete[] xs; |
| 96 | return x; |
| 97 | } |
| 98 | |
| 99 | extern "C" void map_destroy(exprmap *m) |
| 100 | { |
| 101 | for (exprmap::iterator it = m->begin(); it != m->end(); ++it) |
| 102 | pure_free(it->second); |
| 103 | delete m; |
| 104 | } |
| 105 | |
| 106 | %></code></pre> |
| 107 | |
| 108 | <h2>Example</h2> |
| 109 | <pre><code>queens n = catch reverse (search n 1 []) with |
| 110 | search n i p = throw p if i>n; |
| 111 | = void [search n (i+1) ((i,j):p) | j = 1..n; safe (i,j) p]; |
| 112 | safe (i,j) p = ~any (check (i,j)) p; |
| 113 | check (i1,j1) (i2,j2) |
| 114 | = i1==i2 || j1==j2 || i1+j1==i2+j2 || i1-j1==i2-j2; |
| 115 | end;</code></pre> |