olang | O Programming Language


5 Specification

5.1 Introduction

This document specifies the semantics and behavior of the O Programming Language for compiler designers be informed how the language is designed.

This specification is a DRAFT and will be the discussions drive over olang-dev mailing list.

5.2 Language Syntax

This is the O Programming Language EBNF grammar specification[^1]

[^1]: EBNF variant https://github.com/Engelberg/instaparse/tree/v1.4.12 and live test can be accessed here https://mdkrajnak.github.io/ebnftest/

NOTE: This grammar spec is a DRAFT and it covers only a small portion of the language.

(* Entry Point *)
<translation-unit> ::= (<ows> (<external-declaration>) <ows> (<end-of-statement> | <end-of-file>))*

(* Translation Unit *)
<external-declaration> ::= <common-statement> | <function-definition>

(* Variables *)
<variable-definition>  ::= 'var' <ws> <variable-name> <ows> ':' <ows> <type> <ows> <variable-initializer>?
<constant-definition>  ::= 'const' <ws> <variable-name> <ows> ':' <ows> <type> <ows> <variable-initializer>
<variable-name>        ::= <identifier>
<variable-initializer> ::= '=' <ows> <expression>

(* Functions *)
<function-definition> ::= ('extern' <ws>)? 'fn' <ws> <function-name> <ows> '(' ( <ows> | <ows> <function-params> <ows> ) ')' <ows> ':' <ows> <return-type> <ows> <function-body>?
<function-name>       ::= <identifier>
<function-params>     ::= <identifier> <ows> ':' <ows> <type> ( <ows> ',' <ows> <function-params>)*
<return-type>         ::= <type>
<function-body>       ::= <block>
<block>               ::= '{' <ows> <statement> <ows> (<end-of-statement> <ows> <statement> <ows>)* <end-of-statement>? <ows> '}'
<function-args>       ::= <expression> (<ows> ',' <ows> <function-args>)*
<function-call>       ::= <function-name> <ows> '(' ( <ows> | <ows> <function-args> <ows> ) ')'
<statement>           ::= <common-statement> | <if-statement> | <while-statement> | <return-statement> | <function-call>
<if-statement>        ::= 'if' <ws> <expression> <ows> <block> ( <ows> 'else' ( <ows> <block> | <ows> <if-statement> ) )?
<while-statement>     ::= 'while' <ws> <expression> <ows> <block>
<return-statement>    ::= 'return' <ws> <expression>

(* Statements *)
<end-of-statement>    ::= ';' | <line-break>
<common-statement>    ::= <variable-definition> | <constant-definition> | <expression>
<assignment-operator> ::= '='
                        | '*='
                        | '/='
                        | '%='
                        | '+='
                        | '-='
                        | '<<='
                        | '>>='
                        | '&='
                        | '^='
                        | '|='

(* Expressions *)
<expression>                ::= <binary-expression>
<binary-expression>         ::= <assignment-expression>
<assignment-expression>     ::= <logical-or-expression> (<ows> <assignment-operator> <ows> <logical-or-expression>)*
<logical-or-expression>     ::= <logical-and-expression> (<ows> '||' <ows> <logical-and-expression>)*
<logical-and-expression>    ::= <bitwise-or-expression> (<ows> '&&' <ows> <bitwise-or-expression>)*
<bitwise-or-expression>     ::= <bitwise-xor-expression> (<ows> '|' <ows> <bitwise-xor-expression>)*
<bitwise-xor-expression>    ::= <bitwise-and-expression> (<ows> '^' <ows> <bitwise-and-expression>)*
<bitwise-and-expression>    ::= <cmp-equality-expression> (<ows> '&' <ows> <cmp-equality-expression>)*
<cmp-equality-expression>   ::= <cmp-relational-expression> (<ows> ('==' | '!=') <ows> <cmp-relational-expression>)*
<cmp-relational-expression> ::= <bitwise-shift-expression> (<ows> ('<' | '>' | '<=' | '>=') <ows> <bitwise-shift-expression>)*
<bitwise-shift-expression>  ::= <additive-expression> (<ows> ('<<' | '>>') <ows> <additive-expression>)*
<additive-expression>       ::= <multiplicative-expression> (<ows> ('+' | '-') <ows> <multiplicative-expression>)*
<multiplicative-expression> ::= <unary-expression> (<ows> ('*' | '/' | '%') <ows> <unary-expression>)*
<unary-expression>          ::= (<unary-operator> <ows>)* <primary-expression>
<unary-operator>            ::= '&'
                              | '*'
                              | '+'
                              | '-'
                              | '~'
                              | '!'
<primary-expression>        ::= <integer-literal>
                              | <variable-name>
                              | <function-call>
                              | '(' <ows> <expression> <ows> ')'

(* Identifiers *)
<type>       ::= ('u8' | 'u16' | 'u32' | 'u64') (<ows> '*')*

<identifier> ::= (<alpha> | '_') (<alpha> | <digit> | '_')*

(* Literals *)
<integer-literal> ::= <integer-base10> | <integer-base16>
<integer-base10>  ::= #'[1-9]' (<digit> | '_')* | '0'
<integer-base16>  ::= #'0[Xx]' <hex-digit> (<hex-digit> | '_')*

(* Utilities *)
<ws>           ::= <white-space>+
<ows>          ::= <white-space>*
<white-space>  ::= <linear-space> | <line-break>
<line-break>   ::= #'[\n\v\f\r]' | '\r\n'
<linear-space> ::= #'[ \t]'
<alpha>        ::= #'[a-zA-Z]'
<digit>        ::= #'[0-9]'
<hex-digit>    ::= <digit> | #'[a-fA-F]'
<end-of-file>  ::= #'$'