Class: Basic101::Runtime
- Inherits:
-
Object
- Object
- Basic101::Runtime
- Defined in:
- lib/basic101/runtime.rb
Instance Attribute Summary collapse
-
#for_stack ⇒ Object
readonly
Returns the value of attribute for_stack.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #add_function(function) ⇒ Object
- #call_function(identifier, argument_values) ⇒ Object
- #end_program ⇒ Object
- #function_exists?(identifier) ⇒ Boolean
- #get_array(identifier, num_dimensions) ⇒ Object
- #get_data_item ⇒ Object
- #get_scalar(identifier) ⇒ Object
- #gosub_line(line_number) ⇒ Object
- #goto_index(index) ⇒ Object
- #goto_index_after(index) ⇒ Object
- #goto_line(line_number) ⇒ Object
-
#initialize(args = {}) ⇒ Runtime
constructor
A new instance of Runtime.
- #rand ⇒ Object
- #randomize ⇒ Object
- #restore(line_number = nil) ⇒ Object
- #return ⇒ Object
- #run ⇒ Object
- #set_scalar(identifier, value) ⇒ Object
- #transcribe_errors ⇒ Object
- #transcript=(transcript) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Runtime
Returns a new instance of Runtime.
9 10 11 12 13 14 15 16 17 |
# File 'lib/basic101/runtime.rb', line 9 def initialize(args = {}) @output = Output.new(args.fetch(:output_file, $stdout)) @input = Input.new(@output, args.fetch(:input_file, $stdin)) @program = args.fetch(:program, Program.new) @functions = Functions.new @for_stack = ForStack.new @random = Random.new(0) @transcript = NullTranscript.new end |
Instance Attribute Details
#for_stack ⇒ Object (readonly)
Returns the value of attribute for_stack.
7 8 9 |
# File 'lib/basic101/runtime.rb', line 7 def for_stack @for_stack end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
5 6 7 |
# File 'lib/basic101/runtime.rb', line 5 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
6 7 8 |
# File 'lib/basic101/runtime.rb', line 6 def output @output end |
Instance Method Details
#add_function(function) ⇒ Object
94 95 96 |
# File 'lib/basic101/runtime.rb', line 94 def add_function(function) @functions.add_function(function) end |
#call_function(identifier, argument_values) ⇒ Object
90 91 92 |
# File 'lib/basic101/runtime.rb', line 90 def call_function(identifier, argument_values) @functions.call(self, identifier, argument_values) end |
#end_program ⇒ Object
53 54 55 |
# File 'lib/basic101/runtime.rb', line 53 def end_program @program_counter.goto_end end |
#function_exists?(identifier) ⇒ Boolean
86 87 88 |
# File 'lib/basic101/runtime.rb', line 86 def function_exists?(identifier) @functions.has_function?(identifier) end |
#get_array(identifier, num_dimensions) ⇒ Object
106 107 108 109 |
# File 'lib/basic101/runtime.rb', line 106 def get_array(identifier, num_dimensions) @arrays[identifier.to_s] ||= BasicArray.new(num_dimensions, identifier.default) end |
#get_data_item ⇒ Object
111 112 113 114 115 |
# File 'lib/basic101/runtime.rb', line 111 def get_data_item data_item = @data_items.shift raise OutOfDataError unless data_item data_item end |
#get_scalar(identifier) ⇒ Object
98 99 100 |
# File 'lib/basic101/runtime.rb', line 98 def get_scalar(identifier) @scalars[identifier.to_s] ||= BasicInteger.new(0) end |
#gosub_line(line_number) ⇒ Object
37 38 39 |
# File 'lib/basic101/runtime.rb', line 37 def gosub_line(line_number) @program_counter.gosub_line(line_number) end |
#goto_index(index) ⇒ Object
45 46 47 |
# File 'lib/basic101/runtime.rb', line 45 def goto_index(index) @program_counter.goto_index(index) end |
#goto_index_after(index) ⇒ Object
49 50 51 |
# File 'lib/basic101/runtime.rb', line 49 def goto_index_after(index) @program_counter.goto_index_after(index) end |
#goto_line(line_number) ⇒ Object
33 34 35 |
# File 'lib/basic101/runtime.rb', line 33 def goto_line(line_number) @program_counter.goto_line(line_number) end |
#rand ⇒ Object
29 30 31 |
# File 'lib/basic101/runtime.rb', line 29 def rand @random.rand end |
#randomize ⇒ Object
25 26 27 |
# File 'lib/basic101/runtime.rb', line 25 def randomize @random = Random.new end |
#restore(line_number = nil) ⇒ Object
117 118 119 |
# File 'lib/basic101/runtime.rb', line 117 def restore(line_number = nil) @data_items = @program.data_items(line_number) end |
#return ⇒ Object
41 42 43 |
# File 'lib/basic101/runtime.rb', line 41 def return @program_counter.return end |
#run ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/basic101/runtime.rb', line 57 def run transcribe_errors do reset begin while !@program_counter.end? statement = @program_counter.current_statement begin @program_counter.goto_next_statement statement.execute(self) rescue StandardError => e statement.raise_error_with_line_number(e) end end end end end |
#set_scalar(identifier, value) ⇒ Object
102 103 104 |
# File 'lib/basic101/runtime.rb', line 102 def set_scalar(identifier, value) @scalars[identifier.to_s] = value end |
#transcribe_errors ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/basic101/runtime.rb', line 74 def transcribe_errors begin yield rescue Parslet::ParseFailed => e @transcript.save_output_lines e raise rescue Basic101::Error => e @transcript.save_output_lines e raise end end |
#transcript=(transcript) ⇒ Object
19 20 21 22 23 |
# File 'lib/basic101/runtime.rb', line 19 def transcript=(transcript) @transcript = transcript @output.transcript = @transcript @input.transcript = @transcript end |