Skip to content

Commit

Permalink
Merge pull request #62 from project-eutopia/interpreter
Browse files Browse the repository at this point in the history
Interpreter
  • Loading branch information
project-eutopia committed Jan 9, 2018
2 parents 39ee5b6 + 1e96ca1 commit 509bbda
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
15 changes: 13 additions & 2 deletions bin/keisan
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

require "bundler/setup"
require "keisan"
require "keisan/repl"
require "keisan/interpreter"

Keisan::Repl.new.start
file_name = nil
allow_recursive = false

ARGV.each do |arg|
if arg == "--allow_recursive"
allow_recursive = true
else
file_name = arg
end
end

Keisan::Interpreter.new(allow_recursive: allow_recursive).run(file_name)
40 changes: 40 additions & 0 deletions lib/keisan/interpreter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Keisan
class Interpreter
attr_reader :calculator

def initialize(allow_recursive: false)
@calculator = Calculator.new(allow_recursive: allow_recursive)
end

def run(file_name)
if file_name.nil?
run_from_stdin
else
run_from_file(file_name)
end
end

private

def run_from_stdin
run_on_content STDIN.read
end

def run_from_file(file_name)
run_on_content(
File.exists?(file_name) ? File.open(file_name) do |file|
file.read
end : ""
)
end

def run_on_content(content)
content = content.strip
if content.nil? || content.empty?
Repl.new.start
else
calculator.evaluate(content)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/keisan/repl.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "pry"
require "coderay"
require "readline"

module Keisan
Expand Down

0 comments on commit 509bbda

Please sign in to comment.