https://github.com/heapwolf/bale
A transpiler/module system polyfill for C++
https://github.com/heapwolf/bale
Last synced: 10 months ago
JSON representation
A transpiler/module system polyfill for C++
- Host: GitHub
- URL: https://github.com/heapwolf/bale
- Owner: heapwolf
- Created: 2014-09-22T15:49:04.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-08-07T18:07:05.000Z (over 10 years ago)
- Last Synced: 2025-04-22T22:17:07.292Z (10 months ago)
- Language: C++
- Homepage:
- Size: 270 KB
- Stars: 38
- Watchers: 10
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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`](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2073.pdf),
[`this`](http://isocpp.org/files/papers/n4214.pdf), clang's
[`experimental module implementation`](http://clang.llvm.org/docs/Modules.html)
and the node.js [`module system`](http://nodejs.org/api/modules.html).
# 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.
```cpp
//
// 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`](http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders).
## ./PIZZA.CC
The module imported by the code in `index.cc`.
```cpp
#include
#include
//
// 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`.
```cpp
export {
public:
int add(int i) {
return i*i;
}
}
```
## ./CC_MODULES/SERVE/INDEX.CC
```cpp
export {
public:
void plate(int pizza) {
cout << pizza << endl;
}
}
```
# USAGE
```bash
bale ./test/input/index.cc
```