我正在构建一种以清晰为先的语言(编译为C++)。
I'm building a clarity-first language (compiles to C++)

原始链接: https://github.com/taman-islam/rox

ROX 是一种新的、极简的编程语言,优先考虑清晰和直接性。它旨在消除其他语言中常见的隐式行为——例如隐藏的转换和复杂的语法——迫使程序员显式地定义逻辑。ROX 使用 clang++ 编译为 C++20,生成原生可执行文件。 主要特性包括显式类型、通过 `rox_result[T]` 值进行错误处理(而不是异常)、单个循环结构 (`repeat`) 以及严格的编译时检查。该语言有意限制其表面积,提供一组小的核心类型(数字、布尔值、字符串、列表、字典)和函数。 ROX 强调深思熟虑的代码;每个访问和控制结构都是可见的。错误处理需要检查 `isOk()` 并检索值或消息。该项目包含 Two Sum 等算法的经过验证的测试实现,作为回归测试。ROX 并非关于便利性,而是关于一次专注的探索:当语言仅仅*不碍事*时,编程会是什么样子?提供了一个基于 Web 的游乐场,用于交互式测试。

一位 Hacker News 的开发者正在构建 “Rox”,一种新的编程语言,旨在提高清晰度,并编译为 C++。其核心原则是简单性,尤其是在内存管理方面。 Rox 故意避免暴露手动内存管理特性,例如指针或分配 API。相反,它在底层利用 C++ STL 容器(向量、映射)。对于基本类型,值按值传递;对于容器,按只读引用传递,防止意外的数据修改并简化所有权。 本质上,Rox 利用 C++ 的 RAII(资源获取即初始化)进行销毁,确保在作用域结束时清理资源。这提供了 C++ 强大的生命周期语义,以及一个更可控、更不易出错的语言界面。目标是在保持 C++ 编译性能的同时,降低复杂性。
相关文章

原文

ROX is a minimal, clarity-first programming language built on a simple belief:

Programming logic should not have to fight the language.

ROX removes implicit behavior, hidden conversions, and syntactic tricks so that expressing logic feels direct and mechanical rather than negotiated.

ROX compiles .rox source files into C++20 (.cc), which are then compiled into native executables using clang++.

This repository contains the ROX compiler implementation written in C++20.


In many languages, expressing simple logic often requires navigating:

  • Implicit type coercions
  • Silent conversions
  • Operator overloading
  • Hidden control flow
  • Exception systems
  • Special cases

ROX intentionally removes these.

The goal is not convenience. The goal is clarity of expression.

In ROX:

  • Every type is explicit.
  • Every error is a value.
  • Every access is deliberate.
  • Every control structure is visible.
  • Nothing implicit happens behind your back.

You write the logic. The language stays out of the way.


ROX v0 enforces:

  • No implicit type conversions
  • No bracket indexing ([] only for list literals)
  • No exceptions — errors are explicit values (rox_result[T])
  • A single loop construct (repeat)
  • Explicit control flow only
  • Strict compile-time type checking

The surface area is intentionally small and opinionated.


Two Sum implemented in ROX:

function two_sum(list[num] nums, num target) -> list[num] {
    num n = nums.size();

    repeat i in range(0, n, 1) {
        repeat j in range(i + 1, n, 1) {

            rox_result[num] r1 = nums.at(i);
            if (not isOk(r1)) { return [-1, -1]; }
            num v1 = getValue(r1);

            rox_result[num] r2 = nums.at(j);
            if (not isOk(r2)) { return [-1, -1]; }
            num v2 = getValue(r2);

            if (v1 + v2 == target) {
                return [i, j];
            }
        }
    }

    return [-1, -1];
}
  • .at() returns rox_result[T]
  • Errors must be handled explicitly
  • No implicit casts
  • range is the only loop construct
  • Lists are accessed only via .at()

The language forces clarity — not ceremony.

  • num (64 bit signed integer)
  • num32 (32 bit signed integer)
  • bool
  • char
  • string
  • none
  • list[T]
  • dictionary[K, V]
  • rox_result[T]
  • if / else
  • repeat i in range(start, end, step)
  • break
  • continue
  • print(val) -> none (supports string, num, float, bool, char, list)
  • isOk(rox_result[T]) -> bool
  • getValue(rox_result[T]) -> T
  • getError(rox_result[T]) -> string
  • num32_abs(n)
  • num32_min(a, b)
  • num32_max(a, b)
  • num32_pow(base, exp) -> rox_result[num32]
  • num_abs(n)
  • num_min(a, b)
  • num_max(a, b)
  • num_pow(base, exp) -> rox_result[num]
  • float_abs(n)
  • float_min(a, b)
  • float_max(a, b)
  • float_pow(base, exp)
  • float_sqrt(n) -> rox_result[float]
  • float_sin(n)
  • float_cos(n)
  • float_tan(n)
  • float_log(n) -> rox_result[float]
  • float_exp(n)
  • float_floor(n)
  • float_ceil(n)

ROX does not use exceptions. Errors are explicit values:

rox_result[num] r = nums.at(i);
if (not isOk(r)) {
    return [-1, -1];
}
num value = getValue(r);

To get the error message:

if (not isOk(r)) {
    print("Error: ", getError(r), "\n");
}

Nothing throws. Nothing hides.

Strings are immutable sequences of UTF-8 bytes.

string s = "Hello, World!";
print(s);
print("\n");

Hash maps for key-value storage.

dictionary[string, num] scores;
scores.set("Alice", 100);
if (scores.has("Alice")) {
    print(scores.get("Alice"));
}

Comments are single-line and start with //.

// This is a comment
num x = 10; // This is also a comment

ROX is compiled, not interpreted.

.rox.cc → native binary

  1. ROX source is parsed and type-checked.
  2. C++20 code is generated.
  3. clang++ compiles the emitted C++ into an executable.

The generated C++ is intentionally straightforward and readable.

  • C++20-compatible compiler (e.g., clang++)
  • Make
./rox run test/two_sum.rox
./rox format test/two_sum.rox
./rox generate test/two_sum.rox
./rox compile test/two_sum.rox

You can run all verified test programs with the provided script:

Alternatively, run them individually:

./rox run test/two_sum.rox

The test/ directory contains verified implementations of:

  • Two Sum
  • Valid Parentheses
  • Binary Search
  • Maximum Subarray (Kadane’s Algorithm)
  • Longest Substring Without Repeating Characters

These serve as correctness and regression tests for the compiler pipeline.

ROX v0 focuses on:

  • Core type system
  • Explicit error handling
  • Deterministic control flow
  • Clean C++ code generation
  • Minimal language surface

Future directions (ROX++) may include:

  • Module system
  • Expanded standard library
  • Static analysis improvements

ROX is not trying to compete with mainstream languages. It is an exploration of this question:

What does programming look like when the language refuses to be clever?

A local web-based playground is available to try ROX code interactively.

Then open http://localhost:3000 in your browser.

联系我们 contact @ memedata.com