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.
11 12 13 14 15 16 17 18 19 |
# File 'lib/basic101/runtime.rb', line 11 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.
9 10 11 |
# File 'lib/basic101/runtime.rb', line 9 def for_stack @for_stack end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
7 8 9 |
# File 'lib/basic101/runtime.rb', line 7 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
8 9 10 |
# File 'lib/basic101/runtime.rb', line 8 def output @output end |
Instance Method Details
#add_function(function) ⇒ Object
96 97 98 |
# File 'lib/basic101/runtime.rb', line 96 def add_function(function) @functions.add_function(function) end |
#call_function(identifier, argument_values) ⇒ Object
92 93 94 |
# File 'lib/basic101/runtime.rb', line 92 def call_function(identifier, argument_values) @functions.call(self, identifier, argument_values) end |
#end_program ⇒ Object
55 56 57 |
# File 'lib/basic101/runtime.rb', line 55 def end_program @program_counter.goto_end end |
#function_exists?(identifier) ⇒ Boolean
88 89 90 |
# File 'lib/basic101/runtime.rb', line 88 def function_exists?(identifier) @functions.has_function?(identifier) end |
#get_array(identifier, num_dimensions) ⇒ Object
108 109 110 111 |
# File 'lib/basic101/runtime.rb', line 108 def get_array(identifier, num_dimensions) @arrays[identifier.to_s] ||= BasicArray.new(num_dimensions, identifier.default) end |
#get_data_item ⇒ Object
113 114 115 116 117 |
# File 'lib/basic101/runtime.rb', line 113 def get_data_item data_item = @data_items.shift raise OutOfDataError unless data_item data_item end |
#get_scalar(identifier) ⇒ Object
100 101 102 |
# File 'lib/basic101/runtime.rb', line 100 def get_scalar(identifier) @scalars[identifier.to_s] ||= BasicInteger.new(0) end |
#gosub_line(line_number) ⇒ Object
39 40 41 |
# File 'lib/basic101/runtime.rb', line 39 def gosub_line(line_number) @program_counter.gosub_line(line_number) end |
#goto_index(index) ⇒ Object
47 48 49 |
# File 'lib/basic101/runtime.rb', line 47 def goto_index(index) @program_counter.goto_index(index) end |
#goto_index_after(index) ⇒ Object
51 52 53 |
# File 'lib/basic101/runtime.rb', line 51 def goto_index_after(index) @program_counter.goto_index_after(index) end |
#goto_line(line_number) ⇒ Object
35 36 37 |
# File 'lib/basic101/runtime.rb', line 35 def goto_line(line_number) @program_counter.goto_line(line_number) end |
#rand ⇒ Object
31 32 33 |
# File 'lib/basic101/runtime.rb', line 31 def rand @random.rand end |
#randomize ⇒ Object
27 28 29 |
# File 'lib/basic101/runtime.rb', line 27 def randomize @random = Random.new end |
#restore(line_number = nil) ⇒ Object
119 120 121 |
# File 'lib/basic101/runtime.rb', line 119 def restore(line_number = nil) @data_items = @program.data_items(line_number) end |
#return ⇒ Object
43 44 45 |
# File 'lib/basic101/runtime.rb', line 43 def return @program_counter.return end |
#run ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/basic101/runtime.rb', line 59 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
104 105 106 |
# File 'lib/basic101/runtime.rb', line 104 def set_scalar(identifier, value) @scalars[identifier.to_s] = value end |
#transcribe_errors ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/basic101/runtime.rb', line 76 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
21 22 23 24 25 |
# File 'lib/basic101/runtime.rb', line 21 def transcript=(transcript) @transcript = transcript @output.transcript = @transcript @input.transcript = @transcript end |