Skip to content

Instantly share code, notes, and snippets.

@sin-ack
Last active July 30, 2022 01:11
Show Gist options
  • Select an option

  • Save sin-ack/7f6d3fc21e75d547f2d797b1f0cf61d2 to your computer and use it in GitHub Desktop.

Select an option

Save sin-ack/7f6d3fc21e75d547f2d797b1f0cf61d2 to your computer and use it in GitHub Desktop.
Zig Compiler Simulator 2022
import random
import sys
import time
import pathlib
import re
AST_LOWERING_TOTAL = 600
AST_LOWERING_INCREMENT = 50
SEMA_TOTAL = 45000
SEMA_INCREMENT_MIN = 350
SEMA_INCREMENT_MAX = 650
CODEGEN_TOTAL = 70000
CODEGEN_INCREMENT = 1200
LLVM_FRAMES = 450
LLD_FRAMES = 30
FPS = 20 # std.Progress
COMPILER_DIR = pathlib.Path("zig/compiler") # Path to Zig repo
FUNCTION_RE = re.compile(r"^(?:pub )?fn ([\w\d_]+)", re.M)
def print_line(line):
sys.stdout.write("\x1b[2K\r" + line)
time.sleep(1 / FPS)
def get_zig_files():
return (COMPILER_DIR / "src").glob("**/*.zig")
def get_zig_symbols():
files = list(get_zig_files())
random.shuffle(files)
files = [(file, FUNCTION_RE.finditer(file.read_text())) for file in files]
while True:
try:
file, iterator = random.choice(files)
while True:
if random.random() < 0.5:
break
while True:
function = next(iterator)
if random.random() < 0.5:
break
yield f"{file.stem}.{function.group(1)}"
except StopIteration:
continue
def main():
ast_lowering = 0
sema_complete = 0
sema_jobs = 10
codegen_complete = 0
ast_lowering_total = random.randrange(int(AST_LOWERING_TOTAL * 0.9), AST_LOWERING_TOTAL)
zig_files_iterator = get_zig_files()
while ast_lowering < ast_lowering_total:
ast_lowering += random.randrange(AST_LOWERING_INCREMENT)
print_line(f"AST Lowering [{ast_lowering}] {next(zig_files_iterator).name} ...")
symbols = get_zig_symbols()
while sema_jobs < SEMA_TOTAL:
jobs_to_add = random.randrange(SEMA_INCREMENT_MIN, SEMA_INCREMENT_MAX)
complete_to_add = random.randrange(0, jobs_to_add)
sema_complete += complete_to_add
sema_jobs += jobs_to_add
print_line(f"Semantic Analysis [{sema_complete}/{sema_jobs}] {next(symbols)} ...")
while sema_complete < sema_jobs:
sema_complete += random.randrange(SEMA_INCREMENT_MIN, SEMA_INCREMENT_MAX)
print_line(f"Semantic Analysis [{sema_complete}/{sema_jobs}] {next(symbols)} ...")
codegen_total = random.randrange(int(CODEGEN_TOTAL * 0.9), CODEGEN_TOTAL)
while codegen_complete < codegen_total:
codegen_complete += random.randrange(0, CODEGEN_INCREMENT)
print_line(f"Code Generation [{codegen_complete}/{codegen_total}] {next(symbols)} ...")
for _ in range(LLVM_FRAMES):
print_line("LLVM Emit Output...")
for _ in range(LLD_FRAMES):
print_line("LLD Link...")
print_line("\n")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment