Compare commits

..

No commits in common. "49a04fa5ac4d0b7c9c221601102051830b326f07" and "31cb29fe857c118986a5a7edf98a7267aca44e3b" have entirely different histories.

21 changed files with 92 additions and 127 deletions

View File

@ -11,7 +11,7 @@ enum Suit
Spades,
}
val suit = Suit::Hearts
let suit = Suit::Hearts
```
@ -24,7 +24,7 @@ enum IpAddress
V6(String),
}
val addr_1 = IpAddress::V4("192.168.0.1")
let addr_1 = IpAddress::V4("192.168.0.1")
match addr_1
| IpAddress::V4(ip)

View File

@ -4,23 +4,23 @@ thp distinguishes between mutable and immutable variables.
## Immutable variables
Defined with `val`, followed by a variable name and a value.
Defined with `let`, followed by a variable name and a value.
```thp
val surname = "Doe"
val year_of_birth = 1984
let surname = "Doe"
let year_of_birth = 1984
```
### Datatype annotation
Written after the `val` keyword but before the variable name.
Written after the `let` keyword but before the variable name.
```thp
val String surname = "Doe"
val Int year_of_birth = 1984
let String surname = "Doe"
let Int year_of_birth = 1984
```
When annotating an immutable variable the `val` keyword is optional
When annotating an immutable variable the `let` keyword is optional
```thp
// Equivalent to the previous code
@ -34,28 +34,28 @@ This means that if a variable has only a datatype, it is immutable.
## Mutable variables
Defined with `var`, followed by a variable name and a value.
Defined with `let mut`, followed by a variable name and a value.
```thp
var name = "John"
var age = 32
let mut name = "John"
let mut age = 32
```
### Datatype annotation
Written after the `var` keywords but before the variable name.
Written after the `let mut` keywords but before the variable name.
```thp
var String name = "John"
var Int age = 32
let mut String name = "John"
let mut Int age = 32
```
When annotating a mutable variable the keyword `var` is still **required**.
When annotating a mutable variable the keyword `let` is optional. `mut` is still **required**.
```thp
// Equivalent to the previous code
var String name = "John"
var Int age = 32
mut String name = "John"
mut Int age = 32
```

View File

