aboutsummaryrefslogtreecommitdiff

Sil

A compiler for the simple imperative language sil. This compiler is written in Clean and compiles to ABC-assembly, which can be used to generate actual machine code using Clean's code generator or can be interpreted with the ABCMachine project.

Grammar

<Program>        ::= <Initialisation>-list <Function>-list

<Function>       ::= <Type> <Name> '(' <Arg>-clist ')' '{' <CodeBlock> '}'

<CodeBlock>      ::= <Initialisation>-list <Statement>-list

<Initialisation> ::= <Type> <InitName>-clist ';'

<InitName>       ::= <Name> [':=' <Expression>]

<Statement>      ::= [<Name> ':='] <Expression> ';'
                  |  'return' [<Expression>] ';'
                  |  'if' '(' <Expression> ')' '{' <CodeBlock> '}'
                         ['else' 'if' '(' <Expression> ')' '{' <CodeBlock> '}']-list
                         ['else' '{' <CodeBlock> '}']
                  |  'while' '(' <Expression> ')' '{' <CodeBlock> '}'
                  |  '|~' <MachineCode>

<Expression>     ::= <Name>
                  |  <Literal>
                  |  <Name> '(' <Expression>-clist ')'  // Function application
                  |  <Op1> <Expression>                 // Unary operator
                  |  <Expression> <Op2> <Expression>    // Binary operator
                  |  '(' <Expression>-clist ')'         // Tuple
                  |  '[' <Type> ']'                     // Empty list
                  |  '[' <Expression>-clist ']'         // List shortcut
                  |  <Expression> '.' <Name>            // Field application
                  |  '(' <Expression> ')'               // Parenthised expression

<Op1>            ::= '~' | '!'
<Op2>            ::= '+' | '-' | '*' | '/' | '%'           // Int Int -> Int
                  | '==' | '<>' | '<' | '>' | '<=' | '>='  // Int Int -> Bool
                  | '||' | '&&'                            // Bool Bool -> Bool
                  | ':'                                    // a [a] -> [a]

<Type>           ::= 'Bool' | 'Int' | 'Void'
                  |  '(' <Type>-clist ')'     // Tuple
                  |  '[' <Type> ']'           // List

<Literal>        ::= <Bool> | <Int>
<Bool>           ::= 'True' | 'False'
<Int>            ::= <Digit>-list

Comments (single-line //, multi-line /* ... */) are not included in the grammar above.

Dependencies

You need a working Clean installation to build the compiler and at least clm and the code generator to use it.

Vim highlighting

In the vim directory.

Author & License

Copyright © 2017 Camil Staps. Licensed under MIT, see the LICENSE file.