From 49faed4fcb98bf807cab3d171925cac54daf75e9 Mon Sep 17 00:00:00 2001 From: Araozu Date: Fri, 5 Jul 2024 16:58:50 -0500 Subject: [PATCH] Move code snippets to use new component --- src/components/Code.astro | 3 +- src/layouts/ApiLayout.astro | 8 +- src/layouts/PagesLayout.astro | 17 ++++- src/layouts/thpHighlighter.ts | 15 +++- .../basics/{comments.md => comments.mdx} | 13 ++-- .../basics/{datatypes.md => datatypes.mdx} | 24 +++--- src/pages/learn/basics/hello-world.mdx | 9 ++- .../basics/{operators.md => operators.mdx} | 39 +++++----- .../basics/{variables.md => variables.mdx} | 26 +++---- .../classes/{anonymous.md => anonymous.mdx} | 10 ++- .../classes/{definition.md => definition.mdx} | 73 ++++++++++--------- .../classes/{interfaces.md => interfaces.mdx} | 5 +- .../learn/classes/{magic.md => magic.mdx} | 9 ++- .../learn/classes/{static.md => static.mdx} | 13 ++-- .../data-structures/{arrays.md => arrays.mdx} | 9 ++- .../data-structures/{enums.md => enums.mdx} | 9 ++- .../data-structures/{maps.md => maps.mdx} | 37 +++++----- .../data-structures/{tuples.md => tuples.mdx} | 10 ++- .../data-structures/{unions.md => unions.mdx} | 9 ++- .../error-handling/{null.md => null.mdx} | 31 ++++---- src/pages/learn/error-handling/try.mdx | 27 +++---- .../flow-control/{blocks.md => blocks.mdx} | 7 +- .../{conditionals.md => conditionals.mdx} | 13 ++-- .../flow-control/{loops.md => loops.mdx} | 27 +++---- .../flow-control/{match.md => match.mdx} | 5 +- .../{declaration.md => declaration.mdx} | 29 ++++---- .../{higher-order.md => higher-order.mdx} | 9 ++- .../functions/{lambdas.md => lambdas.mdx} | 21 +++--- .../{parameters.md => parameters.mdx} | 38 +++++----- src/pages/learn/index.mdx | 21 +++--- src/pages/learn/templating/intro.mdx | 4 +- 31 files changed, 301 insertions(+), 269 deletions(-) rename src/pages/learn/basics/{comments.md => comments.mdx} (84%) rename src/pages/learn/basics/{datatypes.md => datatypes.mdx} (80%) rename src/pages/learn/basics/{operators.md => operators.mdx} (81%) rename src/pages/learn/basics/{variables.md => variables.mdx} (88%) rename src/pages/learn/classes/{anonymous.md => anonymous.mdx} (82%) rename src/pages/learn/classes/{definition.md => definition.mdx} (87%) rename src/pages/learn/classes/{interfaces.md => interfaces.mdx} (81%) rename src/pages/learn/classes/{magic.md => magic.mdx} (76%) rename src/pages/learn/classes/{static.md => static.mdx} (82%) rename src/pages/learn/data-structures/{arrays.md => arrays.mdx} (83%) rename src/pages/learn/data-structures/{enums.md => enums.mdx} (89%) rename src/pages/learn/data-structures/{maps.md => maps.mdx} (90%) rename src/pages/learn/data-structures/{tuples.md => tuples.mdx} (74%) rename src/pages/learn/data-structures/{unions.md => unions.mdx} (91%) rename src/pages/learn/error-handling/{null.md => null.mdx} (85%) rename src/pages/learn/flow-control/{blocks.md => blocks.mdx} (69%) rename src/pages/learn/flow-control/{conditionals.md => conditionals.mdx} (86%) rename src/pages/learn/flow-control/{loops.md => loops.mdx} (84%) rename src/pages/learn/flow-control/{match.md => match.mdx} (89%) rename src/pages/learn/functions/{declaration.md => declaration.mdx} (85%) rename src/pages/learn/functions/{higher-order.md => higher-order.mdx} (82%) rename src/pages/learn/functions/{lambdas.md => lambdas.mdx} (84%) rename src/pages/learn/functions/{parameters.md => parameters.mdx} (69%) diff --git a/src/components/Code.astro b/src/components/Code.astro index eb3c1c1..0be5525 100644 --- a/src/components/Code.astro +++ b/src/components/Code.astro @@ -7,5 +7,4 @@ const html_code = thp_highlighter(leftTrimDedent(thpcode).join("\n")); ---
thp
+ class="language-thp">thp diff --git a/src/layouts/ApiLayout.astro b/src/layouts/ApiLayout.astro index 60472f4..bd1346f 100644 --- a/src/layouts/ApiLayout.astro +++ b/src/layouts/ApiLayout.astro @@ -3,8 +3,7 @@ import Navbar from "../components/Navbar.astro"; import BaseLayout from "./BaseLayout.astro"; import TOC from "../components/TOC.astro"; -const {headings} = Astro.props; - +const { headings } = Astro.props; --- @@ -41,9 +40,4 @@ const {headings} = Astro.props;
- -
diff --git a/src/layouts/PagesLayout.astro b/src/layouts/PagesLayout.astro index 78acc5b..bdfeae3 100644 --- a/src/layouts/PagesLayout.astro +++ b/src/layouts/PagesLayout.astro @@ -4,7 +4,13 @@ import BaseLayout from "./BaseLayout.astro"; import TOC from "../components/TOC.astro"; import Sidebar from "../components/Sidebar.astro"; -const { frontmatter, headings, posts: _posts, indexSubpath, basePath } = Astro.props; +const { + frontmatter, + headings, + posts: _posts, + indexSubpath, + basePath, +} = Astro.props; const posts: Record[] = _posts; const indexPage = posts.find((post) => post.file.endsWith(indexSubpath)); @@ -30,8 +36,10 @@ if (pagesIndex === undefined) { function validateEntry(entry: PageEntry, basePath: string) { if (!entry.children) { // Attempt to get the page title from frontmatter - const pageData = posts.find((post) => - post.file.endsWith(entry.path + ".md") || post.file.endsWith(entry.path + ".mdx"), + const pageData = posts.find( + (post) => + post.file.endsWith(entry.path + ".md") || + post.file.endsWith(entry.path + ".mdx"), ); if (pageData === undefined) { @@ -112,7 +120,8 @@ for (const entry of pagesIndex) { document.addEventListener("DOMContentLoaded", () => { let current_uri = window.location.pathname; - const sidebar = document.getElementById("sidebar")!.children[0]! as HTMLElement; + const sidebar = document.getElementById("sidebar")! + .children[0]! as HTMLElement; const links = sidebar.querySelectorAll("a"); for (const link of [...links]) { if (link.getAttribute("href") === current_uri) { diff --git a/src/layouts/thpHighlighter.ts b/src/layouts/thpHighlighter.ts index 56907cb..d03e725 100644 --- a/src/layouts/thpHighlighter.ts +++ b/src/layouts/thpHighlighter.ts @@ -1,4 +1,15 @@ -import { thp_highlighter, CodeJar } from "../lexer/highlighter"; -export function highlightOnDom() {} +export function highlightOnDom() { + const pre_elements = document.querySelectorAll("pre"); + for (const pre_el of pre_elements) { + const language = pre_el.getAttribute("data-language"); + if (language === null) { continue; } + + // Create a visual indicador + const indicator = document.createElement("span"); + indicator.className = "absolute top-2 right-2 inline-block text-sm select-none opacity-75"; + indicator.innerText = language; + pre_el.appendChild(indicator); + } +} diff --git a/src/pages/learn/basics/comments.md b/src/pages/learn/basics/comments.mdx similarity index 84% rename from src/pages/learn/basics/comments.md rename to src/pages/learn/basics/comments.mdx index 65f7dc8..897f69a 100644 --- a/src/pages/learn/basics/comments.md +++ b/src/pages/learn/basics/comments.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Comments --- +import Code from "../../../components/Code.astro" # Comments @@ -12,12 +13,12 @@ THP supports single and multi line comments: Begin with double slash `//` and continue until the end of the line. -```thp + ## Multi line @@ -26,19 +27,19 @@ These begin with `/*` and end with `*/`. Everything in between is ignored. Multi line comments can be nested in THP. -```thp + -```thp + ## Documentation comments diff --git a/src/pages/learn/basics/datatypes.md b/src/pages/learn/basics/datatypes.mdx similarity index 80% rename from src/pages/learn/basics/datatypes.md rename to src/pages/learn/basics/datatypes.mdx index 4837e36..403444e 100644 --- a/src/pages/learn/basics/datatypes.md +++ b/src/pages/learn/basics/datatypes.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Datatypes --- +import Code from "../../../components/Code.astro" # Datatypes @@ -14,7 +15,7 @@ The following are basic datatypes. Same as php int -```thp + ## Float @@ -35,10 +36,10 @@ Int not_octal = 032 // This is 32, not 26 Same as php float -```thp + ## String @@ -46,15 +47,15 @@ Float light = 2.99e+8 THP strings use **only** double quotes. Single quotes are used elsewhere. -```thp + Strings have interpolation with `{}`. -```thp + ## Bool @@ -62,10 +63,11 @@ print("Hello, {name}") // Hello, Rose THP booleans are `true` and `false`. They are case sensitive, **only lowercase.** -```thp + + diff --git a/src/pages/learn/basics/hello-world.mdx b/src/pages/learn/basics/hello-world.mdx index 6201166..55107b3 100644 --- a/src/pages/learn/basics/hello-world.mdx +++ b/src/pages/learn/basics/hello-world.mdx @@ -3,6 +3,7 @@ layout: ../../../layouts/DocsLayout.astro title: Hello world --- import InteractiveCode from "../../../components/InteractiveCode.astro"; +import Code from "../../../components/Code.astro" # Hello, world! @@ -16,9 +17,9 @@ detailed later on. To write a hello world program write the following code in a file: -```thp + Then run `thp hello.thp` from your terminal. @@ -34,11 +35,11 @@ echo("B"); echo("C"); ``` -```thp + As a consequence of this, there can only be 1 statement per line. diff --git a/src/pages/learn/basics/operators.md b/src/pages/learn/basics/operators.mdx similarity index 81% rename from src/pages/learn/basics/operators.md rename to src/pages/learn/basics/operators.mdx index e17c88f..5fc8cbb 100644 --- a/src/pages/learn/basics/operators.md +++ b/src/pages/learn/basics/operators.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Operators --- +import Code from "../../../components/Code.astro" # Operators @@ -10,7 +11,7 @@ Most of the PHP operators are present in THP. ## Numbers -```thp + **There are no prefix/postfix increment**, use `+=` or `-=`. -```thp + ### Comparison These operators will not do implicit type conversion. They can only be used with same datatypes. -```thp + v2 v1 >= v2 -``` +`} /> There is only `==` and `!=`. They are equivalent to `===` and `!==`. -```thp + ### Bitwise TBD -```thp + ## Strings @@ -73,15 +74,15 @@ TBD. Strings **do not use `.`** for concatenation. They use `+`. -```thp + However, the plus operator `+` does not implicitly convert types. -```thp + ## Boolean @@ -89,10 +90,10 @@ However, the plus operator `+` does not implicitly convert types. These operators work **only with booleans**, they do not perform type coercion. -```thp + ## Ternary @@ -103,7 +104,7 @@ There is no ternary operator. See [Conditionals](/learn/flow-control/conditional These are detailed in their section: [Nullable types](/learn/error-handling/null) -```thp + diff --git a/src/pages/learn/basics/variables.md b/src/pages/learn/basics/variables.mdx similarity index 88% rename from src/pages/learn/basics/variables.md rename to src/pages/learn/basics/variables.mdx index 8953f42..21d6627 100644 --- a/src/pages/learn/basics/variables.md +++ b/src/pages/learn/basics/variables.mdx @@ -3,6 +3,7 @@ layout: ../../../layouts/DocsLayout.astro title: Variables --- +import Code from "../../../components/Code.astro" # Variables @@ -23,27 +24,27 @@ As a regex: `[a-z_][a-zA-Z0-9_]*` Defined with `val`, followed by a variable name and a value. -```thp + ### Datatype annotation Written after the `val` keyword but before the variable name. -```thp + When annotating an immutable variable the `val` keyword is optional -```thp + This means that if a variable only has a datatype, it is immutable. @@ -53,28 +54,27 @@ This means that if a variable only has a datatype, it is immutable. Defined with `var`, followed by a variable name and a value. -```thp + ### Datatype annotation Written after the `var` keywords but before the variable name. -```thp + When annotating a mutable variable the keyword `var` is still **required**. -```thp + diff --git a/src/pages/learn/classes/anonymous.md b/src/pages/learn/classes/anonymous.mdx similarity index 82% rename from src/pages/learn/classes/anonymous.md rename to src/pages/learn/classes/anonymous.mdx index e722aa7..1a2cf7b 100644 --- a/src/pages/learn/classes/anonymous.md +++ b/src/pages/learn/classes/anonymous.mdx @@ -2,10 +2,12 @@ layout: ../../../layouts/DocsLayout.astro title: Anonymous classes --- +import Code from "../../../components/Code.astro" + # Anonymous classes -```thp + -```thp + SomeClass(param1), SomeInterface { pub fun method() @@ -35,4 +37,4 @@ setLogger(class(Int param1) -> SomeClass(param1), SomeInterface // code } }) -``` +`} /> diff --git a/src/pages/learn/classes/definition.md b/src/pages/learn/classes/definition.mdx similarity index 87% rename from src/pages/learn/classes/definition.md rename to src/pages/learn/classes/definition.mdx index 5b95c86..23afc7d 100644 --- a/src/pages/learn/classes/definition.md +++ b/src/pages/learn/classes/definition.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Classes --- +import Code from "../../../components/Code.astro" # Classes @@ -13,26 +14,26 @@ Syntax and semantics heavily inspired by Kotlin. To create an instance of a class call it as if it were a function, without `new`. -```thp + ## Simple class Classes are declared with the `class` keyword. -```thp + ## Properties Properties are declared with `var`/`val`. They **must** declare their datatype. -```thp + Properties are private by default, but can be made public with `pub`. -```thp + More information about how properties interact with the constructor is found in the contructor section. @@ -66,7 +67,7 @@ is found in the contructor section. Methods are declared with `fun`, as regular functions. -```thp + Methods are private by default, and are made public with `pub`. -```thp + ## This THP uses the dollar sign `$` as this. It is **required** when using a class property/method. -```thp + ## Static members @@ -134,17 +135,17 @@ Static members are detailed in their own page. PHP only allows a single constructor, and so does THP. The basic constructor has the syntax of function parameters. -```thp + The class properties can be declared in the constructor, using the keywords `pub`, `var`, `val`: -```thp + By using this syntax you are declaring properties and assigning them at the same time. @@ -173,14 +174,14 @@ The contructor parameters can also have default values. The constructor is public by default. It can be made private/protected like this: -```thp + ### Derived properties @@ -188,19 +189,19 @@ private constructor( You can declare properties whose values depend on values on the constructor. -```thp + ### Init block @@ -208,7 +209,7 @@ print(a2.name_length) //: 3 If you need to additional logic in the constructor you can use a `init` block. -```thp + ## Inheritance -```thp + ## Mutable methods By default methods cannot mutate the state of the object. -```thp + To do so the method must be annotated. The caller must also declare a mutable variable. -```thp + ## Class constructor that may return an error Working theory: -```thp + diff --git a/src/pages/learn/classes/interfaces.md b/src/pages/learn/classes/interfaces.mdx similarity index 81% rename from src/pages/learn/classes/interfaces.md rename to src/pages/learn/classes/interfaces.mdx index 235a710..8c2e3c9 100644 --- a/src/pages/learn/classes/interfaces.md +++ b/src/pages/learn/classes/interfaces.mdx @@ -2,12 +2,13 @@ layout: ../../../layouts/DocsLayout.astro title: Interfaces --- +import Code from "../../../components/Code.astro" # Interfaces -```thp + Serializable } } -``` +`} /> No interface inheritance. diff --git a/src/pages/learn/classes/magic.md b/src/pages/learn/classes/magic.mdx similarity index 76% rename from src/pages/learn/classes/magic.md rename to src/pages/learn/classes/magic.mdx index 1d9a7da..6279067 100644 --- a/src/pages/learn/classes/magic.md +++ b/src/pages/learn/classes/magic.mdx @@ -2,12 +2,13 @@ layout: ../../../layouts/DocsLayout.astro title: Magic methods --- +import Code from "../../../components/Code.astro" # Magic methods Don't get special treatment. -```thp + -```thp + diff --git a/src/pages/learn/classes/static.md b/src/pages/learn/classes/static.mdx similarity index 82% rename from src/pages/learn/classes/static.md rename to src/pages/learn/classes/static.mdx index 3509318..aa20eb8 100644 --- a/src/pages/learn/classes/static.md +++ b/src/pages/learn/classes/static.mdx @@ -2,13 +2,14 @@ layout: ../../../layouts/DocsLayout.astro title: Static --- +import Code from "../../../components/Code.astro" # Static in classes ## Class constants -```thp + ## Static methods @@ -28,7 +29,7 @@ print(Cat::CONSTANT) aka. plain, old functions -```thp + Int @@ -38,7 +39,7 @@ static Cat } Cat::static_method() -``` +`} /> ## Static properties @@ -46,7 +47,7 @@ Cat::static_method() aka. global variables -```thp + diff --git a/src/pages/learn/data-structures/arrays.md b/src/pages/learn/data-structures/arrays.mdx similarity index 83% rename from src/pages/learn/data-structures/arrays.md rename to src/pages/learn/data-structures/arrays.mdx index c7e39d5..fb21e00 100644 --- a/src/pages/learn/data-structures/arrays.md +++ b/src/pages/learn/data-structures/arrays.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Arrays --- +import Code from "../../../components/Code.astro" # Arrays @@ -9,7 +10,7 @@ Use square brackets as usual. ## Usage -```thp + ## Type signature -```thp + The Array signature __requires__ the word `Array`. There is no `Int[]` or `[Int]` signature, since that would cause diff --git a/src/pages/learn/data-structures/enums.md b/src/pages/learn/data-structures/enums.mdx similarity index 89% rename from src/pages/learn/data-structures/enums.md rename to src/pages/learn/data-structures/enums.mdx index 7a26df8..e6cba14 100644 --- a/src/pages/learn/data-structures/enums.md +++ b/src/pages/learn/data-structures/enums.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Enums --- +import Code from "../../../components/Code.astro" # Enums @@ -15,7 +16,7 @@ THP enums are a 1 to 1 map of PHP enums, with a slightly different syntax. Enums don't have a scalar value by default. -```thp + ## Backed enums Backed enums can have a scalar for each case. The scalar values can only be `String` or `Int`. -```thp + All cases must explicitly define their value, there is no automatic generation. diff --git a/src/pages/learn/data-structures/maps.md b/src/pages/learn/data-structures/maps.mdx similarity index 90% rename from src/pages/learn/data-structures/maps.md rename to src/pages/learn/data-structures/maps.mdx index e0f4271..d48067b 100644 --- a/src/pages/learn/data-structures/maps.md +++ b/src/pages/learn/data-structures/maps.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Maps --- +import Code from "../../../components/Code.astro" # Maps @@ -14,7 +15,7 @@ There can also be anonymous maps, which may contain any key. ## Named Maps -```thp + To access the fields of a map we use square braces `[]`. -```thp + Or dot access `.` if the field's name is a valid identifier. -```thp + ## Anonymous maps @@ -58,23 +59,23 @@ print(mary_jane.name) An anonymous map allows us to store and retrieve any key of any datatype. They are declared as `Map`. -```thp + Anonymous maps can also can have their type omitted. -```thp + If the compiler encounters a map without a type (that is, `.{}`) and doesn't expect a specific type, it will assume it is an @@ -82,30 +83,30 @@ anonymous map. We can freely assign fields to an anonymous map: -```thp + However, if we try to access a field of an anonymous map we'll get a nullable type, and we must annotate it. -```thp + Instead, we can use the `get` function of the map, which expects a datatype and returns that type as nullable -```thp + Both ways to get a value will check that the key exists in the map, and that it has the correct datatype. If either the key doesn't exist @@ -113,10 +114,10 @@ or it has a different datatype, it will return `null`. We can also use dynamic keys, following the same rules: -```thp + diff --git a/src/pages/learn/data-structures/tuples.md b/src/pages/learn/data-structures/tuples.mdx similarity index 74% rename from src/pages/learn/data-structures/tuples.md rename to src/pages/learn/data-structures/tuples.mdx index d43ecca..a93701a 100644 --- a/src/pages/learn/data-structures/tuples.md +++ b/src/pages/learn/data-structures/tuples.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Tuples --- +import Code from "../../../components/Code.astro" # Tuples @@ -9,18 +10,19 @@ Uses `#()` just to avoid confusion with function calls and grouping (`()`). ## Definition -```thp + + ## Signature -```thp + diff --git a/src/pages/learn/data-structures/unions.md b/src/pages/learn/data-structures/unions.mdx similarity index 91% rename from src/pages/learn/data-structures/unions.md rename to src/pages/learn/data-structures/unions.mdx index b6f3b25..4a661df 100644 --- a/src/pages/learn/data-structures/unions.md +++ b/src/pages/learn/data-structures/unions.mdx @@ -2,12 +2,13 @@ layout: ../../../layouts/DocsLayout.astro title: Tagged unions --- +import Code from "../../../components/Code.astro" # Tagged unions Tagged unions can hold a value from a fixed selection of types. -```thp + ## Pattern matching -```thp + ## Internal representation diff --git a/src/pages/learn/error-handling/null.md b/src/pages/learn/error-handling/null.mdx similarity index 85% rename from src/pages/learn/error-handling/null.md rename to src/pages/learn/error-handling/null.mdx index 1e4f54b..d6d5035 100644 --- a/src/pages/learn/error-handling/null.md +++ b/src/pages/learn/error-handling/null.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Nullable types --- +import Code from "../../../components/Code.astro" # Nullable types @@ -12,18 +13,18 @@ by the question mark `?` character. For instance, a POST request may have a `username` parameter, or it may not. This can be represented with an `String?`. -```thp + When we have a `Type?` we cannot use it directly. We must first check if the value is null, and then use it. -```thp + We must check explicitly that the value is not null. Doing `if new_username {}` alone is not allowed. @@ -32,30 +33,30 @@ We must check explicitly that the value is not null. Doing To create a nullable type we must explicitly annotate the type. -```thp + Other examples: -```thp + String? {} val result = get_first([]) -``` +`} /> ## Optional chaining If you have a `Type?` and you wish to access a field of `Type` if it exists, you can use the optional chaining operator. -```thp + - If `person` is null, `person?.name` will return `null` - If `person` is not null, `person?.name` will return `name` @@ -65,12 +66,12 @@ val name = person?.name The Elvis operator `??` is used to give a default value in case a `null` is found. -```thp + Int? {...} val test_score = get_score() ?? 0 -``` +`} /> For the above code: @@ -79,9 +80,9 @@ For the above code: You can use the Elvis operator to return early -```thp + diff --git a/src/pages/learn/error-handling/try.mdx b/src/pages/learn/error-handling/try.mdx index 0c2ec84..c09dbd3 100644 --- a/src/pages/learn/error-handling/try.mdx +++ b/src/pages/learn/error-handling/try.mdx @@ -3,6 +3,7 @@ layout: ../../../layouts/DocsLayout.astro title: Try/Exceptions --- import InteractiveCode from "../../../components/InteractiveCode.astro"; +import Code from "../../../components/Code.astro" # Try/exceptions @@ -17,7 +18,7 @@ is used. For example, a function that returned a `DivisionByZero` may be written like this: -```thp + Int!DivisionByZero { if number == 0 @@ -27,7 +28,7 @@ fun invert(Int number) -> Int!DivisionByZero return 1 / number } -``` +`} /> In the previous segment, `Int!DivisionByZero` denotates that the function may return either an `Int` or an `DivisionByZero`. @@ -44,12 +45,12 @@ TODO: fix? If there are multiple error types that the function can return, you can use the `|` operator: -```thp + Int!Exceptions { /* ... */} -``` +`} /> @@ -132,14 +133,14 @@ otherwise will assign the success value and continue. Try/return will run a function and assign its value if `Ok` is found. Otherwise, it will return a new value specified by the programmer. -```thp + Int { val result = try dangerous() return 0 // ... } -``` +`} /> In the previous example: @@ -226,27 +227,27 @@ Either way, the function will continue executing. Try/catch allows the error to be manually used & handled. -```thp + A try/catch may have many `catch` clauses: -```thp + diff --git a/src/pages/learn/flow-control/blocks.md b/src/pages/learn/flow-control/blocks.mdx similarity index 69% rename from src/pages/learn/flow-control/blocks.md rename to src/pages/learn/flow-control/blocks.mdx index 1aa9737..95dd5db 100644 --- a/src/pages/learn/flow-control/blocks.md +++ b/src/pages/learn/flow-control/blocks.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Blocks --- +import Code from "../../../components/Code.astro" # Blocks @@ -11,13 +12,13 @@ Blocks can be assigned to variables, in which case they are executed and their last expression is returned. -```thp + diff --git a/src/pages/learn/flow-control/conditionals.md b/src/pages/learn/flow-control/conditionals.mdx similarity index 86% rename from src/pages/learn/flow-control/conditionals.md rename to src/pages/learn/flow-control/conditionals.mdx index 3fabea4..b1cdce5 100644 --- a/src/pages/learn/flow-control/conditionals.md +++ b/src/pages/learn/flow-control/conditionals.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Conditionals --- +import Code from "../../../components/Code.astro" # Conditionals @@ -12,7 +13,7 @@ title: Conditionals - There's no ternary operator -```thp + ## Check for datatypes -```thp + ## If variable is of enum TBD -```thp + 0 { print("user_id is greater than 0: {user_id}") } -``` +`} /> diff --git a/src/pages/learn/flow-control/loops.md b/src/pages/learn/flow-control/loops.mdx similarity index 84% rename from src/pages/learn/flow-control/loops.md rename to src/pages/learn/flow-control/loops.mdx index 34d6eb3..3dd53a7 100644 --- a/src/pages/learn/flow-control/loops.md +++ b/src/pages/learn/flow-control/loops.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Loops --- +import Code from "../../../components/Code.astro" # Loops @@ -11,16 +12,16 @@ This is simmilar to PHP's `foreach`. There is no equivalent to PHP's `for`. Braces are required. -```thp + -```thp + {value}") } -``` +`} /> ## While loop -```thp + ## Infinite loop Instead of doing `while true {}` use `loop`. -```thp + ## Labelled loops You can give labels to loops, allowing you to `break` and `continue` in nested loops. -```thp + diff --git a/src/pages/learn/flow-control/match.md b/src/pages/learn/flow-control/match.mdx similarity index 89% rename from src/pages/learn/flow-control/match.md rename to src/pages/learn/flow-control/match.mdx index 6419f81..de79cd5 100644 --- a/src/pages/learn/flow-control/match.md +++ b/src/pages/learn/flow-control/match.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Match --- +import Code from "../../../components/Code.astro" # Match @@ -9,7 +10,7 @@ title: Match Braces are **required**. -```thp + diff --git a/src/pages/learn/functions/declaration.md b/src/pages/learn/functions/declaration.mdx similarity index 85% rename from src/pages/learn/functions/declaration.md rename to src/pages/learn/functions/declaration.mdx index ba2c1ae..1cab1c6 100644 --- a/src/pages/learn/functions/declaration.md +++ b/src/pages/learn/functions/declaration.mdx @@ -2,6 +2,7 @@ layout: ../../../layouts/DocsLayout.astro title: Declaration --- +import Code from "../../../components/Code.astro" # Declaration @@ -10,42 +11,42 @@ Function names **must** begin with a lowercase letter. ## No parameters, no return -```thp + ## With return type -```thp + Int { Random::get(0, 35_222) } val number = get_random_number() -``` +`} /> ## With parameters and return type -```thp + Int { Random::get_secure(min, max) } val number = get_secure_random_number(0, 65535) -``` +`} /> ## Generic types -```thp + T { array[0] @@ -55,23 +56,23 @@ val first = get_first_item[Int](numbers) // The type annotation is optional if the compiler can infer the type val first = get_first_item(numbers) -``` +`} /> ## Signature -```thp + () () -> Int (Int, Int) -> Int [T](Array[T]) -> T -``` +`} /> ## Named arguments -```thp + TBD: If & how named arguments affect the order of the parameters ## Named arguments with different names -```thp + diff --git a/src/pages/learn/functions/higher-order.md b/src/pages/learn/functions/higher-order.mdx similarity index 82% rename from src/pages/learn/functions/higher-order.md rename to src/pages/learn/functions/higher-order.mdx index 4b24183..790ac40 100644 --- a/src/pages/learn/functions/higher-order.md +++ b/src/pages/learn/functions/higher-order.mdx @@ -2,23 +2,24 @@ layout: ../../../layouts/DocsLayout.astro title: Higher Order Functions --- +import Code from "../../../components/Code.astro" # Higher Order functions ## Function as parameter -```thp + B function) -> Array[B] { // implementation } -``` +`} /> ## Function as return -```thp + () -> Int { // code... @@ -30,7 +31,7 @@ fun generate_generator() -> () -> Int val generator = generate_generator() // A function val value = generate_generator()() // An Int -``` +`} /> diff --git a/src/pages/learn/functions/lambdas.md b/src/pages/learn/functions/lambdas.mdx similarity index 84% rename from src/pages/learn/functions/lambdas.md rename to src/pages/learn/functions/lambdas.mdx index 03d1665..2d18872 100644 --- a/src/pages/learn/functions/lambdas.md +++ b/src/pages/learn/functions/lambdas.mdx @@ -2,13 +2,14 @@ layout: ../../../layouts/DocsLayout.astro title: Lambdas --- +import Code from "../../../components/Code.astro" # Lambdas / Anonymous functions ## Anonymous function -```thp + Int { x + y } @@ -17,7 +18,7 @@ fun(Int x, Int y) -> Int { numbers.map(fun(x) { x * 2 }) -``` +`} /> @@ -26,7 +27,7 @@ numbers.map(fun(x) { By default closures **always** capture variables as **references**. -```thp + You can force a closure to capture variables by value. -```thp + -```thp + ## Lambdas @@ -70,7 +71,7 @@ Inside the lambda you can access the parameters as `$1`, `$2`, etc. Finally, lambdas be written outside of the parenthesis of function calls. -```thp + diff --git a/src/pages/learn/functions/parameters.md b/src/pages/learn/functions/parameters.mdx similarity index 69% rename from src/pages/learn/functions/parameters.md rename to src/pages/learn/functions/parameters.mdx index f47bd6c..f5d1769 100644 --- a/src/pages/learn/functions/parameters.md +++ b/src/pages/learn/functions/parameters.mdx @@ -2,39 +2,40 @@ layout: ../../../layouts/DocsLayout.astro title: Function parameters --- +import Code from "../../../components/Code.astro" # Function parameters ## Immutable reference -```thp + When using a regular type as a parameter, only it's immutable properties can be used -```thp + Int { - val items_count = numbers.size() // Ok, `size` is pure + val items_count = numbers.size() // Ok, \`size\` is pure items_count } -``` +`} /> To use immutable properties you must use a mutable reference. ## Mutable reference -```thp + Placing a `mut` before the type makes the parameter a mutable reference. Mutable methods can be used, and the original @@ -42,37 +43,34 @@ data **can** be mutated. The caller *must* also use `mut`. -```thp + ## Clone -```thp + Using the `clone` keyword before the type creates a mutable copy of the parameter (CoW). The original data will **not** be mutated. -```thp + diff --git a/src/pages/learn/index.mdx b/src/pages/learn/index.mdx index 9b764af..066945d 100644 --- a/src/pages/learn/index.mdx +++ b/src/pages/learn/index.mdx @@ -54,6 +54,7 @@ pagesLayout: - path: intro --- import InteractiveCode from "../../components/InteractiveCode.astro"; +import Code from "../../components/Code.astro" # Welcome @@ -144,11 +145,11 @@ $has_key = str_contains($haystack, 'needle'); print("has key? " . $has_key); ``` -```thp + - Explicit variable declaration - No `$` for variable names (and thus no `$$variable`, use a map instead) @@ -170,14 +171,14 @@ $obj = [ ] ``` -```thp + - Tuples, Arrays, Sets, Maps are clearly different - JSON-like object syntax @@ -192,11 +193,11 @@ $cat->meow(); ``` -```thp + - Instantiate classes without `new` - Use dot `.` instead of arrow `->` syntax @@ -211,10 +212,10 @@ use \Some\Deeply\Nested\Interface ``` -```thp + - Different module syntax - Explicit module declaration @@ -237,7 +238,7 @@ Where possible THP will compile to available PHP functions/classes/methods/etc. For example: -```thp + ```php // Would compile to: diff --git a/src/pages/learn/templating/intro.mdx b/src/pages/learn/templating/intro.mdx index d240510..7653f57 100644 --- a/src/pages/learn/templating/intro.mdx +++ b/src/pages/learn/templating/intro.mdx @@ -61,13 +61,13 @@ and compose them. The following would be the equivalent in THP: -```thp + Html { } -``` +`} /> It is very similar to React. The HTML is inside the THP code, not the other way around, so you can have arbitrary logic in the component.