konpeito 0.1.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.
Files changed (180) hide show
  1. checksums.yaml +7 -0
  2. data/.ruby-version +1 -0
  3. data/CHANGELOG.md +75 -0
  4. data/CONTRIBUTING.md +123 -0
  5. data/LICENSE +21 -0
  6. data/README.md +257 -0
  7. data/Rakefile +11 -0
  8. data/bin/konpeito +6 -0
  9. data/konpeito.gemspec +43 -0
  10. data/lib/konpeito/ast/typed_ast.rb +620 -0
  11. data/lib/konpeito/ast/visitor.rb +78 -0
  12. data/lib/konpeito/cache/cache_manager.rb +230 -0
  13. data/lib/konpeito/cache/dependency_graph.rb +192 -0
  14. data/lib/konpeito/cache.rb +8 -0
  15. data/lib/konpeito/cli/base_command.rb +187 -0
  16. data/lib/konpeito/cli/build_command.rb +220 -0
  17. data/lib/konpeito/cli/check_command.rb +104 -0
  18. data/lib/konpeito/cli/config.rb +231 -0
  19. data/lib/konpeito/cli/deps_command.rb +128 -0
  20. data/lib/konpeito/cli/doctor_command.rb +340 -0
  21. data/lib/konpeito/cli/fmt_command.rb +199 -0
  22. data/lib/konpeito/cli/init_command.rb +312 -0
  23. data/lib/konpeito/cli/lsp_command.rb +40 -0
  24. data/lib/konpeito/cli/run_command.rb +150 -0
  25. data/lib/konpeito/cli/test_command.rb +248 -0
  26. data/lib/konpeito/cli/watch_command.rb +212 -0
  27. data/lib/konpeito/cli.rb +301 -0
  28. data/lib/konpeito/codegen/builtin_methods.rb +229 -0
  29. data/lib/konpeito/codegen/cruby_backend.rb +1090 -0
  30. data/lib/konpeito/codegen/debug_info.rb +352 -0
  31. data/lib/konpeito/codegen/inliner.rb +486 -0
  32. data/lib/konpeito/codegen/jvm_backend.rb +197 -0
  33. data/lib/konpeito/codegen/jvm_generator.rb +13412 -0
  34. data/lib/konpeito/codegen/llvm_generator.rb +13191 -0
  35. data/lib/konpeito/codegen/loop_optimizer.rb +363 -0
  36. data/lib/konpeito/codegen/monomorphizer.rb +359 -0
  37. data/lib/konpeito/codegen/profile_runtime.c +341 -0
  38. data/lib/konpeito/codegen/profiler.rb +99 -0
  39. data/lib/konpeito/compiler.rb +592 -0
  40. data/lib/konpeito/dependency_resolver.rb +296 -0
  41. data/lib/konpeito/diagnostics/collector.rb +127 -0
  42. data/lib/konpeito/diagnostics/diagnostic.rb +237 -0
  43. data/lib/konpeito/diagnostics/renderer.rb +144 -0
  44. data/lib/konpeito/formatter/formatter.rb +1214 -0
  45. data/lib/konpeito/hir/builder.rb +7167 -0
  46. data/lib/konpeito/hir/nodes.rb +2465 -0
  47. data/lib/konpeito/lsp/document_manager.rb +820 -0
  48. data/lib/konpeito/lsp/server.rb +183 -0
  49. data/lib/konpeito/lsp/transport.rb +38 -0
  50. data/lib/konpeito/parser/prism_adapter.rb +65 -0
  51. data/lib/konpeito/platform.rb +103 -0
  52. data/lib/konpeito/profile/report.rb +136 -0
  53. data/lib/konpeito/rbs_inline/preprocessor.rb +199 -0
  54. data/lib/konpeito/stdlib/compression/compression.rb +72 -0
  55. data/lib/konpeito/stdlib/compression/compression.rbs +60 -0
  56. data/lib/konpeito/stdlib/compression/compression_native.c +415 -0
  57. data/lib/konpeito/stdlib/compression/extconf.rb +19 -0
  58. data/lib/konpeito/stdlib/crypto/crypto.rb +85 -0
  59. data/lib/konpeito/stdlib/crypto/crypto.rbs +74 -0
  60. data/lib/konpeito/stdlib/crypto/crypto_native.c +312 -0
  61. data/lib/konpeito/stdlib/crypto/extconf.rb +40 -0
  62. data/lib/konpeito/stdlib/http/extconf.rb +19 -0
  63. data/lib/konpeito/stdlib/http/http.rb +125 -0
  64. data/lib/konpeito/stdlib/http/http.rbs +57 -0
  65. data/lib/konpeito/stdlib/http/http_native.c +440 -0
  66. data/lib/konpeito/stdlib/json/extconf.rb +17 -0
  67. data/lib/konpeito/stdlib/json/json.rb +44 -0
  68. data/lib/konpeito/stdlib/json/json.rbs +33 -0
  69. data/lib/konpeito/stdlib/json/json_native.c +286 -0
  70. data/lib/konpeito/stdlib/ui/extconf.rb +216 -0
  71. data/lib/konpeito/stdlib/ui/konpeito_ui_native.cpp +1625 -0
  72. data/lib/konpeito/stdlib/ui/konpeito_ui_native.h +162 -0
  73. data/lib/konpeito/stdlib/ui/ui.rb +318 -0
  74. data/lib/konpeito/stdlib/ui/ui.rbs +247 -0
  75. data/lib/konpeito/type_checker/annotation_parser.rb +67 -0
  76. data/lib/konpeito/type_checker/hm_inferrer.rb +2565 -0
  77. data/lib/konpeito/type_checker/inferrer.rb +565 -0
  78. data/lib/konpeito/type_checker/rbs_loader.rb +1621 -0
  79. data/lib/konpeito/type_checker/type_resolver.rb +276 -0
  80. data/lib/konpeito/type_checker/types.rb +1434 -0
  81. data/lib/konpeito/type_checker/unification.rb +323 -0
  82. data/lib/konpeito/ui/animation/animated_state.rb +80 -0
  83. data/lib/konpeito/ui/animation/easing.rb +59 -0
  84. data/lib/konpeito/ui/animation/value_tween.rb +66 -0
  85. data/lib/konpeito/ui/app.rb +379 -0
  86. data/lib/konpeito/ui/box.rb +38 -0
  87. data/lib/konpeito/ui/castella.rb +70 -0
  88. data/lib/konpeito/ui/castella_native.rb +76 -0
  89. data/lib/konpeito/ui/chart/area_chart.rb +305 -0
  90. data/lib/konpeito/ui/chart/bar_chart.rb +288 -0
  91. data/lib/konpeito/ui/chart/base_chart.rb +210 -0
  92. data/lib/konpeito/ui/chart/chart_helpers.rb +79 -0
  93. data/lib/konpeito/ui/chart/gauge_chart.rb +171 -0
  94. data/lib/konpeito/ui/chart/heatmap_chart.rb +222 -0
  95. data/lib/konpeito/ui/chart/line_chart.rb +289 -0
  96. data/lib/konpeito/ui/chart/pie_chart.rb +219 -0
  97. data/lib/konpeito/ui/chart/scales.rb +77 -0
  98. data/lib/konpeito/ui/chart/scatter_chart.rb +303 -0
  99. data/lib/konpeito/ui/chart/stacked_bar_chart.rb +276 -0
  100. data/lib/konpeito/ui/column.rb +271 -0
  101. data/lib/konpeito/ui/core.rb +2199 -0
  102. data/lib/konpeito/ui/dsl.rb +443 -0
  103. data/lib/konpeito/ui/frame.rb +171 -0
  104. data/lib/konpeito/ui/frame_native.rb +494 -0
  105. data/lib/konpeito/ui/markdown/ast.rb +124 -0
  106. data/lib/konpeito/ui/markdown/mermaid/layout.rb +387 -0
  107. data/lib/konpeito/ui/markdown/mermaid/models.rb +232 -0
  108. data/lib/konpeito/ui/markdown/mermaid/parser.rb +519 -0
  109. data/lib/konpeito/ui/markdown/mermaid/renderer.rb +336 -0
  110. data/lib/konpeito/ui/markdown/parser.rb +805 -0
  111. data/lib/konpeito/ui/markdown/renderer.rb +639 -0
  112. data/lib/konpeito/ui/markdown/theme.rb +165 -0
  113. data/lib/konpeito/ui/render_node.rb +260 -0
  114. data/lib/konpeito/ui/row.rb +207 -0
  115. data/lib/konpeito/ui/spacer.rb +18 -0
  116. data/lib/konpeito/ui/style.rb +799 -0
  117. data/lib/konpeito/ui/theme.rb +563 -0
  118. data/lib/konpeito/ui/themes/material.rb +35 -0
  119. data/lib/konpeito/ui/themes/tokyo_night.rb +6 -0
  120. data/lib/konpeito/ui/widgets/button.rb +103 -0
  121. data/lib/konpeito/ui/widgets/calendar.rb +1034 -0
  122. data/lib/konpeito/ui/widgets/checkbox.rb +119 -0
  123. data/lib/konpeito/ui/widgets/container.rb +91 -0
  124. data/lib/konpeito/ui/widgets/data_table.rb +667 -0
  125. data/lib/konpeito/ui/widgets/divider.rb +29 -0
  126. data/lib/konpeito/ui/widgets/image.rb +105 -0
  127. data/lib/konpeito/ui/widgets/input.rb +485 -0
  128. data/lib/konpeito/ui/widgets/markdown.rb +57 -0
  129. data/lib/konpeito/ui/widgets/modal.rb +163 -0
  130. data/lib/konpeito/ui/widgets/multiline_input.rb +968 -0
  131. data/lib/konpeito/ui/widgets/multiline_text.rb +180 -0
  132. data/lib/konpeito/ui/widgets/net_image.rb +100 -0
  133. data/lib/konpeito/ui/widgets/progress_bar.rb +70 -0
  134. data/lib/konpeito/ui/widgets/radio_buttons.rb +93 -0
  135. data/lib/konpeito/ui/widgets/slider.rb +133 -0
  136. data/lib/konpeito/ui/widgets/switch.rb +84 -0
  137. data/lib/konpeito/ui/widgets/tabs.rb +157 -0
  138. data/lib/konpeito/ui/widgets/text.rb +110 -0
  139. data/lib/konpeito/ui/widgets/tree.rb +426 -0
  140. data/lib/konpeito/version.rb +5 -0
  141. data/lib/konpeito.rb +109 -0
  142. data/test_native_array.rb +172 -0
  143. data/test_native_array_class.rb +197 -0
  144. data/test_native_class.rb +151 -0
  145. data/tools/konpeito-asm/build.sh +65 -0
  146. data/tools/konpeito-asm/lib/asm-9.7.1.jar +0 -0
  147. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KArray.class +0 -0
  148. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KCompression.class +0 -0
  149. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KConditionVariable.class +0 -0
  150. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KCrypto.class +0 -0
  151. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KFile.class +0 -0
  152. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KHTTP.class +0 -0
  153. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KHash.class +0 -0
  154. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KJSON$Parser.class +0 -0
  155. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KJSON.class +0 -0
  156. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KMath.class +0 -0
  157. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KRactor.class +0 -0
  158. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KRactorPort.class +0 -0
  159. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KSizedQueue.class +0 -0
  160. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KThread.class +0 -0
  161. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/KTime.class +0 -0
  162. data/tools/konpeito-asm/runtime-classes/konpeito/runtime/RubyDispatch.class +0 -0
  163. data/tools/konpeito-asm/src/ClassIntrospector.java +312 -0
  164. data/tools/konpeito-asm/src/KonpeitoAssembler.java +659 -0
  165. data/tools/konpeito-asm/src/konpeito/runtime/KArray.java +390 -0
  166. data/tools/konpeito-asm/src/konpeito/runtime/KCompression.java +168 -0
  167. data/tools/konpeito-asm/src/konpeito/runtime/KConditionVariable.java +48 -0
  168. data/tools/konpeito-asm/src/konpeito/runtime/KCrypto.java +151 -0
  169. data/tools/konpeito-asm/src/konpeito/runtime/KFile.java +100 -0
  170. data/tools/konpeito-asm/src/konpeito/runtime/KHTTP.java +113 -0
  171. data/tools/konpeito-asm/src/konpeito/runtime/KHash.java +228 -0
  172. data/tools/konpeito-asm/src/konpeito/runtime/KJSON.java +405 -0
  173. data/tools/konpeito-asm/src/konpeito/runtime/KMath.java +54 -0
  174. data/tools/konpeito-asm/src/konpeito/runtime/KRactor.java +244 -0
  175. data/tools/konpeito-asm/src/konpeito/runtime/KRactorPort.java +53 -0
  176. data/tools/konpeito-asm/src/konpeito/runtime/KSizedQueue.java +49 -0
  177. data/tools/konpeito-asm/src/konpeito/runtime/KThread.java +49 -0
  178. data/tools/konpeito-asm/src/konpeito/runtime/KTime.java +53 -0
  179. data/tools/konpeito-asm/src/konpeito/runtime/RubyDispatch.java +416 -0
  180. metadata +267 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e06886ab2b9bd1bcc2638dcc1c0788c7a0bbe40c0416a630eb89ca8e86945a7a
