git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: Add pure11-specific README
commitd5d960839ae445aff668659dfe9f31cfdedf8011 authorAndy Arvanitis <andyarvanitis@gmail.com> dateTue 15 Nov 2016, 16:16 (10 years ago) parent3aa71c3
1 file changed+121
README.markdown+121
README.markdownnew file+121
@@ -0,0 +1,121 @@
1+[![PureScript](https://raw.githubusercontent.com/purescript/purescript/master/logo.png)](http://purescript.org)
2+
3+**Pure11** is an experimental C++11/native compiler backend for [PureScript](https://github.com/purescript/purescript). It attempts to generate "sane", performant, and portable C++11 code as an intermediate language, which is then compiled to a native executable binary. This enables easy interoperability with existing C/C++ frameworks and libraries on a number of platforms.
4+
5+---
6+
7+### **Please note that this project and [purescript-c++](https://github.com/pure11/purescript-c++) are now the same**
8+
9+---
10+
11+#### Performance
12+
13+* No runtime system (beyond some support classes and the standard C++11 runtime library)
14+* For automatic memory management, uses either native C++11 reference counting (`std::shared_ptr`), or the [Boehm-Demers-Weiser Garbage Collector](http://hboehm.info/gc/) -- **selectable when building** (see instructions below)
15+* Uses PureScript's normal tail call optimizations for generated C++11 code
16+
17+#### Differences from PureScript:
18+
19+* Foreign imports are C++11 (or C) instead of JavaScript ([FFI examples](https://github.com/pure11/pure11/wiki/FFI_Examples))
20+* Foreign functions are not written as curried functions (any necessary currying is done implicitly by the compiler)
21+* Compiler is `pcc` instead of `psc`, `make` is recommended for building
22+* No Pure11-specific REPL
23+
24+#### Other notes:
25+
26+* PureScript arrays are implmented using [`std::deque`](http://en.cppreference.com/w/cpp/container/deque) (random access *O(1)*)
27+* `String` types are implmented using either C-strings (for literals) or `std::string`
28+* `Number` is C++ `double`, `Int` is C++ `int`, `Char` is `char`, `Boolean` is `bool`
29+* [The current tested/supported package set is here.](https://github.com/pure11/package-sets/blob/psc-0.10.2/packages.json) Please feel free to submit a PR if you've had success with any others.
30+
31+#### TO-DO:
32+
33+* Get automated builds/tests fully up and running [(some work already done)](https://github.com/pure11/purescript/blob/pure11/pcc/TestMain.hs)
34+* Unicode (UTF-8) support for `String` (possibly use code from my Idris backend)
35+* Lots of testing!
36+
37+#### Future ideas:
38+
39+* Nice facilities (modules) for concurrency/parallelism, using `std::thread`, `std::async`, etc. under the hood
40+* `BigInt` via GNU GMP (or an alternative)
41+
42+#### Requirements
43+
44+* Everything you need to build [PureScript](https://github.com/purescript/purescript)
45+* A C++11-capable toolchain, e.g. recent versions of clang, gcc, Microsoft Visual Studio 2015
46+* GNU Make is the default supported build tool, but you should be able to use your favorite C++ build system, tools, debuggers, etc.
47+
48+#### Examples
49+
50+**`fib.purs`**
51+```PureScript
52+module Main where
53+ import Prelude
54+ import Control.Monad.Eff.Console (log, logShow)
55+
56+ fib :: Int -> Int
57+ fib 0 = 0
58+ fib 1 = 1
59+ fib n = fib (n - 2) + fib (n - 1)
60+
61+ main = do
62+ log "Here's the result of fib 10:"
63+ logShow (fib 10)
64+```
65+**`fib.cc`**
66+```c++
67+#include "Main/Main.hh"
68+namespace Main {
69+ using namespace PureScript;
70+ using namespace Prelude;
71+ using namespace Control_Monad_Eff_Console;
72+ using namespace Data_Semiring;
73+ using namespace Data_Ring;
74+ using namespace Control_Bind;
75+ using namespace Control_Monad_Eff;
76+ using namespace Data_Show;
77+
78+ auto fib(int v) -> int {
79+ switch (cast<int>(v)) {
80+ case 0: return 0;
81+ case 1: return 1;
82+ };
83+ return fib(v - 2) + fib(v - 1);
84+ };
85+ auto main() -> any {
86+ Control_Monad_Eff_Console::log("Here's the result of fib 10:")();
87+ return Control_Monad_Eff_Console::logShow(Data_Show::showInt, fib(10))();
88+ };
89+}
90+
91+auto main(int, char *[]) -> int {
92+ using namespace Main;
93+ INITIALIZE_GC();
94+ Main::main();
95+ return 0;
96+};
97+```
98+---
99+#### [FFI examples](https://github.com/pure11/pure11/wiki/FFI_Examples)
100+---
101+#### Getting Started
102+This assumes you are running OS X or a Unix-like system (Linux, *BSD, etc.), and already have the ability to [build PureScript from source] (http://www.purescript.org/download/) (bottom of the page) -- but instead of cloning and building the repository in the instructions, make sure to use [this project's repo](https://github.com/pure11/purescript).
103+
104+1. Make sure you have developer tools for your system installed. For OS X, you'll need a recent version of Xcode. For Linux, etc., you need gcc 4.9.2 or later, including g++ support. You can also use clang 3.5 or later, but it still requires gcc for its C++ standard libraries.
105+
106+2. **Optional and advanced:** If you wish to use the Boehm garbage collector (better runtime performance, in general, but less tested), install it for your system. For OS X, [brew](http://brew.sh/) is an easy method and recommended (package `bdw-gc`). For linux, it's available under package names such as `libgc-dev` (for Debian/Ubuntu).
107+
108+3. Create a working directory wherever you like, and a `src` subdirectory under it, which will be where you will place your own PureScript source files.
109+
110+4. From your working directory, run the `{installation_path}/pcc` command with no arguments. This will generate `Makefile` and `psc-package.json` files in that directory. There are some usage notes in the generated `Makefile`.
111+
112+5. Use PureScript's standard `psc-package` utility to add and manage package dependencies.
113+
114+6. You should now be ready to build a PureScript program.
115+ * As stated above, place your source file(s) in the working directory's `src` subdirectory and execute `make`. If your machine has multiple cores, you might want to use `make -jN`, where `N` is the number of cores.
116+
117+ * **Optional and advanced:** To use the garbage collector, add `GC=yes` to the `make` command line (or you can modify the `Makefile` that you generated in step 4).
118+
119+ * This will generate the C++ source tree for your program and then build an executable binary. The resulting executable will be in the `bin` subdirectory under the output directory and called `main` (so `output/bin/main`, by default).
120+
121+---