Skip to content

Instantly share code, notes, and snippets.

@tompng
Created October 5, 2024 10:39
Show Gist options
  • Save tompng/228c943a347f9c57ec8615a002fc8ae5 to your computer and use it in GitHub Desktop.
Save tompng/228c943a347f9c57ec8615a002fc8ae5 to your computer and use it in GitHub Desktop.
require 'prism'
require 'ripper'
chars = %w[1 % = a b == * . ( ) , % ? ** & [ ] { } | ; : => ~ ! ^ || && '' .. ... < > + $a @a] + [' ', "\n", ' if ', ' and ',' rescue ', ' in ']
p seed: seed = rand(10000)
srand seed
$VERBOSE=nil
(3..10).each do |n|
[chars.size**n, 100000].min.times do |i|
code = n.times.map{chars.sample}.join
p [n, i] if i % 1000 == 0
debug = false
next if code =~ /\{\s*<<[-~]?[a-zA-Z1']/
# next unless n==6 && i > 50400; debug=1
[
"b=1;#{code}",
"(#{code})",
"p{|#{code}|}",
"def f(#{code});end",
"*x=#{code}",
"b=1 if #{code}",
"#{code} if 1",
"class #{code}; end",
"def (#{code}).f; end",
"1 rescue #{code}",
"begin; #{code} rescue 1; end",
"1 in #{code};",
"class A<#{code}; end",
"class <<#{code}; end",
"for a in #{code}; end",
"def f x=...#{code}; end",
"p 1, #{code}",
"{#{code}=>1}",
"{a:#{code}}",
"->#{code}{}",
"def f(...);b=1;#{code};end",
"def f(*,**,&);b=1;#{code};end",
"def f(b)=#{code}",
].each do |code2|
next if code2 =~ /(in|=>).*\*\s*([a-z][a-z\d]*)?\s*,/m
next if code2 =~ /:\s*\(\s*\*/
next if code2 =~ /\{.+:.*\(\s*\*/
next if code2 =~ /\n\s*\*[a-z]*\s*\)/
p code2 if debug
# begin; RubyVM::InstructionSequence.compile(code2); rescue SyntaxError; end
p code2 if !!Ripper.sexp(code2) != Prism.parse_success?(code2)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment