Class: Stone::AST::StringLiteral

Inherits:
Expression show all
Defined in:
lib/stone/ast/string_literal.rb

Instance Attribute Summary collapse

Attributes inherited from Stone::AST

#children, #name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ StringLiteral

Returns a new instance of StringLiteral.



18
19
20
21
# File 'lib/stone/ast/string_literal.rb', line 18

def initialize(value)
  @name = :string_literal
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



16
17
18
# File 'lib/stone/ast/string_literal.rb', line 16

def value
  @value
end

Class Method Details

.parse(text, location) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/stone/ast/string_literal.rb', line 8

def self.parse(text, location)
  # Remove surrounding quotes, but do NOT process escape sequences.
  value = text[1..-2]
  new(value)
rescue ex
  raise Stone::Error.new(ex.message, location)
end

Instance Method Details

#bytesizeObject

Byte count (for system/memory operations)



24
25
26
# File 'lib/stone/ast/string_literal.rb', line 24

def bytesize
  @value.bytesize
end

#lengthObject

Character count (for user-facing string length)



29
30
31
# File 'lib/stone/ast/string_literal.rb', line 29

def length
  @value.length
end

#to_llir(builder, mod, _scope = Stone::Scope.top_level) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/stone/ast/string_literal.rb', line 33

def to_llir(builder, mod, _scope = Stone::Scope.top_level)
  string_global = create_global_string(mod)

  # Get pointer to the string data (GEP to first element)
  # Array size is bytesize + 1 for null terminator
  builder.gep2(
    LLVM::Type.array(LLVM::Int8, @value.bytesize + 1),
    string_global,
    [LLVM::Int64.from_i(0), LLVM::Int64.from_i(0)],
    "string_ptr"
  )
  # Return pointer directly - no ptr2int (CHERI-safe)
end

#type(_context = nil) ⇒ Object



73
74
75
# File 'lib/stone/ast/string_literal.rb', line 73

def type(_context = nil)
  Stone::Type::String
end