Giter VIP home page Giter VIP logo

purescript-native's Introduction

PureScript

This is an experimental C++11/native compiler backend for 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.


Please note that projects purescript-native and pure11 are now the same


Performance

  • No runtime system (beyond some support classes and the standard C++11 runtime library)
  • For automatic memory management, uses either native C++11 reference counting (std::shared_ptr), or the Boehm-Demers-Weiser Garbage Collector -- selectable when building (see instructions below)
  • Uses PureScript's normal tail call optimizations for generated C++11 code

Differences from PureScript:

  • Foreign imports are C++11 (or C) instead of JavaScript (FFI examples)
  • Foreign functions are not written as curried functions (any necessary currying is done implicitly by the compiler)
  • Compiler is pcc instead of psc, make is recommended for building
  • No native/pure11-specific REPL

Other notes:

  • PureScript arrays are implemented using std::deque (random access O(1))
  • String types are implemented using either C-strings (for literals) or std::string
  • Number is C++ double, Int is C++ int, Char is char, Boolean is bool
  • The current tested/supported package set is here. Please feel free to submit a PR if you've had success with any others.

TO-DO:

  • Get automated builds/tests fully up and running (some work already done)
  • Unicode (UTF-8) support for String (possibly use code from my Idris backend)
  • Lots of testing!

Future ideas:

  • Nice facilities (modules) for concurrency/parallelism, using std::thread, std::async, etc. under the hood
  • BigInt via GNU GMP (or an alternative)

Requirements

  • Everything you need to build PureScript (if you're running macOS 10.11+, you can use pre-built binaries from here)
  • A C++11-capable toolchain, e.g. recent versions of clang, gcc, Microsoft Visual Studio 2015
  • GNU Make is the default supported build tool, but you should be able to use your favorite C++ build system, tools, debuggers, etc.

Examples

fib.purs

module Main where
  import Prelude
  import Control.Monad.Eff.Console (log, logShow)

  fib :: Int -> Int
  fib 0 = 0
  fib 1 = 1
  fib n = fib (n - 2) + fib (n - 1)

  main = do
    log "Here's the result of fib 10:"
    logShow (fib 10)

fib.cc

#include "Main/Main.hh"
namespace Main {
  using namespace PureScript;
  using namespace Prelude;
  using namespace Control_Monad_Eff_Console;
  using namespace Data_Semiring;
  using namespace Data_Ring;
  using namespace Control_Bind;
  using namespace Control_Monad_Eff;
  using namespace Data_Show;
  
  auto fib(int v) -> int {
    switch (cast<int>(v)) {
      case 0: return 0;
      case 1: return 1;
    };
    return fib(v - 2) + fib(v - 1);
  };
  auto main() -> any {
    Control_Monad_Eff_Console::log("Here's the result of fib 10:")();
    return Control_Monad_Eff_Console::logShow(Data_Show::showInt, fib(10))();
  };
}

auto main(int, char *[]) -> int {
  using namespace Main;
  INITIALIZE_GC();
  Main::main();
  return 0;
};


Getting Started

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.

If you're running macOS 10.11+ (El Capitan or later), you can avoid building it yourself by using these pre-built snapshot binaries, but make sure you've installed a recent version of purescript first.

  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.

  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 is an easy method and recommended (package bdw-gc). For linux, it's available under package names such as libgc-dev (for Debian/Ubuntu).

  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.

  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.

  5. Use PureScript's standard psc-package utility to add and manage package dependencies.

  6. You should now be ready to build a PureScript program.

  • 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.

  • 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).

  • 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).


purescript-native's People

Contributors

paf31 avatar garyb avatar andyarvanitis avatar hdgarrood avatar joneshf avatar kritzcreek avatar phadej avatar puffnfresh avatar nwolverson avatar ardumont avatar liamgoodacre avatar michaelficarra avatar 5outh avatar bogdanp avatar panhania avatar japesinator avatar zudov avatar mjgpy3 avatar erdeszt avatar dylex avatar nicodelpiano avatar michaelxavier avatar epost avatar andreypopp avatar brandonhamilton avatar vkorablin avatar faineance avatar natefaubion avatar frigoeu avatar soupi avatar

Watchers

Nikolay Grebenshikov avatar paling avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.