Skip to content
rdcxPublic

About

PHP compiler written in Go

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

php-compiler

This is a PHP-subset compiler

Current supported PHP subset

  • Required open tag: <?php
  • Literals: int, float, string
  • Variables: $name = expression;
  • Constants: const NAME = expression;
  • Expressions: variable/const references, unary minus, + - * /, parentheses
  • Function declarations: function name($a, $b) { return ...; }
  • Typed function args: function name(int $a, float $b, string $c) { ... }
  • Classes and methods: class Name { public function f(...) { ... } }
  • Visibility modifiers on methods: public, private, protected
  • Constructors: __construct(...)
  • Object instantiation and method calls: new Name(...), $obj->method(...)
  • Named arguments for user function/method/constructor calls: f(a: 1, b: 2)
  • Class type hints in function/method arguments: function use(Box $b) { ... }
  • Interfaces and traits: interface, trait, implements, class use TraitName;
  • Inheritance: class extends and interface extends
  • File inclusion: include, include_once, require, require_once
    • Includes are resolved relative to the current file path at compile time.
    • Path expressions can be compile-time strings: "x.php", $var, or const identifiers.
    • require* fails compilation when a file is missing.
    • include* skips missing files (no warning output yet).
  • User-defined function calls: name(1, 2)
  • Builtin function calls: echo(...), print(...)

Project structure

  • ast/ AST node definitions
  • lexer/ tokenization for PHP subset syntax
  • parser/ Pratt parser
  • code/ bytecode opcodes and instruction encoding
  • compiler/ AST -> bytecode compiler
  • vm/ bytecode virtual machine runtime
  • object/ runtime object/value types
  • repl/ interactive shell
  • examples/ sample PHP programs
  • eval/, bench/ placeholders for future parity with Monkey layout

Run

cd /home/ross/projects/pogo
go test ./...
go run . examples/math.php
go run . examples/classes_public_private.php
go run . examples/classes_protected.php
go run . examples/classes_type_hints.php
go run . examples/interfaces_traits.php
go run . examples/extensions.php
go run . examples/includes_main.php
go run . examples/includes_variable_main.php

Build Single Executable (Embedded Bytecode)

This compiles a .php source file to bytecode, generates a temporary runner project at /tmp/pogo-<unixms>, embeds the bytecode with go:embed, and builds one standalone binary.

cd /home/ross/projects/pogo
go run . build examples/math.php -o ./math.bin
./math.bin

Options:

  • -o <path>: output binary path (default: input basename)
  • --keep-temp: keep generated /tmp/pogo-<unixms> runner project

Example output

consts:
  RATE = 1.5
vars:
  $base = 10
  $total = 15

Next incremental steps

  1. Add comparisons/conditionals (if, ===, etc.).
  2. Add control flow (while, for, break, continue).
  3. Expand builtins and string/array operations.
  4. Add dynamic include paths and runtime include return values.

About

PHP compiler written in Go

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages