2 releases

0.1.1 Aug 10, 2020
0.1.0 Aug 10, 2020

#624 in Programming languages

BSD-2-Clause

5KB
51 lines

cexe interpreter

This is a #! interpreter for when you can't use a real one.

You can use cexe to run arbitrary /bin/sh scripts to launch your program. This let's you perform additional set up, or call out to compilers, or do anything else you could possibly want to do to actually intepret your file.

#! /usr/bin/env cexe
# export A="An Environment Variable"
# exec nix run -f '<nixpkgs>' pkgs.python3 -c python3 "$0" "$@"

import sys;
import os;

print("Welcome to Python!")
print(os.getenv("A"))
print(sys.argv)

You simply place a comment block (with a consistent prefix for each line) with the shell script to run to launch your program. That script is given the file to be interpreted as the $0 argument, and the rest of the arguments after that, just like a regular shell script. The script goes until the first non-prefixed line.

#! /usr/bin/env cexe
// tmp="$(mktemp -d)"
// rustc -o "$tmp/$(basename $0)" 1>&2 "$0"
// "$tmp/$(basename $0)" "$@"
// r="$?"
// rm -Rf $tmp
// exit "$r"

pub fn main() {
    println!("Hello, rust");
    for arg in std::env::args() {
        println!("{}", arg);
    }
}

No runtime deps