Add docs
This commit is contained in:
parent
9fa2d8caa3
commit
253e4bbb48
@ -30,4 +30,5 @@ Only these two:
|
||||
*/
|
||||
```
|
||||
|
||||
TBD: Doc comments use triple slash `///`? or `/** */`?
|
||||
|
||||
|
@ -5,7 +5,10 @@ title: Datatypes
|
||||
|
||||
# Datatypes
|
||||
|
||||
Datatypes's first character are always uppercase.
|
||||
THP requires that all datatypes start their name with an
|
||||
uppercase letter.
|
||||
|
||||
The following are basic datatypes.
|
||||
|
||||
## Int
|
||||
|
||||
@ -13,8 +16,17 @@ Same as php int
|
||||
|
||||
```thp
|
||||
Int age = 32
|
||||
// Hexadecimal numbers start with 0x
|
||||
Int red = 0xff0000
|
||||
// Octal numbers start with 0o
|
||||
Int permissions = 0o775
|
||||
// Binary numbers start with 0b
|
||||
Int char_code = 0b01000110
|
||||
|
||||
// IMPORTANT!
|
||||
// Since Octal starts with `0o`, using just a leading 0
|
||||
// will result in a decimal!
|
||||
Int not_octal = 032 // This is 32, not 26
|
||||
```
|
||||
|
||||
|
||||
@ -25,26 +37,35 @@ Same as php float
|
||||
|
||||
```thp
|
||||
Float pi = 3.141592
|
||||
Float light = 2.99e+8
|
||||
```
|
||||
|
||||
|
||||
## String
|
||||
|
||||
Same as php string
|
||||
THP strings use **only** double quotes. Single quotes are
|
||||
used elsewhere.
|
||||
|
||||
```thp
|
||||
String name = "Rose"
|
||||
```
|
||||
|
||||
|
||||
## Bool
|
||||
|
||||
Same as php bool
|
||||
Strings have interpolation with `{}`.
|
||||
|
||||
```thp
|
||||
Bool is_true = true
|
||||
print("Hello, {name}") // Hello, Rose
|
||||
```
|
||||
|
||||
|
||||
## Bool
|
||||
|
||||
THP booleans are `true` and `false`. They are case sensitive,
|
||||
**only lowercase.**
|
||||
|
||||
```thp
|
||||
Bool is_true = true
|
||||
Bool is_false = false
|
||||
|
||||
// This is a compile error
|
||||
val invalid = TRUE
|
||||
```
|
||||
|
@ -1,10 +1,120 @@
|
||||
---
|
||||
layout: ../../../layouts/PagesLayout.astro
|
||||
title: Install
|
||||
title: Operators
|
||||
---
|
||||
|
||||
# Operators
|
||||
|
||||
Some common operators
|
||||
|
||||
Most of the PHP operators are present in THP.
|
||||
|
||||
## Numbers
|
||||
|
||||
```thp
|
||||
var number = 322
|
||||
|
||||
number + 1
|
||||
number - 1
|
||||
number * 1
|
||||
number / 1
|
||||
number % 2
|
||||
|
||||
number += 1
|
||||
number -= 1
|
||||
number *= 1
|
||||
number /= 1
|
||||
number %= 2
|
||||
```
|
||||
|
||||
**There are no prefix/postfix increment**, use `+=` or `-=`.
|
||||
|
||||
```thp
|
||||
// Use
|
||||
number += 1
|
||||
|
||||
// instead of
|
||||
number++ // This is a compile error
|
||||
```
|
||||
|
||||
### Comparison
|
||||
|
||||
These operators will not do implicit type conversion. They can
|
||||
only be used with same datatypes.
|
||||
|
||||
```thp
|
||||
v1 < v2
|
||||
v1 <= v2
|
||||
v1 > v2
|
||||
v1 >= v2
|
||||
```
|
||||
|
||||
There is only `==` and `!=`. They are equivalent to `===` and `!==`.
|
||||
|
||||
```thp
|
||||
v1 == v2
|
||||
v1 != v2
|
||||
```
|
||||
|
||||
|
||||
### Bitwise
|
||||
|
||||
TBD
|
||||
|
||||
```thp
|
||||
number and 1
|
||||
number or 2
|
||||
number xor 1
|
||||
number xand 1
|
||||
```
|
||||
|
||||
## Strings
|
||||
|
||||
TBD.
|
||||
|
||||
Strings **do not use `.`** for concatenation. They use `+`.
|
||||
|
||||
```thp
|
||||
"Hello " + "world."
|
||||
```
|
||||
|
||||
However, the plus operator `+` does not implicitly convert types.
|
||||
|
||||
```thp
|
||||
"Hello " + 322 // This is an error
|
||||
```
|
||||
|
||||
|
||||
## Boolean
|
||||
|
||||
These operators work **only with booleans**, they do not perform
|
||||
type coercion.
|
||||
|
||||
```thp
|
||||
c1 && c2
|
||||
c1 || c2
|
||||
```
|
||||
|
||||
## Ternary
|
||||
|
||||
There is no ternary operator. See [Conditionals](/learn/flow-control/conditionals) for alternatives.
|
||||
|
||||
|
||||
## Null
|
||||
|
||||
These are detailed in their section: [Nullable types](/learn/error-handling/null)
|
||||
|
||||
```thp
|
||||
val person = some_fun()
|
||||
|
||||
person?.name
|
||||
person?.name ?? "Jane"
|
||||
person?.greet?.()
|
||||
|
||||
if person?
|
||||
{
|
||||
person.name
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -8,6 +8,11 @@ title: Variables
|
||||
|
||||
thp distinguishes between mutable and immutable variables.
|
||||
|
||||
Variables have to be declared in THP, to avoid issues with scopes.
|
||||
It is a compile error to use undeclared variables.
|
||||
|
||||
Variable names **must** begin with a lowercase letter.
|
||||
|
||||
## Immutable variables
|
||||
|
||||
Defined with `val`, followed by a variable name and a value.
|
||||
|
@ -5,6 +5,8 @@ title: Maps
|
||||
|
||||
# Maps
|
||||
|
||||
TBD.
|
||||
|
||||
Also known as Associative Arrays, or Objects in other languages.
|
||||
|
||||
All maps must have a definition, which define their fields and datatypes.
|
||||
|
@ -5,5 +5,19 @@ title: Blocks
|
||||
|
||||
# Blocks
|
||||
|
||||
Blocks :D
|
||||
Blocks of code are denoted with brackets `{}`.
|
||||
|
||||
Blocks can be assigned to variables, in which case
|
||||
they are executed and their last expression is
|
||||
returned.
|
||||
|
||||
```thp
|
||||
val result = {
|
||||
val temp = 161
|
||||
|
||||
temp * 2 // This will be assigned to `result`
|
||||
}
|
||||
|
||||
print(result) // 322
|
||||
```
|
||||
|
||||
|
@ -44,6 +44,8 @@ if variable is Datatype
|
||||
|
||||
## If variable is of enum
|
||||
|
||||
TBD
|
||||
|
||||
```thp
|
||||
val user_id = POST::get("user_id")
|
||||
|
||||
|
@ -7,6 +7,8 @@ title: Loops
|
||||
|
||||
## For loop
|
||||
|
||||
This is simmilar to PHP's `foreach`. There is no equivalent to PHP's `for`.
|
||||
|
||||
Braces are required.
|
||||
|
||||
```thp
|
||||
@ -51,7 +53,7 @@ for value in collection
|
||||
|
||||
```thp
|
||||
val colors = ["red", "green", "blue"]
|
||||
val mut index = 0
|
||||
var index = 0
|
||||
|
||||
while index < colors.size()
|
||||
{
|
||||
|
@ -5,6 +5,8 @@ title: Declaration
|
||||
|
||||
# Declaration
|
||||
|
||||
Function names **must** begin with a lowercase letter.
|
||||
|
||||
|
||||
## No parameters, no return
|
||||
|
||||
|
@ -11,13 +11,14 @@ pagesLayout:
|
||||
- path: variables
|
||||
- path: datatypes
|
||||
- path: comments
|
||||
- path: operators
|
||||
- path: flow-control
|
||||
title: Flow control
|
||||
children:
|
||||
- path: blocks
|
||||
- path: conditionals
|
||||
- path: loops
|
||||
- path: match
|
||||
- path: blocks
|
||||
- path: data-structures
|
||||
title: Data structures
|
||||
children:
|
||||
|
Loading…
Reference in New Issue
Block a user