Class: Literal
Overview
The literal class represents a variable that doesn’t reference a different variable. For example ‘any string’ or 889.
Scope - a literal strictly speaking doesn’t need a scope id since
its scope will always be the root of the runtime method
therefore allowing it to be created anywhere.
Instance Attribute Summary collapse
-
#scope_id ⇒ Object
Returns the value of attribute scope_id.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#allowed?(method_call) ⇒ Boolean
Check whether this variable can use the specifed method call.
- #copy ⇒ Object
- #copy_requirements ⇒ Object
- #creation ⇒ Object
-
#declaration_statement ⇒ Object
Returns a statement that declares a literal.
- #describe ⇒ Object
-
#destructive_instance_calls(available = []) ⇒ Object
Returns an array of desctructive instance calls given the available variables.
- #equivalent?(to) ⇒ Boolean
-
#initialize(value) ⇒ Literal
constructor
A new instance of Literal.
- #to_declaration ⇒ Object
- #to_literal_string ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(value) ⇒ Literal
Returns a new instance of Literal.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/core/literal/Literal.rb', line 14 def initialize(value) super() # Check the datatype of the literal case value.class.to_s when 'String' when 'Numeric' when 'NilClass' when 'Array' when 'Fixnum' when 'Symbol' else raise UnliteralisableError.new('Unknown literal data type: '+value.class.to_s) end # Declare the accessable variable @value = value end |
Instance Attribute Details
#scope_id ⇒ Object
Returns the value of attribute scope_id.
11 12 13 |
# File 'lib/core/literal/Literal.rb', line 11 def scope_id @scope_id end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
11 12 13 |
# File 'lib/core/literal/Literal.rb', line 11 def value @value end |
Instance Method Details
#allowed?(method_call) ⇒ Boolean
Check whether this variable can use the specifed method call.
57 58 59 |
# File 'lib/core/literal/Literal.rb', line 57 def allowed? method_call return StatementCheck.new.valid_syntax?(self.write+'.'+method_call.write) end |
#copy ⇒ Object
46 47 48 |
# File 'lib/core/literal/Literal.rb', line 46 def copy return Literal.new(@value) end |
#copy_requirements ⇒ Object
50 51 52 |
# File 'lib/core/literal/Literal.rb', line 50 def copy_requirements return [Requirement.new(This.new,Equal.new,Literal.new(@value.clone))] end |
#creation ⇒ Object
67 68 69 |
# File 'lib/core/literal/Literal.rb', line 67 def creation return 'Literal.new('+@value.creation+')' end |
#declaration_statement ⇒ Object
Returns a statement that declares a literal
63 64 65 |
# File 'lib/core/literal/Literal.rb', line 63 def declaration_statement return Statement.new(ClassMethodCallContainer.new(LiteralClass.new,New.new,@value)) end |
#describe ⇒ Object
42 43 44 |
# File 'lib/core/literal/Literal.rb', line 42 def describe return write end |
#destructive_instance_calls(available = []) ⇒ Object
Returns an array of desctructive instance calls given the available variables.
98 99 100 |
# File 'lib/core/literal/Literal.rb', line 98 def destructive_instance_calls(available=[]) return [] end |
#equivalent?(to) ⇒ Boolean
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/core/literal/Literal.rb', line 79 def equivalent?(to) return false unless to.class == self.class if @value.kind_of?(Array) @value.zip(to.value) do |x,y| return false unless x.equivalent?(y) end return true else return true if @value == to.value end return false end |
#to_declaration ⇒ Object
75 76 77 |
# File 'lib/core/literal/Literal.rb', line 75 def to_declaration return LiteralDeclaration.new(@value) end |
#to_literal_string ⇒ Object
71 72 73 |
# File 'lib/core/literal/Literal.rb', line 71 def to_literal_string return write end |
#write ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/core/literal/Literal.rb', line 33 def write() if @value.class.to_s == 'String' then return safe_quote(@value) end if @value.class.to_s == 'Symbol' then return @value.write end if @value.kind_of?(Array) return @value.write end return @value.to_s end |