Preview
Open Original
The PHP Interpreter
A small-footprint implementation of the PHP programming language.
----------------------------------------------------------
Jim PHP
----------------------------------------------------------
The main goal of Jim PHP is to improve my skills in:
Git for repository management
The PHP language, also from a low-level perspective
C++ language and OOP concepts
----------------------------------------------------------
Architecture
----------------------------------------------------------
The Jim PHP architecture is divided into three levels.
Each level behaves like an object, and these three objects communicate
with each other.
LEXER - Splits the source code into tokens
PARSER - Builds the AST (Abstract Syntax Tree) from the tokens
INTERPRETER - Analyzes the AST and e...
The PHP Interpreter
A small-footprint implementation of the PHP programming language.
----------------------------------------------------------
Jim PHP
----------------------------------------------------------
The main goal of Jim PHP is to improve my skills in:
Git for repository management
The PHP language, also from a low-level perspective
C++ language and OOP concepts
----------------------------------------------------------
Architecture
----------------------------------------------------------
The Jim PHP architecture is divided into three levels.
Each level behaves like an object, and these three objects communicate
with each other.
LEXER - Splits the source code into tokens
PARSER - Builds the AST (Abstract Syntax Tree) from the tokens
INTERPRETER - Analyzes the AST and executes its nodes
----------------------------------------------------------
Other Details
----------------------------------------------------------
This project is inspired by Jim Tcl by Salvatore Sanfilippo.
Jim PHP follows a different approach in its architecture.
The Lexer (Tokenizer) follows a similar philosophy, but the Parser and Interpreter
are based on different ideas.
Note: Jim PHP uses an AST-based interpreter, not a run-time oriented one like Jim Tcl.
----------------------------------------------------------
Daily Goal
----------------------------------------------------------
[DONE] DAY ZERO
Set up Git and GitHub. Studied the general architecture.
Wrote the README file and the CMakeLists.txt.
Understood the basic structure and goals.
[DONE] DAY ONE
Started studying how PHP code could be executed.
Jim PHP can run code in three ways, similar to Jim Tcl:
- Inline string: std::string php_code = "1+1;"; (for testing only)
- Command line: jimphp -r 'echo 1+1;'
- File execution: jimphp sum.php
Note: To execute shell commands, Jim PHP uses jimphp, while Jim Tcl uses jimsh.
Worked on inline string execution and Lexer implementation with a token structure.
We need tokens because the Parser will operate on individual tokens.
Lexer.cpp can now tokenize expressions β "1+1" becomes "1", "+", "1".
[DONE] DAY TWO
Started fixing issues in Lexer.cpp.
Issue #1:
If you hardcode a PHP expression like:
std::string php_code = "(10.2+0.5*(2-0.4))2+(2.14)";
the Lexer would return "Unknown character" because it didnβt yet recognize
symbols like ), {, * and so on.
Yesterday, Jim PHP was tested only with simple expressions like "1+1".
Obviously, thatβs not acceptable. We needed a better Lexer that can
tokenize code more accurately and recognize symbols properly.
Jim PHP now implements these category for token structures:
-Char Tokens: a-z A-Z and _
-Num Tokens: 0-9
-Punct (Punctuation) Tokens: . , : ;
-Oper (Operator) Tokens: + - * / = % ^
-Parent (Parenthesis) Tokens: ()[]{}
-SChar (Special char) Tokens: ! @ # $ & ? < > \ | ' " and == != >= <= && ||
In this way we can write more complex PHP expressions like:
std::string php_code = "$hello = 5.5 + 10 * (3 - 1); // test! @#|_\";
Result:
SCHAR: $ | CHAR: hello_user | OPER: = | NUM: 5 | PUNCT: . | NUM: 5 | OPER: + | NUM: 10 | OPER: * | LPAREN: ( | NUM: 3 | OPER: - | NUM: 1 | RPAREN: ) | PUNCT: ; | OPER: / | OPER: / | CHAR: test | SCHAR: ! | SCHAR: @ | SCHAR: # | SCHAR: | | CHAR: _ | SCHAR: \ | SCHAR: "
----------------------------------------------------------
File and Folder Structure
----------------------------------------------------------
jimphp/
βββ CMakeLists.txt
βββ README.md
βββ examples/
β βββ hello.php
β βββ math.php
β βββ if_else.php
βββ include/
β βββ Token.hpp
β βββ ASTNode.hpp
β βββ Value.hpp
β βββ SymbolTable.hpp
β βββ Lexer/
β β βββ Lexer.hpp
β βββ Parser/
β β βββ Parser.hpp
β βββ Interpreter/
β βββ Interpreter.hpp
βββ src/
β βββ main.cpp
β βββ Lexer/
β β βββ Lexer.cpp
β βββ Parser/
β β βββ Parser.cpp
β βββ Interpreter/
β βββ Interpreter.cpp
βββ tests/
βββ lexer_test.cpp
βββ parser_test.cpp
βββ interpreter_test.cpp