Someone written a B4X compiler in Rust

How well does it work?
Not sure. It is still in pre-alpha version.
I don't have experience in Rust ecosystem.
From my reading, it is now testing on basic sub parser and features like List and Maps.
 
The obvious question is why? But that question does not always need an elaborate answer :) Because I wanted to is a perfectly valid one.
 
Well, we can ask him HERE in the forum!!!


Hmmm Last seem in ....2018
 
But the repo files are updated just today...
And how to quote by @ the user two-word named ?
 
Last edited:
The obvious question is why?
Not obvious to me but I could not resist asking Claude (Opus 5.5 High):

what are the merits of rust with respect to b4x
Rust and B4X are built for different jobs, so the useful question is where Rust would earn a place next to B4X, not whether it should replace it.

Where Rust is stronger
  • Speed and resources. Rust compiles to native machine code and has no garbage collector. Its speed is close to C and C++, and memory use is low and predictable. B4A and B4J run on the JVM or Android runtime, and B4i compiles through Objective-C. Both are fast enough for most app work, but they can't match Rust on CPU-heavy tasks.
  • Memory safety checked at compile time. The borrow checker stops whole classes of bugs before the code runs: null dereferences, data races, use-after-free. Code that compiles tends to keep working, which helps a lot with long-lived backend services.
  • Safer concurrency. The compiler won't let you share mutable data between threads unsafely. Async Rust (Tokio) is a strong fit for servers that make many network calls, like fanning out to S3, DynamoDB or Bedrock.
  • AWS Lambda. This is probably Rust's biggest advantage for you. Rust Lambdas have very short cold starts (often tens of milliseconds) and need little memory. Lambda bills by memory × duration, so a high-volume function can cost noticeably less. The official AWS SDK for Rust is mature, and cargo-lambda makes building and deploying straightforward.
  • Package ecosystem. Cargo and crates.io give you well-maintained libraries for image processing, cryptography, parsing, compression and so on. B4X relies on wrapped Java or Objective-C libraries, and some of those wrappers have to be written or maintained by hand.
  • Portable core logic. A Rust library can compile to Android (through JNI), iOS (as a static library), desktop, server and WebAssembly. You can write a tricky algorithm once and call it from any of them, including from B4X.
  • Long-term outlook. Rust has big corporate backing (AWS, Microsoft, Google, the Linux kernel) and a large community. B4X is excellent but depends heavily on one developer and a smaller forum community.
Where B4X is stronger
  • Fast app development. B4X has visual designers, a VB-like syntax, and one language for native Android, iOS and desktop apps. Rust has no mature equivalent for native mobile UI. Tauri 2, Dioxus and Slint exist, but they're still young and feel nothing like the B4X designers.
  • Learning curve. It takes weeks to months to get productive in Rust, and the ownership model and lifetimes frustrate most newcomers. In B4X you're productive almost immediately.
  • Build and edit speed. Rust compiles slowly, while B4X's build-and-debug cycle is quick, which suits iterative work.
  • Mobile platform integration. Permissions, camera, notifications and app lifecycle are handled for you in B4X. In Rust you'd be writing bridge code by hand.
A practical split

Most developers in your position would keep B4X for the apps and UI. They'd bring in Rust where performance or cost really matters:
  1. Lambda functions that are called often or need to respond quickly, especially CPU-heavy ones like image resizing, watermarking, thumbnail generation or batch photo processing.
  2. A Rust core library for heavy computation, called from B4A or B4i through native bindings, but only if a real bottleneck shows up.
For ordinary Lambda functions that just glue AWS services together, the gain over Python or Node is small. It's often not worth the extra learning and slower builds unless the volume is high.

If it would help, I can sketch a small Rust Lambda (for example, S3 trigger → resize image → write back) so you can compare it with what you'd normally write.

why would Someone write a B4X compiler in Rust
I couldn't find any public project that does this, so this is about why someone might. The short answer: B4X itself works well, and most of the reasons are about the tooling around it.

1. Run on any platform, as one binary. The official B4X IDE and compiler are Windows/.NET programs, so Mac and Linux users rely on Wine, a VM, or the remote Mac builder for B4i. A Rust compiler builds to one self-contained executable for Windows, macOS (Intel and ARM) and Linux, with no runtime to install. This is probably the strongest reason.

2. Support in other editors. Rust is very good for writing a Language Server, using crates like tower-lsp and salsa, which rust-analyzer is built on. A B4X language server would give you autocomplete, go-to-definition, inline errors and refactoring in VS Code, Zed, Neovim or JetBrains. You'd no longer be tied to the B4X IDE.

3. Rust is well suited to writing compilers. Rust's enums and pattern matching fit syntax trees and code-generation passes naturally. Mature libraries cover each stage: logos for tokenising, chumsky and lalrpop for parsing, inkwell for LLVM, and Cranelift for generating native code. There's a precedent too: a Rust project called elderheim is building a compiler for legacy languages, starting with BASIC, using Cranelift for native code generation.

