diff --git a/ruby/Rakefile b/ruby/Rakefile index bfd5f99..ba8c644 100644 --- a/ruby/Rakefile +++ b/ruby/Rakefile @@ -1,6 +1,4 @@ require "minitest/test_task" -Minitest::TestTask.create do |t| - t.test_globs = "./test_*.rb" -end +Minitest::TestTask.create task default: :test diff --git a/ruby/bin/lox b/ruby/bin/lox new file mode 100755 index 0000000..c0a2c0a --- /dev/null +++ b/ruby/bin/lox @@ -0,0 +1,40 @@ +#!/usr/bin/env ruby -w + +$LOAD_PATH.unshift(File.expand_path("../lib", __dir__)) + +require "lox" + +def run_prompt + loop do + print "> " + line = gets + break if line.nil? || line.empty? + begin + run(line) + rescue Lox::Error => e + puts e.message + end + end +end + +def run_file(io) + run(io.read) +rescue Lox::Error => e + puts e.message + exit 65 +end + +def run(src) + Lox::Runner.new.run(src) +end + + +if __FILE__ == $0 + puts "Usage: #$0 [script]" or exit 64 if ARGV.length > 1 + + if ARGV.empty? + run_prompt + else + run_file(ARGF) + end +end diff --git a/ruby/lox.rb b/ruby/lib/lox.rb old mode 100755 new mode 100644 similarity index 90% rename from ruby/lox.rb rename to ruby/lib/lox.rb index b7dbda2..416c12b --- a/ruby/lox.rb +++ b/ruby/lib/lox.rb @@ -13,30 +13,6 @@ module Lox end end - def self.run_prompt - loop do - print "> " - line = gets - break if line.empty? - begin - run(line) - rescue Error => e - puts e.message - end - end - end - - def self.run_file(io) - run(io.read) - rescue Error - puts e.message - exit 65 - end - - def self.run(src) - Runner.new.run(src) - end - def self.error(line, msg) raise Error(line:, message:) end @@ -229,13 +205,3 @@ module Lox end end end - -if __FILE__ == $0 - puts "Usage: #$0 [script]" or exit 64 if ARGV.length > 1 - - if ARGV.empty? - Lox.run_prompt - else - Lox.run_file(ARGF) - end -end diff --git a/ruby/test_lox.rb b/ruby/test/test_lox.rb similarity index 95% rename from ruby/test_lox.rb rename to ruby/test/test_lox.rb index e217fd1..1aeff9e 100644 --- a/ruby/test_lox.rb +++ b/ruby/test/test_lox.rb @@ -9,9 +9,10 @@ require "pry" class TestLox < Minitest::Test def test_error_on_more_than_one_arg - o, s = Open3.capture2("./lox.rb foo bar") + lox_path = File.expand_path("../bin/lox", __dir__) + o, s = Open3.capture2(lox_path, "foo", "bar") assert_equal 64, s.exitstatus - assert_equal "Usage: ./lox.rb [script]\n", o + assert_equal "Usage: #{lox_path} [script]\n", o end end