feat: add JavaScript backend for browser/Node.js compilation

Implement Phase 1 of the browser/frontend plan with a complete JS
code generator that compiles Lux programs to JavaScript:

- Basic types: Int, Float, Bool, String, Unit → JS primitives
- Functions and closures → native JS functions with closure capture
- Pattern matching → if/else chains with tag checks
- ADT definitions → tagged object constructors
- Built-in types (Option, Result) → Lux.Some/None/Ok/Err helpers
- Effects → handler objects passed as parameters
- List operations → Array methods (map, filter, reduce, etc.)
- Top-level let bindings and run expressions

CLI usage:
  lux compile app.lux --target js -o app.js
  lux compile app.lux --target js --run

Tested with factorial, datatypes, functional, tailcall, and hello
examples - all producing correct output when run in Node.js.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-14 21:22:43 -05:00
parent 9f9543a881
commit 10ff8dc6ad
3 changed files with 1176 additions and 3 deletions

1071
src/codegen/js_backend.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -2,10 +2,13 @@
//!
//! This module provides compilation to various targets:
//! - C: For native compilation via GCC/Clang
//! - JavaScript: For frontend/browser deployment (planned)
//! - JavaScript: For frontend/browser deployment
//! - WebAssembly: For portable deployment (planned)
pub mod c_backend;
pub mod js_backend;
#[allow(unused_imports)]
pub use c_backend::CBackend;
#[allow(unused_imports)]
pub use js_backend::JsBackend;