Almac language report

ortfero

Almac is a small imperative language for the aldan system. This report states the rules needed to implement its compiler; the standard library is written in almac and documented in its own modules.

1. Lexical rules

affine    as        asm       break     const
continue  defer     else      enum      failure
false     final     fn        for       from
guard     if        import    inline    into
mod       none      on        raise     record
return    true      union     var       when
with

2. Declarations and scope

3. Constants

3.1 Array constants

3.2 Record constants

3.3 Scoped constants

4. Types

4.1 Basic types

type     size  align  meaning
bool     1     1      logical
byte     1     1      8-bit field, unsigned; the byte
hword    2     2      16-bit field, unsigned
word     4     4      32-bit field, unsigned
int      8     8      signed integer
dword    8     8      64-bit field, unsigned
double   8     8      IEEE-754 double
address  8     8      raw address, non-null
string                alias for []byte
integer               type of an unconverted literal

4.2 Arrays and slices

4.3 Records

4.4 Pointers

4.5 Enumerations and unions

4.6 Function types

5. Variables

5.1 Birth rule

5.2 Final variables

6. Expressions

6.1 Precedence (high to low)

postfix     call, index, field, deref
cast        as
unary       -
multiply    *  /  mod
add         +  -
relation    ==  !=  <  <=  >  >=   (non-associative)
assign      =  +=  -=              (non-chaining)

6.2 and, or, not

6.3 Arithmetic

6.4 Bit fields

6.5 Relations

6.6 Conversions

Explicit (x as t):

Implicit (assignment, argument, comparison):

7. Statements

7.1 Assignment

7.2 Calls

7.3 If

7.4 When

7.5 Guard

7.6 For

7.7 Return

7.8 Defer

7.9 Failures

7.10 With

8. Functions

8.1 Parameters

8.2 Inline functions

8.3 Forward declarations and aliases

8.4 Assembly bodies

9. Modules

9.1 Setup and teardown

10. Mirror

10.1 Failures

10.2 Types

0 primitive  1 array     2 record    3 pointer   4 slice
5 named      6 function  7 incomplete            8 enum
9 union     10 union subtype

10.3 Running source

value union nothing
  | number {of type_ref, item int}
  | real {of type_ref, item double}
  | bytes {of type_ref, item string}
  | raised {f fault_kind}
  | rejected {report string}
program union
  ready {entry dword, size int, of type_ref}
  | rejected {report string}

10.4 Code

none frame argument copy zero tag leave save restore
pass call release forget fault_check fault_out park
clean unpark raise on next_handler keep handled reraise
defer keep_result ret test skip_else loop brk cont when

10.5 Frames

10.6 Modules and the image

Appendix — Syntax

EBNF: = defines, | alternative, ( ) group, [ ] optional, { } repetition, "x" terminal.

module       = [ importlist ] { declaration }
                 [ "..." { definition } ] .
importlist   = "import" string { "," string } .

declaration  = functionproto | vardecl | constdecl
             | typedecl | failuredecl .
definition   = fndef | declaration .

functionproto = ident "fn" signature [ "as" qualident ] .
vardecl      = ( "var" | "final" ) vardefs .
constdecl    = ( ident | ident "." ident ) "=" constval .
constval     = element | [ "[" "]" type ] arrayconst
             | typename recordconst .
element      = constexpr | "fn" qualident | recordconst .
constexpr    = constterm { ( "+" | "-" ) constterm } .
constterm    = constcast
               { ( "*" | "/" | "mod" ) constcast } .
constcast    = constunary [ "as" type ] .
constunary   = [ "-" ] constprimary .
constprimary = constatom
             | ( "and" | "or" ) [ constatom
               { "," constatom } ]
             | "not" constatom .
constatom    = "true" | "false" | "none"
             | integer | hex | fp | character | string
             | qualident [ "." ident ]
             | "(" constexpr ")" .
arrayconst   = "[" element { "," element } [ "," ]
               "]" .
recordconst  = "{" [ constinit { "," constinit }
               [ "," ] ] "}" .
constinit    = ident ":" element .
typedecl     = ident
               ( ( "as" | "from" | "affine" ) typename
               | structtype ) .
failuredecl  = ident "failure"
               ( "as" qualident
               | "{" ident { "," ident } "}" ) .
