Skip to content

Commit

Permalink
Added basic examples for the language!
Browse files Browse the repository at this point in the history
  • Loading branch information
hrszpuk committed Dec 30, 2024
1 parent d5dbb83 commit 2a5520b
Show file tree
Hide file tree
Showing 45 changed files with 288 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/Common Programming Concepts/Comments.drg
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions examples/Common Programming Concepts/Control Flow.drg
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions examples/Common Programming Concepts/Data Types.drg
Original file line number Diff line number Diff line change
@@ -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
*/
15 changes: 15 additions & 0 deletions examples/Common Programming Concepts/Functions.drg
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions examples/Common Programming Concepts/Operators.drg
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* Here are all the operators i guess

=
+ - / * %
&& != ! || ==
+= -= *= /= %
..
& | ^
> >= <= <=
>> <<
~
? :
*/
21 changes: 21 additions & 0 deletions examples/Common Programming Concepts/References and Copying.drg
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions examples/Common Programming Concepts/Variables.drg
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

/* Variable Declaration

Variables:
`let <identifier <type>`
`let <identifier> <type> = <expr>`
`let <identifier> = <expr>` <-- type inference

Constants:
`const <identifier> <type>`
`const <identifier> <type> = <expr>`
`const <identifier> = <expr>` <-- 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()
14 changes: 14 additions & 0 deletions examples/Error Handling/Option and Result types.drg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

// The prelude includes Option and Result types

func add<T>(a T, b T) -> Result<T>

// Pattern matching the result
let added = match add(10, 10) {
Result::success(s): s
Result::error(s): {
println(s)
exit(1)
}
}

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
8 changes: 8 additions & 0 deletions examples/Generics/Generics for functions.drg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

fn add<T>(a T, b T) -> T { return a + b }

// We can ensure a trait is implemented for a given type
fn sub<T: Numeric>(a T, b T) -> T { return a - b }

// We can have multiple generic types
fn mul<T: Numeric, K: Numeric, Y: Numeric>(a T, b K) -> Y { return a + b }
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
26 changes: 26 additions & 0 deletions examples/Strings, Lists, Tuples, and Sets/Lists.drg
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

// Lists
// Type signature: [<type>]
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"]
17 changes: 17 additions & 0 deletions examples/Strings, Lists, Tuples, and Sets/Sets.drg
Original file line number Diff line number Diff line change
@@ -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"

33 changes: 33 additions & 0 deletions examples/Strings, Lists, Tuples, and Sets/Strings.drg
Original file line number Diff line number Diff line change
@@ -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"
16 changes: 16 additions & 0 deletions examples/Strings, Lists, Tuples, and Sets/Tuples.drg
Original file line number Diff line number Diff line change
@@ -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 {}

15 changes: 15 additions & 0 deletions examples/User-defined Types/Enumerations.drg
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

type Colour enum {
Red,
Green,
Blue
}

let blue = Colour.Blue

type Direction enum {
Up = "^",
Right = "->",
Down = "v",
Left = "<-"
}
18 changes: 18 additions & 0 deletions examples/User-defined Types/Structures.drg
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions examples/User-defined Types/Trait.drg
Original file line number Diff line number Diff line change
@@ -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
}
}
9 changes: 9 additions & 0 deletions examples/User-defined Types/Type Aliases.drg
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 2a5520b

Please sign in to comment.