@ -8,7 +8,7 @@ Basically kotlin syntax.
`new` not required, in fact, forbidden.
```thp
val dog = Dog()
let dog = Dog()
```
## Simple class
@ -18,7 +18,7 @@ Why'd you do this tho?
```thp
class SimpleClass
val instance = SimpleClass()
let instance = SimpleClass()
```
## Properties & methods
@ -27,10 +27,10 @@ val instance = SimpleClass()
class SimpleClass
{
// Properties are private by default
var String? name = ...
mut String? name = ...
// Made public with `pub`
pub var String? surname = ...
pub mut String? surname = ...
// Methods are private by default
fun display_name()
@ -58,9 +58,10 @@ Kotlin style
```thp
class Cat(
var String name,
var Int lives = 9,
val String surname = "Doe",
// If a parameter has pub, protected or private they are promoted to properties
private String name,
pub mut Int lives = 9,
protected String surname = "Doe",
)
{
pub fun get_name() -> String
@ -89,13 +90,14 @@ print(michifu.get_name())
With kotlin's `init` block.
```thp
class Dog(val String name)
class Dog(pub String name)
{
Int name_length = name.length()
Int name_length
init
{
print("Dog has been instantiated")
$name_length = name.length()
}
}
```
@ -105,7 +107,7 @@ class Dog(val String name)
Kotlin style
```thp
class Animal(var String name)
class Animal(pub String name)
{
pub fun say_name()
{
@ -118,35 +120,6 @@ class Cat(String name, Int lives) -> Animal(name)
Cat("Michi", 9).say_name()
```
## Mutable methods
By default methods cannot mutate the state of the object.
```thp
class Animal(var String name)
{
pub fun set_name(String new_name)
{
$name = new_name // Error: Cannot mutate $
}
}
```
To do so the method must be annotated. The caller must also
declare a mutable variable.
```thp
class Animal(var String name)
{
pub mut fun set_name(String new_name)
{
$name = new_name // Ok
}
}
var michi = Animal("Michifu")
michi.set_name("Garfield")
```

View File

@ -16,9 +16,10 @@ class Cat
```thp
val option = Some("GAAA")
val Some(value) = option
let option = Some("GAAA")
let Some(value) = option
let colors = Array("red", "green", "blue")
let Array()
val colors = Array("red", "green", "blue")
val Array()
```

View File

@ -5,13 +5,13 @@ Use square brackets as usual.
## Usage
```thp
val fruits = ["apple", "banana", "cherry"]
val apple = fruits[0]
let fruits = ["apple", "banana", "cherry"]
let apple = fruits[0]
print(apple)
val mut numbers = [0, 1, 2, 3]
let mut numbers = [0, 1, 2, 3]
numbers[3] = 5

View File

@ -6,7 +6,7 @@ Also known as Associative Arrays
## Usage without a declaration
```thp
val mut person = Obj {
let mut person = Obj {
name: "John",
surname: "Doe",
age: 33,
@ -31,7 +31,7 @@ obj Person = {
}
val john_doe = Person {
let john_doe = Person {
name: "John",
surname: "Doe",
age: 33,

View File

@ -3,7 +3,7 @@
```thp
// Set[Int]
val ages = Set(30, 31, 33, 35)
let ages = Set(30, 31, 33, 35)
for age in ages {
print("{age}")

View File

@ -6,9 +6,9 @@ calls (`()`).
## Definition
```thp
val person = #("John", "Doe", 32)
let person = #("John", "Doe", 32)
val #(name, surname, age) = person
let #(name, surname, age) = person
```

View File

@ -22,7 +22,7 @@ else
}
val result = if condition { value1 } else { value2 }
let result = if condition { value1 } else { value2 }
```
@ -40,7 +40,7 @@ if variable is Datatype
## If variable is of enum
```thp
val user_id = POST::get("user_id")
let user_id = POST::get("user_id")
if Some(user_id) = user_id
{

View File

@ -5,7 +5,7 @@
Braces are required.
```thp
val numbers = [0, 1, 2, 3]
let numbers = [0, 1, 2, 3]
for number in numbers
{
@ -19,7 +19,7 @@ for #(index, number) in numbers.entries()
```
```thp
val dict = Obj {
let dict = Obj {
apple: 10,
banana: 7,
cherries: 3,
@ -45,8 +45,8 @@ for value in collection
## While loop
```thp
val colors = ["red", "green", "blue"]
val mut index = 0
let colors = ["red", "green", "blue"]
let mut index = 0
while index < colors.size()
{

View File

@ -5,7 +5,7 @@
Braces are **required**.
```thp
val user_id = POST::get("user_id")
let user_id = POST::get("user_id")
match user_id

View File

@ -21,7 +21,7 @@ fun get_random_number() -> Int
Random::get(0, 35_222)
}
val number = get_random_number()
let number = get_random_number()
```
## With parameters and return type
@ -32,7 +32,7 @@ fun get_secure_random_number(Int min, Int max) -> Int
Random::get_secure(min, max)
}
val number = get_secure_random_number(0, 65535)
let number = get_secure_random_number(0, 65535)
```
@ -44,10 +44,10 @@ fun get_first_item[T](Array[T] array) -> T
array[0]
}
val first = get_first_item[Int](numbers)
let first = get_first_item[Int](numbers)
// The type annotation is optional if the compiler can infer the type
val first = get_first_item(numbers)
let first = get_first_item(numbers)
```
@ -91,7 +91,7 @@ fun greet(
print("Hello {name} from {city}!")
}
greet("John", from: "LA")
greet(name: "John", from: "LA")
```

View File

@ -23,8 +23,8 @@ fun generate_generator() -> () -> Int
}
val generator = generate_generator() // A function
val value = generate_generator()() // An Int
let generator = generate_generator() // A function
let value = generate_generator()() // An Int
```

View File

@ -22,9 +22,9 @@ By default closures **always** capture variables as **references**.
```thp
var x = 20
let mut x = 20
val f = fun() {
let f = fun() {
print(x)
}
@ -44,9 +44,9 @@ fun(parameters) clone(variables) {
```
```thp
var x = 20
let mut x = 20
val f = fun() clone(x) {
let f = fun() clone(x) {
print(x)
}

View File

@ -10,18 +10,16 @@ fun add_25(Array[Int] numbers) {
```
When using a regular type as a parameter, only it's immutable
properties can be used
properties can be used inside the function
```thp
fun count(Array[Int] numbers) -> Int {
val items_count = numbers.size() // Ok, `size` is pure
let items_count = numbers.size() // Ok, `size` is pure
items_count
}
```
To use immutable properties you must use a mutable reference.
## Mutable reference
@ -38,7 +36,7 @@ data **can** be mutated.
The caller *must* also use `mut`.
```thp
val numbers = Array(0, 1, 2, 3)
let numbers = Array(0, 1, 2, 3)
push_25(mut numbers) // Pass `numbers` as reference.
@ -60,7 +58,7 @@ of the parameter (CoW). The original data will **not** be mutated.
```thp
val numbers = Array(1, 2, 3, 4)
let numbers = Array(1, 2, 3, 4)
add_25(clone numbers) // Pass `numbers` as clone.

View File

@ -16,21 +16,20 @@ If you want to learn the language, go to the learn section.
## Goals
- Bring static typing to PHP: Not just type hints, not just `mixed` for everything
- Bring static typing to PHP: Not just type hints, not use `mixed` for everything
that isn't a primitive type.
- Generics & ADTs
- Avoid implicit type conversion.
- Avoid automatic type conversion.
- Remove the inconsistencies in the language.
- Organize the stdlib into modules.
- Organize the stdlib.
- Differentiate between Arrays, Tuples, Maps and Sets.
- Create a **consistent** language.
- Have typings for popular libraries (like TS's `.d.ts`).
- Create typings for popular libraries (like TS's `.d.ts`).
- Have a simple instalation and configuration (requiring just Composer).
- Ship a fast, native binary (written in Rust) (why use PHP when we can go **_blazingly fast_**?).
- Ship a fast, native binary (written in Rust) (why use PHP when we can go native?).
- Sub 10ms watch mode.
- Support in-place compilation.
- (Try to) emit readable PHP.
- Implement an LSP server.
- Implement a formatter.
- Emit readable PHP code.
- Implement a LSP server.
## Not goals
@ -56,7 +55,7 @@ These are **not** aspects that THP looks to solve or implement.
$has_key = str_contains($haystack, 'needle');
// THP
val has_key = haystack.contains("needle")
let has_key = haystack.contains("needle")
```
- Explicit variable declaration
@ -84,7 +83,7 @@ Obj {
```
- Tuples, Arrays, Sets, Maps are clearly different
- JSON-like object syntax
- JS-like object syntax
---
@ -94,7 +93,7 @@ $cat = new Cat("Michifu", 7);
$cat->meow();
// THP
val cat = Cat("Michifu", 7)
let cat = Cat("Michifu", 7)
cat.meow();
```
@ -134,7 +133,7 @@ For example:
```thp
// This expression
val greeting =
let greeting =
match get_person()
| Some(person) if person.age > 18
{
@ -177,7 +176,7 @@ enum IpAddress {
V6(String),
}
val ip_1 = IpAddress::V4("255.255.0.0")
let ip_1 = IpAddress::V4("255.255.0.0")
// Would possibly compile to:

View File

@ -13,7 +13,7 @@
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;500;600;700;800;900&display=swap"
href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;500;600;700;800;900&family=Fugaz+One&family=Inconsolata&family=Inter&display=swap"
rel="stylesheet">
<link
href="https://fonts.googleapis.com/css2?family=Fira+Code&family=Josefin+Sans:ital,wght@0,400;1,700&display=swap"
@ -40,8 +40,6 @@
</h1>
<p class="font-display text-c-text opacity-60 text-xl pt-4">
Syntax, stdlib and types for PHP
<br>
Written in Rust
</p>
</div>
@ -62,17 +60,17 @@
<pre style="padding: 0 !important;">
<code class="language-thp">
fun find_person(Str person_id) -> Result[Str] {
val person_id = try person_id.parse[Int]()
let person_id = try person_id.parse[Int]()
try Person::find_by_id(person_id)
}
val id = POST::get("person_id") ?: die
val person = find_person(person_id: id) ?: die
let id = POST::get("person_id") ?: die
let person = find_person(person_id: id) ?: die
print(
&lt;a href="/person/reports/{person.id}"&gt;
Hello {person.name}
&lt;/a&gt;
&lt;/div&gt;
)
</code>
</pre>
@ -88,11 +86,7 @@
Learn
</a>
<a
class="inline-block font-display text-lg border-2 border-sky-400 py-3 px-8 mx-6 rounded
transition-colors hover:text-black hover:bg-sky-400"
href="/install/"
>
<a class="inline-block font-display text-lg border-2 border-[#5BCEFA] py-3 px-8 mx-6 rounded" href="/install/">
Install
</a>
</div>

View File

@ -15,7 +15,7 @@ Prism.languages.thp = {
pattern: /(["])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
greedy: true,
},
"keyword": /\b(?:static|const|enum|loop|use|break|catch|continue|do|else|finally|for|fun|if|in|fn|nil|return|throw|try|while|type|match|with|of|abstract|class|interface|private|pub|obj|override|open|init|val|var|mut|clone)\b/,
"keyword": /\b(?:static|const|enum|loop|use|break|catch|continue|do|else|finally|for|fun|if|in|fn|nil|return|throw|try|while|type|match|with|of|abstract|class|interface|private|pub|obj|override|open|init|let|mut|clone)\b/,
"number": /\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i,
"operator": /[<>]=?|[!=]=?=?|--?|\$|\+\+?|&&?|\|\|?|[?*/~^%]/,
"punctuation": /[{}[\];(),.]/,

View File

@ -14,11 +14,14 @@
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;500;600;700;800;900&display=swap"
href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;500;600;700;800;900&family=Fugaz+One&family=Inconsolata&family=Inter&display=swap"
rel="stylesheet">
<link
href="https://fonts.googleapis.com/css2?family=Fira+Code&family=Josefin+Sans:ital,wght@0,400;1,700&display=swap"
rel="stylesheet">
<style>
</style>
</head>
<body class="bg-c-bg text-c-text">
@ -41,7 +44,7 @@
<div class="pt-12 max-h-screen border-r border-r-[rgba(91,205,250,0.25)] overflow-x-scroll sticky top-0">
<nav class="rounded-md mt-2 p-4">
<h2 class="text-xl font-display pb-4 opacity-75">On this page</h2>
<h2 class="text-xl font-display pb-4">On this page</h2>
{{sidebar}}
</nav>

View File

@ -22,14 +22,11 @@ module.exports = {
"body": ["'Fira Sans'", "Inter", "sans-serif"],
},
},
corePlugins: {
container: false
},
plugins: [
function ({ addComponents }) {
addComponents({
'.container': {
width: '98%',
maxWidth: '95%',
'@screen sm': {
maxWidth: '640px',
},

View File

@ -17,7 +17,7 @@
@media (prefers-color-scheme: light) {
:root {
--c-bg: rgb(255, 253, 255);
--c-bg: rgb(255, 247, 255);
--c-text: #121212;
--c-purple: #374259;
--c-purple-light: #BA94D1;