diff --git a/README.md b/README.md index b62e3f3..c3c9007 100755 --- a/README.md +++ b/README.md @@ -275,15 +275,15 @@ Mers has the following builtin types: + list types are written as a single type enclosed in square brackets: [string]. TODO! this will likely change to [string ...] or something similar to allow 1-long tuples in function args. + list values are created by putting any number of statements in square brackets, prefixing the closing bracket with ...: ["hello" "mers" "world" ...]. - functions - + function types are written as fn(args) out_type (TODO! implement this) - + function values are created using the ( ) syntax: anonymous_power_function = (a int b int) a.pow(b). - + to run anonymous functions, use the run() builtin: anonymous_power_function.run(4 2) evaluates to 16. - + note: functions are defined using the fn () syntax and are different from anonymous functions because they aren't values and can be run directory: fn power_function(a int b int) a.pow(b) => power_function(4 2) => 16 + + function types are written as `fn(args) out_type`. (TODO! implement this) + + function values are created using the `(first_arg_name first_arg_type second_arg_name second_arg_type) statement` syntax: `anonymous_power_function = (a int b int) a.pow(b)`. + + to run anonymous functions, use the run() builtin: `anonymous_power_function.run(4 2)` evaluates to `16`. + + note: functions are defined using the `fn name(args) statement` syntax and are different from anonymous functions because they aren't values and can be run directly: `fn power_function(a int b int) a.pow(b)` => `power_function(4 2)` => `16` - thread - + a special type returned by the thread builtin. It is similar to JavaScript promises and can be awaited to get the value once it has finished computing. + + a special type returned by the thread builtin. It is similar to JavaScript promises and can be awaited to get the value once it has finished computing. Reading the thread example is probably the best way to see how this works. - reference - + a mutable reference to a value. & for the type and & for a reference value. + + a mutable reference to a value. `&type` for the type and `&statement` for a reference value (usually `&varname`). - enum + wraps any other value with a certain identifier. - + the type is written as (): many builtins use Err(String) to report errors. - + to get a value, use : . + + the type is written as `enum(type)`: many builtins use `Err(String)` to report errors. + + to get a value, use `enum: statement`. `Err: "something went wrong"` diff --git a/mers/Cargo.lock b/mers/Cargo.lock old mode 100644 new mode 100755 diff --git a/mers_libs/http_requests_v1/Cargo.lock b/mers_libs/http_requests_v1/Cargo.lock old mode 100644 new mode 100755 diff --git a/thread.txt b/thread.txt new file mode 100755 index 0000000..36b3ad7 --- /dev/null +++ b/thread.txt @@ -0,0 +1,35 @@ +// 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.add(1) + sum = sum.add(i) + if fake_delay sleep(1) + progress = i.mul(100).div(max) + } + "the sum of all numbers from 0 to {0} is {1}!".format(max sum) +} + +// 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 +while { + sleep(1) + println("{0}%".format(progress)) + progress.eq(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))