Misty

Minimal Implementation Language: Minl

In another thirty years people will laugh at anyone who tries to invent a language without closures, just as they'll laugh now at anyone who tries to invent a language without recursion.

Mark Jason Dominus (2005)

The Misty virtual machine will be implemented in Minl. Minl is a minimal systems programming language. It is a low level systems language for implementing operating system kernels, memory managers, virtual machines, and drivers. It is a slightly lower level language than BCPL. Its functions do not support recursion, which reduces the cost of function invocation. It is not intended to be used as an application language or for writing compilers.

There is only one type in the language: a 64 bit word. It can be used to represent numbers, bit fields, characters, and pointers to words. Pointers point to 64-bit values, not bytes.

Values are not signed. Instead, there are signed and unsigned operators. The signed operators have a $ prefix as in $> (signed greater than) and $right (shift right with sign extension). The if statement treats 0 as false, and other values as true. The relational operators return -1 for true. The truth operator returns -1 if its operand is not zero. The not operator flips all of the bits.

Note that Misty has very different conventions for representing logical values and pointers.

Minl does not multiply or divide.

A comment starts with # and goes to the end of the line.

All memory allocation is static. The locations of all variables and parameters are known at compile time. The language does not provide any dynamic memory allocation services because the language is intended to be used to implement such services.

A module begins with the word minl followed by the type and name of the module. The type is either program or inclusion.

The [ ]brackets dereferencing operator uses the left side as pointer, and the right side as a word offset from the pointer. As an rval it retrieves a word from memory. As an lval in an assign statement, it replaces a word in memory. The right side offset locates a word. Do not use + to do pointer arithmetic. Instead use @pointer[offset]. It is not possible to obtain pointers to variables or functions.

A program is composed of several divisions: include constant variable external private public.

include

The include division contains a list of one or more text literals that identify inclusions.

include
    "standard.minl"

constant

The constant division contains constants. The values can be expressions containing number literals and previously declared constants.

constant
    true: 896
    false: 640
    null: 128

variable

The variable division is used to declare and initialize variables. The initializes can be expressions containing number literals and previously declared constants and variables. The contents of variables are changed by the assign statement.

variable
    i: 0
    j: i + 1

external

The external division creates linkages to functions. The functions may be from other languages. A function can be identified as foreign by specifying a language (such as c, cplusplus, or asm). This determines the name munging, register preservation, and argument passing for the function.

external
    exit.c(exit_code)

private

The private division creates functions. All of these functions will be inlined. The local division declares variables in a function that are initialized every time the function is called and forgotten when it returns.

private
    double(number)
    begin
        return number left 1
    end double

    triple(number)
    local
        dub: double(number)
    begin
        return dub + number
    end triple

    remove_trailing_zeros(n)
    begin
        do loop
            if n and 1 <> 0
                break loop
            fi
            assign n: n right 1
        od loop
        return n
    end remove_trailing_zeros

    count_trailing_zeros(n)
    local
        count: 0
    begin
        if n = 0
            return 64
        fi
        do
            if n and 1 <> 0
                return count
            fi
            assign n: n right 1
            assign count: count + 1
        od
    end count_trailing_zeros

public

The public division creates functions that can be called from the outside. In most languages, a program has a single entry point. In Minl, each public function is an entry point.

public
    greatest_common_divisor(a, b)
    local
        shift
        temp
    begin
        if a = 0
            return b
        fi
        if b = 0
            return a
        fi
        assign shift: count_trailing_zeros(a or b)
        assign a: a right shift
        assign b: b right shift
        do
            if a < b
                assign b: b - a
            else
                assign temp: a
                assign a: b
                assign b: b - temp
            fi
            if b = 0
                break
            fi
            assign b: remove_trailing_zeros(b)
        od
        return a left shift
    end gcd

Functions are very light weight. There is no call stack. Functions are not reentrant. Recursion is not allowed. Functions may not be nested. A function can take at most 4 arguments. Variadic varargs are not supported.

The language does not have exceptions because it does not have a stack to unwind.

Grammar

misty "minl" space type space title linebreak divisions "end" space title

type "program" "inclusion"

divisions division division linebreak divisions

division "include" indent include outdent "constant" indent constant outdent "variable" indent variable outdent "external" indent external outdent "private" indent private outdent "public" indent public outdent

include text text linebreak include

constant declare declare linebreak variable

variable declare declare linebreak variable

external signature signature linebreak external

private function function linebreak private

public function function linebreak public

signature name '(' parameters ')'

external_signature name language '(' parameters ')'

language "" '.' name

declare name ':' space expression

function signature linebreak local begin indent statements outdent "end" space name

parameters "" more_parameters

more_parameters name name space more_parameters

local "" "local" indent locals outdent

locals name_or_declare name_or_declare linebreak locals

name_or_declare name declare

begin "begin" indent statements outdent

statement assignment_statement break_statement call_statement do_statement if_statement return_statement

statements statement statement linebreak statements

assignment_statement "assign" space declare

break_statement "break" label

call_statement "call" expression '(' expressions ')'

do_statement "do" label indent statements outdent "od" label

if_statement "if" expression indent statements outdent optional_else "fi"

optional_else "" else

else "else" space "if" expression statements else "else" indent statements outdent

return_statement "return" "return" space expression

label "" space name

expression paren_expression suffix number suffix name suffix prefix_op space expression suffix '@' expression dereference

expressions "" expression expression space expressions

open_expression paren_expression open_suffix number open_suffix name open_suffix space prefix_op separation expression open_suffix

open_expressions "" expression expression linebreak expressions

paren_expression '(' paren_filler ')'

paren_filler expression indent open_expression optional_ternary outdent

optional_ternary "" ternary

ternary linebreak "then" space expression linebreak "else" space expression

prefix_op "abs" absolute value "neg" negate "not" bitwise not (word) "truth" negate

infix_op '+' addition '-' subtraction "left" left shift "right" right shift "$right" right shift with sign extension "rotate" rotate left "and" bitwise and "or" bitwise or "xor" bitwise exclusive or "bit" bit selection "/\" short circuit logical and "\/" short circuit logical or '=' equal "<>" not equal '$<' signed less than "$<=" signed less than or equal '$>' signed greater than "$>=" signed greater than or equal "<" unsigned less than "<=" unsigned less than or equal ">" unsigned greater than ">=" unsigned greater than or equal

invocation '(' arguments ')'

arguments "" expression closed_arguments indent open_expression arguments_or_ternary outdent

closed_arguments "" space expression closed_arguments

arguments_or_ternary ternary open_arguments

open_arguments "" open_expression open_arguments

dereference '[' dereference_filler ']'

dereference_filler "" expression indent open_expression outdent

suffix "" invocation suffix dereference suffix space infix_operator space expression

open_suffix "" invocation open_suffix dereference open_suffix space infix_operator separation open_expression

separation space linebreak

Operator Precedence

Operator Precedence
tightest ( )   [ ]
  @
abs   neg   not   truth
left   right   $right   rotate
and or xor
bit
+   -
=   <>   <   >   <=   >=  $<   $>   $<=   $>=
\/   /\
loosest then else

Infix operators of the same precedence level group left to right. Higher-precedence operators bind tighter than lower-precedence ones. The suffix operators have the highest precedence (tightest). The ternary operator has the lowest (loosest) precedence. Grouping can be made explicit with parens.

Examples:
7 + 5 and 1                        # 8
10 - 5 + 2                         # 7
false /\ true \/ true              # true