Class: Carbon::Compiler::Node::Expression::Literal

Inherits:
Base
  • Object
show all
Defined in:
lib/carbon/compiler/node/expression/literal.rb

Overview

A literal expression. Contains information about how the literal expression should be represented.

Constant Summary collapse

REPLACE =

Replacements for strings.

Returns:

  • ({String => String})
{
  '\"' => '"',
  '\\\\' => '\\',
  '\/' => "/",
  '\b' => "\b",
  '\f' => "\f",
  '\n' => "\n",
  '\r' => "\r",
  '\t' => "\t"
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#children, #location

Instance Method Summary collapse

Methods inherited from Base

#[], #accept, #attributes!, #behavior?, #data, #data?, #each, #identity?, #initialize, #map!, mapping, #merge!, #type!, #update!

Constructor Details

This class inherits a constructor from Carbon::Compiler::Node::Base

Instance Method Details

#typeCarbon::Concrete::Type

Yields the Carbon type representing the literal.

Returns:

  • (Carbon::Concrete::Type)


45
46
47
48
49
50
51
52
53
# File 'lib/carbon/compiler/node/expression/literal.rb', line 45

def type
  case @children.first.type
  # when :NUMBER then Carbon::Type("Carbon::Int32")
  when :NUMBER then number_type
  when :FLOAT then Carbon::Type("Carbon::Float")
  when :STRING then Carbon::Type("Carbon::String")
  else fail
  end
end

#value::Numeric, ::String

Yields a LLVM value representing the literal.

Returns:

  • (::Numeric, ::String)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/carbon/compiler/node/expression/literal.rb', line 28

def value
  case @children.first.type
  when :NUMBER then number_value
    # LLVM.Int(@children.first.value.to_i)
  when :FLOAT
    LLVM.Float(@children.first.value.to_f)
  when :STRING
    fail
    @children.first.value[1..-2]
      .gsub(%r{\\["\\/bfnrt]}, REPLACE)
  else fail
  end
end