<commit_before>#!/bin/bash

usage() {
	cat << EOF
Usage: nucc.sh [-hai] <source file> [<output file>]

Invoke the bootstrap compiler to compile the given nucleus source file.

    -h    display this help message
    -a    produce assembly output
    -i    produce LLVM IR output
EOF
}

output_ir=false
output_asm=false

script_dir=$(dirname "$(realpath "$0")")

OPTIND=1
while getopts 'hai' opt; do
	case "$opt" in
		h)
			usage
			exit 0
			;;
		a)
			output_asm=true
			;;
		i)
			output_ir=true
			;;
		'?')
			usage >&2
			exit 1
			;;
	esac
done
shift "$((OPTIND-1))"

if [ $output_ir = true ] && [ $output_asm = true ]; then
	echo "Error: only one of {-a, -i} can be specified"
	exit 1
fi

if [ "$#" -lt 1 ]; then
	usage >&2
	exit 1
fi

input="$(realpath $1)"
input_basename="$(realpath "$(basename "$input")")"

if [ $output_ir = true ]; then
	output="${input_basename%%.*}.ll"
elif [ $output_asm = true ]; then
	output="${input_basename%%.*}.s"
else
	output="$(realpath a.out)"
fi

if [ "$#" -gt 1 ]; then
	output="$(realpath $2)"
fi

cd "$script_dir"

make -C ../runtime > /dev/null
if [ $? -ne 0 ]; then
	echo Compiling the runtime failed, aborting
	exit 1
fi

bc=$(mktemp ${input%%.*}_XXX.bc)

sbcl --script nucc.lisp "$input" "$bc"
if [ $? -ne 0 ]; then
	rm "$bc"
	exit 1
fi

if [ $output_ir = true ]; then
	llvm-dis-3.5 "$bc" -o "$output"
	rm "$bc"
	exit 0
fi

if [ $output_asm = true ]; then
	llc-3.5 -filetype=asm "$bc" ../runtime/nuc-runtime.bc -o "$output"
	rm "$bc"
	exit 0
fi

obj=${input%%.*}.o
llc-3.5 -filetype=obj "$bc" -o "$obj"

rm "$bc"

# TODO: ld?
gcc "$obj" -o "$output"

rm "$obj"
exit 0<commit_msg>Add option to nucc.sh to run the binary immediately.<commit_after>#!/bin/bash

usage() {
	cat << EOF
Usage: nucc.sh [-hai] <source file> [<output file>]

Invoke the bootstrap compiler to compile the given nucleus source file.

    -h    display this help message
    -a    produce assembly output
    -i    produce LLVM IR output
    -r    run the program immediately
EOF
}

output_ir=false
output_asm=false

script_dir=$(dirname "$(realpath "$0")")

OPTIND=1
while getopts 'hair' opt; do
	case "$opt" in
		h)
			usage
			exit 0
			;;
		a)
			output_asm=true
			;;
		i)
			output_ir=true
			;;
		r)
			run=true
			;;
		'?')
			usage >&2
			exit 1
			;;
	esac
done
shift "$((OPTIND-1))"

if [ $output_ir = true ] && [ $output_asm = true ]; then
	echo "Error: only one of {-a, -i} can be specified"
	exit 1
fi

if [ "$#" -lt 1 ]; then
	usage >&2
	exit 1
fi

input="$(realpath $1)"
input_basename="$(realpath "$(basename "$input")")"

if [ $output_ir = true ]; then
	output="${input_basename%%.*}.ll"
elif [ $output_asm = true ]; then
	output="${input_basename%%.*}.s"
else
	output="$(realpath a.out)"
fi

if [ "$#" -gt 1 ]; then
	output="$(realpath $2)"
fi

cd "$script_dir"

make -C ../runtime > /dev/null
if [ $? -ne 0 ]; then
	echo Compiling the runtime failed, aborting
	exit 1
fi

bc=$(mktemp ${input%%.*}_XXX.bc)

sbcl --script nucc.lisp "$input" "$bc"
if [ $? -ne 0 ]; then
	rm "$bc"
	exit 1
fi

if [ $output_ir = true ]; then
	llvm-dis-3.5 "$bc" -o "$output"
	rm "$bc"
	exit 0
fi

if [ $output_asm = true ]; then
	llc-3.5 -filetype=asm "$bc" ../runtime/nuc-runtime.bc -o "$output"
	rm "$bc"
	exit 0
fi

obj=${input%%.*}.o
llc-3.5 -filetype=obj "$bc" -o "$obj"

rm "$bc"

# TODO: ld?
gcc "$obj" -o "$output"

rm "$obj"

if [ $run = true ]; then
	"$output"
	rm "$output"
fi

exit 0
{'fraction_non_alphanumeric': 0.18453427065026362, 'fraction_numerical': 0.015231400117164616, 'mean_word_length': 3.0654761904761907, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5058454', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1361, 'n_words': 426}