Class: Literal

Inherits:
Object show all
Includes:
Token
Defined in:
lib/core/literal/Literal.rb

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

Instance Method Summary collapse

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_idObject

Returns the value of attribute scope_id.



11
12
13
# File 'lib/core/literal/Literal.rb', line 11

def scope_id
  @scope_id
end

#valueObject (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.

Returns:



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

#copyObject



46
47
48
# File 'lib/core/literal/Literal.rb', line 46

def copy
  return Literal.new(@value)
end

#copy_requirementsObject



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

#creationObject



67
68
69
# File 'lib/core/literal/Literal.rb', line 67

def creation
  return 'Literal.new('+@value.creation+')'
end

#declaration_statementObject

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

#describeObject



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.

Parameters:

  • available (defaults to: [])

    An array of other variables that can be used as parameters.



98
99
100
# File 'lib/core/literal/Literal.rb', line 98

def destructive_instance_calls(available=[])
  return []
end

#equivalent?(to) ⇒ Boolean

Returns:



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_declarationObject



75
76
77
# File 'lib/core/literal/Literal.rb', line 75

def to_declaration
  return LiteralDeclaration.new(@value)
end

#to_literal_stringObject



71
72
73
# File 'lib/core/literal/Literal.rb', line 71

def to_literal_string
  return write
end

#writeObject



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