Tuesday, April 04, 2006

More ParseTree Goodness

Sean and I decided to work on finding noops in conditionals for checkr. It turns out ParseTree made this a lot easier than we thought it would. Here's the abbreviated code:

class CheckR
  def clean_conditional?(klass)
    ParseTest.new.process(ParseTree.new.parse_tree(klass))
    false
  end
end

class ParseTest < SexpProcessor
  def assignment_in_conditional?(exp)
    @saw_lasgn = false
    test_result = process(exp.shift)
    raise CheckRAssignmentInConditional if @saw_lasgn
    test_result
  end

  # new method
  def noop_in_conditional?(exp)
    raise CheckRNoop unless exp
    exp
  end

  def process_if(exp)
    sexp_type = exp.shift
    else_exp = exp.pop
    then_exp = exp.pop

    s(sexp_type,
      assignment_in_conditional?(exp),
      process(noop_in_conditional?(then_exp)),
      process(else_exp))
  end
end
I'm sure there's some more refactoring in there, but I can't see it right now. Probably because I should already be in bed. I guess I'll write a new test for Sean in the morning.

No comments: