Class: Theta::Base
- Inherits:
-
Object
- Object
- Theta::Base
- Defined in:
- lib/theta.rb
Overview
runs the interpreter
Instance Method Summary collapse
-
#initialize ⇒ Base
constructor
A new instance of Base.
-
#load_file(file_path) ⇒ Object
load and run the given file.
-
#repl ⇒ Object
start an interactive interpreter.
- #repl_help ⇒ Object
-
#run(program) ⇒ Object
run given code.
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
7 8 9 |
# File 'lib/theta.rb', line 7 def initialize @interpreter = Interpreter.new end |
Instance Method Details
#load_file(file_path) ⇒ Object
load and run the given file
12 13 14 15 |
# File 'lib/theta.rb', line 12 def load_file(file_path) program = File.open(file_path) { |f| f.read } run program end |
#repl ⇒ Object
start an interactive interpreter
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/theta.rb', line 26 def repl puts "For interpreter commands, type 'help'" while true print "theta> " input = gets.chomp case input #when "clear" # puts "Resetting environment..." # @interpreter = Interpreter.new when "exit", 24.chr puts "Exiting..." return when "help" repl_help else begin output = @interpreter.run(input) rescue SyntaxError end if not output.nil? and not output.empty? output.each { |value| puts @interpreter.make_readable(value) } end end end end |
#repl_help ⇒ Object
52 53 54 55 56 |
# File 'lib/theta.rb', line 52 def repl_help #puts "'clear' resets the environment" puts "'exit' or Ctrl-X will exit the interpreter" puts "'help' will display this message" end |
#run(program) ⇒ Object
run given code
18 19 20 21 22 23 |
# File 'lib/theta.rb', line 18 def run(program) output = @interpreter.run(program) if not output.nil? and not output.empty? output.each { |value| puts @interpreter.make_readable(value) } end end |