-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic examples for the language!
- Loading branch information
Showing
45 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
examples/Common Programming Concepts/References and Copying.drg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "<-" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |