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}

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

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.