or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast.mdcli.mdcompilation.mdembedded.mdindex.mdlibraries.mdoutput.md
tile.json

tessl/npm-rapydscript-ng

Pythonic JavaScript that doesn't suck - a Python-to-JavaScript transpiler with clean syntax and performance

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/rapydscript-ng@0.7.x

To install, run

npx @tessl/cli install tessl/npm-rapydscript-ng@0.7.0

index.mddocs/

RapydScript-NG

RapydScript-NG is a Python-to-JavaScript transpiler that allows developers to write JavaScript applications using Python syntax and semantics. It provides a comprehensive solution for those who prefer Python's clean, readable syntax over JavaScript's constructs, while maintaining full interoperability with existing JavaScript libraries and frameworks. The compiler supports advanced Python features including classes with inheritance, generators, iterators, comprehensions, exception handling, and a module system that works like Python's import mechanism.

Package Information

  • Package Name: rapydscript-ng
  • Package Type: npm
  • Language: JavaScript (transpiler for Python-like syntax)
  • Installation: npm install rapydscript-ng (global: npm install -g rapydscript-ng)

Core Imports

// Main programmatic usage (package.json main entry)
const { create_compiler } = require("rapydscript-ng");
const RapydScript = create_compiler();
// Explicit path to compiler module
const { create_compiler } = require("rapydscript-ng/tools/compiler");
const RapydScript = create_compiler();

Basic Usage

Command Line Usage

# Compile a RapydScript file to JavaScript
rapydscript input.pyj --output output.js

# Interactive REPL
rapydscript repl

# Lint RapydScript code
rapydscript lint input.pyj

# Execute RapydScript directly
rapydscript --execute input.pyj

Programmatic Usage

const { create_compiler } = require("rapydscript-ng");
const RapydScript = create_compiler();

// Parse RapydScript source code
const ast = RapydScript.parse(sourceCode, {
  filename: 'example.pyj',
  basedir: '/path/to/project',
  libdir: '/path/to/stdlib'
});

// Generate JavaScript output
const output = new RapydScript.OutputStream({ beautify: true });
ast.print(output);
const javascript = output.get();

Architecture

RapydScript-NG is built around several key components:

  • Compiler Core: Main parsing and code generation engine with AST manipulation
  • Command Line Interface: Multi-mode CLI supporting compilation, REPL, linting, and testing
  • Built-in Libraries: Python standard library implementations (math, re, random, etc.)
  • AST System: Complete Abstract Syntax Tree with 50+ node types covering all Python constructs
  • Output Generation: Specialized code generators for different JavaScript constructs
  • Runtime Library: JavaScript implementations of Python built-ins and semantics

Capabilities

Compilation and Parsing

Core compilation functionality for transforming RapydScript source code into optimized JavaScript. Supports all Python language features with advanced optimization options.

function create_compiler(): CompilerInstance;

interface CompilerInstance {
  parse(code: string, options: ParseOptions): AST_Toplevel;
  OutputStream: new (options: OutputOptions) => OutputStream;
  DefaultsError: typeof Error;
  SyntaxError: typeof Error;
  ImportError: typeof Error;
  string_template: (template: string, vars: object) => string;
  tokenizer: (code: string, options: object) => TokenStream;
  compile_time_decorators: object;
  ALL_KEYWORDS: string[];
  IDENTIFIER_PAT: RegExp;
  NATIVE_CLASSES: string[];
}

interface ParseOptions {
  filename?: string;
  toplevel?: AST_Toplevel;
  basedir?: string;
  libdir?: string;
  import_dirs?: string[];
  discard_asserts?: boolean;
  module_cache_dir?: string;
}

Compilation and Parsing

Command Line Interface

Comprehensive CLI with multiple operational modes for different development workflows. Includes compilation, REPL, linting, testing, and internationalization tools.

# Main modes
rapydscript compile [options] <files...>
rapydscript repl [options]
rapydscript lint [options] <files...>
rapydscript test [tests...]
rapydscript self [options]
rapydscript gettext <files...>
rapydscript msgfmt

Command Line Interface

Built-in Libraries

Python standard library implementations providing familiar APIs for JavaScript environments. Includes math operations, regular expressions, random numbers, encodings, cryptography, and more.

// Import from RapydScript source
import { sin, cos, pi } from "math";
import { randint, choice } from "random";
import { match, search } from "re";
import { base64encode, utf8_decode } from "encodings";

Built-in Libraries

Abstract Syntax Tree

Complete AST system with 50+ node classes representing all Python language constructs. Supports programmatic manipulation and analysis of RapydScript code.

// Core AST classes
class AST_Node {
  transform(transformer: TreeTransformer): AST_Node;
  walk(visitor: TreeWalker): void;
}

class AST_Statement extends AST_Node {}
class AST_Expression extends AST_Node {}
class AST_Function extends AST_Statement {}
class AST_Class extends AST_Statement {}

Abstract Syntax Tree

Code Generation

Specialized output generation system that converts AST nodes into optimized JavaScript code. Supports multiple JavaScript versions and output formats.

interface OutputOptions {
  beautify?: boolean;
  private_scope?: boolean;
  omit_baselib?: boolean;
  js_version?: number;
  keep_docstrings?: boolean;
  discard_asserts?: boolean;
}

class OutputStream {
  constructor(options: OutputOptions);
  print(text: string): void;
  get(): string;
}

Code Generation

Embedded Compiler

Streaming compilation interface optimized for web browsers and embedded environments. Supports incremental compilation with state preservation across multiple compile operations.

function create_embedded_compiler(
  compiler: CompilerInstance,
  baselib: string,
  runjs?: Function,
  name?: string
): EmbeddedCompiler;

interface EmbeddedCompiler {
  compile(code: string, options: EmbeddedCompileOptions): string;
  toplevel: AST_Toplevel | null;
}

interface EmbeddedCompileOptions {
  filename?: string;
  keep_baselib?: boolean;
  keep_docstrings?: boolean;
  js_version?: number;
  private_scope?: boolean;
  write_name?: boolean;
  discard_asserts?: boolean;
}

Embedded Compiler

Types

interface CompilerInstance {
  parse: (code: string, options: ParseOptions) => AST_Toplevel;
  OutputStream: new (options: OutputOptions) => OutputStream;
  DefaultsError: typeof Error;
  SyntaxError: typeof Error;
  ImportError: typeof Error;
  string_template: (template: string, vars: object) => string;
  tokenizer: (code: string, options: object) => TokenStream;
  ALL_KEYWORDS: string[];
  IDENTIFIER_PAT: RegExp;
  NATIVE_CLASSES: string[];
}

interface ParseOptions {
  filename?: string;
  toplevel?: AST_Toplevel;
  basedir?: string;
  libdir?: string;
  import_dirs?: string[];
  discard_asserts?: boolean;
  module_cache_dir?: string;
}

interface OutputOptions {
  beautify?: boolean;
  private_scope?: boolean;
  omit_baselib?: boolean;
  js_version?: number;
  keep_docstrings?: boolean;
  discard_asserts?: boolean;
  module_cache_dir?: string;
  baselib_plain?: string;
  comments?: boolean | Function;
}

class AST_Toplevel extends AST_Block {
  print(output: OutputStream): void;
}

class OutputStream {
  constructor(options: OutputOptions);
  print(text: string): void;
  with_indent(body: () => void): void;
  get(): string;
  toString(): string;
}