Show HN: Tight C,一种仅有 10 个关键字的系统级编程语言
Show HN: Tight C, a systems language with 10 keywords

原始链接: https://github.com/alonsovm44/tc-lang/

Tight-C 是一门极简的系统编程语言,旨在提供媲美 C 语言的强大性能,同时摒弃现代编程语言中复杂的机制与“隐式魔术”。它直接编译为 C11 代码,去除了垃圾回收、类型推断和面向对象编程(OOP),转而采用显式且可预测的代码编写方式。 该语言仅包含 10 个关键字(包括用于内存管理的 `defer`、定义函数的 `fn` 以及控制可见性的 `pub`),占用极小。它支持原生指针与带切片功能的胖指针、用于保证内存布局可预测的紧凑结构体,并提供与 C 语言无缝对接的 FFI。通过剔除历史包袱,Tight-C 提供了一套透明的工具集,既能让单个开发者完全理解并实现,又足以支撑真实的系统级编程需求。 主要特性包括: * **手动内存控制:** 通过 `alloc`/`free` 实现直接内存访问。 * **可预测性:** 结构体无填充,且不存在隐式变量遮蔽。 * **可读的输出:** 生成简洁、可读且兼容标准工具链的 C 代码。 * **丰富的标准库:** 内置用于 I/O、字符串、数学运算、内存操作及类型转换的模块。 Tight-C 证明了高性能系统代码无需复杂的架构设计。

这篇 Hacker News 讨论帖探讨了 `tc-lang`,一种能转译为 C 语言的小型编程语言。 该项目收到的反馈褒贬不一。一些用户称赞其“简洁之美”和优雅的设计;另一些人则对其效用提出质疑,认为在已有处理胖指针(fat pointers)等功能的 C 语言库的情况下,使用它并没有太大必要。 争议的焦点之一是 AI 的作用;一位评论者表示怀疑,称该项目在大型语言模型(LLM)时代显得“空洞”,并质疑其代码是否为人工编写。技术层面的批评主要集中在语言设计上:评论者建议标准库应更贴近 C 语言的命名规范,而非设置不必要的障碍,并指出了该语言似乎缺乏 `typedef` 等功能。另一些人则针对描述该语言语法的术语展开了辩论,特别是关于关键字与类型之间的区别。 总的来说,尽管用户认可该项目作为一种学习练习的价值,但他们仍建议创建者提供更多关于开发过程及该语言长期愿景的背景信息。
相关文章

原文

Simplest possible, usable systems language

Version Language License Platform Repo Size


Tight-C is a minimal systems programming language that compiles to C. It has 10 keywords, no garbage collector, no inference, no OOP — just explicit, predictable code with C-level power.

  • 10 keywordsif, loop, break, defer, ret, struct, fn, use, pub, pin
  • No hidden magic — no GC, no type inference, no shadowing, no aliasing
  • Raw pointers (->) and fat pointers (=>) with built-in slicing
  • Manual memoryalloc() / free() with defer for cleanup
  • Packed structs — no padding, predictable layout
  • C FFIextern "C" for direct interop
  • Compiles to C11 — readable output, use any C toolchain
# Build the compiler
make

# Compile stdlib
./tcc stdlib/io.tc -o stdlib/io.h

# Compile a program
./tcc samples/fizzbuzz.tc -o fizzbuzz.c

# Build and run
gcc fizzbuzz.c -std=c11 -o fizzbuzz
./fizzbuzz
use "stdlib/io.tc"

void fn main: {
    print("hello, world")
}
i32 x = 10
f64 pi = 3.14
u8 byte

Uninitialized variables default to 0.

i32 fn add: i32 a, i32 b {
    ret a + b
}
struct Point {
    i32 x,
    i32 y
}

Point p
p.x = 10
i32 x = 42
->i32 ptr = @x       // address-of
->ptr = 99           // dereference

=>i32 slice = arr[1:4]  // fat pointer (slice)
i32 len = slice.len      // built-in length
if (x > 0) { ... }

loop { ... break }

loop if (i < 10) { ... }
->i32 arr = alloc(i32, 100)
defer { free(arr) }
extern "C" {
    i32 fn printf: ->i8 fmt, ... {}
}
Tight-C C Equivalent
i8 char
i16 int16_t
i32 int32_t
i64 int64_t
u8 uint8_t
u16 uint16_t
u32 uint32_t
u64 uint64_t
f32 float
f64 double
void void
tc-lang/
  compiler/
    include/     # Header files
    src/         # Compiler source (C)
  stdlib/        # Standard library (.tc)
  samples/       # Example programs
  docs/          # Language specification
  Makefile       # Build system

stdlib/io.tc — I/O

Function Description
print(s) Print string + newline
printn(s) Print string, no newline
printi(n) Print i64 + newline
printin(n) Print i64, no newline
readi() Read i64 from stdin
readc() Read single char from stdin

stdlib/str.tc — Strings

Function Description
slen(s) String length
seq(a, b) String equality (returns 1 if equal)
scpy(dest, src) Copy string
scat(dest, src) Concatenate strings
sneq(a, b, n) Compare first n bytes
sfind(s, c) Find first char occurrence
sfindlast(s, c) Find last char occurrence
shas(haystack, needle) Find substring

stdlib/math.tc — Math

Function Description
iabs(x) Absolute value (integer)
min(a, b) Minimum of two integers
max(a, b) Maximum of two integers
clamp(x, lo, hi) Clamp value to range
sqrt64(x) Square root (f64)
pow64(base, exp) Power (f64)
fabs64(x) Absolute value (f64)
sin, cos, tan Trig functions (extern C)
log, log2, log10 Logarithms (extern C)

stdlib/mem.tc — Memory

Function Description
zero(ptr, n) Zero out n bytes
copy(dest, src, n) Copy n bytes (overlap safe)
memeq(a, b, n) Compare n bytes (1 if equal)
fill(ptr, val, n) Fill n bytes with value

stdlib/conv.tc — Conversions

Function Description
stoi(s) String to i64
stoib(s, base) String to i64 with base
stof(s) String to f64
itos(n, buf, size) i64 to string (into buffer)
ftos(n, buf, size) f64 to string (into buffer)

Requires gcc and make.

make          # Build tcc
make clean    # Remove build artifacts

Everything that can be built in the stdlib has to.

Tight-C bets that C's power doesn't require C's complexity. Strip away the historical baggage and you get a language a single person can implement, understand fully, and still write real systems code in.


Built by @alonsovm44

联系我们 contact @ memedata.com