Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env ruby
grammar, iterations, extra = ARGV
if grammar == nil or !File.exists?(grammar)
puts "no grammar file"
exit
end
symbols = {}
File.open(grammar, "r") do |file|
file.each do |line|
next if line =~ /^\s*#/
if line =~ /^\s*(\w+)\s*->\s*(.+)\s*$/
symbols[$1] = $2.split(//)
end
end
end
str = $stdin.gets.chomp.split(//)
iterations.to_i.times do
str = str.map { |c|
symbols.key?(c) ? symbols[c] : c
}.flatten
end
puts str.join + 0.chr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment