Language reference
MUL Source — compact guide
MUL 0.1.0. Everything you need to write valid MUL today. Full reference: docs/language/. Grammar: spec/grammar.ebnf.
Program shape
// expect: total
// expect: 703
module demo fn sub(a:i32,b:i32)->i32{let diff=a-b return diff}fn isSmall(n:i32)->bool pure{return n>=0&&n<10}pub fn main()->i32 io{print("total")var total=sub(10,3)*100 if isSmall(3){total=total+sub(sub(5,1),1)}else{return 0}return total}
- One module per file:
module name.partfirst, then functions in any order. [pub] fn name(p: i32, q: string) -> i32 [pure|io] { statements }.- Effects: no effect written means
pure. Apurefunction cannot call aniofunction orprint(MUL-T013); aniofunction may call both. print(s)(built-in,io) writes astringand a line feed. It has no value: only a statement (MUL-T014).printcannot name a function.- Statements:
let x = e,var x = e(optional: i32/: bool/: string),x = e(only avar),f(args)(a call; its value is discarded),{ ... }(nested scope),if c { } else if d { } else { },while c { },return e. No other expression is a statement. - Every path ends with
return; anifends a path only with anelseand areturnin every arm; awhilenever ends a path, so areturnfollows it. Nothing may follow a statement that ends the path. whiletests itsboolcondition before every iteration; the body is a fresh scope each time;returninside it leaves the function. Nobreakorcontinue, no iteration limit.- A variable is visible to the end of its block. No shadowing: a visible name (parameter or variable) cannot be declared again.
mul runcallsmain, which must befn() -> i32; its value is the exit code (low 8 bits).--print-resultprints it last and exits with 0.- Functions may call themselves and functions defined later in the file.
- Layout (RFC-011): write the compact layout shown above, the canonical form.
A space only between two word tokens (keywords, names, numbers:
return n,pub fn,i32 io,var i), a line feed only after a comment; no indentation, no other spaces or line feeds. No statement separator is needed:let diff=a-b return diff. Line feeds never matter, so any layout parses;mul fmtrewrites a file in the compact layout,mul fmt --prettyin the layout for people (fn sub(a: i32, b: i32) -> i32 {, one statement per line, four-space indent). - Comments:
//to end of line. No semicolons. Identifiers are ASCII. - Keywords:
module pub fn return let var if else while true false pure io. - String literals:
"..."on one line, UTF-8, escapes\n \t \\ \"; anything else isMUL-S006.
Grammar
module = "module" ident { "." ident } { function } ;
function = [ "pub" ] "fn" ident "(" [ param { "," param } ] ")" "->" ident
[ "pure" | "io" ] block ;
param = ident ":" ident ;
block = "{" { stmt } "}" ;
stmt = "return" expr | ( "let" | "var" ) ident [ ":" ident ] "=" expr
| ident "=" expr | call | block
| "if" expr block { "else" "if" expr block } [ "else" block ]
| "while" expr block ;
expr = and { "||" and } ;
and = compare { "&&" compare } ;
compare = sum [ ( "==" | "!=" | "<" | "<=" | ">" | ">=" ) sum ] ;
sum = term { ( "+" | "-" ) term } ;
term = unary { ( "*" | "/" | "%" ) unary } ;
unary = ( "-" | "!" ) unary | int | string | "true" | "false"
| call | ident | "(" expr ")" ;
call = ident "(" [ expr { "," expr } ] ")" ;
Types and arithmetic
- Types:
i32,bool,string; no conversions. Literals: decimal0..=2147483647(-2147483648is allowed),true,false,"text". - A
stringis immutable: store, pass, return,print. No+,==(MUL-T015), length or indexing yet. + - * / %and unary-takei32;< <= > >=takei32;== !=take two values of one type;&& || !takebool. Mismatch:MUL-T012.- Precedence: unary
- !>* / %>+ -> comparisons >&&>||; left-associative; comparisons do not chain (a < b < cisMUL-S005). - Evaluation is left to right; call arguments before the call.
&&/||skip the right operand when the left one decides the result. - Overflow traps (
MUL-R002), never wraps; each operation is checked on its own. Division by zero traps (MUL-R003), checked before overflow;-2147483648 / -1and% -1overflow. The first trap in evaluation order wins. /truncates toward zero;%has the sign of the dividend:-7 / 2 = -3,-7 % 2 = -1.- A name
xis a parameter or variable;x(...)is a call. Separate namespaces. - Every initializer and assignment is evaluated, so its traps happen even if the value is unused.
- At most 256 operators, calls and parentheses per expression; blocks nest at most 64 deep; at most 10 000 active calls.
Not available
No module-level constants, for, break/continue, if as an expression,
string operations, printing numbers, input, other numeric types, conversions, structs, imports,
generics, exceptions, effects other than pure/io. A parameterless function plays the
role of a constant.
Errors
Compile-time and run-time: file:line:col: error[CODE]: message [byte N] (byte offset from
0, for one-line files; a long line is shown around the error). A run-time trap points at
the operation that caused it, ends the program, exit code 1; earlier output stays.
Normative rules: RFC-003 (SEM-NN). mul test <dir> runs .mul files that
start with // expect: <line> (last one: value of main) or end the header
with // expect-error: <code>.
| Code | Meaning |
|---|---|
| S001 | unexpected character |
| S002 | unexpected token |
| S003 | expression too complex |
| S004 | blocks nested deeper than 64 |
| S005 | chained comparison (a < b < c) |
| S006 | bad string literal (quote, escape, control character) |
| T001 | duplicate function, or a function named print |
| T002 | unknown type (use i32, bool or string) |
| T003 | literal out of i32 range |
| T004 | some path does not end with return (a while never ends one) |
| T005 | code after return (or after a block or if that always returns) |
| T006 | duplicate parameter |
| T007 | unknown name (not declared, or its block has ended) |
| T008 | unknown function |
| T009 | wrong number of arguments |
| T010 | name already defined (no shadowing) |
| T011 | assignment to a let or a parameter (use var) |
| T012 | type mismatch (i32 vs bool) |
| T013 | pure function calls an io one or print |
| T014 | print used as a value |
| T015 | ==/!= on string |
| R001 | no main |
| R002 | overflow |
| R003 | division by zero |
| R005 | call depth over 10 000 |
| R006 | main is not fn() -> i32 |
Codes are written MUL-<code>, e.g. MUL-T007. MUL-V* and MUL-R004 are
compiler bugs.
Idioms
// expect: 55
module demo fn mod(a:i32,m:i32)->i32{return((a%m)+m)%m}fn sumTo(n:i32)->i32{var total=0 var i=1 while i<=n{total=total+i i=i+1}return total}pub fn main()->i32{return sumTo(10)*mod(-7,2)}
This page is docs/language/ in the repository, published as it is. Every mul code block on it is compiled and run by the test suite.