Coder Social home page Coder Social logo

bale's Introduction

SYNOPSIS

A transpiler/module system polyfill for C++.

MOTIVATION

C++ does not have a real module system. One is in the works but in the meanwhile, here is a future-compatible polyfill. Inspired by this, this, clang's experimental module implementation and the node.js module system.

PROJECT STATUS

Experimental. A work in progress.

EXAMPLE

A modue is a discrete unit of code, encapsulated in a file. It exposes at least one public value, function, class, etc. Let's use the following file strcture as an example...

index.cc
pizza.cc
cheese.cc
cc_modules/
  serve/
    index.cc

./INDEX.CC

index.cc is the typical entry point for most programs.

//
// import the file "pizza.cc" as "pizza".
//
import pizza "./pizza.cc";

//
// import the module "serve" as "serve".
//
import serve "serve";

int main() {
  serve.plate(pizza.make());
}

To understand how the math module is imported, read this.

./PIZZA.CC

The module imported by the code in index.cc.

#include <iostream>
#include <math.h>

//
// GLOBAL SCOPE
//
// Anything outside of export will be global, this will be 
// accessible from ANY file. you should not put anything here 
// unless you REALLY MEAN TO.
//

export {

  //
  // MODULE SCOPE
  //
  // Variables defined here are implicitly private, they are
  // not exported, this applies to varibles imported from other 
  // modules.
  //
  const double pi = M_PI;

  //
  // import the file "./cheese.cc" as "chz"
  //
  import chz "./cheese.cc";

  //
  // Variables found after the "public:" label are exported!
  //
  public:

    int make(int qty) {
      return qty * pi + chz.add(secret_sauce);
    }

  //
  // You can switch back and forth between public and private
  // by using the "private:" label.
  //
  private:
    int secret_sauce = 42;
}

./CHEESE.CC

This file happens to be included by pizza.cc.

export {
  public:
    int add(int i) {
      return i*i;
    }
}

./CC_MODULES/SERVE/INDEX.CC

export {
  public:
    void plate(int pizza) {
      cout << pizza << endl;
    }
}

USAGE

bale ./test/input/index.cc

bale's People

Contributors

heapwolf avatar dominictarr 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.