@forthix/forthic

中文文档
0.24.0发布于 2026/11/14BSD-2-ClauseTypeScriptESM + CJS近 30 天下载 5204

Stack-based, concatenative language for composable transformations - TypeScript/JavaScript runtime

原始使用文档
文档目录31 节
  1. What is Forthic?
  2. Quick Example
  3. Create a Module
  4. Use It
  5. Installation
  6. npm
  7. yarn
  8. Requirement: a global Temporal
  9. Getting Started
  10. Node.js Usage
  11. Browser Usage
  12. Creating Your First Module
  13. Features
  14. Standard Library
  15. Easy Module Creation
  16. TypeScript & JavaScript Support
  17. Package Exports
  18. Documentation
  19. Learning Resources
  20. This Runtime
  21. Core Forthic Concepts
  22. Examples
  23. Building
  24. Multi-Runtime Execution
  25. Quick Example
  26. Approaches
  27. Learn More
  28. Contributing
  29. Community
  30. License
  31. Related

Forthic TypeScript Runtime

A TypeScript/JavaScript runtime for Forthic - the stack-based, concatenative language for composable transformations.

Use Forthic to wrap your TypeScript/JavaScript code within composable words, leveraging categorical principles for clean, powerful abstractions.

Learn at forthix.com | Forthic Docs | Getting Started | Examples | API Docs


What is Forthic?

Forthic enables categorical coding - a way to solve problems by viewing them in terms of trasnformation rather than copmutation. This TypeScript runtime lets you:

  1. Wrap existing code with simple decorators
  2. Compose transformations cleanly using stack-based operations
  3. Build powerful abstractions from simple primitives

Learn more about Categorical Coding →

See the Forthic repository for technical documentation and API references.


Quick Example

Create a Module

import { DecoratedModule, Word } from '@forthix/forthic';

export class AnalyticsModule extends DecoratedModule {
  constructor() {
    super("analytics");
  }

  @Word("( numbers:number[] -- avg:number )", "Calculate average")
  async AVERAGE(numbers: number[]) {
    return numbers.reduce((a, b) => a + b, 0) / numbers.length;
  }

  @Word("( numbers:number[] stdDevs:number -- filtered:number[] )", "Filter outliers")
  async FILTER_OUTLIERS(numbers: number[], stdDevs: number) {
    // Your existing logic here
    return filteredNumbers;
  }
}

Use It

import { Interpreter } from '@forthix/forthic';
import { AnalyticsModule } from './analytics-module';

const interp = new Interpreter();
interp.register_module(new AnalyticsModule());

await interp.run(`
  ["analytics"] USE-MODULES

  [1 2 3 100 4 5] 2 FILTER-OUTLIERS AVERAGE
`);

const result = interp.stack_pop(); // Clean average without outliers

Installation

npm

npm install @forthix/forthic

yarn

yarn add @forthix/forthic

Requirement: a global Temporal

Forthic's date and time values are built on the TC39 Temporal API. The runtime references a global Temporal object and does not bundle a polyfill, so the host is responsible for providing one. Any Forthic program that uses dates/times (TODAY, NOW, date literals like 2020-06-05, the datetime module, or wire serialization of temporal values) will throw Temporal is not defined if no global Temporal is available.

Native Temporal is not yet shipped unflagged in Node.js, Chrome, or Safari, so most consumers need a polyfill. Install temporal-polyfill and import it once at your application's entry point, before running any Forthic:

npm install temporal-polyfill
import "temporal-polyfill/global"; // installs globalThis.Temporal
import { Interpreter } from "@forthix/forthic";

temporal-polyfill is declared as an optional peer dependency: skip it only if your runtime already provides a native global Temporal, or if your programs never touch dates/times. For TypeScript type-checking of Temporal types, also add temporal-spec as a dev dependency.


Getting Started

Node.js Usage

import { Interpreter } from '@forthix/forthic';

const interp = new Interpreter();

// Execute Forthic code
await interp.run(`
  [1 2 3 4 5] "2 *" MAP  # Double each element
`);

const result = interp.stack_pop(); // [2, 4, 6, 8, 10]

Browser Usage

<script type="module">
  import { Interpreter } from 'https://unpkg.com/@forthix/forthic';

  const interp = new Interpreter();
  await interp.run('[1 2 3] "2 *" MAP');
  console.log(interp.stack_pop()); // [2, 4, 6]
</script>

Creating Your First Module

import { DecoratedModule, Word } from '@forthix/forthic';

export class MyModule extends DecoratedModule {
  constructor() {
    super("mymodule");
  }

  @Word("( data:any[] -- result:any )", "Process data your way")
  async PROCESS(data: any[]) {
    // Wrap your existing code
    return myExistingFunction(data);
  }
}

// Register and use
const interp = new Interpreter();
interp.register_module(new MyModule());

await interp.run(`
  ["mymodule"] USE-MODULES
  SOME-DATA PROCESS
`);

See examples/README.md for detailed tutorials and examples.


Features

