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

Insert

> 编程语言
开源

Insert 是一种用于自修改代码的编程语言。

198 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Insert 是一种用于自修改代码的编程语言。

Insert

Insert is a programming language for self-modifying code.

It can be used to create programs that produce modified versions of themselves. For example, the pong game above (which was one of the winners of IOCCC29) can be found at test/snapshots/pong-target.stdout, with the source code used to create it at test/src/pong.int and a helper script to run it at quineRun.sh.

Each iteration of this program produces the source code to create the next frame, with the current frame (display and game state) rendered inside the source code itself. For a high-level overview of how this all works, check out the IOCCC writeup.

A simple quine

This program prints a copy of itself with an embedded counter incremented each time it runs:

typescript
static incrVal: i32 = binding incrValMarker (0);

static quineStr: &[string] = $quine;

import "std/quine.int";

function main() -> i32 {
    quineStr[incrValMarker] = string(incrVal + 1);
    printQuine(quineStr, $quineLen);
}

$quine gives you your own source as an array of string fragments, binding marks a specific value in that array so you can find and overwrite it, and printQuine prints the result. See docs/self-modifying-code.md for how this works.

Getting started

Make sure you're using rustup, as this project requires a nightly toolchain (specified in rust-toolchain.toml).

bash
# Compile an Insert program to C.
cargo run -- test/src/pong.int > main.c

# Build and run the C program.
gcc main.c -o a.out && ./a.out

# Or use the included script to iterate it.
./quineRun.sh

For a full walkthrough, including how to run a self-modifying program, see docs/getting-started.md.

Documentation

  • Getting started - install, build, and run your first program.
  • Language guide - types, functions, control flow, and the rest of the syntax.
  • Self-modifying code - markers, bindings, and quines.
  • Architecture - how the compiler is put together.

High-level architecture

The compiler has three main stages, and you can inspect the output using --stage. For a deeper look, including how quines are emitted, see docs/architecture.md.

AST

First, the input text is transformed into an AST. This step is performed by the pest library, and can be found under src/parser/program.pest.

Then, this AST is converted into MIR (this should really just be IR, although the name is a remnant from an older version of the compiler that had two levels of IR). This conversion can be found under src/parser/mod.rs. This conversion is mostly one-to-one, although a small amount of syntax sugar is removed.

Here, we declare markers which maintain their order in the program all the way down to the output. We can also declare bindings, which wrap an expression in two markers (before and after) to modify it specifically.

MIR

The MIR (mid-level intermediate representation) is where most of the compilation work is done. You can see a high-level view of what happens in src/mir/mod.rs (under visit_mir). Initially, the MIR looks like the AST, and is gradually transformed as more compiler passes run. This transformation includes things like type checking, constant evaluation, and expression simplification.

After these transformations, the MIR is lowered directly to target code.

C

C conversion is mostly one-to-one with the MIR. Target lowering works based on tokens (which allow special formatting to be applied to code), and the C lowering defines special rules for how tokens can be merged (whether spaces are required to avoid ambiguity). The main lowering code can be found under src/codegen/c/lower.rs.

To handle quines, the C lowering first creates a full version of the program (separated by markers, and with quine variables preserved in it). Then, it computes all the quine variables from this and injects them back into the program. Currently, all this behavior is hardcoded, but in the future it may be possible to write custom code to create different kind of quines.

Right now, quines work by creating a data array, separated by markers/quine variables, which contains the full content of the program except for the array itself (which is an empty string). Then, the array is printed out, and when it encounters an empty string, it prints out the array itself.

There are also some optimizations during lowering to compress the code using defines.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

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

> 标签

Rust

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

> 工具信息

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

> 相关工具

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