Basic Syntax
This is a quick overview on the structure of NumFu programs.
Basic Program
A NumFu program consists of expressions and optional assertions
42 // Simple expression
println("Hello!") // Function call
let x = 5 in x * 2 // Let binding
sqrt(25) ---> $ == 5 // Assertion
42
Hello!
10
5
Every expression is evaluated and the result is written to stdout. For information on the separation of expressions, see below.
Expression Termination
Semicolons (;
) mark the definitive end of an expression. They can be omitted but are highly advised in more complex programs since they prevent unexpected bugs like this, because NumFu generally ignores whitespace and newlines:
42 //Number
[1,2,3,4] // List
[at REPL:2:1]
[2] [1] // List
^^^
TypeError: 'Number' object is not subscriptable
NumFu identified the list as index for the previous expression.
Comments
Single-line Comments
// This is a single-line comment
1+1 // End-of-line comment
Multi-line Comments
/*
This is a multi-line comment
that can span several lines
and is useful for longer explanations
*/
Operator Precedence
From highest to lowest precedence:
- Parentheses:
()
- Function calls:
f(x)
, Indexing:a[i]
- Exponentiation:
^
(right-associative) - Unary operators:
+
,-
,!
- Multiplication, Division, Modulo:
*
,/
,%
- Addition, Subtraction:
+
,-
- Comparison:
<
,>
,<=
,>=
- Equality:
==
,!=
- Logical AND:
&&
- Logical OR:
||
- Composition:
>>
- Pipe:
|>
2 + 3 * 4 // 14
2^3^2 // 512 (2^(3^2), right-associative)
!true && false // false ((!true) && false)
1 < 2 == true // true ((1 < 2) == true)
x |> f >> g // x |> (f >> g) (composition before pipe)
Associativity Rules
Left-associative Operators
10 - 5 - 2 // 3 ((10 - 5) - 2)
12 / 4 / 3 // 1 ((12 / 4) / 3)
1 < 2 < 3 // true ((1 < 2) && (2 < 3))
Right-associative Operators
2^3^2 // 512 (2^(3^2))
f >> g >> h // f >> (g >> h)
Reserved Words
The keywords and special symbols listed below cannot be used as variable names or function parameters. The parser will throw an error if you try to use them.
let
, in
, if
, then
, else
, del
,import
, export
, from
, true
, false
, $
, _
Identifier Rules
Valid Identifiers
// Letters, numbers, underscores
myVariable
_private
camelCase
snake_case
var123
_123
Invalid Identifiers
// Cannot start with numbers
123var
// No unicode letters
αβγ
变量
// Cannot contain special characters
my-var
my@var
my var