PHPackages                             lwlwilliam/tiny-compiler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lwlwilliam/tiny-compiler

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lwlwilliam/tiny-compiler
========================

A toy compiler written in PHP

03PHP

Since Sep 5Pushed 8mo agoCompare

[ Source](https://github.com/lwlwilliam/tiny-compiler)[ Packagist](https://packagist.org/packages/lwlwilliam/tiny-compiler)[ RSS](/packages/lwlwilliam-tiny-compiler/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

原生 PHP 实现的小型编译器
---------------

[](#原生-php-实现的小型编译器)

用原生`PHP`实现的一个小型编译器，无第三方依赖，包含词法分析、语法分析、字节码生成、虚拟机执行四个部分。

### 支持特性

[](#支持特性)

- 变量(`let`)
- 常量(`const`)
- 函数(`func`)
- 判断语句(`if/else`)
- 循环语句(`while/for`)
- 返回语句(`return`)
- 引入语句(`include`)
- 块语句(`{}`)
- 表达式语句
- 数字、字符串、布尔值、`null`、数组类型
- 拥有作用域与生命周期
- 内置输出函数 `print(expr, ...)`（可变参数，每个参数独立输出并换行）

### 语言规范

[](#语言规范)

#### 关键字

[](#关键字)

`let`、`const`、`func`、`return`、`if`、`else`、`while`、`for`、`true`、`false`、`null`

#### 语句

[](#语句)

- `let`变量声明语句：`let x = 1;` 或 `let x;`
- `const`常量语句：`const PI = 3.14;`
- `func`函数声明语句：`func xxx() {}`
- `if`判断语句：`if (cond) stmt else stmt`
- `while`、`for`循环语句：`while (cond) stmt`；`for (init; cond; step) stmt`
- `return`返回语句：`return expr;`
- `include`语句：`include "path";`
- `{}`块语句：`{ ... }` 形成新作用域
- 表达式语句：`expr;`

#### 表达式

[](#表达式)

- 赋值表达式：`x = expr;`、`arr[i] = expr;`
- 二元表达式：`+ - * / % == != <  >= && ||`
- 一元表达式：`- !`
- 字面量：数字、字符串、布尔值、`null`
- 变量
- 数组字面量：`[a, b]`
- 索引：`arr[i]`
- 分组：`(expr)`
- 调用：`f(a, b)`

#### 字节码与 VM

[](#字节码与-vm)

- 栈式虚拟机，函数调用栈
- 指令（初版）：
    - 常量与变量：`CONST k`、`LOAD_GLOBAL i`、`STORE_GLOBAL i`、`LOAD_LOCAL i`、`STORE_LOCAL i`、`POP`
    - 算术与比较：`ADD SUB MUL DIV MOD NEG`、`EQ NE LT LE GT GE`
    - 逻辑：`NOT`、短路通过跳转实现 `AND OR`
    - 控制流：`JMP addr`、`JMP_IF_FALSE addr`
    - 调用：`CALL funcIndex argc`、`RET`、`HALT`
    - 数组：`ARRAY_NEW n`、`ARRAY_GET`、`ARRAY_SET`
    - 内置：`PRINT`（对栈顶进行输出并换行）
- 全局表：按名称分配索引；局部表：按函数内分配槽位

### 要求

[](#要求)

`PHP 8.3`或`PHP 8.4`。

### 安装使用

[](#安装使用)

```
$ composer require lwlwilliam/tiny-compiler:dev-main
$ cp -r vendor/lwlwilliam/tiny-compiler/examples # 注意：examples 后没有跟“/”
$ php examples/run.php
```

###  Health Score

16

—

LowBetter than 5% of packages

Maintenance43

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/4d61563734c0df32f0bbac3eb43e81167dba0d9cfd3515e386e478c92f35a15b?d=identicon)[lwlwilliam](/maintainers/lwlwilliam)

---

Top Contributors

[![lwlwilliam](https://avatars.githubusercontent.com/u/25366940?v=4)](https://github.com/lwlwilliam "lwlwilliam (11 commits)")

### Embed Badge

![Health badge](/badges/lwlwilliam-tiny-compiler/health.svg)

```
[![Health](https://phpackages.com/badges/lwlwilliam-tiny-compiler/health.svg)](https://phpackages.com/packages/lwlwilliam-tiny-compiler)
```

###  Alternatives

[koalafacade/diamond-console

Laravel Package for generate DDD structure easily for your projects

641.4k](/packages/koalafacade-diamond-console)[rich-davis1/phpw2v

A PHP implementation of Word2Vec, a popular word embedding algorithm created by Tomas Mikolov and popularized by Radim Řehůřek &amp; Peter Sojka with the Gensim Python library.

481.2k](/packages/rich-davis1-phpw2v)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
