|
|
@ -1,8 +1,14 @@
|
|
|
|
#!/usr/bin/env ruby -w
|
|
|
|
#!/usr/bin/env ruby -w
|
|
|
|
|
|
|
|
|
|
|
|
module Lox
|
|
|
|
module Lox
|
|
|
|
class << self
|
|
|
|
class Error < StandardError
|
|
|
|
@had_error = false
|
|
|
|
def initialize(line:, where: "", message:)
|
|
|
|
|
|
|
|
@line, @where, @message = line, where, message
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
|
|
|
|
"[line #@line] Error#@where: #@message"
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.run_prompt
|
|
|
|
def self.run_prompt
|
|
|
@ -10,33 +16,42 @@ module Lox
|
|
|
|
print "> "
|
|
|
|
print "> "
|
|
|
|
line = gets
|
|
|
|
line = gets
|
|
|
|
break if line.empty?
|
|
|
|
break if line.empty?
|
|
|
|
|
|
|
|
begin
|
|
|
|
run(line)
|
|
|
|
run(line)
|
|
|
|
@had_error = false
|
|
|
|
rescue Error => e
|
|
|
|
|
|
|
|
puts e.message
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.run_file(io)
|
|
|
|
def self.run_file(io)
|
|
|
|
run(io.read)
|
|
|
|
run(io.read)
|
|
|
|
|
|
|
|
rescue Error
|
|
|
|
exit 65 if @had_error
|
|
|
|
puts e.message
|
|
|
|
|
|
|
|
exit 65
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.run(src)
|
|
|
|
def self.run(src)
|
|
|
|
scanner = Scanner.new(src)
|
|
|
|
Runner.new(src).run
|
|
|
|
tokens = scanner.scan()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
tokens.each do |token|
|
|
|
|
def self.error(line, msg)
|
|
|
|
puts token
|
|
|
|
raise Error(line:, message:)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Runner
|
|
|
|
|
|
|
|
def initialize(scanner:)
|
|
|
|
|
|
|
|
@scanner = scanner
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.error(line, msg)
|
|
|
|
def run(src)
|
|
|
|
report(line, "", msg)
|
|
|
|
@scanner.scan(src)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.report(line, where, message)
|
|
|
|
class Scanner
|
|
|
|
puts "[line #{line}] Error#{where}: #{message}"
|
|
|
|
def scan(src)
|
|
|
|
@had_error = true;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|