4
+ data.tar.gz: 3921d4b460e10864329173fcdf6ba0c925c89ad9d7959fab5ac74c640041e0e4
5
+ SHA512:
6
+ metadata.gz: 970fff312f787c003978377350ff5cbeeccd46aab454999b1214d1c17a2e04530c2ec9149a4015b6c477cd21984b43ebf03390120cd1ce9b22670fe767845338
7
+ data.tar.gz: 32b1f3c137e224a89f57df986f667a3bc4fbe9ea12997b340927ed70e7b00458b10106be3b1f827f86ff4e147e79eafc94601b4026ce225284e4fe2d06a4597c
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 4.0.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,75 @@
1
+ # Changelog
2
+
3
+ All notable changes to Konpeito will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2026-02-13
9
+
10
+ ### Added
11
+
12
+ #### Compiler Core
13
+ - Prism parser integration for Ruby 4.0 syntax
14
+ - Hindley-Milner type inference (Algorithm W) with RBS integration
15
+ - High-level IR (HIR) with SSA form
16
+ - Typed AST generation
17
+
18
+ #### LLVM Backend (CRuby Extension)
19
+ - Native code generation via LLVM 20
20
+ - CRuby extension output (.so/.bundle)
21
+ - Unboxed arithmetic for Integer/Float (5-5000x faster than YJIT)
22
+ - Monomorphization and function inlining
23
+ - Loop-invariant code motion (LICM)
24
+ - LLVM O2 optimization pass integration
25
+
26
+ #### JVM Backend
27
+ - Standalone .jar generation via ASM bytecode
28
+ - Java 21 Virtual Threads support
29
+ - Java interop (RBS-free, classpath introspection)
30
+ - Block-to-SAM conversion
31
+ - Pattern matching (Array/Hash/Capture/Pin/Rest)
32
+ - Module/Mixin support (include/extend/prepend)
33
+
34
+ #### Native Data Structures
35
+ - `NativeArray[T]` - contiguous unboxed arrays (up to 31x faster)
36
+ - `NativeClass` - fixed-layout C structs with GC integration
37
+ - `NativeHash[K,V]` - generic hash map with linear probing
38
+ - `StaticArray[T,N]` - stack-allocated fixed-size arrays
39
+ - `Slice[T]` - bounds-checked pointer views
40
+ - `ByteBuffer` / `StringBuffer` / `ByteSlice`
41
+ - `NativeString` - UTF-8 byte/char operations
42
+ - `@struct` - value type structs (register passing)
43
+
44
+ #### Standard Library
45
+ - `KonpeitoJSON` - JSON parse/generate (yyjson)
46
+ - `KonpeitoHTTP` - HTTP client (libcurl)
47
+ - `KonpeitoCrypto` - cryptographic primitives (OpenSSL)
48
+ - `KonpeitoCompression` - gzip/deflate/zlib compression
49
+
50
+ #### Ruby Language Features
51
+ - Classes, modules, inheritance, mixins
52
+ - Blocks, yield, Proc/Lambda
53
+ - Pattern matching (case/in with literals, arrays, hashes, guards, captures, pins)
54
+ - Exception handling (begin/rescue/else/ensure)
55
+ - Fiber, Thread, Mutex, ConditionVariable, SizedQueue
56
+ - String interpolation, regular expressions, ranges
57
+ - Keyword arguments, rest arguments, splat expansion
58
+ - Compound assignment (`+=`, `||=`, `&&=`)
59
+ - Safe navigation (`&.`), `defined?`, `alias`
60
+ - Open classes, `class << self`, endless methods
61
+ - Numbered block parameters (`_1`, `_2`), `it` parameter
62
+
63
+ #### Developer Tools
64
+ - LSP server (diagnostics, hover, completion, go-to-definition, references, rename)
65
+ - DWARF debug info (`-g` flag, lldb/gdb support)
66
+ - Profiling instrumentation (`--profile`)
67
+ - Incremental compilation (`--incremental`)
68
+ - Rust/Elm-style diagnostic error messages
69
+
70
+ #### C Interop
71
+ - `%a{cfunc}` / `%a{ffi}` - direct C function calls
72
+ - `%a{extern}` - external C struct wrappers
73
+ - `%a{simd}` - SIMD vectorization
74
+
75
+ [0.1.0]: https://github.com/i2y/konpeito/releases/tag/v0.1.0
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,123 @@
1
+ # Contributing to Konpeito
2
+
3
+ Thank you for your interest in contributing to Konpeito! This document provides guidelines and instructions for contributing.
4
+
5
+ ## Development Setup
6
+
7
+ ### Prerequisites
8
+
9
+ - Ruby 4.0.1+
10
+ - LLVM 20
11
+ - Java 21+ (for JVM backend only)
12
+ - Bundler
13
+
14
+ ### Installing LLVM 20
15
+
16
+ **macOS:**
17
+ ```bash
18
+ brew install llvm@20
19
+ ln -sf /opt/homebrew/opt/llvm@20/lib/libLLVM-20.dylib /opt/homebrew/lib/
20
+ ```
21
+
22
+ **Ubuntu / Debian:**
23
+ ```bash
24
+ sudo apt install llvm-20 clang-20
25
+ ```
26
+
27
+ **Fedora:**
28
+ ```bash
29
+ sudo dnf install llvm20 clang20
30
+ ```
31
+
32
+ **Windows (MSYS2 / MinGW):**
33
+ ```bash
34
+ winget install LLVM.LLVM
35
+ ```
36
+
37
+ ### Setup
38
+
39
+ ```bash
40
+ # Clone the repository
41
+ git clone https://github.com/i2y/konpeito.git
42
+ cd konpeito
43
+
44
+ # Install Ruby 4.0.1
45
+ rbenv install 4.0.1
46
+ rbenv local 4.0.1
47
+
48
+ # Install dependencies
49
+ bundle install
50
+
51
+ # Run tests
52
+ bundle exec rake test
53
+ ```
54
+
55
+ ### Running Specific Tests
56
+
57
+ ```bash
58
+ # All tests
59
+ bundle exec rake test
60
+
61
+ # JVM backend only
62
+ bundle exec ruby -Ilib:test test/jvm/jvm_backend_test.rb
63
+
64
+ # Specific test file
65
+ bundle exec ruby -Ilib:test test/codegen/rescue_else_test.rb
66
+ ```
67
+
68
+ ## How to Contribute
69
+
70
+ ### Reporting Bugs
71
+
72
+ 1. Check existing [issues](https://github.com/i2y/konpeito/issues) to avoid duplicates
73
+ 2. Use the bug report template
74
+ 3. Include: Ruby version, LLVM version, OS, minimal reproduction code
75
+
76
+ ### Suggesting Features
77
+
78
+ 1. Open an issue using the feature request template
79
+ 2. Describe the use case and expected behavior
80
+ 3. If proposing a syntax extension, show example Ruby code and expected compiled output
81
+
82
+ ### Submitting Pull Requests
83
+
84
+ 1. Fork the repository
85
+ 2. Create a feature branch (`git checkout -b feature/my-feature`)
86
+ 3. Make your changes
87
+ 4. Add tests for new functionality
88
+ 5. Ensure all tests pass (`bundle exec rake test`)
89
+ 6. Submit a pull request using the PR template
90
+
91
+ ### Code Style
92
+
93
+ - Follow existing code patterns in the codebase
94
+ - No trailing whitespace
95
+ - Use 2-space indentation (Ruby standard)
96
+ - Add tests for all new features and bug fixes
97
+
98
+ ## Architecture Overview
99
+
100
+ See [docs/architecture.md](docs/architecture.md) for a detailed architecture guide.
101
+
102
+ Key directories:
103
+
104
+ | Directory | Purpose |
105
+ |-----------|---------|
106
+ | `lib/konpeito/parser/` | Prism parser adapter |
107
+ | `lib/konpeito/type_checker/` | HM type inference + RBS |
108
+ | `lib/konpeito/hir/` | High-level IR |
109
+ | `lib/konpeito/codegen/` | LLVM and JVM code generation |
110
+ | `lib/konpeito/stdlib/` | Native standard library (C extensions) |
111
+ | `tools/konpeito-asm/` | JVM ASM tool (JSON IR to .class) |
112
+ | `test/` | Test suite |
113
+
114
+ ## Compiler Design Principles
115
+
116
+ 1. **No ambiguous behavior**: If a type cannot be determined, fall back to dynamic dispatch with a warning — never guess heuristically. Adding RBS promotes the fallback to static dispatch
117
+ 2. **HM inference is primary, RBS is auxiliary**: Code should compile correctly without RBS files
118
+ 3. **No heuristic guessing**: Never search all classes to guess which one has a matching method name
119
+ 4. **Follow Kotlin's design for JVM**: Kotlin is the reference for JVM type inference and code generation
120
+
121
+ ## License
122
+
123
+ By contributing to Konpeito, you agree that your contributions will be licensed under the [BSD Zero Clause License (0BSD)](LICENSE).
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yasushi Itoh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,257 @@
1
+ # Konpeito
2
+
3
+ > *Konpeito (konpeitō) — Japanese sugar crystals. Crystallizing Ruby into native code.*
4
+
5
+ A gradually typed Ruby compiler with Hindley-Milner type inference, dual LLVM/JVM backends, and seamless Java interop.
6
+
7
+ Write ordinary Ruby. Konpeito infers types automatically, compiles to fast native code, and falls back to dynamic dispatch where it can't resolve statically — with a warning so you always know.
8
+
9
+ ## How It Works
10
+
11
+ Konpeito uses a three-tier type resolution strategy:
12
+
13
+ 1. **HM inference** resolves most types automatically — no annotations needed. Like Crystal, but for Ruby.
14
+ 2. **RBS annotations** add precision where needed. Optional type hints that help the compiler optimize further.
15
+ 3. **Dynamic fallback** handles the rest. Unresolved calls compile to runtime dispatch (LLVM: `rb_funcallv`, JVM: `invokedynamic`), and the compiler warns you.
16
+
17
+ Adding an RBS signature promotes a dynamic fallback to static dispatch. The compiler tells you where the boundaries are — fix them if you want, or leave them dynamic. Your call.
18
+
19
+ ## Quick Start
20
+
21
+ ### Prerequisites
22
+
23
+ Ruby 4.0+ is required. Java 21+ is needed for the JVM backend. LLVM 20 is needed only for the CRuby native backend.
24
+
25
+ ```bash
26
+ gem install konpeito
27
+ ```
28
+
29
+ **JVM backend** (recommended — standalone JARs, Castella UI, Java interop):
30
+ ```bash
31
+ # macOS
32
+ brew install openjdk@21
33
+
34
+ # Ubuntu / Debian
35
+ sudo apt install openjdk-21-jdk
36
+
37
+ # Fedora
38
+ sudo dnf install java-21-openjdk-devel
39
+
40
+ # Windows (MSYS2 / MinGW)
41
+ winget install EclipseAdoptium.Temurin.21.JDK
42
+ ```
43
+
44
+ **CRuby native backend** (optional — C extensions):
45
+ ```bash
46
+ gem install ruby-llvm
47
+
48
+ # macOS
49
+ brew install llvm@20
50
+ ln -sf /opt/homebrew/opt/llvm@20/lib/libLLVM-20.dylib /opt/homebrew/lib/
51
+
52
+ # Ubuntu / Debian
53
+ sudo apt install llvm-20 clang-20
54
+
55
+ # Fedora
56
+ sudo dnf install llvm20 clang20
57
+
58
+ # Windows (MSYS2 / MinGW)
59
+ winget install LLVM.LLVM
60
+ ```
61
+
62
+ ### Hello World
63
+
64
+ Write a small Ruby file:
65
+
66
+ ```ruby
67
+ # math.rb
68
+ def add(a, b)
69
+ a + b
70
+ end
71
+
72
+ def sum_up_to(n)
73
+ total = 0
74
+ i = 1
75
+ while i <= n
76
+ total = total + i
77
+ i = i + 1
78
+ end
79
+ total
80
+ end
81
+ ```
82
+
83
+ Compile and use it from Ruby:
84
+
85
+ ```bash
86
+ konpeito build math.rb # produces math.bundle (macOS), math.so (Linux), or math.dll (Windows)
87
+ ```
88
+
89
+ ```ruby
90
+ require_relative "math"
91
+ puts add(3, 4) # => 7
92
+ puts sum_up_to(100) # => 5050
93
+ ```
94
+
95
+ A few more commands to get you going:
96
+
97
+ ```bash
98
+ konpeito doctor # check your environment
99
+ konpeito run --target jvm main.rb # compile and run in one step
100
+ konpeito fmt # format source files
101
+ konpeito fmt --check # check formatting without modifying
102
+ ```
103
+
104
+ ## Features
105
+
106
+ - **HM Type Inference** — Types are inferred automatically. No annotations needed for most code.
107
+ - **Gradual Typing** — Static where possible, dynamic where necessary. The compiler shows you the boundary.
108
+ - **Flow Typing** — Type narrowing via `if x.nil?`, `case/in Integer`, boolean guards, and more.
109
+ - **Unboxed Arithmetic** — Integer and Float operations compile to native CPU instructions, skipping Ruby's method dispatch entirely.
110
+ - **Loop Optimizations** — LICM, inlined iterators (`each`, `map`, `reduce`, `times`), and LLVM O2 passes.
111
+ - **CRuby C Extensions** — Output plugs directly into your existing Ruby app via `require`.
112
+ - **JVM Backend** — Generate standalone `.jar` files that run on any Java 21+ VM.
113
+ - **Java Interop** — Call Java libraries directly with full type safety. Java type information flows into HM inference automatically.
114
+ - **Native Data Structures** — `NativeArray[T]`, `NativeHash[K,V]`, `StaticArray[T,N]`, `Slice[T]`, `@struct` value types for high-performance data handling.
115
+ - **C Interop** — Call external C libraries with `%a{cfunc}` / `%a{ffi}`, plus built-in HTTP (libcurl), Crypto (OpenSSL), and Compression (zlib) modules.
116
+ - **SIMD Vectorization** — `%a{simd}` compiles vector types to LLVM vector instructions.
117
+ - **Operator Overloading** — Define `+`, `-`, `*`, `==`, `<=>`, etc. on your own classes with full type inference.
118
+ - **Pattern Matching** — Full `case/in` support with array, hash, guard, and capture patterns.
119
+ - **Modern Ruby Syntax** — `_1`/`_2` numbered params, `it`, endless methods, `class << self`, safe navigation (`&.`), and more.
120
+ - **Concurrency** — Fiber, Thread, Mutex, ConditionVariable, SizedQueue, and Ractor (with `Ractor::Port`, `Ractor.select`, `Ractor[:key]` local storage, `name:`, `monitor`/`unmonitor`). JVM Ractor uses Virtual Threads for scheduling but does not enforce object isolation — objects are shared by reference, unlike CRuby's strict isolation model.
121
+ - **Built-in Tooling** — Formatter (`fmt`), LSP (hover, completion, go-to-def, references, rename), debug info (`-g`), and profiling (`--profile`).
122
+ - **Castella UI** — A reactive GUI framework for the JVM backend (see below).
123
+
124
+ ## JVM Backend
125
+
126
+ Konpeito can also compile to JVM bytecode, producing standalone JAR files:
127
+
128
+ ```bash
129
+ konpeito build --target jvm -o app.jar main.rb
130
+ # or compile and run immediately:
131
+ konpeito build --target jvm --run main.rb
132
+ ```
133
+
134
+ The JVM backend supports seamless Java interop — call Java libraries directly from your Ruby code without writing any glue. Java type information is introspected from the classpath and fed into HM inference, so calling Java APIs is type-safe without annotations.
135
+
136
+ ## Castella UI
137
+
138
+ Castella is a reactive GUI framework that runs on the JVM backend, powered by [JWM](https://github.com/nicenote/jwm) + [Skija](https://github.com/nicenote/skija) for cross-platform rendering. Based on a port of [Castella for Python](https://github.com/i2y/castella).
139
+
140
+ ```ruby
141
+ class CounterApp < Component
142
+ def initialize
143
+ super
144
+ @count = state(0)
145
+ end
146
+
147
+ def view
148
+ label = "Count: " + @count.value.to_s
149
+ Column(
150
+ Text(label).font_size(32),
151
+ Row(
152
+ Button(" - ").on_click { @count -= 1 },
153
+ Button(" + ").on_click { @count += 1 }
154
+ )
155
+ )
156
+ end
157
+ end
158
+ ```
159
+
160
+ A block-based DSL is also available:
161
+
162
+ ```ruby
163
+ def view
164
+ column(padding: 16.0) do
165
+ text "Counter: #{@count}", font_size: 24.0, color: 0xFFC0CAF5
166
+ row(spacing: 8.0) do
167
+ button("-", width: 60.0) { @count -= 1 }
168
+ button("+", width: 60.0) { @count += 1 }
169
+ end
170
+ end
171
+ end
172
+ ```
173
+
174
+ Available widgets: `Text`, `Button`, `TextInput`, `MultilineText`, `Column`, `Row`, `Image`, `Checkbox`, `Slider`, `ProgressBar`, `Tabs`, `DataTable`, `TreeView`, `BarChart`, `Markdown`, and more.
175
+
176
+ ## Performance
177
+
178
+ Konpeito shines in compute-heavy, typed loops where unboxed arithmetic and backend optimizations kick in. All benchmarks compare against Ruby 4.0.1 with YJIT enabled on Apple M4 Max.
179
+
180
+ ### LLVM Backend (CRuby Extension)
181
+
182
+ | Benchmark | vs Ruby (YJIT) |
183
+ |---|---|
184
+ | N-Body simulation (5M steps) | **81x** faster |
185
+ | Numeric method inlining (abs, even?, odd?) | **25-29x** faster |
186
+ | Range enumerable (each, reduce, select) | **40-53x** faster |
187
+ | Integer#times (nested, typed) | **891-972x** faster |
188
+ | Typed reduce over Array[Integer] | **7x** faster |
189
+ | Loop sum (n=100) | **18x** faster |
190
+ | Typed counter loop (LICM + LLVM O2) | **5,345x** faster |
191
+ | StaticArray/NativeArray sum (4 elements) | **65x** faster |
192
+ | StaticArray/NativeArray sum (16 elements) | **232x** faster |
193
+
194
+ These numbers compare native-internal performance (the loop itself runs inside compiled code). Single cross-boundary calls see smaller gains due to CRuby interop overhead.
195
+
196
+ ### JVM Backend (Standalone JAR)
197
+
198
+ Benchmarks run on Java 21 (HotSpot) with JIT warmup. "Realistic" mode uses variable arguments to prevent constant folding.
199
+
200
+ | Benchmark (10M iterations) | Ruby (YJIT) | Konpeito JVM | Speedup |
201
+ |---|---|---|---|
202
+ | Multiply Add (realistic) | 196 ms | 5.0 ms | **39x** faster |
203
+ | Compute Chain (realistic) | 300 ms | 5.1 ms | **59x** faster |
204
+ | Arithmetic Intensive (realistic) | 272 ms | 5.0 ms | **55x** faster |
205
+ | Loop Sum (realistic) | 107 ms | 2.6 ms | **41x** faster |
206
+ | Fibonacci fib(30) x 10 (recursive) | 300 ms | 9.8 ms | **31x** faster |
207
+
208
+ The JVM backend benefits from HotSpot's JIT compilation on top of Konpeito's static type resolution, yielding **30-60x** speedups for numeric workloads.
209
+
210
+ > **Environment:** Apple M4 Max, Ruby 4.0.1 + YJIT, Java 21.0.10 (HotSpot), macOS 15.
211
+
212
+ ## Documentation
213
+
214
+ ### User Guides
215
+
216
+ - **[Getting Started](docs/getting-started.md)** — Installation, Hello World, first project, Castella UI tutorial
217
+ - **[CLI Reference](docs/cli-reference.md)** — All commands, options, and configuration
218
+ - **[API Reference](docs/api-reference.md)** — Castella UI widgets, native data structures, standard library
219
+
220
+ ### Architecture & Design
221
+
222
+ - **[Architecture](docs/architecture.md)** — Full compiler pipeline, design philosophy, and roadmap
223
+ - **[JVM Backend](docs/architecture-jvm.md)** — Dual-backend strategy, JVM codegen, Java interop
224
+ - **[Castella UI](docs/castella-ui.md)** — GUI framework design and widget reference
225
+ - **[Native Stdlib Proposal](docs/native-stdlib-proposal.md)** — NativeArray, StaticArray, Slice, and friends
226
+ - **[Language Specification](docs/language-specification.md)** — Supported syntax, type system rules, backend behavior
227
+ - **[RBS Requirements](docs/rbs-requirements-en.md)** — When you need RBS files and when you don't
228
+
229
+ ## Requirements
230
+
231
+ | Dependency | Version | Required for |
232
+ |---|---|---|
233
+ | Ruby | 4.0.1+ | Always |
234
+ | Java | 21+ | JVM backend |
235
+ | LLVM | 20 | CRuby native backend |
236
+ | ruby-llvm gem | ~> 20.1 | CRuby native backend |
237
+ | Platform | macOS (ARM64/x64), Linux (x64/ARM64), Windows (x64, MSYS2/MinGW) | — |
238
+
239
+ ## Built with AI
240
+
241
+ This project was developed collaboratively between a human director ([Yasushi Itoh](https://github.com/i2y)) and [Claude Code](https://claude.com/claude-code) by Anthropic. The human set the vision, made design decisions, and guided the direction; the AI wrote the implementation. It's an experiment in what's possible when human judgment meets AI capability.
242
+
243
+ ## Status
244
+
245
+ Konpeito is in an early stage. Bugs and undocumented limitations should be expected. Actively improving — bug reports and feedback are very welcome.
246
+
247
+ The JVM backend might be more mature than the LLVM/CRuby backend at this point. If you're getting started, try the JVM backend first (`--target jvm`).
248
+
249
+ ## Contributing
250
+
251
+ Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for setup instructions and guidelines.
252
+
253
+ The core principle: **no ambiguous behavior**. If the compiler can't determine a type, it falls back to dynamic dispatch with a warning — never guesses heuristically. Adding RBS promotes the fallback to static dispatch.
254
+
255
+ ## License
256
+
257
+ [MIT](LICENSE) — Copyright (c) 2026 Yasushi Itoh
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ end
10
+
11
+ task default: :test
data/bin/konpeito ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "konpeito"
5
+
6
+ Konpeito::CLI.new(ARGV).run
data/konpeito.gemspec ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/konpeito/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "konpeito"
7
+ spec.version = Konpeito::VERSION
8
+ spec.authors = ["Yasushi Itoh"]
9
+ spec.email = ["i2y@users.noreply.github.com"]
10
+
11
+ spec.summary = "A gradually typed Ruby compiler with dual LLVM/JVM backends"
12
+ spec.description = "Konpeito is a gradually typed ahead-of-time compiler for Ruby with " \
13
+ "Hindley-Milner type inference and dual LLVM/JVM backends. Compile Ruby " \
14
+ "to CRuby C extensions (.so) or standalone JARs with seamless Java interop. " \
15
+ "Includes Castella UI, a reactive GUI framework powered by Skia, " \
16
+ "based on a port of Castella for Python (github.com/i2y/castella)."
17
+ spec.homepage = "https://github.com/i2y/konpeito"
18
+ spec.license = "MIT"
19
+ spec.required_ruby_version = ">= 4.0.0"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
23
+ spec.metadata["bug_tracker_uri"] = "#{spec.homepage}/issues"
24
+
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) ||
28
+ f.start_with?(*%w[test/ spec/ features/ .git .github appveyor Gemfile
29
+ benchmark/ docs/ examples/ tmp/ CLAUDE.md])
30
+ end
31
+ end
32
+ spec.bindir = "bin"
33
+ spec.executables = ["konpeito"]
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_dependency "prism"
37
+ spec.add_dependency "rbs"
38
+ spec.add_dependency "language_server-protocol", "~> 3.17"
39
+
40
+ # ruby-llvm is optional — only needed for native/CRuby extension compilation (--target native).
41
+ # JVM backend (--target jvm), type checking, LSP, and formatting work without it.
42
+ # Install manually: gem install ruby-llvm
43
+ end