Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
Created April 13, 2020 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelfeathers/615047a5b2ed9de40a27593a4439d55f to your computer and use it in GitHub Desktop.
Save michaelfeathers/615047a5b2ed9de40a27593a4439d55f to your computer and use it in GitHub Desktop.
class Command
def run line, session
return unless matches? line
process line, session
end
end
class FixView < Command
def matches? line
line.split == ["fix"]
end
def process line, session
session.fix
end
end
class FixNew < Command
def matches? line
line.split == ["fix","new"]
end
def process line, session
session.fix_new
end
end
class FixAdd < Command
def matches? line
line.split.take(2) == ["fix", "add"]
end
def process line, session
session.fix_add(line.split.drop(2).join(" "))
end
end
class FixRemove < Command
def matches? line
line.split.take(2) == ["fix", "remove"]
end
def process line, session
session.fix_remove
end
end
class Ask < Command
def matches? line
line.split.take(1) == ["ask"]
end
def process line, session
session.ask(line.split.drop(1).join(" "))
end
end
class Push < Command
def matches? line
tokens = line.split
tokens.count >= 2 && tokens[0] == "push"
end
def process line, session
session.push(line.split.drop(1).join(" "))
end
end
class C11R
@@commands = [FixNew.new,
FixView.new,
FixAdd.new,
FixRemove.new,
Ask.new,
Push.new]
def initialize
@session = Session.new
end
def run
ARGF.each_line {|li| on_line(li) }
end
def on_line line
@@commands.each {|c| c.run(line, @session) }
end
end
class Session
def initialize
@fix = []
@question = @answer = ""
end
def fix_new
@fix = []
end
def fix
puts @fix.join(";")
end
def fix_add line
@fix << line
end
def fix_remove
@fix = @fix[0..-2]
end
def ask line
@question = line
@answer = eval [@fix, @question].join(";")
puts @answer
@answer
end
def push line
puts "test \"#{line}\""
puts " assert_eq(#{@answer},#{@question}))"
puts "end"
@question = @answer = ""
end
end
C11R.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment