Skip to content

Instantly share code, notes, and snippets.

@cjsmeele
Last active October 26, 2021 11:52
Show Gist options
  • Save cjsmeele/1eb26afb8822f0e9916e677ebd1c758e to your computer and use it in GitHub Desktop.
Save cjsmeele/1eb26afb8822f0e9916e677ebd1c758e to your computer and use it in GitHub Desktop.
better_python3
#!/usr/bin/env perl
# This adds the post-inc, post-dec, pre-inc and pre-dec operators to python!
# Additionally, it supports the -O3 optimization feature.
my $optimized = 0;
if (@ARGV and $ARGV[0] eq '-O3') {
shift;
$optimized = 1;
}
open my $python, '|-', '/usr/bin/python3', '-' or die "boooo $1";
while (<>) {
s/^(\s*)([^#]*)(\w+)\s*\+\+(.*)/$1$2$3$4\n$1$3 += 1/;
s/^(\s*)([^#]*)\+\+\s*(\w+)(.*)/$1$3 += 1\n$1$2$3$4/;
s/^(\s*)([^#]*)(\w+)\s*--(.*)/$1$2$3$4\n$1$3 -= 1/;
s/^(\s*)([^#]*)--\s*(\w+)(.*)/$1$3 -= 1\n$1$2$3$4/;
next if $optimized and rand > 0.8;
print $python $_;
}
# Example python script:
#
# y = 0
# for i in range(0, 5):
# print(i, ++y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment