From 2a5520bb27cddfb40a73d86d4e3d71c676fe4b55 Mon Sep 17 00:00:00 2001 From: hrszpuk <107559570+hrszpuk@users.noreply.github.com> Date: Mon, 30 Dec 2024 06:17:27 +0000 Subject: [PATCH] Added basic examples for the language! --- .../Common Programming Concepts/Comments.drg | 7 ++++ .../Control Flow.drg | 22 +++++++++++++ .../Data Types.drg | 10 ++++++ .../Common Programming Concepts/Functions.drg | 15 +++++++++ .../Common Programming Concepts/Operators.drg | 13 ++++++++ .../References and Copying.drg | 21 ++++++++++++ .../Common Programming Concepts/Variables.drg | 27 +++++++++++++++ .../Option and Result types.drg | 14 ++++++++ .../Alternatives.drg | 0 .../Applicatives.drg | 0 .../Closures.drg | 0 .../Functors.drg | 0 .../Iterators.drg | 0 .../Lambdas.drg | 0 .../Monads.drg | 0 .../Patterns and matching.drg | 0 .../Traits.drg | 0 examples/Generics/Generics for functions.drg | 8 +++++ examples/Generics/Generics for methods.drg | 0 examples/Generics/Generics for types.drg | 0 examples/Modules and Packages/Modules.drg | 0 examples/Modules and Packages/Packages.drg | 0 .../Encapsulation.drg | 0 .../Inheritance via traits.drg | 0 .../Methods.drg | 0 examples/Standard Library/Array.drg | 0 examples/Standard Library/Filesystem.drg | 0 examples/Standard Library/Format.drg | 0 examples/Standard Library/Functional.drg | 0 examples/Standard Library/Hashmap.drg | 0 examples/Standard Library/IO.drg | 0 examples/Standard Library/Maths.drg | 0 examples/Standard Library/Prelude.drg | 0 examples/Standard Library/Random.drg | 0 examples/Standard Library/Regex.drg | 0 examples/Standard Library/Stack.drg | 0 examples/Standard Library/String.drg | 0 .../Lists.drg | 26 +++++++++++++++ .../Strings, Lists, Tuples, and Sets/Sets.drg | 17 ++++++++++ .../Strings.drg | 33 +++++++++++++++++++ .../Tuples.drg | 16 +++++++++ examples/User-defined Types/Enumerations.drg | 15 +++++++++ examples/User-defined Types/Structures.drg | 18 ++++++++++ examples/User-defined Types/Trait.drg | 17 ++++++++++ examples/User-defined Types/Type Aliases.drg | 9 +++++ 45 files changed, 288 insertions(+) create mode 100644 examples/Common Programming Concepts/Comments.drg create mode 100644 examples/Common Programming Concepts/Control Flow.drg create mode 100644 examples/Common Programming Concepts/Data Types.drg create mode 100644 examples/Common Programming Concepts/Functions.drg create mode 100644 examples/Common Programming Concepts/Operators.drg create mode 100644 examples/Common Programming Concepts/References and Copying.drg create mode 100644 examples/Common Programming Concepts/Variables.drg create mode 100644 examples/Error Handling/Option and Result types.drg create mode 100644 examples/Functional Programming Features/Alternatives.drg create mode 100644 examples/Functional Programming Features/Applicatives.drg create mode 100644 examples/Functional Programming Features/Closures.drg create mode 100644 examples/Functional Programming Features/Functors.drg create mode 100644 examples/Functional Programming Features/Iterators.drg create mode 100644 examples/Functional Programming Features/Lambdas.drg create mode 100644 examples/Functional Programming Features/Monads.drg create mode 100644 examples/Functional Programming Features/Patterns and matching.drg create mode 100644 examples/Functional Programming Features/Traits.drg create mode 100644 examples/Generics/Generics for functions.drg create mode 100644 examples/Generics/Generics for methods.drg create mode 100644 examples/Generics/Generics for types.drg create mode 100644 examples/Modules and Packages/Modules.drg create mode 100644 examples/Modules and Packages/Packages.drg create mode 100644 examples/Object Oriented Programming Features/Encapsulation.drg create mode 100644 examples/Object Oriented Programming Features/Inheritance via traits.drg create mode 100644 examples/Object Oriented Programming Features/Methods.drg create mode 100644 examples/Standard Library/Array.drg create mode 100644 examples/Standard Library/Filesystem.drg create mode 100644 examples/Standard Library/Format.drg create mode 100644 examples/Standard Library/Functional.drg create mode 100644 examples/Standard Library/Hashmap.drg create mode 100644 examples/Standard Library/IO.drg create mode 100644 examples/Standard Library/Maths.drg create mode 100644 examples/Standard Library/Prelude.drg create mode 100644 examples/Standard Library/Random.drg create mode 100644 examples/Standard Library/Regex.drg create mode 100644 examples/Standard Library/Stack.drg create mode 100644 examples/Standard Library/String.drg create mode 100644 examples/Strings, Lists, Tuples, and Sets/Lists.drg create mode 100644 examples/Strings, Lists, Tuples, and Sets/Sets.drg create mode 100644 examples/Strings, Lists, Tuples, and Sets/Strings.drg create mode 100644 examples/Strings, Lists, Tuples, and Sets/Tuples.drg create mode 100644 examples/User-defined Types/Enumerations.drg create mode 100644 examples/User-defined Types/Structures.drg create mode 100644 examples/User-defined Types/Trait.drg create mode 100644 examples/User-defined Types/Type Aliases.drg diff --git a/examples/Common Programming Concepts/Comments.drg b/examples/Common Programming Concepts/Comments.drg new file mode 100644 index 0000000..e587e58 --- /dev/null +++ b/examples/Common Programming Concepts/Comments.drg @@ -0,0 +1,7 @@ +// This is a single line comment +/* + This is a multi-line comment +*/ + +// Comments can go in between shit: +let /* look! */ x = /* neat! */ 10 \ No newline at end of file diff --git a/examples/Common Programming Concepts/Control Flow.drg b/examples/Common Programming Concepts/Control Flow.drg new file mode 100644 index 0000000..5302d80 --- /dev/null +++ b/examples/Common Programming Concepts/Control Flow.drg @@ -0,0 +1,22 @@ +// If statements +if false { + // Won't execute +} else if true { + // Will execute +} else { + // Will only execute if all previous ifs are false +} + +// Match statement +let x = 10 + +match x { + 0: println("Zero") + 1..5: println("NUMBER") + 5: println("5") + >5: println("More than 5") + _: println("Negative!") +} + +// Ternary operator +let y = true ? 10 : 20 diff --git a/examples/Common Programming Concepts/Data Types.drg b/examples/Common Programming Concepts/Data Types.drg new file mode 100644 index 0000000..5c4bad4 --- /dev/null +++ b/examples/Common Programming Concepts/Data Types.drg @@ -0,0 +1,10 @@ +/* +int <-- Platform dependent, I'd recommend :) +uint <-- Platform dependent, I'd recommend :) +i8 i16 i32 i64 i128 <-- Signed integer types +u8 u16 u32 u64 u128 <-- Unsigned integer types +float <-- Platform dependent, I'd recommend :) +f32 f64 <-- Floating Point Types +string <-- List of characters +bool <-- True or False +*/ \ No newline at end of file diff --git a/examples/Common Programming Concepts/Functions.drg b/examples/Common Programming Concepts/Functions.drg new file mode 100644 index 0000000..bf4d260 --- /dev/null +++ b/examples/Common Programming Concepts/Functions.drg @@ -0,0 +1,15 @@ +// Functions are pretty normal +fn add(a int, b int) -> int { return a + b } +add(10, 20) + +fn hello_world() { + println("Hello, World!") +} + +// Lambda functions +let add_one = (a int) -> int => a + 1 +add_one(10, add_one(10)) + +// Lambda type +let add_two (int) -> int +add_two = (a) => a + 2 \ No newline at end of file diff --git a/examples/Common Programming Concepts/Operators.drg b/examples/Common Programming Concepts/Operators.drg new file mode 100644 index 0000000..2301d61 --- /dev/null +++ b/examples/Common Programming Concepts/Operators.drg @@ -0,0 +1,13 @@ +/* Here are all the operators i guess + += ++ - / * % +&& != ! || == ++= -= *= /= % +.. +& | ^ +> >= <= <= +>> << +~ +? : +*/ \ No newline at end of file diff --git a/examples/Common Programming Concepts/References and Copying.drg b/examples/Common Programming Concepts/References and Copying.drg new file mode 100644 index 0000000..f8c642e --- /dev/null +++ b/examples/Common Programming Concepts/References and Copying.drg @@ -0,0 +1,21 @@ +// By default everything is passed around the program through copying. +let x = 10 +let y = x // The value of x is copied to y. + +// This is true even for more complex types. + +// However, we can reference any type of information, which allows it to be passed around the program +// without having to copy each time, which can get expensive. + +let a = 10 +let b = &a + +println(b) // 10 + +// References are immutable by default, you can use &m for a mutable reference +let c = &m a +c += 1 // a and b are both now 11 + +// References to constant cannot be modified. +const pi = 3.14 +let pi_ref = &m pi // error \ No newline at end of file diff --git a/examples/Common Programming Concepts/Variables.drg b/examples/Common Programming Concepts/Variables.drg new file mode 100644 index 0000000..9d06342 --- /dev/null +++ b/examples/Common Programming Concepts/Variables.drg @@ -0,0 +1,27 @@ + +/* Variable Declaration + +Variables: +`let ` +`let = ` +`let = ` <-- type inference + +Constants: +`const ` +`const = ` +`const = ` <-- type inference +*/ + +const username string +const age int = 100 + 100 +const id = 1034 + +let inventory [string] +let count int = 0 +let greeting = "!dlrow olleH" + +// Variable Assignment +username = "alice" // This is allowed only once as the constant has not be assigned anything prior +inventory = ["Sword", "Shield", "Potion"] +count += 1 +greeting = greeting.reverse() diff --git a/examples/Error Handling/Option and Result types.drg b/examples/Error Handling/Option and Result types.drg new file mode 100644 index 0000000..f290e96 --- /dev/null +++ b/examples/Error Handling/Option and Result types.drg @@ -0,0 +1,14 @@ + +// The prelude includes Option and Result types + +func add(a T, b T) -> Result + +// Pattern matching the result +let added = match add(10, 10) { + Result::success(s): s + Result::error(s): { + println(s) + exit(1) + } +} + diff --git a/examples/Functional Programming Features/Alternatives.drg b/examples/Functional Programming Features/Alternatives.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Functional Programming Features/Applicatives.drg b/examples/Functional Programming Features/Applicatives.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Functional Programming Features/Closures.drg b/examples/Functional Programming Features/Closures.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Functional Programming Features/Functors.drg b/examples/Functional Programming Features/Functors.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Functional Programming Features/Iterators.drg b/examples/Functional Programming Features/Iterators.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Functional Programming Features/Lambdas.drg b/examples/Functional Programming Features/Lambdas.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Functional Programming Features/Monads.drg b/examples/Functional Programming Features/Monads.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Functional Programming Features/Patterns and matching.drg b/examples/Functional Programming Features/Patterns and matching.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Functional Programming Features/Traits.drg b/examples/Functional Programming Features/Traits.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Generics/Generics for functions.drg b/examples/Generics/Generics for functions.drg new file mode 100644 index 0000000..ef72bf6 --- /dev/null +++ b/examples/Generics/Generics for functions.drg @@ -0,0 +1,8 @@ + +fn add(a T, b T) -> T { return a + b } + +// We can ensure a trait is implemented for a given type +fn sub(a T, b T) -> T { return a - b } + +// We can have multiple generic types +fn mul(a T, b K) -> Y { return a + b } \ No newline at end of file diff --git a/examples/Generics/Generics for methods.drg b/examples/Generics/Generics for methods.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Generics/Generics for types.drg b/examples/Generics/Generics for types.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Modules and Packages/Modules.drg b/examples/Modules and Packages/Modules.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Modules and Packages/Packages.drg b/examples/Modules and Packages/Packages.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Object Oriented Programming Features/Encapsulation.drg b/examples/Object Oriented Programming Features/Encapsulation.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Object Oriented Programming Features/Inheritance via traits.drg b/examples/Object Oriented Programming Features/Inheritance via traits.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Object Oriented Programming Features/Methods.drg b/examples/Object Oriented Programming Features/Methods.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/Array.drg b/examples/Standard Library/Array.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/Filesystem.drg b/examples/Standard Library/Filesystem.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/Format.drg b/examples/Standard Library/Format.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/Functional.drg b/examples/Standard Library/Functional.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/Hashmap.drg b/examples/Standard Library/Hashmap.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/IO.drg b/examples/Standard Library/IO.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/Maths.drg b/examples/Standard Library/Maths.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/Prelude.drg b/examples/Standard Library/Prelude.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/Random.drg b/examples/Standard Library/Random.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/Regex.drg b/examples/Standard Library/Regex.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/Stack.drg b/examples/Standard Library/Stack.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Standard Library/String.drg b/examples/Standard Library/String.drg new file mode 100644 index 0000000..e69de29 diff --git a/examples/Strings, Lists, Tuples, and Sets/Lists.drg b/examples/Strings, Lists, Tuples, and Sets/Lists.drg new file mode 100644 index 0000000..f17a31e --- /dev/null +++ b/examples/Strings, Lists, Tuples, and Sets/Lists.drg @@ -0,0 +1,26 @@ + +// Lists +// Type signature: [] +let names [string] +names = ["Jerry", "Barry", "Caesar"] + +// List methods +names.clear() +names.count() +names.find() +names.pop() +names.push() +names.extend() +names.insert() +names.reverse() +names.remove() + +// Looping +for name in names {} +for name, index in names {} + +// Indexing +names[0] + +// Slicing [start:stop:step] +names[0:2:-1] = ["Barry", "Jerry"] \ No newline at end of file diff --git a/examples/Strings, Lists, Tuples, and Sets/Sets.drg b/examples/Strings, Lists, Tuples, and Sets/Sets.drg new file mode 100644 index 0000000..f1538c8 --- /dev/null +++ b/examples/Strings, Lists, Tuples, and Sets/Sets.drg @@ -0,0 +1,17 @@ + + +let names {string} +names = {"Jerry", "Barry", "Caesar"} + +// List methods +names.clear() +names.pop() +names.push() + +// Looping +for name in names {} +for name, index in names {} + +// Indexing +names[0] // "Jerry" + diff --git a/examples/Strings, Lists, Tuples, and Sets/Strings.drg b/examples/Strings, Lists, Tuples, and Sets/Strings.drg new file mode 100644 index 0000000..40817d5 --- /dev/null +++ b/examples/Strings, Lists, Tuples, and Sets/Strings.drg @@ -0,0 +1,33 @@ + +// Strings +let msg string +msg = "Hello, World!" + +// String methods (prelude) +msg.reverse() +msg.replace("!", "?") +msg.lowercase() +msg.uppercase() +msg.is_lowercase() +msg.is_uppercase() +msg.count() +msg.trim() +msg.find() +msg.split() + +// String methods can also be applied to string literals +"Test".reverse().find("T") // return 3 + +// Looping over a string +for c in "hello" {} +for c, index in "hello" {} + +// Indexing a string +"hello"[2] // "l" + +// Concatenation +"hello" + " world" +"hello" += " world" + +// Slicing [start:stop:step] +"hello world"[5:11] // "world" \ No newline at end of file diff --git a/examples/Strings, Lists, Tuples, and Sets/Tuples.drg b/examples/Strings, Lists, Tuples, and Sets/Tuples.drg new file mode 100644 index 0000000..b94c3fc --- /dev/null +++ b/examples/Strings, Lists, Tuples, and Sets/Tuples.drg @@ -0,0 +1,16 @@ + +let position2d (x int, y int) +position2d = (10, 10) + +position2d[0] +position2d[1] + +position2d.x +position2d.y + +position2d.count() +position2d.find() + +for p in position2d {} +for p, index in position2d {} + diff --git a/examples/User-defined Types/Enumerations.drg b/examples/User-defined Types/Enumerations.drg new file mode 100644 index 0000000..c32c30b --- /dev/null +++ b/examples/User-defined Types/Enumerations.drg @@ -0,0 +1,15 @@ + +type Colour enum { + Red, + Green, + Blue +} + +let blue = Colour.Blue + +type Direction enum { + Up = "^", + Right = "->", + Down = "v", + Left = "<-" +} diff --git a/examples/User-defined Types/Structures.drg b/examples/User-defined Types/Structures.drg new file mode 100644 index 0000000..0c0dba9 --- /dev/null +++ b/examples/User-defined Types/Structures.drg @@ -0,0 +1,18 @@ + +type User struct { + name string, + pass string, + favourite_colour Colour +} + +let barry = User{ + "Barry", "123", Colour.Blue, +} + +println(barry.name) + +const jerry = User{ + name: "Jerry", + pass: "321", + favourite_colour: .Blue +} diff --git a/examples/User-defined Types/Trait.drg b/examples/User-defined Types/Trait.drg new file mode 100644 index 0000000..ed68fe3 --- /dev/null +++ b/examples/User-defined Types/Trait.drg @@ -0,0 +1,17 @@ + +trait Animal { + fn new(name string) -> self + fn name(&self) -> string +} + +type Sheep struct { name string } + +impl Animal for Sheep { + fn new(name string) -> Sheep { + return Sheep { name } + } + + fn name(&self) -> string { + return self.name + } +} \ No newline at end of file diff --git a/examples/User-defined Types/Type Aliases.drg b/examples/User-defined Types/Type Aliases.drg new file mode 100644 index 0000000..86af65e --- /dev/null +++ b/examples/User-defined Types/Type Aliases.drg @@ -0,0 +1,9 @@ + +// Here we can use `number` instead of `int` +type number int + +// However, the underlying type is still exposed, traits will still match to both. +// For example with the Eq trait: number(10) == int(10) // true + +let x number = 10 +let xs string = x.(string) \ No newline at end of file