百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

gravity

> 编程语言
开源

重力编程语言

4.5K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

重力编程语言

**Gravity** is a powerful, dynamically typed, lightweight, embeddable programming language written in C without any external dependencies (except for stdlib). It is a class-based concurrent scripting language with modern Swift-like syntax. **Gravity** supports procedural programming, object-oriented programming, functional programming, and data-driven programming. Thanks to special built-in methods, it can also be used as a prototype-based programming language. **Gravity** has been developed from scratch for the Creo project in order to offer an easy way to write portable code for the iOS and Android platforms. It is written in portable C code that can be compiled on any platform using a C99 compiler. The VM code is about 6.5K lines long, the multipass compiler code is about 10K lines and the shared code is about 4.7K lines long. The compiler and virtual machine combined add less than 200KB to the executable on a 64-bit system. ## What Gravity code looks like ``` … ``` ## Features * multipass compiler with optimizer * dynamic typing * classes and inheritance * higher-order functions and classes * lexical scoping * coroutines (via fibers) * nested classes * closures * garbage collection (mark-and-sweep) * operator overriding * string interpolation * enums, modules, and structs (value types) * switch/case and ranges * optional modules (Math, File, JSON, ENV) * powerful embedding API with bridging support * built-in unit tests * built-in JSON serializer/deserializer * **optional semicolons** ## Building **Make (Linux / macOS / BSD)** ```bash make # Build the gravity CLI executable make mode=debug # Debug build with symbols make lib # Build shared library (libgravity.dylib/so/dll) make staticlib # Build static library (libgravity.a) make example # Build the C embedding API example make clean # Clean all build artifacts ``` **CMake (cross-platform, including Windows)** ```bash cmake -B build cmake --build build # Optionally disable the CLI and build the library only: cmake -B build -DBUILD_CLI=OFF cmake --build build ``` Requires a C99 compiler. No external dependencies. ## Usage ```bash ./gravity file.gravity # Compile and execute a source file ./gravity -c file.gravity # Compile to bytecode (outputs gravity.g) ./gravity -o out.json -c file.gravity # Compile to a specific output file ./gravity -x gravity.g # Execute precompiled bytecode ./gravity -i 'return 2 + 3' # Execute inline code ./gravity -t test/unittest # Run unit tests ``` ## Testing ```bash ./gravity -t test/unittest # Run all unit tests via the VM ./test/unittest/run_all.sh # Run all unit tests via shell script (with per-test timeouts) ./gravity test/unittest/somefile.gravity # Run a single test file ``` The `test/` directory also contains `fuzzy/` (randomised fuzzing inputs) and `infiniteloop/` (tests that must terminate with a runtime error rather than hang). ## Project Structure ``` src/ ├── cli/ Command-line interface ├── compiler/ Lexer, parser, AST, semantic analysis, IR, optimizer, codegen ├── runtime/ Stack-based VM, built-in types and core methods ├── shared/ Value representation, opcodes, hash table, array, memory/GC ├── optionals/ Optional modules: Math, File, JSON, ENV └── utils/ Debug disassembler, JSON serialization, file I/O, UTF-8 ``` For a comprehensive technical deep-dive into the implementation, see [ARCHITECTURE.md](ARCHITECTURE.md). ## Embedding API Gravity is designed to be embedded inside a host application. The complete API lives in `src/runtime/gravity_vm.h` and `src/compiler/gravity_compiler.h`. A minimal example: ``` … ``` See [`examples/example.c`](examples/example.c) and the [embedding documentation](https://gravity-lang.org) for the full bridging API. ## Special thanks Gravity was supported by a couple of open-source projects. The inspiration for closures comes from the elegant Lua programming language; specifically from the document Closures in Lua. For fibers, upvalues handling and some parts of the garbage collector, my gratitude goes to Bob Nystrom and his excellent Wren programming language. A very special thanks should also go to my friend **Andrea Donetti** who helped me debugging and testing various aspects of the language. ## Documentation The Getting Started page is a guide for downloading and compiling the language. There is also a more extensive language documentation. Official [wiki](https://github.com/marcobambini/gravity/wiki) is used to collect related projects and tools. For implementation internals, see the [Architecture Document](ARCHITECTURE.md). ## Where Gravity is used * Gravity is the core language built into Creo (https://creolabs.com) * Gravity is the scripting language for the Untold game engine (https://youtu.be/OGrWq8jpK14?t=58) ## Changelog See [CHANGELOG.md](CHANGELOG.md) for a summary of changes across versions. ## Community Questions, ideas, and general discussion are welcome in [GitHub Discussions](https://github.com/marcobambini/gravity/discussions). ## Contributing Contributions to Gravity are welcomed and encouraged!
More information is available in the official [CONTRIBUTING](CONTRIBUTING.md) file. * Open an issue: * if you need help * if you find a bug * if you have a feature request * to ask a general question * Submit a pull request: * if you want to contribute ## License Gravity is available under the permissive MIT license.

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •multipass compiler with optimizer
  • •dynamic typing
  • •classes and inheritance
  • •higher-order functions and classes
  • •lexical scoping
  • •coroutines (via fibers)
  • •nested classes
  • •closures
  • •garbage collection (mark-and-sweep)
  • •operator overriding

> 标签

Cbridgebytecodecclosure

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言