vardefs      = vardef { "," vardef } [ "," ] .
vardef       = ident { "," ident } type .

fndef        = ident [ "inline" ] "fn" signature
               ( "as" qualident | "..."
               | { ( "var" | "final" ) vardefs }
                 ( body | asmbody ) ) .
asmbody      = "asm" [ "naked" ] compound .
signature    = "(" [ param { "," param } ] ")"
               [ "->" type ] [ failures ] .
param        = ( "into" | "var" | "from" ) ident type
             | ident [ "..." ] type .
failures     = "!" ( "..." | qualident
               { "|" qualident } [ "|" "..." ] ) .

type         = [ "const" ] ( typename | structtype ) .
structtype   = "^" type [ "|" "none" ]
             | "[" [ constexpr ] "]" type
             | "record" recdef
             | "enum" ident { "|" ident }
             | "union" unionvariant { "|" unionvariant }
             | "fn" signature .
typename     = qualident [ "." ident ] .
recdef       = "{" [ vardefs ] "}" .
unionvariant = ident [ recdef ] .
qualident    = ident [ "." ident ] .

body         = "{" [ stmts ] { handler } "}" .
handler      = "on" ( "..." [ "with" ident ]
                    | failureref { "|" failureref } )
               ":" stmts .
failureref   = qualident [ "." ident ] .
compound     = "{" [ stmts ] "}" .
stmts        = stmt { stmt } .
stmt         = return | defer | raise | for | forwith
             | break | continue | if | guard | when
             | with | ( expr ";" ) .
return       = "return" expr ";" .
defer        = "defer" expr ";" .
raise        = "raise" qualident "." ident ";" .
for          = "for" [ expr [ ";" expr ] ] compound .
forwith      = "for" "with" ident "=" expr ";" expr
               compound .
if           = "if" expr compound
               [ "else" ( if | compound ) ] .
guard        = "guard" ident [ typename ]
               "|" escape ";" .
escape       = "return" arg
             | "raise" qualident "." ident
             | "break" | "continue" .
when         = "when" expr
               "{" { case } [ elsecase ] "}" .
case         = "*" caselabel { "," caselabel }
               ":" stmts .
caselabel    = integer | hex | character | qualident .
elsecase     = "*" "else" ":" stmts .
with         = "with" ident "=" designator ";" .
designator   = atom { index | field | deref } .
break        = "break" ";" .
continue     = "continue" ";" .

expr         = assign .
assign       = compare
               [ ( "=" | "+=" | "-=" ) compare
               | "=" recordexpr ] .
recordexpr   = "{" [ fieldinit { "," fieldinit }
               [ "," ] ] "}" .
fieldinit    = ident ":" ( expr | recordexpr ) .
compare      = addition [ relation addition ] .
relation     = "==" | "!=" | "<" | "<=" | ">" | ">=" .
addition     = multiply { ( "+" | "-" ) multiply } .
multiply     = cast { ( "*" | "/" | "mod" ) cast } .
cast         = unary [ "as" type ] .
unary        = [ "-" ] postfix .
postfix      = primary { index | field | deref }
               [ args ] .
args         = arg { "," arg } .
arg          = [ "into" | "var" | "from" ] atom
               { index | field | deref }
             | "into" .
index        = "[" expr [ ":" expr ] "]" .
field        = "." ident .
deref        = "^" .
primary      = atom | ifexpr .
atom         = "none" | "true" | "false"
             | integer | hex | fp | character | string
             | qualident
             | "fn" atom { index | field | deref }
             | "(" expr ")" .
ifexpr       = "if" expr compound
               [ "else" ( ifexpr | compound ) ] .

ident        = letter { letter | digit } .
letter       = "a" ... "z" | "_" .
digit        = "0" ... "9" .
hexdigit     = digit | "a" ... "f" .
integer      = digit { digit } .
hex          = "0" "x" hexdigit { hexdigit } .
fp           = digit { digit } "." digit { digit }
               [ "e" [ "+" | "-" ] digit { digit } ] .
character    = "'" any-byte-except-quote "'" .
string       = '"' { any-byte-except-quote } '"'
             | shortstring .
shortstring  = "'"
               { any-byte-except-whitespace-or-closer } .
comment      = "--" { any-byte-except-newline } newline .