Ranger Compiler
raw JSON → 2.1.70 verified Fri May 01 auth: no javascript
Ranger Compiler (v2.1.70, experimental) is a self-hosting cross-language compiler that translates Ranger source code into JavaScript, Java, Go, Swift, PHP, C++, C#, and Scala. It features type safety, classes, inheritance, operator overloading, lambda functions, generic traits, class extensions, and type inference. Unlike single-target compilers, Ranger allows writing portable algorithms that can run on multiple platforms. The compiler runs on Node.js and can be used from TypeScript. Currently, only JavaScript target is fully self-hosted; other targets are in varying stages of completeness. The package is experimental and released on npm.
Common errors
error Cannot find module 'ranger-compiler' ↓
cause Package not installed or not installed globally when using CLI.
fix
Run
npm install -g ranger-compiler for CLI, or npm install ranger-compiler --save-dev for local usage in a project. error addFile is not a function ↓
cause Using a named import instead of default import.
fix
Use
import R from 'ranger-compiler' and then R.addFile(...). error R is not a function ↓
cause In older versions (<2.0), default export was a function. If using a newer version, this is incorrect.
fix
For v2.0+, use
import R from 'ranger-compiler'; and call methods on R instead of calling R directly. error TypeError: R.compile is not a function ↓
cause Not loading standard library files or missing addFile calls.
fix
Ensure you load required files (Lang.clj, stdlib.clj, stdops.clj, JSON.clj) via
R.addFile() before calling R.compile(). Warnings
breaking The default export changed from a function to an object in v2.0. Old code using `import R from 'ranger-compiler'` as a function call will break. ↓
fix Update code to use R.addFile, R.compile etc. instead of calling R as a function.
breaking The `-typescript` flag was removed in v2.1.0. TypeScript annotations are now generated by default for JavaScript output. ↓
fix Remove the `-typescript` flag from CLI invocation. The compiler now always generates TypeScript-annotated JavaScript when compiling to ES6.
deprecated The `-scalafiddle` flag is deprecated and will be removed in a future version. ↓
fix Use `-l=scala` and configure Scala output manually if needed.
gotcha The compiler is experimental; not all target languages produce compilable output. Only JavaScript target is fully self-hosted. ↓
fix For production, use JavaScript target. Test other targets thoroughly before relying on them.
gotcha The package does not include the standard library files; they must be loaded separately from the 'libs' directory in the package installation. ↓
fix Locate the libs folder relative to the package (e.g., node_modules/ranger-compiler/libs/) and load the necessary .clj files using addFile.
Install
npm install ranger-compiler yarn add ranger-compiler pnpm add ranger-compiler Imports
- Ranger (default export) wrong
const R = require('ranger-compiler')correctimport R from 'ranger-compiler' - addFile wrong
import { addFile } from 'ranger-compiler';correctimport R from 'ranger-compiler'; R.addFile('file.clj', source); - compile (static method) wrong
import { compile } from 'ranger-compiler';correctimport R from 'ranger-compiler'; R.compile({ files: [], outputDir: 'bin' }); - type exports (TypeScript) wrong
import R from 'ranger-compiler'; // if only types neededcorrectimport type R from 'ranger-compiler';
Quickstart
// Install: npm install -g ranger-compiler
// Save this as hello.clj:
// class Hello {
// static fn main () {
// print "Hello World"
// }
// }
// Then run: ranger-compiler hello.clj
// Output: bin/hello.js
// Alternatively, from TypeScript:
import * as fs from 'fs';
import R from 'ranger-compiler';
// Load standard library files
R.addFile('Lang.clj', fs.readFileSync('./libs/Lang.clj', 'utf8'));
R.addFile('stdlib.clj', fs.readFileSync('./libs/stdlib.clj', 'utf8'));
R.addFile('stdops.clj', fs.readFileSync('./libs/stdops.clj', 'utf8'));
R.addFile('JSON.clj', fs.readFileSync('./libs/JSON.clj', 'utf8'));
// Add your source files
const files = ['hello.clj', 'other.clj'];
files.forEach(f => R.addFile(f, fs.readFileSync(f, 'utf8')));
// Compile to JavaScript
const result = R.compile({
files: files,
outputDir: 'bin',
language: 'es6'
});
console.log('Compilation successful:', result);