Stopwatch
Stopwatch is a language invented by User:GDavid.
Program structure
A Stopwatch program consists of function declarations, global variable assignments and global variable declarations. The Main function is called on start.
Functions
Function declarations are of the form
name(argument1, argument2, ...) { body }
(shorthand for assigning a lambda to name)
Lambda functions can be created with
(argument1, argument2, ...) { body }
A function can be called with
name(argument1, argument2, ...)
Recursion is not allowed.
Variables
Variables can be assigned (only once) with
name = value
Variables are implicitly declared in function scope. To declare a variable in the scope of the enclosing block, use
var name
Name collisions inside functions are not allowed.
Values
Number constants can be used in the code, but no arithmetic operators are available.
A stopwatch can be created (but not started) using watch.
A stopwatch can be started/resumed using start watch and paused using stop watch.
A stopwatch can fire a split event with split watch.
Start, stop, split and time watch return the current time of the watch.
Functions are values.
Flow control
You can wait N seconds using
sleep N
(N is the return value of sleep)
Multiple instructions can be executed in parallel with
parallel { instruction1 instruction2 ... }
The parallel block finishes when all instructions inside finish.
Sequential execution (useful in parallel)
do { ... }
Infinite loop
repeat { ... }
Repeat until N splits reached on a stopwatch
forsplits (stopwatch, N) { ... }
Break out of loop
break
Continue looping with next iteration
continue
Return from a function early
return value
0 is returned when control reaches end of function.
I/O
Wait for a keypress
wait in
Output a character
out char_code
Any unicode character should be accepted. Invalid codes don't do anything.
Examples
// add two numbers add(a, b) { w = watch start w sleep a sleep b return stop w }
// subtract b from a sub(a, b) { w = watch parallel { sleep a do { sleep b start w } } return stop w }
// multiply two numbers mul(a, b) { w = watch start w forsplits (w, b) { sleep a split w } return stop w }
// division div(a, b) { t = sub(b, 1) w = watch parallel { do { sleep a return time w } repeat { sleep t start w sleep 1 stop w } } }
min(a, b) { parallel { return sleep a return sleep b } }
max(a, b) { parallel { do { sleep a return b } do { sleep b return a } } }