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

View File

@ -4,23 +4,23 @@ thp distinguishes between mutable and immutable variables.
## 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 ```thp
val surname = "Doe" let surname = "Doe"
val year_of_birth = 1984 let year_of_birth = 1984
``` ```
### Datatype annotation ### Datatype annotation
Written after the `val` keyword but before the variable name. Written after the `let` keyword but before the variable name.
```thp ```thp
val String surname = "Doe" let String surname = "Doe"
val Int year_of_birth = 1984 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 ```thp
// Equivalent to the previous code // Equivalent to the previous code
@ -34,28 +34,28 @@ This means that if a variable has only a datatype, it is immutable.
## Mutable variables ## 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 ```thp
var name = "John" let mut name = "John"
var age = 32 let mut age = 32
``` ```
### Datatype annotation ### Datatype annotation
Written after the `var` keywords but before the variable name. Written after the `let mut` keywords but before the variable name.
```thp ```thp
var String name = "John" let mut String name = "John"
var Int age = 32 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 ```thp
// Equivalent to the previous code // Equivalent to the previous code
var String name = "John" mut String name = "John"
var Int age = 32 mut Int age = 32
``` ```

View File

@ -8,7 +8,7 @@ Basically kotlin syntax.
`new` not required, in fact, forbidden. `new` not required, in fact, forbidden.
```thp ```thp
val dog = Dog() let dog = Dog()
``` ```
## Simple class ## Simple class
@ -18,7 +18,7 @@ Why'd you do this tho?
```thp ```thp
class SimpleClass class SimpleClass
val instance = SimpleClass() let instance = SimpleClass()
``` ```
## Properties & methods ## Properties & methods
@ -27,10 +27,10 @@ val instance = SimpleClass()
class SimpleClass class SimpleClass
{ {
// Properties are private by default // Properties are private by default
var String? name = ... mut String? name = ...
// Made public with `pub` // Made public with `pub`
pub var String? surname = ... pub mut String? surname = ...
// Methods are private by default // Methods are private by default
fun display_name() fun display_name()
@ -58,9 +58,10 @@ Kotlin style
```thp ```thp
class Cat( class Cat(
var String name, // If a parameter has pub, protected or private they are promoted to properties
var Int lives = 9, private String name,
val String surname = "Doe", pub mut Int lives = 9,
protected String surname = "Doe",
) )
{ {
pub fun get_name() -> String pub fun get_name() -> String
@ -89,13 +90,14 @@ print(michifu.get_name())
With kotlin's `init` block. With kotlin's `init` block.
```thp ```thp
class Dog(val String name) class Dog(pub String name)
{ {
Int name_length = name.length() Int name_length
init init
{ {
print("Dog has been instantiated") print("Dog has been instantiated")
$name_length = name.length()
} }
} }
``` ```
@ -105,7 +107,7 @@ class Dog(val String name)
Kotlin style Kotlin style
```thp ```thp
class Animal(var String name) class Animal(pub String name)
{ {
pub fun say_name() pub fun say_name()
{ {
@ -118,35 +120,6 @@ class Cat(String name, Int lives) -> Animal(name)
Cat("Michi", 9).say_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 ```thp
val option = Some("GAAA") let option = Some("GAAA")
val Some(value) = option 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 ## Usage
```thp ```thp
val fruits = ["apple", "banana", "cherry"] let fruits = ["apple", "banana", "cherry"]
val apple = fruits[0] let apple = fruits[0]
print(apple) print(apple)
val mut numbers = [0, 1, 2, 3] let mut numbers = [0, 1, 2, 3]
numbers[3] = 5 numbers[3] = 5

View File

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

View File

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

View File

@ -6,9 +6,9 @@ calls (`()`).
## Definition ## Definition
```thp ```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 ## If variable is of enum
```thp ```thp
val user_id = POST::get("user_id") let user_id = POST::get("user_id")
if Some(user_id) = user_id if Some(user_id) = user_id
{ {

View File

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

View File

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

View File

@ -21,7 +21,7 @@ fun get_random_number() -> Int
Random::get(0, 35_222) Random::get(0, 35_222)
} }
val number = get_random_number() let number = get_random_number()
``` ```
## With parameters and return type ## With parameters and return type
@ -32,7 +32,7 @@ fun get_secure_random_number(Int min, Int max) -> Int
Random::get_secure(min, max) 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] 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 // 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}!") 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 let generator = generate_generator() // A function
val value = generate_generator()() // An Int let value = generate_generator()() // An Int
``` ```

