Home

Awesome

MetaScript

Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that would otherwise be done at runtime.

MetaScript is a tool for build time meta programming using JavaScript as the meta language. Written between the lines it enables developers to transform sources in pretty much every way possible.

<p align="center"> <img src="https://raw.github.com/dcodeIO/MetaScript/master/example.jpg" /> </p>

Donate

How does it work?

If you already know JavaScript, adding some meta is as simple as remembering that:

MetaScript then turns the meta inside out, making it the actual program, that outputs the contents in between.

A simple example

Let's assume that you have a library and that you want its version number to be included as the constant MyLibrary.VERSION. With meta, this is as simple as:

MyLibrary.VERSION = /*?== VERSION */;
// or, alternatively, if VERSION is always string-safe:
MyLibrary.VERSION = "/*?= VERSION */";

This is what the meta program, when compiled, will look like:

  write('MyLibrary.VERSION = ');
write(JSON.stringify(VERSION));
  write(';\n');

Accordingly, a transformation of the source done by running that exact meta program with a scope of { VERSION: "1.0" } will result in:

MyLibrary.VERSION = "1.0";

It's just that simple and everything else is, of course, up to your imagination.

Advanced examples

Of course it's possible to do much more with it, like declaring macros and defining an entire set of useful utility functions, just like with any sort of preprocessor:

That's a globally available utility function as a snippet:

//?...
simpleIncludeExample = function(file) {
    write(indent(require("fs").readFileSync(file).toString("utf8")), __);
}
//?.

or, as a block:

/*? simpleIncludeExample = function(file) {
    write(indent(require("fs").readFileSync(file).toString("utf8")), __);
} */

Using it:

//? simpleIncludeExample("some/other/file.js")

This is, of course, just an example. See built-in utility for what's actually available out of the box.

That's a globally available macro using inline blocks:

//? ASSERT_OFFSET = function(varname) {
    if (/*?= varname */ < 0 || /*?= varname */ > this.capacity()) {
        throw RangeError("Illegal /*?= varname */");
    }
//? }

Using it:

function writeInt8(value, offset) {
    //? ASSERT_OFFSET('offset');
    ...
}

Results in:

function writeInt8(value, offset) {
    if (offset < 0 || offset > this.capacity()) {
        throw RangeError("Illegal offset");
    }
    ...
}

Some examples are available in the tests folder. While these are JavaScript examples, MetaScript should fit nicely with any other programming language that uses // ... and /* ... */ style comments.

API

The API is pretty much straight forward:

One step compilation / transformation:

Command line

Transforming sources on the fly is simple with node:

npm install -g metascript

 Usage: metascript sourcefile -SOMEDEFINE="some" -OTHERDEFINE="thing" [> outfile]

And in the case that you have to craft your own runtime, the raw compiler is also available as metac:

 Usage: metac sourcefile [> outfile]

Built-in utility

There are a few quite useful utility functions available to every meta program:

Additionally, there are a few internal variables. Most notably there is the variable __ (2x underscore) that remembers the current indentation level. This is used for example to indent included sources exactly like the meta block that contains the include call.

Using utility dependencies

In case this isn't obvious: Add the dependency to your package.json and, in MetaScript, use:

//? myutility = require('metascript-myutility')

Usage as a task

Documentation

License: Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html