Skip to content
tphp-langPublic

About

TinyPHP 一个 AOT 编译器。将 代码转译为人类可读 C 代码,再经行编译。

Topics

Resources

Stars

12 stars

Watchers

0 watching

Forks

Repository files navigation

TinyPHP

强类型 → 人类可读 C 代码 → 原生可执行文件。 语法严格按 doc/grammar 17 章(唯一规范准绳)。

interface Greeter
{
    public function greet(): string;
}

class World implements Greeter
{
    public function greet(): string
    {
        return "hello, world!";
    }
}

function main(): void
{
    $g:Greeter = new World();
    println($g->greet());

    $n = parse() or { 3 }; // 出错时取默认值
    println("n = " + strval($n));
}

// !int:成功返回 int,失败 error() 会被调用方的 or{} 捕获
function parse(): !int
{
    error("not ready");
}
$ tphp run hello.tp
生成 C 源码: build/hello.c
> tcc -o hello.exe -I runtime build/hello.c
编译完成: hello.exe
hello, world!
n = 3

快速开始

下载 CI 打包的 tphp[.exe] 即用(内嵌 PHP 运行时 + 随包 TCC,运行期零依赖; 获取方式见 doc/ops/release.md)。从源码运行编译器才需要 PHP >= 8.4:

tphp run examples/01_hello.tp            # 编译并立即运行
tphp build examples/04_class.tp --emit-c # 只生成 C 源码(build/)
php tests/run.php                        # 全量测试(断言 leaks=0)

更多指令见 doc/ops/compiler.md。

文档导航

位置 内容
doc/grammar/ 语法规范 17 章(唯一准绳)
doc/guide/grammar.md 语法速查(EBNF 概览)
doc/guide/type.md 类型系统与 C 层映射
doc/guide/closure.md / function.md 闭包语义 / 内置函数
doc/guide/memory.md 内存安全模型
doc/ops/compiler.md / release.md CLI 指令 / 打包分发

交叉编译

tphp build main.tp -os linux -arch arm64   # → Linux arm64 静态 ELF(musl 静态,可直接上板)
tphp build main.tp -os windows -arch i386  # → 32 位 Windows exe

--no-main 库模式(只出 C,宿主直接调用);--cc / --cflag 透传编译参数。

打包分发

CI 自动打包 tphp[.exe] + runtime/ + tcc/(真文件随包、运行期零解压)上传 Actions Artifacts;本地一键打包见 doc/ops/release.md。

架构

Pref → Scanner+Parser → Checker(两遍) → Gen → .c → TCC/GCC/Clang → 可执行文件

Checker 标注类型、Gen 纯消费;类型单一事实来源在 Table;分节生成 + #line 回源。

测试

php tests/run.php    # 全量(108 用例,断言 leaks=0)
php tests/cross.php  # 交叉编译
php tests/dot.php    # 目录输入

用例在 tests/cases/,文件头 // expect: / // expect-error: / // expect-panic: 声明预期;明确不做清单见 doc/plan/not-doing.md。

About

TinyPHP 一个 AOT 编译器。将 代码转译为人类可读 C 代码,再经行编译。

Topics

Resources

Stars

12 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages