From 23c5455a3782305d4e70741ee2860596a9ffaa01 Mon Sep 17 00:00:00 2001 From: Nicolas Schmidt Date: Tue, 22 Oct 2024 14:54:16 +0200 Subject: [PATCH 1/2] Work on shake specification --- docusaurus.config.ts | 31 ++-- .../language-features/_category_.json | 8 ++ specification/language-features/index.mdx | 4 + specification/language-features/variables.mdx | 134 ++++++++++++++++++ 4 files changed, 165 insertions(+), 12 deletions(-) create mode 100644 specification/language-features/_category_.json create mode 100644 specification/language-features/index.mdx create mode 100644 specification/language-features/variables.mdx diff --git a/docusaurus.config.ts b/docusaurus.config.ts index 1456dfe..f756157 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -40,6 +40,12 @@ const packages: readonly PackageEntry[] = [ ]; const docs: readonly DocEntry[] = [ + { + label: "Language Features", + id: "language-features", + path: "specification/language-features", + url: "language-features", + }, { label: "Bytecode", id: "bytecode", @@ -58,7 +64,7 @@ const docs: readonly DocEntry[] = [ id: e.id, path: e.path, url: e.url, - }) satisfies DocEntry, + }) satisfies DocEntry ), ]; @@ -116,6 +122,17 @@ const config: Config = { label: "Website", position: "left", }, + ...docs + .filter((doc) => doc.label) + .map( + (doc) => + ({ + to: `/${doc.url}${doc.index ? `/${doc.index}` : ""}`, + label: doc.label, + position: "left", + }) satisfies NavbarItem + ), + { to: "/", label: "Packages", @@ -126,20 +143,10 @@ const config: Config = { ({ to: `/${e.url}`, label: e.label, - }) satisfies NavbarItem, + }) satisfies NavbarItem ), ], }, - ...docs - .filter((doc) => doc.label) - .map( - (doc) => - ({ - to: `/${doc.url}${doc.index ? `/${doc.index}` : ""}`, - label: doc.label, - position: "left", - }) satisfies NavbarItem, - ), { href: "https://github.com/shakelang/shake", label: "GitHub", diff --git a/specification/language-features/_category_.json b/specification/language-features/_category_.json new file mode 100644 index 0000000..3f7f953 --- /dev/null +++ b/specification/language-features/_category_.json @@ -0,0 +1,8 @@ +{ + "label": "Language Features", + "position": 1, + "link": { + "type": "generated-index", + "description": "The Specification of the Shake language features" + } +} diff --git a/specification/language-features/index.mdx b/specification/language-features/index.mdx new file mode 100644 index 0000000..22f1765 --- /dev/null +++ b/specification/language-features/index.mdx @@ -0,0 +1,4 @@ +--- +position: 1 +title: Introduction +--- diff --git a/specification/language-features/variables.mdx b/specification/language-features/variables.mdx new file mode 100644 index 0000000..5e6d90c --- /dev/null +++ b/specification/language-features/variables.mdx @@ -0,0 +1,134 @@ +# Shake Variables + +Variables in shake are defined using the `var` and `val` keywords. +`var` is used to define a variable that can be reassigned, +while `val` is used to define a variable that cannot be reassigned. + +## § 1 Variable Declaration + +### § 1.1 `var` Keyword + +The `var` keyword is used to declare a variable that can be reassigned. +The syntax is as follows: + +```shake +var variableName: Type = value +``` + +### § 1.2 `val` Keyword + +The `val` keyword is used to declare a variable that cannot be reassigned. + +```shake +val variableName: Type = value +``` + +## § 2 Variable Types + +### § 2.1 Primitive Types + +Shake supports the following builtin (primitive) data types: + +- `byte` - 8-bit signed integer +- `short` - 16-bit signed integer +- `int` - 32-bit signed integer +- `long` - 64-bit signed integer +- `float` - 32-bit floating point number +- `double` - 64-bit floating point number +- `ubyte` - 8-bit unsigned integer +- `ushort` - 16-bit unsigned integer +- `uint` - 32-bit unsigned integer +- `ulong` - 64-bit unsigned integer +- `char` - 16-bit unicode character (UTF-16) +- `boolean` - boolean value + +### § 2.2 Custom Types + +Custom types can be defined using the `class` keyword. + +```shake +class ClassName { + // class body +} +``` + +### § 2.3 Type Inference + +Shake supports type inference, which means that you don't have to specify the type of a variable explicitly. +The type of the variable is inferred from the value assigned to it. + +```shake +var x = 10 // x is of type int +val y = "Hello" // y is of type string +``` + +### Common Types / Structures + +- `String` - A sequence of characters +- `Array` - An array of elements of type `T` + _Note_ Arrays are not implemented via a built-in type, but are instead implemented as a library type. + +## § 3 Variable Scope + +Variables in shake have block scope. +This means that a variable declared inside a block is only accessible within that block. + +```shake +{ + var x = 10 + println(x) // 10 +} +``` + +## § 4 Constants + +Constants are declared using the `const` keyword. +A constant is a variable whose value cannot be changed once it is assigned. + +```shake +const val PI: double = 3.14159 +``` + +## § 5 Nullability + +By default, variables in shake are non-nullable. +This means that a variable cannot be assigned a `null` value unless it is explicitly declared as nullable. + +```shake +var x: int? = null +``` + +## § 6 Type Casting + +Type casting is the process of converting a variable from one type to another. +Shake supports both implicit and explicit type casting. + +### § 6.1 Implicit Type Casting + +Implicit type casting is done automatically by the compiler when it is safe to do so. + +```shake +var x: int = 10 +var y: double = x // Implicit type casting +``` + +### § 6.2 Explicit Type Casting + +Explicit type casting is done manually by the programmer using the `as` keyword. + +```shake +var x: double = 10.5 +var y: int = x as int // Explicit type casting +``` + +## § 7 Type Checking + +Type checking is the process of verifying that a variable is of a certain type. +Shake supports the `is` operator for type checking. + +```shake +var x: int = 10 +if (x is int) { + println("x is an integer") +} +``` From b8b85c47f7905f8f43bc62d0a808b8b42222eb64 Mon Sep 17 00:00:00 2001 From: "shakebot[bot]" Date: Tue, 22 Oct 2024 12:54:53 +0000 Subject: [PATCH 2/2] [Automated] Format using prettier --- docusaurus.config.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docusaurus.config.ts b/docusaurus.config.ts index f756157..b60e532 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -64,7 +64,7 @@ const docs: readonly DocEntry[] = [ id: e.id, path: e.path, url: e.url, - }) satisfies DocEntry + }) satisfies DocEntry, ), ]; @@ -130,7 +130,7 @@ const config: Config = { to: `/${doc.url}${doc.index ? `/${doc.index}` : ""}`, label: doc.label, position: "left", - }) satisfies NavbarItem + }) satisfies NavbarItem, ), { @@ -143,7 +143,7 @@ const config: Config = { ({ to: `/${e.url}`, label: e.label, - }) satisfies NavbarItem + }) satisfies NavbarItem, ), ], },