4. New output targets. Today B4X transpiles to Java, Objective-C, C++ (B4R) and, through BANano, JavaScript. A new compiler could add:
  • native executables with no JVM, such as fast-starting B4X command-line tools or B4X on AWS Lambda with Rust-level cold starts
  • WebAssembly output
  • a compiler that itself runs in the browser as WASM, for an online B4X playground
5. Independence from one maintainer. The B4X compiler is closed source and depends heavily on one person, Erel. An open, community-owned implementation protects existing B4X code if that ever changes. Languages like Python, Ruby and JavaScript have alternative implementations partly for this reason.

6. Learning. Writing a compiler for a language you know well is a classic way to learn both Rust and compilers. A VB-like language like B4X is a manageable size for that.

Why it would be hard
  • The libraries are the real work. B4X's value comes from its wrapped libraries (Views, Camera, Http and so on) and the runtime glue for each platform. A new parser and code generator would be maybe 20% of the job. Matching every platform library would be the other 80%.
  • The visual designer. The designer files would also need supporting, or rebuilding.
  • Keeping up with changes. A second implementation is always catching up with the official one.
  • Rust isn't the only option. .NET now runs on all three platforms too, so cross-platform alone doesn't require Rust. The real draws are single-binary distribution, the language-server tooling and WASM.
In practice the realistic version is a Rust-based B4X language server plus a transpiler for a subset of the language. Reproducing the whole B4X stack would be far bigger.

see https://github.com/FeliceMente/ferrobasic
So it exists, and it lines up closely with the reasons I gave. FerroBASIC describes itself as "a B4X-compatible, multi-target BASIC compiler, written in Rust."

How it matches the reasons above
  • Language server first. Today it ships a diagnostics-only language server for LSP-capable editors, and DAP support is planned. That's the editor-independence motive.
  • New targets. Besides JVM/Android it plans native desktop (Windows, macOS, Linux), iOS, and WebAssembly via LLVM, plus microcontrollers such as the Raspberry Pi Pico. That covers B4J, B4i and B4R ground, with native code and WASM added.
  • An independent implementation. It is not affiliated with or endorsed by Anywhere Software, the maker of B4X. Its MIT licence makes it the open alternative I described.
What I like about it
  • Compatibility is tested, not claimed. The author says it will be "measured by differential tests against the original toolchain, not claimed." The repo includes tooling to record outputs from the original toolchain, so each test checks FerroBASIC's output against real B4X output. That's the right way to build a second implementation of a language.
  • The structure is a serious compiler design. It's split into separate stages: parsing, project reading, name and type resolution (HIR), a typed intermediate form (MIR), the interpreter and differential testing. It's modelled on rustc and rust-analyzer rather than being a quick hack.
Reality check
  • Pre-alpha. Milestones M0 and M1 are complete, and M2 (classes and events) is next. Without classes and events it can't run real B4X apps yet.
  • No real compiler yet. What works is Level 0 programs (one module, the language core, and List/Map). ferro bundle just packages the interpreter with the program's source, so it runs at interpreter speed. Native compilation waits for milestone M4.
  • Tiny community. It has 3 stars, 1 watcher, 0 forks, despite 388 commits. That suggests one person working hard on it, which brings back the single-maintainer problem it's partly meant to solve.
  • Probably built largely with AI agents. It includes AGENTS.md for AI coding agents. That would explain a high commit count from a solo author. It's not a bad sign in itself, but code like that needs careful review.
  • The hard part is still ahead. Matching the platform libraries (Views, the designer files, Android and iOS runtime glue) is where most of the effort will go, and it hasn't started on that yet.
Nothing here is ready for SPS or other production work. It's worth starring and watching, though. If M2 lands and the differential tests keep passing, the language server alone could make it useful well before the compiler is finished. It could also be worth raising on the B4X forum, since the author's differential-testing approach would benefit from more people contributing real-world test projects.

I high lit the last sentence in the last response above:

It could also be worth raising on the B4X forum, since the author's differential-testing approach would benefit from more people contributing real-world test projects.
 
Last edited:
It would be interesting to know which B4X version it targets, as the compiler used by Erel has been somewhat improved...
would be curious to "see" both results side by side for the same project
 
What is the purpose to quote him since he is not active in the forum?
If anyone have question or want to contribute to his repo, just contact him on GitHub.
 
I just want to point out the obvious: It's entirely possible to be active in the forum and not be logged-in. It seems reasonable to assume that Marco has seen this thread. :)


Hey Marco,

It would be fun and interesting if you logged in and told us a bit about your progress and the plans with your Rust project.
 
told us a bit about your progress and the plans with your Rust project
The details are written in the GitHub repo.

There are active branches (dev) commited 18 minutes ago.
 
Last edited:
Back
Top