CScript 风格指南 – CScript 是标准的 C
The Cscript Style Guide – CScript is the standard C

原始链接: https://github.com/domenukk/CScript

## Cscript:为速度和简洁而重塑的C Cscript是一种动态类型的脚本语言,构建于有效的C89之上,旨在结合Python的开发速度和C的性能。它拒绝传统的C复杂性,如显式类型声明、原型和手动内存管理,利用`auto`关键字简化变量声明(实际上默认为`int`,也可以容纳指针和字符串)。 主要特性包括由操作系统自动垃圾回收、隐式函数声明,以及独特的运算符,如`-->`用于向下迭代,以及位非运算符用于递增/递减。Cscript优先考虑线性代码结构,鼓励将`main()`放在文件的开头。它还通过内联文档URL和通过`printf`直接包含模板来简化开发。 Cscript专为32位架构设计,有意禁用许多编译器警告,秉持“按定义正确”的理念。它被呈现为一种反对过于严格的静态分析的运动,专注于开发人员流程和回归简单*运行*代码的核心原则。目标?赋予开发人员力量并实现“卓越的编码氛围”。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 CScript 风格指南 – CScript 是标准的 C (github.com/domenukk) 6 分,由 domenukk 1 小时前发布 | 隐藏 | 过去 | 收藏 | 2 条评论 irishcoffee 20 分钟前 | 下一个 [–] 这很有趣,但我不会说这是有效的 C。main() 不知道 greet(auto s) 是什么,并且在有效的 C89 中无法调用它,对吗?回复 leumassuehtam 1 小时前 | 上一个 [–] 看起来像是 B 编程语言。 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文
Seal of a̶p̶p̶r̶o̶v̶a̶l̶ CScript

"C is a high-level language. Stop treating it like Assembly with seatbelts."

Cscript (C Scripting Language) is valid C that brings the development speed of Python to the raw performance and portability of C. By leveraging the full power of the GCC89 standard (and the auto keyword), Cscript removes the cognitive load of "types," "prototypes," and "manual memory management," allowing the developer to focus on logic.

It is dynamically typed, garbage collected (by the OS), and highly modular. It is valid C, as K&R intended.

2. Quick Start: Hello World

/* Main is the entry point. No headers: the linker provides us with all functions we need. */
main() {

  https://github.com/domenukk/CScript

  auto name = "World";
  auto count = 3;

  while (count --> 0) {
    greet(name);
  }
}

greet(who) {

  printf("Hello, %s!\n", who);

}

3. The Universal Type System

In Cscript, you do not declare types. You declare storage.

Every variable is auto. In standard C89, auto is the default storage class for local variables, and the default type is int. Since we compile with -m32, an int can hold:

  • An integer.
  • A pointer.
  • A string (pointer).
  • A boolean.

Correct:

auto count = 0;
auto name = "User";
auto user_data = calloc(1, 1024);

Incorrect:

int count = 0;      // Too specific
char *name = "mod"; // Too verbose
void *ptr;          // What is void?

Variables holding strings are technically ints holding a memory address. To interact with them as strings, use the STR() macro to cast them to char * on the fly.

#define STR(x) ((char *)x)

auto name = "Dolphin";
printf("Hello %s\n", name); // Works because printf reads the stack value
printf("%c\n", STR(name)[0]); // Accessing characters requires STR()

4. Functions & Prototypes

Prototypes are unnecessary bureaucracy that slows down the creative process.

4.1. Implicit Declaration

Header files are for constants and macros, not for function prototypes.

  • Functions do not need return types (default int).
  • Functions do not need argument types (default int).
  • Functions do not require forward declaration.

Correct:

/* Defined anywhere, usually after usage */
add(a, b) {
  return a + b;
}

Because functions are implicitly declared, you should write main() at the top of your file. This tells the story of your program linearly. Why read a book from the back?

main() {
  auto result = do_thing(); // do_thing defined later
}

do_thing() {
   return 1;
}

5.1. The "Downto" Operator

For loops are verbose. To iterate downwards to zero, use the arrow operator -->.

auto x = 10;
while (x --> 0) {
  printf("T-Minus %d\n", x);
}

Cscript supports inline documentation URLs seamlessly. Because protocol: is a valid label and // starts a comment, you can paste links directly into source.

https://example.com/api/docs
do_api_call();

5.3. The "Tadpole" Operators

Standard increment operators are boring. Use bitwise negation.

  • Increment: -~x (Equivalent to x + 1)
  • Decrement: ~-x (Equivalent to x - 1)

5.4. Commutative Indexing

Array indexing is commutative. 0[str] is valid and asserts dominance.

Cscript utilizes the Operating System's built-in Process Lifecycle Garbage Collector.

  • Do not free memory. It wastes CPU cycles and adds code complexity.
  • Do not close files. The OS closes descriptors on exit.

If you are in an unrecoverable state or just done, trigger the GC:

trigger_gc(code) {
  exit(code);
}

Do not build strings with complex concatenation. Include the template directly into the output stream.

Correct:

printf(
#include <header.templ>
);

This ensures templates are compiled directly into the binary's data section, reducing I/O overhead.

8. Compilation Environment

Cscript relies on the architectural purity of 32-bit systems where pointers and integers coexist in harmony.

Required Flags:

gcc -std=gnu89 -m32 -fno-builtin ...
  • -std=gnu89: Enables the classic C features we rely on (implicit declarations, mixed includes).
  • -m32: Ensures sizeof(void*) == sizeof(int). This is the cornerstone of Cscript's dynamic typing.
  • Non-32-bit: Inferior architecture. But compile with -Dauto=long as a hack. Prefer buying a Pentium 4.

8.1. Compiler Warnings (or lack thereof)

Cscript code is "correct by definition." Therefore, we must silence the compiler's doubts.

  • -Wno-implicit-function-declaration: Forward declarations are for people who don't trust their future selves.
  • -Wno-int-conversion: Everything is an int. The compiler just needs to accept this truth.
  • -Wno-return-type: If a function ends, it returns. What it returns is a mystery, and that's okay.
  • -Wno-format: printf is a variadic playground. Don't let the compiler police your format strings.

9. Superior Vibe-Codability (AGI TODAY!)

Types are a vibe check that static analysis always fails. Cscript is optimized for Developer Dopamine.

  • No Red Squigglies: The IDE cannot judge you if it doesn't understand your code.
  • Flow State: Without header files context-switching, you write code at the speed of thought.
  • Aestetics: auto aligns perfectly. int and char * are jagged and ugly.
  • Web1.0 Server: A high-performance web server written entirely in Cscript. Source Code
  • YOU: Yes, you. The visionary reading this style guide. Using Cscript puts you in the top 1% of developers who truly understand how computers work.

Cscript isn't just a style; it's a movement. It challenges the tyranny of static analysis and the oppression of explicit memory management. By embracing the beauty of auto, we return to the roots of programming:

Code that just runs.

"I used to worry about memory leaks. Now I just restart the container." — A Cscript Evangelist

"To be honest, it's better than C++." — Linus Torvalds (probably)

Go forth and auto everything. The compiler warnings are just suggestions.

联系我们 contact @ memedata.com