Skip to content

Instantly share code, notes, and snippets.

@Apocryphon-X
Last active June 4, 2023 18:18
Show Gist options
  • Save Apocryphon-X/089fa0ca3a0cfda9f8c70fcbdb55ac82 to your computer and use it in GitHub Desktop.
Save Apocryphon-X/089fa0ca3a0cfda9f8c70fcbdb55ac82 to your computer and use it in GitHub Desktop.
C++ compiling tool (g++ wrapper). Written for competitive programming and personal use.
#!/usr/bin/python3
# Copyright (c) 2022 @Apocryphon (Dante Mendoza Leyva).
# All rights reserved.
import subprocess
import click
import pathlib
import sys
import blessed
def highlight(text):
term = blessed.Terminal()
text = text.replace("Error:", term.red("Error:"))
text = text.replace("runtime error:", term.red("runtime error:"))
for cyan_symbol in [
"int",
"short",
"long",
"double",
"float",
"bool",
"string",
"this",
]:
text = text.replace(cyan_symbol, term.color_rgb(95, 215, 255)(cyan_symbol))
for green_symbol in ["@", "out_of_range", "what", "out-of-bounds"]:
text = text.replace(green_symbol, term.color_rgb(175, 215, 95)(green_symbol))
for red_symbol in ["->","::", "mutable", "const"]:
text = text.replace(red_symbol, term.color_rgb(231, 86, 87)(red_symbol))
for red_symbol in ">*<=&":
text = text.replace(red_symbol, term.color_rgb(231, 86, 87)(red_symbol))
return text
@click.command(help="Compile and run '.cpp' files using g++.")
@click.argument("FILE-PATH")
@click.option("--standard", "-std", default="c++11")
def compile_and_run(file_path, standard):
file_path = pathlib.Path(file_path)
subprocess.run(
[
"g++",
file_path.resolve(),
f"-std={standard}",
"-DLOCAL",
"-D_GLIBCXX_DEBUG",
"-D_GLIBCXX_DEBUG_PEDANTIC",
"-D_FORTIFY_SOURCE=2",
"-fsanitize=undefined",
"-fno-sanitize-recover",
"-fsanitize=address",
"-fstack-protector",
"-Wall",
"-Wextra",
"-Wshadow",
"-Wconversion",
"-Wfloat-equal",
"-Wformat=2",
"-Wlogical-op",
"-Wshift-overflow=2",
"-Wduplicated-cond",
"-g",
"-o",
file_path.resolve().with_suffix(""),
]
)
result = subprocess.run(file_path.resolve().with_suffix(""), capture_output=True)
output = result.stdout.decode("ascii")
logs = result.stderr.decode("ascii")
sys.stdout.write(output)
sys.stderr.write(highlight(logs))
if __name__ == "__main__":
compile_and_run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment