Module: Stone::Types

Defined in:
lib/stone/types.rb

Constant Summary collapse

INT_MIN =

-9_223_372_036_854_775_808

-(2**63)   # -9_223_372_036_854_775_808
INT_MAX =

+9_223_372_036_854_775_807

2**63 - 1

Class Method Summary collapse

Class Method Details

.bootstrap_registry!Object



17
18
19
20
21
22
23
# File 'lib/stone/types.rb', line 17

def bootstrap_registry!
  types = create_primitive_types
  register_all_types(types)
  setup_property_types(types)
  setup_type_constants(types)
  Stone::TypeRegistry.instance
end

.create_primitive_typesObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/stone/types.rb', line 25

def create_primitive_types
  {
    int: Stone::Type.primitive(name: "Int", llvm_type: LLVM::Int64.type, min: INT_MIN, max: INT_MAX),
    bool: Stone::Type.primitive(name: "Bool", llvm_type: LLVM::Int1.type),
    string: Stone::Type.primitive(name: "String", llvm_type: LLVM::Int64.type),
    type: Stone::Type.primitive(name: "Type", llvm_type: LLVM::Type.pointer),
    null: Stone::Type.primitive(name: "Null", llvm_type: LLVM::Type.ptr),
    # FieldList is a list-like type for record field metadata
    # TODO: Replace with List(Field) once generics are available
    field_list: Stone::Type.primitive(name: "FieldList", llvm_type: LLVM::Type.pointer)
  }
end

.register_all_types(types) ⇒ Object



38
39
40
41
# File 'lib/stone/types.rb', line 38

def register_all_types(types)
  registry = Stone::TypeRegistry.instance
  types.each_value { |type| registry.register(type) }
end

.setup_bool_properties(types) ⇒ Object



78
79
80
# File 'lib/stone/types.rb', line 78

def setup_bool_properties(types)
  types[:bool].property_types.merge!("not" => types[:bool], "as_String" => types[:string])
end

.setup_field_list_properties(types) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/stone/types.rb', line 62

def setup_field_list_properties(types)
  types[:field_list].property_types.merge!(
    "first" => types[:field_list],  # .first returns the FieldList itself (list-like access)
    "name" => types[:string],       # Field name
    "type" => types[:type],         # Type of the field
    "rest" => types[:field_list]    # Next FieldList or NULL
  )
end

.setup_int_properties(types) ⇒ Object



71
72
73
74
75
76
# File 'lib/stone/types.rb', line 71

def setup_int_properties(types)
  types[:int].property_types.merge!(
    "positive?" => types[:bool], "negative?" => types[:bool],
    "zero?" => types[:bool], "as_String" => types[:string]
  )
end

.setup_property_types(types) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/stone/types.rb', line 43

def setup_property_types(types)
  setup_int_properties(types)
  setup_bool_properties(types)
  setup_string_properties(types)
  setup_type_properties(types)
  setup_field_list_properties(types)
end

.setup_string_properties(types) ⇒ Object



82
83
84
85
86
# File 'lib/stone/types.rb', line 82

def setup_string_properties(types)
  types[:string].property_types.merge!(
    "byte_count" => types[:int], "empty?" => types[:bool], "as_String" => types[:string]
  )
end

.setup_type_constants(types) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/stone/types.rb', line 88

def setup_type_constants(types)
  # Define type constants on Stone::Type for convenient access
  # Only define if not already defined (avoids warnings during test resets)
  types.each_value do |type|
    define_type_constant(type.name.to_sym, type)
  end
  define_type_constant(:Registry, Stone::TypeRegistry.instance)
end

.setup_type_properties(types) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/stone/types.rb', line 51

def setup_type_properties(types)
  types[:type].property_types.merge!(
    "as_String" => types[:string],
    "record?" => types[:bool],
    "primitive?" => types[:bool],
    "kind" => types[:int],
    "size" => types[:int],
    "fields" => types[:field_list]
  )
end