Class: Nudge::NudgeProgram
- Inherits:
-
Object
- Object
- Nudge::NudgeProgram
- Defined in:
- lib/interpreter/nudge_program.rb
Instance Attribute Summary collapse
-
#code_section ⇒ Object
Returns the value of attribute code_section.
-
#footnote_section ⇒ Object
Returns the value of attribute footnote_section.
-
#footnotes ⇒ Object
Returns the value of attribute footnotes.
-
#linked_code ⇒ Object
Returns the value of attribute linked_code.
-
#points ⇒ Object
readonly
Returns the value of attribute points.
-
#raw_code ⇒ Object
Returns the value of attribute raw_code.
Class Method Summary collapse
Instance Method Summary collapse
- #[](which) ⇒ Object
- #blueprint ⇒ Object
- #cleanup_strings_from_linked_code! ⇒ Object
- #contains_codevalues?(program_blueprint = @raw_code) ⇒ Boolean
- #contains_valuepoints?(program_blueprint = @raw_code) ⇒ Boolean
- #deep_copy ⇒ Object
- #delete_point(which) ⇒ Object
-
#initialize(sourcecode) ⇒ NudgeProgram
constructor
A new instance of NudgeProgram.
- #insert_point_before(which, new_junk) ⇒ Object
- #parses?(program_blueprint = @code_section) ⇒ Boolean
- #replace_point(which, new_junk) ⇒ Object
- #tidy ⇒ Object
- #unused_footnotes ⇒ Object
Constructor Details
#initialize(sourcecode) ⇒ NudgeProgram
Returns a new instance of NudgeProgram.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/interpreter/nudge_program.rb', line 18 def initialize(sourcecode) raise(ArgumentError, "NudgeProgram.new should be passed a string") unless sourcecode.kind_of?(String) @raw_code = sourcecode split_at_first_guillemet=@raw_code.partition( /^(?=«)/ ) @code_section = split_at_first_guillemet[0].strip @footnote_section = split_at_first_guillemet[2].strip parsed_code = NudgeTree.from(@raw_code) @linked_code = parsed_code[:tree] @footnotes = parsed_code[:unused] @points = self.points end |
Instance Attribute Details
#code_section ⇒ Object
Returns the value of attribute code_section.
14 15 16 |
# File 'lib/interpreter/nudge_program.rb', line 14 def code_section @code_section end |
#footnote_section ⇒ Object
Returns the value of attribute footnote_section.
14 15 16 |
# File 'lib/interpreter/nudge_program.rb', line 14 def footnote_section @footnote_section end |
#footnotes ⇒ Object
Returns the value of attribute footnotes.
12 13 14 |
# File 'lib/interpreter/nudge_program.rb', line 12 def footnotes @footnotes end |
#linked_code ⇒ Object
Returns the value of attribute linked_code.
12 13 14 |
# File 'lib/interpreter/nudge_program.rb', line 12 def linked_code @linked_code end |
#points ⇒ Object (readonly)
Returns the value of attribute points.
15 16 17 |
# File 'lib/interpreter/nudge_program.rb', line 15 def points @points end |
#raw_code ⇒ Object
Returns the value of attribute raw_code.
13 14 15 |
# File 'lib/interpreter/nudge_program.rb', line 13 def raw_code @raw_code end |
Class Method Details
.random(options = {}) ⇒ Object
6 7 8 9 |
# File 'lib/interpreter/nudge_program.rb', line 6 def self.random( = {}) sourcecode = CodeType.any_value() NudgeProgram.new(sourcecode) end |
Instance Method Details
#[](which) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/interpreter/nudge_program.rb', line 44 def [](which) raise ArgumentError,"Cannot retrieve #{which}th program point" if which < 1 raise ArgumentError,"Cannot retrieve #{which}th program point" if which > self.points if which == 1 result = @linked_code else result = @linked_code.each_with_index do |node,index| break(node) if index == which-1 end end return result end |
#blueprint ⇒ Object
159 160 161 162 163 164 165 166 167 |
# File 'lib/interpreter/nudge_program.rb', line 159 def blueprint if @linked_code.kind_of? NilPoint return "" else code_section, footnote_section = @linked_code.blueprint_parts unused_footnotes.each {|fn| footnote_section += "\n#{fn}"} return (code_section.strip + " \n" + footnote_section.strip).strip end end |
#cleanup_strings_from_linked_code! ⇒ Object
138 139 140 141 142 143 |
# File 'lib/interpreter/nudge_program.rb', line 138 def cleanup_strings_from_linked_code! @raw_code = self.blueprint split_at_first_guillemet=@raw_code.partition( /^(?=«)/ ) @code_section = split_at_first_guillemet[0].strip @footnote_section = split_at_first_guillemet[2].strip end |
#contains_codevalues?(program_blueprint = @raw_code) ⇒ Boolean
170 171 172 |
# File 'lib/interpreter/nudge_program.rb', line 170 def contains_codevalues?(program_blueprint = @raw_code) (program_blueprint =~ /value\s*«code»/) != nil end |
#contains_valuepoints?(program_blueprint = @raw_code) ⇒ Boolean
174 175 176 |
# File 'lib/interpreter/nudge_program.rb', line 174 def contains_valuepoints?(program_blueprint = @raw_code) (program_blueprint =~ /value\s*«[\p{Alpha}][_\p{Alnum}]*»/) != nil end |
#deep_copy ⇒ Object
59 60 61 |
# File 'lib/interpreter/nudge_program.rb', line 59 def deep_copy NudgeProgram.new(self.blueprint) end |
#delete_point(which) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/interpreter/nudge_program.rb', line 115 def delete_point(which) raise ArgumentError,"Cannot delete #{which}th program point" if which < 1 raise ArgumentError,"Cannot delete #{which}th program point" if which > self.points if which == 1 result = NudgeProgram.new("block {}") result.footnotes = self.footnotes else result = self.deep_copy to_delete = result[which] parent = result.linked_code.detect do |wrapper| wrapper.contents.include?(to_delete) if wrapper.respond_to?(:contents) end parent_index = parent.contents.find_index(to_delete) parent.contents.delete_at(parent_index) end result.cleanup_strings_from_linked_code! return result end |
#insert_point_before(which, new_junk) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/interpreter/nudge_program.rb', line 89 def insert_point_before(which, new_junk) raise ArgumentError,"Cannot insert at #{which}th position" if which < 1 raise ArgumentError,"Cannot insert before #{which}th program point" if which > (self.points+1) raise ArgumentError,"Cannot insert #{new_junk.class}" unless new_junk.kind_of?(ProgramPoint) copy = self.deep_copy case which when 1 copy.linked_code = CodeblockPoint.new([new_junk, copy.linked_code]) when copy.points+1 copy.linked_code = CodeblockPoint.new([copy.linked_code, new_junk]) else stick_before_this = copy[which] parent_node = copy.linked_code.detect do |wrapper| wrapper.contents.include?(stick_before_this) if wrapper.respond_to?(:contents) end parent_index = parent_node.contents.find_index(stick_before_this) parent_node.contents.insert(parent_index, new_junk) end copy.cleanup_strings_from_linked_code! return copy end |
#parses?(program_blueprint = @code_section) ⇒ Boolean
147 148 149 |
# File 'lib/interpreter/nudge_program.rb', line 147 def parses?(program_blueprint = @code_section) !NudgeTree.from(program_blueprint)[:tree].kind_of?(NilPoint) end |
#replace_point(which, new_junk) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/interpreter/nudge_program.rb', line 65 def replace_point(which,new_junk) raise ArgumentError,"Cannot replace #{which}th program point" if which < 1 raise ArgumentError,"Cannot replace #{which}th program point" if which > self.points raise ArgumentError,"Cannot insert #{new_junk.class}" unless new_junk.kind_of?(ProgramPoint) result = self.deep_copy if which == 1 result.linked_code = new_junk else to_replace = result[which] parent = result.linked_code.detect do |wrapper| wrapper.contents.include?(to_replace) if wrapper.respond_to?(:contents) end parent_index = parent.contents.find_index(to_replace) parent.contents[parent_index] = new_junk end result.cleanup_strings_from_linked_code! return result end |
#tidy ⇒ Object
153 154 155 |
# File 'lib/interpreter/nudge_program.rb', line 153 def tidy NudgeTree.from(@raw_code)[:tree].tidy end |
#unused_footnotes ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/interpreter/nudge_program.rb', line 34 def unused_footnotes leftovers = [] @footnotes.each do |key,val| val.each {|fn| leftovers << "«#{key.to_s}» #{fn.strip}"} end return leftovers end |