|
#!/usr/bin/env python3 |
|
|
|
import sys |
|
|
|
langs = {"c", "d", "ll", "ml", "nim", "rs", "v", "zig"} |
|
|
|
snippets = { |
|
"head": { |
|
"c": |
|
'#include <stdio.h>\n' |
|
'int main() {' |
|
'int x = 0;', |
|
"d": |
|
'import std.stdio;' |
|
'void main() {' |
|
'int x = 0;', |
|
"ll": |
|
'@fmt = private unnamed_addr constant [4 x i8] c"%d\\0a\\00"\n' |
|
'declare i32 @printf(i8* nocapture, ...) nounwind\n' |
|
'define i32 @main() {\n' |
|
'%fmt = getelementptr [4 x i8], [4 x i8]* @fmt, i32 0, i32 0\n' |
|
'%x = alloca i32\n', |
|
"ml": |
|
'let x = ref 0\n' |
|
'let () = ', |
|
"nim": |
|
'var x = 0\n', |
|
"rs": |
|
'fn main() {' |
|
'let mut x: u8;', |
|
"v": |
|
'fn main() {\n' |
|
'mut x := 0\n', |
|
"zig": |
|
'const print = @import("std").debug.print;' |
|
'pub fn main() void {' |
|
'var x: u8 = 0;', |
|
}, |
|
|
|
"content": { |
|
"c": 'x = $VAL; printf("%d\\n", x);', |
|
"d": 'x = $VAL; writefln("%d", x);', |
|
"ll": 'store i32 $VAL, i32* %x\n' |
|
'%tmp$IDX = load i32, i32* %x\n' |
|
'call i32 (i8*, ...) @printf(i8* %fmt, i32 %tmp$IDX)\n', |
|
"ml": 'x := $VAL; print_endline (string_of_int !x);', |
|
"nim": 'x = $VAL; echo $x;', |
|
"rs": 'x = $VAL; println!("{}", x);', |
|
"v": "x = $VAL\nprintln('$x')\n", |
|
"zig": 'x = $VAL; print("{}\\n", .{x});', |
|
}, |
|
|
|
"end": { |
|
"c": '}', |
|
"d": '}', |
|
"ll": 'ret i32 0\n}', |
|
"ml": '', |
|
"nim": '', |
|
"rs": '}', |
|
"v": '}', |
|
"zig": '}', |
|
}, |
|
|
|
"compile": { |
|
"c": 'cc main.c', |
|
"d": 'dmd main.d', |
|
"ll": 'llc --filetype=obj -O=0 main.ll && cc main.o', |
|
"ml": 'ocamlopt main.ml', |
|
"nim": 'nim compile main.nim', |
|
"rs": 'rustc main.rs', |
|
"v": 'v main.v', |
|
"zig": 'zig build-exe main.zig', |
|
}, |
|
|
|
"clean": { |
|
"c": 'rm main.c a.out', |
|
"d": 'rm main.d main.o main', |
|
"ll": 'rm main.ll main.o a.out', |
|
"ml": 'rm main.ml main.cmi main.cmx main.o a.out', |
|
"nim": 'rm main.nim main', |
|
"rs": 'rm main.rs main', |
|
"v": 'rm main.v main', |
|
"zig": 'rm -r zig-cache main.zig main', |
|
}, |
|
} |
|
|
|
if len(sys.argv) != 3: |
|
print("usage: {} <language> <num. of statements>".format(sys.argv[0])) |
|
sys.exit(1) |
|
|
|
lang = sys.argv[1] |
|
iters = int(sys.argv[2]) |
|
|
|
if lang not in langs: |
|
print("no such language: {}".format(lang)) |
|
sys.exit(1) |
|
|
|
print("generating...", end=" ", flush=True) |
|
|
|
def snippet(name): |
|
return snippets[name].get(lang, "") |
|
|
|
filename = "main." + lang |
|
|
|
with open(filename, "w") as f: |
|
f.write(snippet("head")) |
|
|
|
for i in range(iters): |
|
s = snippet("content") \ |
|
.replace("$VAL", str(i % 2 + 2)) \ |
|
.replace("$IDX", str(i + 1)) |
|
f.write(s) |
|
|
|
f.write(snippet("end")) |
|
|
|
print("wrote to {}".format(filename)) |
|
|
|
import os, time |
|
|
|
start = time.time() |
|
print("compiling...") |
|
os.system(snippet("compile")) |
|
end = time.time() |
|
print("total time: {}s".format(end - start)) |
|
|
|
print("cleaning up...") |
|
os.system(snippet("clean")) |