thp-lang.org/md/learn/index.md

195 lines
3.6 KiB
Markdown
Raw Normal View History

2023-10-02 01:41:38 +00:00
![Accurate visual description of THP](/img/desc_thp.jpg)
# Welcome
Welcome to the documentation of the THP programming languague.
2023-07-01 01:00:17 +00:00
THP is a new programming language that compiles to PHP.
2023-09-15 03:15:35 +00:00
<br>
This page discusses some of the design decitions of the language,
2023-10-02 01:41:38 +00:00
if you want to install THP go to the [installation guide](/installation-guide)
2023-09-15 03:15:35 +00:00
If you want to learn the language, go to the learn section.
## Goals
2024-02-20 10:53:38 +00:00
- Bring static typing to PHP: Not just type hints, not just `mixed` for everything
2023-07-01 01:00:17 +00:00
that isn't a primitive type.
2024-02-20 10:53:38 +00:00
- Generics & ADTs
- Avoid implicit type conversion.
2023-07-01 01:00:17 +00:00
- Remove the inconsistencies in the language.
2024-02-20 10:53:38 +00:00
- Organize the stdlib into modules.
2023-07-01 01:00:17 +00:00
- Differentiate between Arrays, Tuples, Maps and Sets.
- Create a **consistent** language.
2024-02-20 10:53:38 +00:00
- Have typings for popular libraries (like TS's `.d.ts`).
2023-07-01 01:00:17 +00:00
- Have a simple instalation and configuration (requiring just Composer).
2024-02-20 10:53:38 +00:00
- Ship a fast, native binary (written in Rust) (why use PHP when we can go **_blazingly fast_**?).
2023-07-01 01:00:17 +00:00
- Support in-place compilation.
2024-02-20 10:53:38 +00:00
- (Try to) emit readable PHP.
- Implement an LSP server.
- Implement a formatter.
2023-07-01 01:00:17 +00:00
## Not goals
These are **not** aspects that THP looks to solve or implement.
2023-07-01 01:55:29 +00:00
- Be what TypeScript is for JavaScript (PHP with types).
2023-07-01 01:00:17 +00:00
- Use PHP syntax/conventions.
2023-07-01 01:55:29 +00:00
- Be familiar for PHP developers.
2023-07-01 01:00:17 +00:00
## Philosophy
2023-07-01 01:55:29 +00:00
- Consistency over familiarity
- Change over conventions
- Explicit over implicit
2023-07-01 01:00:17 +00:00
## Some differences with PHP
```thp
// PHP
$has_key = str_contains($haystack, 'needle');
// THP
2024-02-20 10:17:21 +00:00
val has_key = haystack.contains("needle")
```
- Explicit variable declaration
- No `$` for variable names (and thus no `$$variable`)
- No semicolons
- Use methods on common datatypes
- Strings use only double quotes
---
```thp
// PHP
[
'names' => ['Toni', 'Stark'],
'age' => 33,
'numbers' => [32, 64, 128]
]
// THP
2024-03-27 13:12:32 +00:00
.{
names: #("Toni", "Stark"), // Tuple
age: 33,
numbers: [32, 64, 128]
}
```
- Tuples, Arrays, Sets, Maps are clearly different
2024-02-20 10:17:21 +00:00
- JSON-like object syntax
---
```thp
// PHP
$cat = new Cat("Michifu", 7);
$cat->meow();
// THP
2024-02-20 10:17:21 +00:00
val cat = Cat("Michifu", 7)
cat.meow();
```
- No `new` for classes
- Use dot `.` instead of arrow `->` syntax
---
```thp
// PHP
use \Some\Deeply\Nested\Class
use \Some\Deeply\Nested\Interface
// THP
use Some::Deeply::Nested::{Class, Interface}
```
- Different module syntax
2024-01-01 14:11:41 +00:00
- Explicit module declaration
- PSR-4 required
2024-01-01 14:11:41 +00:00
- No `include`, `include_once`, `require` or `require_once`
---
Other things:
- Pattern matching
- ADTs
2024-01-01 14:11:41 +00:00
## Runtime changes
2024-01-01 14:11:41 +00:00
Where possible THP will compile to available PHP functions/classes/methods/etc.
2024-01-01 14:11:41 +00:00
For example:
2024-01-01 14:11:41 +00:00
```thp
// This expression
2024-02-20 10:17:21 +00:00
val greeting =
2024-01-01 14:11:41 +00:00
match get_person()
| Some(person) if person.age > 18
{
"Welcome, {person.name}"
}
| Some(person)
{
"I'm sorry {person.name}, you need to be 18 or older"
}
| None
{
"Nobody is here"
}
// Would compile to:
$greeting = null;
$_person = get_person();
if ($_person !== null) {
if ($_person["age"] > 18) {
$greeting = "Welcome, " . $_person["name"];
}
else {
$greeting = "I'm sorry " . $_person["name"] . ", you need to be 18 or older";
}
}
else {
$greeting = "Nobody is here";
}
```
2024-01-01 14:11:41 +00:00
However, more advanced datatypes & helper functions will require a sort of
runtime (new classes/functions/etc) or abuse the language's syntax/semantics.
2024-01-01 14:11:41 +00:00
```thp
// TBD: Enum compilation
enum IpAddress {
V4(String),
V6(String),
}
2024-02-20 10:17:21 +00:00
val ip_1 = IpAddress::V4("255.255.0.0")
2024-01-01 14:11:41 +00:00
// Would possibly compile to:
enum IpAddress {
V4,
V6,
}
2024-01-01 14:11:41 +00:00
$ip_1 = [IpAddress::V4, "255.255.0.0"]
```
2024-01-01 14:11:41 +00:00
Such changes will be documented