Skip to content

Script Mode

Derek Snider edited this page Jul 23, 2026 · 1 revision

Script Mode

You don't need main(). A .mad file can be just statements, top to bottom — madc synthesizes main(argc, argv) around them and runs the file like a script:

printf("hello from script mode: %d\n", 6 * 7);
$ madc hello.mad
hello from script mode: 42

No #include, no main, no build step. (Auto-header inclusion resolved printf automatically.)

Shebang

Make scripts directly executable:

#!/usr/bin/env madc
printf("running as an executable script\n");
chmod +x hello.mad
./hello.mad

Arguments: the madc::sys object

#include <ns_madc> gives you the madc::sys system object — madc's take on Python's import sys:

#!/usr/bin/env madc
#include <iostream>
#include <ns_madc>
using namespace std;

for ( int i = 0; i < madc::sys.argv.count(); ++i )
    cout << i << ": " << madc::sys.argv[i] << endl;
$ madc args.mad alpha beta
0: args.mad
1: alpha
2: beta

Classic int main(int argc, char **argv) continues to work exactly as in C — script mode only kicks in for files with top-level statements.

Semantics

  • Top-level statements execute in order, as the body of the synthesized main
  • Global variables with non-constant initializers are initialized dynamically before the first statement runs
  • The script's exit status is the value of the last reached exit() / return, with full parity between JIT and native runs
  • Script-mode files compile to native executables too: madc -o hello hello.mad works on a main-less script

What's next?

Clone this wiki locally