View File

@ -22,9 +22,9 @@ By default closures **always** capture variables as **references**.
```thp ```thp
var x = 20 let mut x = 20
val f = fun() { let f = fun() {
print(x) print(x)
} }
@ -44,9 +44,9 @@ fun(parameters) clone(variables) {
``` ```
```thp ```thp
var x = 20 let mut x = 20
val f = fun() clone(x) { let f = fun() clone(x) {
print(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 When using a regular type as a parameter, only it's immutable
properties can be used properties can be used inside the function
```thp ```thp
fun count(Array[Int] numbers) -> Int { 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 items_count
} }
``` ```
To use immutable properties you must use a mutable reference.
## Mutable reference ## Mutable reference
@ -38,7 +36,7 @@ data **can** be mutated.
The caller *must* also use `mut`. The caller *must* also use `mut`.
```thp ```thp
val numbers = Array(0, 1, 2, 3) let numbers = Array(0, 1, 2, 3)
push_25(mut numbers) // Pass `numbers` as reference. push_25(mut numbers) // Pass `numbers` as reference.
@ -60,7 +58,7 @@ of the parameter (CoW). The original data will **not** be mutated.
```thp ```thp
val numbers = Array(1, 2, 3, 4) let numbers = Array(1, 2, 3, 4)
add_25(clone numbers) // Pass `numbers` as clone. 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 ## 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. that isn't a primitive type.
- Generics & ADTs - Avoid automatic type conversion.
- Avoid implicit type conversion.
- Remove the inconsistencies in the language. - Remove the inconsistencies in the language.
- Organize the stdlib into modules. - Organize the stdlib.
- Differentiate between Arrays, Tuples, Maps and Sets. - Differentiate between Arrays, Tuples, Maps and Sets.
- Create a **consistent** language. - 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). - 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. - Support in-place compilation.
- (Try to) emit readable PHP. - Emit readable PHP code.
- Implement an LSP server. - Implement a LSP server.
- Implement a formatter.
## Not goals ## Not goals
@ -56,7 +55,7 @@ These are **not** aspects that THP looks to solve or implement.
$has_key = str_contains($haystack, 'needle'); $has_key = str_contains($haystack, 'needle');
// THP // THP
val has_key = haystack.contains("needle") let has_key = haystack.contains("needle")
``` ```
- Explicit variable declaration - Explicit variable declaration
@ -84,7 +83,7 @@ Obj {
``` ```
- Tuples, Arrays, Sets, Maps are clearly different - 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(); $cat->meow();
// THP // THP
val cat = Cat("Michifu", 7) let cat = Cat("Michifu", 7)
cat.meow(); cat.meow();
``` ```
@ -134,7 +133,7 @@ For example:
```thp ```thp
// This expression // This expression
val greeting = let greeting =
match get_person() match get_person()
| Some(person) if person.age > 18 | Some(person) if person.age > 18
{ {
@ -177,7 +176,7 @@ enum IpAddress {
V6(String), V6(String),
} }
val ip_1 = IpAddress::V4("255.255.0.0") let ip_1 = IpAddress::V4("255.255.0.0")
// Would possibly compile to: // Would possibly compile to:

View File

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

View File

@ -15,7 +15,7 @@ Prism.languages.thp = {
pattern: /(["])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/, pattern: /(["])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
greedy: true, 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, "number": /\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i,
"operator": /[<>]=?|[!=]=?=?|--?|\$|\+\+?|&&?|\|\|?|[?*/~^%]/, "operator": /[<>]=?|[!=]=?=?|--?|\$|\+\+?|&&?|\|\|?|[?*/~^%]/,
"punctuation": /[{}[\];(),.]/, "punctuation": /[{}[\];(),.]/,

View File

@ -14,11 +14,14 @@
<link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link <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"> rel="stylesheet">
<link <link
href="https://fonts.googleapis.com/css2?family=Fira+Code&family=Josefin+Sans:ital,wght@0,400;1,700&display=swap" href="https://fonts.googleapis.com/css2?family=Fira+Code&family=Josefin+Sans:ital,wght@0,400;1,700&display=swap"
rel="stylesheet"> rel="stylesheet">
<style>
</style>
</head> </head>
<body class="bg-c-bg text-c-text"> <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"> <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"> <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}} {{sidebar}}
</nav> </nav>

View File

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

View File

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