Skip to content

Instantly share code, notes, and snippets.

@shinh
Last active May 28, 2018 15:36
Show Gist options
  • Save shinh/0602ee7e215d1d483681685d6e6cb408 to your computer and use it in GitHub Desktop.
Save shinh/0602ee7e215d1d483681685d6e6cb408 to your computer and use it in GitHub Desktop.
eregon.rb, decoded by s/eval/print/
# https://github.com/tric/trick2018/blob/master/honorable-mentions/eregon/entry.rb
#
# Just replacing eval by print should yield:
i,s,S=ARGF.read,[],/"(?:\\["\\\/bfnrtu]|[^"\\])*"/
while i[S]
s << $` << $&
i=$'
end
s << i
I=s.each{|e|e.gsub!(/\s+/,"")if e[0]!=?"}.join
G=/(?<obj>\{(?:\g<pair>(?:,\g<pair>)*)?\}){0}(?<pair>\g<str>:\g<val>){0}(?<ary>\[(?:\g<val>(?:,\g<val>)*)?\]){0}(?<val>-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|true|false|null|\g<str>|\g<ary>|\g<obj>){0}(?<str>#{S}){0}/
R=G.names.map{|n|[n,/#{G}\G\g<#{n}>/]}.to_h
M=G.to_s
C=/(\\?[^()|g]*)/
Q,H,T=/(?:\(\?:)?/,/\\g<(\w+)>/,/\)\*\)\?/
@i=0
L=->s{
@i+=s.tr("\\","").size
nil
}
def m(n)
I.index(R[n],@i) or return
a = $&
M[/\(\?<#{n}>#{C}#{Q}#{H}#{Q}#{C}#{H}#{T}?#{C}/]
if $2&&$2==$4
p [1, n]
L[$1]
a=[]
k=m($2)
a<<k if k
a<<m($2)while I[@i]==$3&&@i+=1
L[$5]
n=="obj" ? a.to_h : a
elsif $2
p [2, n]
[*L[$1],m($2),*L[$3],m($4)]
else M[/\(\?<#{n}>.+?\{0\}/].scan(H){|z,|z=m(z)and return z}
p [3, n, a]
@i+=a.size
eval"null=nil
#{a}"
end
end
pp m("val")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment