Built-in Functions
Output
merx provides three output statements: println, print, and error.
println
Prints the value of an expression to standard output, followed by a newline:
mermaid
flowchart TD
Start --> A[println 'Hello!']
A --> Endconsole
$ merx run hello.mmd
Hello!print
Prints the value of an expression to standard output without a trailing newline:
mermaid
flowchart TD
Start --> A[print 'Hello, ']
A --> B[println 'World!']
B --> Endconsole
$ merx run hello.mmd
Hello, World!error
Prints the value of an expression to standard error, followed by a newline. Execution continues normally after the error statement:
mermaid
flowchart TD
Start --> A[error 'something went wrong']
A --> B[println 'still running']
B --> Endconsole
$ merx run error.mmd
something went wrong
still runningOutput Format
Each type has a specific output format:
| Type | Format | Example |
|---|---|---|
int | Decimal number | 42, -17 |
str | The string itself (no quotes) | hello |
bool | true or false | true |
Input
The input Expression
The input expression reads a single line from standard input and returns it as a string:
mermaid
flowchart TD
Start --> A[print 'What is your name? ']
A --> B[name = input]
B --> C[println 'Hello, ' + name + '!']
C --> Endconsole
$ merx run greet.mmd
What is your name? merx
Hello, merx!- Trailing newline characters (
\r,\n) are stripped - If the input reaches EOF, an empty string is returned
Converting Input Types
Since input always returns a string, use the as operator to convert it to the desired type:
mermaid
flowchart TD
Start --> A[print 'Enter a number: ']
A --> B[n = input as int]
B --> C[println n * 2]
C --> Endconsole
$ merx run double.mmd
Enter a number: 21
42If the input cannot be converted (e.g., 'abc' as int), a runtime error occurs.
Practical Example: Sum Calculator
This program reads numbers from the user until they enter 0, then prints the sum:
mermaid
flowchart TD
Start --> A[sum = 0]
A --> B[print 'Enter a number (0 to stop): ']
B --> C[n = input as int]
C --> D{n == 0?}
D -->|Yes| E[println 'Sum: ' + sum as str]
E --> End
D -->|No| F[sum = sum + n]
F --> Bconsole
$ merx run sum.mmd
Enter a number (0 to stop): 10
Enter a number (0 to stop): 20
Enter a number (0 to stop): 30
Enter a number (0 to stop): 0
Sum: 60