konpeito 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test_native_class.rb DELETED
@@ -1,151 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Simple test for NativeClass (Point with x, y fields)
4
-
5
- $LOAD_PATH.unshift File.expand_path("lib", __dir__)
6
- require "konpeito"
7
-
8
- source = <<~RUBY
9
- def test_create
10
- p = Point.new
11
- p.x = 3.0
12
- p.y = 4.0
13
- p.x
14
- end
15
-
16
- def test_distance(x1, y1, x2, y2)
17
- p1 = Point.new
18
- p1.x = x1
19
- p1.y = y1
20
-
21
- p2 = Point.new
22
- p2.x = x2
23
- p2.y = y2
24
-
25
- dx = p2.x - p1.x
26
- dy = p2.y - p1.y
27
- dx * dx + dy * dy
28
- end
29
-
30
- def test_sum_coords
31
- p = Point.new
32
- p.x = 10.0
33
- p.y = 20.0
34
- p.x + p.y
35
- end
36
- RUBY
37
-
38
- rbs = <<~RBS
39
- # @native
40
- class Point
41
- @x: Float
42
- @y: Float
43
-
44
- def self.new: () -> Point
45
- def x: () -> Float
46
- def x=: (Float value) -> Float
47
- def y: () -> Float
48
- def y=: (Float value) -> Float
49
- end
50
-
51
- class Object
52
- def test_create: () -> Float
53
- def test_distance: (Float x1, Float y1, Float x2, Float y2) -> Float
54
- def test_sum_coords: () -> Float
55
- end
56
- RBS
57
-
58
- require "tempfile"
59
- require "fileutils"
60
-
61
- tmp_dir = "tmp"
62
- FileUtils.mkdir_p(tmp_dir)
63
-
64
- source_path = File.join(tmp_dir, "test_native_class.rb")
65
- rbs_path = File.join(tmp_dir, "test_native_class.rbs")
66
- output_path = File.join(tmp_dir, "test_native_class.bundle")
67
-
68
- File.write(source_path, source)
69
- File.write(rbs_path, rbs)
70
-
71
- compiler = Konpeito::Compiler.new(
72
- source_file: source_path,
73
- output_file: output_path,
74
- format: :cruby_ext,
75
- rbs_paths: [rbs_path],
76
- optimize: false,
77
- verbose: true
78
- )
79
-
80
- # Patch to capture LLVM IR
81
- module Konpeito
82
- module Codegen
83
- class LLVMGenerator
84
- def to_ir
85
- @mod.to_s
86
- end
87
- end
88
- end
89
- end
90
-
91
- puts "Compiling..."
92
-
93
- # Manual compilation steps to capture IR
94
- ast = compiler.send(:parse)
95
- typed_ast = compiler.send(:type_check_ast, ast)
96
- hir = compiler.send(:generate_hir, typed_ast)
97
-
98
- puts "\n=== HIR Functions ==="
99
- hir.functions.each do |func|
100
- puts "Function: #{func.name}"
101
- func.body.each do |block|
102
- puts " Block: #{block.label}"
103
- block.instructions.each do |inst|
104
- puts " #{inst.class.name.split('::').last}: #{inst.result_var}"
105
- end
106
- puts " Terminator: #{block.terminator.class.name.split('::').last}" if block.terminator
107
- end
108
- end
109
-
110
- llvm_gen = Konpeito::Codegen::LLVMGenerator.new(
111
- module_name: "test_native_class",
112
- monomorphizer: nil
113
- )
114
- llvm_gen.generate(hir)
115
-
116
- puts "\n=== LLVM IR ==="
117
- puts llvm_gen.to_ir
118
-
119
- puts "\n=== Compiling to native ==="
120
- backend = Konpeito::Codegen::CRubyBackend.new(
121
- llvm_gen,
122
- output_file: output_path,
123
- module_name: "test_native_class"
124
- )
125
- backend.generate
126
- puts "Generated: #{output_path}"
127
-
128
- puts "\n=== Testing ==="
129
- require File.expand_path(output_path)
130
-
131
- puts "\nTest 1: test_create"
132
- result = Object.new.send(:test_create)
133
- expected = 3.0
134
- puts "test_create() = #{result}"
135
- puts "Expected: #{expected}"
136
- puts (result - expected).abs < 0.001 ? "PASS" : "FAIL"
137
-
138
- puts "\nTest 2: test_distance"
139
- # Distance squared from (0,0) to (3,4) = 9 + 16 = 25
140
- result = Object.new.send(:test_distance, 0.0, 0.0, 3.0, 4.0)
141
- expected = 25.0
142
- puts "test_distance(0, 0, 3, 4) = #{result}"
143
- puts "Expected: #{expected}"
144
- puts (result - expected).abs < 0.001 ? "PASS" : "FAIL"
145
-
146
- puts "\nTest 3: test_sum_coords"
147
- result = Object.new.send(:test_sum_coords)
148
- expected = 30.0
149
- puts "test_sum_coords() = #{result}"
150
- puts "Expected: #{expected}"
151
- puts (result - expected).abs < 0.001 ? "PASS" : "FAIL"