mirror of
https://github.com/Dummi26/mers.git
synced 2025-12-15 19:47:50 +01:00
move old mers to old/ and update gitignore
This commit is contained in:
28
old/examples/amogus.mers
Executable file
28
old/examples/amogus.mers
Executable file
File diff suppressed because one or more lines are too long
19
old/examples/custom_type.mers
Executable file
19
old/examples/custom_type.mers
Executable file
@@ -0,0 +1,19 @@
|
||||
// linked list
|
||||
type elem [int []/elem]
|
||||
|
||||
fn print_linked_list(start elem) {
|
||||
loop {
|
||||
println(start.0.to_string())
|
||||
elem := start.1
|
||||
switch! elem {
|
||||
[] elem {
|
||||
println("[END]")
|
||||
true // break
|
||||
}
|
||||
elem elem &start = elem // continue
|
||||
}
|
||||
}
|
||||
[]
|
||||
}
|
||||
|
||||
[1 [2 [3 [5 [7 []]]]]].print_linked_list()
|
||||
12
old/examples/destructuring_assignment.mers
Executable file
12
old/examples/destructuring_assignment.mers
Executable file
@@ -0,0 +1,12 @@
|
||||
x := "https:\//www.google.com"
|
||||
|
||||
x.debug()
|
||||
|
||||
[a, [b, c]] := [1, ["str", 12.5]]
|
||||
|
||||
println("---")
|
||||
println(a.to_string() + " " + b + " " + c.to_string())
|
||||
println("~~~")
|
||||
|
||||
// switch! run_command("curl", [x, ...]) {
|
||||
// }
|
||||
3
old/examples/force_output_type.mers
Executable file
3
old/examples/force_output_type.mers
Executable file
@@ -0,0 +1,3 @@
|
||||
fn plus(a int, b int) -> int { a + b }
|
||||
|
||||
10.plus(20).debug()
|
||||
27
old/examples/functions_double_definitions.mers
Executable file
27
old/examples/functions_double_definitions.mers
Executable file
@@ -0,0 +1,27 @@
|
||||
type person [string int]
|
||||
fn name(p person/&person) p.0
|
||||
fn age(p person/&person) p.1
|
||||
|
||||
type village [[float float] string]
|
||||
fn name(v village/&village) v.1
|
||||
fn location(v village/&village) v.0
|
||||
fn x_coordinate(v village/&village) v.0.0
|
||||
fn y_coordinate(v village/&village) v.0.1
|
||||
|
||||
|
||||
|
||||
customer := ["Max M.", 43]
|
||||
home_town := [[12.3, 5.09], "Maxburg"]
|
||||
|
||||
debug(customer)
|
||||
debug(customer.name())
|
||||
debug(&customer.name())
|
||||
|
||||
debug(home_town)
|
||||
debug(home_town.name())
|
||||
debug(home_town.location())
|
||||
|
||||
println(
|
||||
"Hello, " + customer.name()
|
||||
+ " from " + home_town.name()
|
||||
)
|
||||
12
old/examples/get_ref.mers
Executable file
12
old/examples/get_ref.mers
Executable file
@@ -0,0 +1,12 @@
|
||||
list := [1 2 3 4 5 6 7 8 9 ...]
|
||||
|
||||
// calling get on an &list will get a reference
|
||||
&list.get(2).assume1() = 24
|
||||
// calling get on a list will get a value
|
||||
should_not_be_changeable := list.get(3).assume1()
|
||||
&should_not_be_changeable = 24
|
||||
|
||||
if list.get(2) != [24] println("[!!] list.get(2) != 24 (was {0})".format(list.get(2).to_string()))
|
||||
if list.get(3) == [24] println("[!!] list.get(3) == 24")
|
||||
|
||||
list.debug()
|
||||
39
old/examples/iterators.mers
Executable file
39
old/examples/iterators.mers
Executable file
@@ -0,0 +1,39 @@
|
||||
fn map_string_to_int(list [string ...], func fn((string) int)) {
|
||||
// return a function that can be used as an iterator
|
||||
() {
|
||||
// this function will be called by the for loop
|
||||
// whenever it wants to retrieve the next item in the collection.
|
||||
// get the element
|
||||
elem := &list.remove(0)
|
||||
switch! elem {
|
||||
// if the element was present, run the map function to convert it from a string to an int and return the result
|
||||
[string] [elem] {
|
||||
[func.run(elem)]
|
||||
}
|
||||
// if there are no more elements, return something that doesn't match.
|
||||
[] [] []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for v ["1" "2" "5" "-10" ...].map_string_to_int((s string) s.parse_int().assume1("list contained strings that can't be parsed to an int!")) {
|
||||
debug(v)
|
||||
}
|
||||
|
||||
|
||||
// returns an iterator to iteratively compute square numbers
|
||||
// using the fact that (n+1)^2 = n^2 + 2n + 1
|
||||
fn square_numbers() {
|
||||
i := 0
|
||||
val := 0
|
||||
() {
|
||||
&val = val + { 2 * i } + 1
|
||||
&i = i + 1
|
||||
[val]
|
||||
}
|
||||
}
|
||||
|
||||
for n square_numbers() {
|
||||
println(n.to_string())
|
||||
n >= 100
|
||||
}
|
||||
8
old/examples/macro.mers
Executable file
8
old/examples/macro.mers
Executable file
@@ -0,0 +1,8 @@
|
||||
val := "some string"
|
||||
val := !(mers {
|
||||
println("macro is running now!")
|
||||
"macro returned value"
|
||||
})
|
||||
println(val)
|
||||
val := !(mers "my_macro.mers")
|
||||
println(val)
|
||||
10
old/examples/modify_variable.mers
Executable file
10
old/examples/modify_variable.mers
Executable file
@@ -0,0 +1,10 @@
|
||||
// NOTE: Might change, but this is the current state of things
|
||||
x := 10
|
||||
t := thread(() {
|
||||
sleep(0.25)
|
||||
println(x.to_string())
|
||||
})
|
||||
&x = 20 // -> 20 20 because it modifies the original variable x
|
||||
// x := 20 // -> 10 20 because it shadows the original variable x
|
||||
t.await()
|
||||
println(x.to_string())
|
||||
2
old/examples/my_macro.mers
Executable file
2
old/examples/my_macro.mers
Executable file
@@ -0,0 +1,2 @@
|
||||
println("my macro running in ./my_macro.mers")
|
||||
"value returned from ./my_macro.mers :)"
|
||||
19
old/examples/struct_fields.mers
Executable file
19
old/examples/struct_fields.mers
Executable file
@@ -0,0 +1,19 @@
|
||||
// mers doesn't have structs, so instead we define a type:
|
||||
type myStruct [
|
||||
int,
|
||||
string
|
||||
]
|
||||
// to give names to the fields, we define functions:
|
||||
fn count(s myStruct) s.0
|
||||
// to allow users to change the value, add &myStruct to the valid types for s (only through references can values be changed)
|
||||
fn note(s myStruct/&myStruct) s.1
|
||||
|
||||
my_struct := [12, "test"]
|
||||
|
||||
my_struct.count().debug()
|
||||
|
||||
my_struct.note().debug()
|
||||
|
||||
&my_struct.note() = "changed"
|
||||
|
||||
my_struct.note().debug()
|
||||
19
old/examples/switch_match.mers
Executable file
19
old/examples/switch_match.mers
Executable file
@@ -0,0 +1,19 @@
|
||||
x := if true [10 "my text"] else 10
|
||||
|
||||
switch! x {
|
||||
[int string] [num text] {
|
||||
num.debug()
|
||||
text.debug()
|
||||
}
|
||||
int num {
|
||||
println("number:")
|
||||
num.debug()
|
||||
}
|
||||
}
|
||||
|
||||
text := "12.5"
|
||||
match {
|
||||
parse_int(text) num println("int: " + num.to_string())
|
||||
parse_float(text) num println("float: " + num.to_string())
|
||||
[true] [] println("not a number: " + text)
|
||||
}
|
||||
13
old/examples/the_any_type.mers
Executable file
13
old/examples/the_any_type.mers
Executable file
@@ -0,0 +1,13 @@
|
||||
fn debug_any(val any) {
|
||||
val.debug()
|
||||
}
|
||||
|
||||
"something".debug_any()
|
||||
|
||||
fn just_return(val any) {
|
||||
val
|
||||
}.debug()
|
||||
|
||||
v := "text"
|
||||
v.debug()
|
||||
v.just_return().debug()
|
||||
36
old/examples/thread.mers
Executable file
36
old/examples/thread.mers
Executable file
@@ -0,0 +1,36 @@
|
||||
// this is true by default so the example doesn't finish too quickly or too slowly depending on your hardware.
|
||||
// you can set it to false and tweak the max value for a more authentic cpu-heavy workload.
|
||||
fake_delay := true
|
||||
|
||||
// this will be shared between the two threads to report the progress in percent (0-100%).
|
||||
progress := 0
|
||||
|
||||
// an anonymous function that sums all numbers from 0 to max.
|
||||
// it captures the progress variable and uses it to report its status to the main thread, which will periodically print the current progress.
|
||||
// once done, it returns a string with the sum of all numbers.
|
||||
calculator := (max int) {
|
||||
sum := 0
|
||||
for i max {
|
||||
i := i + 1
|
||||
// println("i: {0} s: {1}".format(i.to_string() sum.to_string()))
|
||||
&sum = sum + i
|
||||
if fake_delay sleep(1)
|
||||
&progress = i * 100 / max
|
||||
}
|
||||
"the sum of all numbers from 0 to {0} is {1}!".format(max.to_string() sum.to_string())
|
||||
}
|
||||
|
||||
// start the thread. if fake_delay is true, calculate 1+2+3+4+5+6+7+8+9+10. if fake_delay is false, count up to some ridiculously large number.
|
||||
slow_calculation_thread := calculator.thread(if fake_delay 10 else 20000000)
|
||||
|
||||
// every second, print the progress. once it reaches 100%, stop
|
||||
loop {
|
||||
sleep(1)
|
||||
println("{0}%".format(progress.to_string()))
|
||||
progress == 100 // break from the loop
|
||||
}
|
||||
|
||||
// use await() to get the result from the thread. if the thread is still running, this will block until the thread finishes.
|
||||
result := slow_calculation_thread.await()
|
||||
|
||||
println("Thread finished, result: {0}".format(result))
|
||||
Reference in New Issue
Block a user