Standard Library

The TypeScript runtime includes comprehensive standard modules:

  • array - MAP, SELECT, SORT, GROUP-BY, ZIP, REDUCE, FLATTEN (30+ operations)
  • record - REC@, <REC, MERGE, KEYS, VALUES, INVERT-KEYS
  • string - SPLIT, JOIN, UPPERCASE, LOWERCASE, TRIM, REPLACE
  • math - +, -, *, /, ROUND, ABS, MIN, MAX, AVERAGE
  • datetime - >DATE, >DATETIME, ADD-DAYS, FORMAT, DIFF-DAYS (Temporal API)
  • json - >JSON, JSON>, JSON-PRETTIFY
  • boolean - ==, <, >, AND, OR, NOT, IN

See docs/modules/ for complete reference.

Easy Module Creation

The @Word decorator makes wrapping code trivial:

@Word("( input:type -- output:type )", "Description", "MY-WORD")
async MY_WORD(input: any) {
  return yourLogic(input);
}

TypeScript & JavaScript Support

  • Full TypeScript type definitions
  • Works with ES modules and CommonJS
  • Browser and Node.js compatible
  • Supports async/await

Package Exports

forthic-ts provides multiple import paths for different use cases:

// Main package - Core interpreter and standard library (works everywhere)
import { Interpreter, DecoratedModule, Word } from '@forthix/forthic';

// WebSocket support - Browser-compatible multi-runtime execution
import { ActionCableClient, WebSocketRemoteModule } from '@forthix/forthic/websocket';

// JSON-RPC support - Node.js-only multi-runtime execution
import { JsonRpcClient, RemoteModule, startJsonRpcServer } from '@forthix/forthic/jsonrpc';

Environment Compatibility:

  • Main package (@forthix/forthic): Works in both Node.js and browsers
  • WebSocket (@forthix/forthic/websocket): Works in both Node.js and browsers
  • JSON-RPC (@forthix/forthic/jsonrpc): Node.js only (uses http and global fetch)

Documentation

Learning Resources

This Runtime

Core Forthic Concepts


Examples

See examples in the examples directory.


Building

# Install dependencies
npm install

# Build both CJS and ESM
npm run build

# Run tests
npm test

# Generate documentation
npm run docs:build

Multi-Runtime Execution

Call code from other language runtimes seamlessly - use Python's pandas from TypeScript, or TypeScript's fs module from Ruby.

Quick Example

import { Interpreter } from '@forthix/forthic';
import { JsonRpcClient, RemoteModule } from '@forthix/forthic/jsonrpc';

const interp = new Interpreter();

// Connect to Python runtime
const client = new JsonRpcClient('localhost:8765');
const pandas = new RemoteModule('pandas', client, 'python');
await pandas.initialize();

interp.register_module(pandas);

// Now use Python pandas from TypeScript!
await interp.run(`["pandas"] USE-MODULES  [records] DF-FROM-RECORDS`);

Approaches

  • JSON-RPC - Node.js ↔ Python ↔ Ruby (server-to-server, no native dependencies)
  • WebSocket - Browser ↔ Rails (ActionCable, client-server)

Learn More

📖 Complete Multi-Runtime Documentation

Runtime Status: ✅ TypeScript, Python, Ruby | 🚧 Rust | 📋 Java, .NET


Contributing

We welcome contributions! See CONTRIBUTING.md for:

  • Development setup
  • TypeScript coding standards
  • Testing guidelines
  • PR process

Also see the main Forthic contributing guide for philosophy and community guidelines.


Community


License

BSD-2-Clause License - Copyright 2024 LinkedIn Corporation. Copyright 2025 Forthix LLC.



Forthic: Wrap. Compose. Abstract.

版本与发布记录

@forthix/forthic 共发布 37 个版本,首次发布于 2025/10/14;过去 12 个月发布了 31 个版本。

版本发布日期距今
0.24.0latest2026/11/142 天前
0.23.02026/11/0412 天前
0.22.02026/10/2027 天前
0.21.02026/09/281 个月前
0.20.02026/09/102 个月前
0.19.02026/09/102 个月前

关于 @forthix/forthic 的常见问题

@forthix/forthic 是什么?

Stack-based, concatenative language for composable transformations - TypeScript/JavaScript runtime

如何安装 @forthix/forthic?

在项目目录运行 npm install @forthix/forthic,或使用 pnpm add / yarn add / bun add。

@forthix/forthic 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@forthix/forthic 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@forthix/forthic 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@forthix/forthic 可以商用吗?

@forthix/forthic 使用 BSD-2-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@forthix/forthic 的下载量有多少?

根据 npm 官方下载统计,@forthix/forthic 在 2026/10/16 – 2026/11/14 这 30 天内被下载 5,204 次,日均约 173 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@forthix/forthic 还在维护吗?

最新版本 0.24.0 发布于 2026/11/14(2 天前);至今共发布 37 个版本,其中过去 12 个月发布了 31 个;首次发布于 2025/10/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。