diff options
Diffstat (limited to 'docs/SYNTAX.md')
-rw-r--r-- | docs/SYNTAX.md | 98 |
1 files changed, 51 insertions, 47 deletions
diff --git a/docs/SYNTAX.md b/docs/SYNTAX.md index 1bd3331..6561acb 100644 --- a/docs/SYNTAX.md +++ b/docs/SYNTAX.md @@ -6,18 +6,18 @@ The following keywords are reserved: - variables: `let` `var` `const` -- control flow: `if` `elif` `else` +- control flow: `if` `then` `elif` `else` - pattern matching: `match` `of` -- loops: `loop` `while` `for` `in` -- blocks: `block` `break` `continue` `return` -- functions: `func` `mut` `static` `varargs` -- modules: `pub` `mod` `use` `as` - error handling: `try` `catch` `finally` -- metaprogramming: `macro` `quote` `when` -- types: `type` `distinct` `ref` -- types: `struct` `tuple` `union` `enum` `interface` +- loops: `while` `do` `for` `in` +- blocks: `loop` `block` `break` `continue` `return` +- modules: `pub` `mod` `use` `as` +- functions: `func` `varargs` +- metaprogramming: `macro` `quote` `static` `when` +- ownership: `lent` `mut` `ref` `refc` +- types: `type` `distinct` `struct` `tuple` `union` `enum` `class` - reserved: - - `impl` `object` `class` `concept` `auto` `empty` `effect` `case` + - `impl` `object` `interface` `concept` `auto` `empty` `effect` `case` - `suspend` `resume` `spawn` `pool` `thread` `closure` - `cyclic` `acyclic` `sink` `move` `destroy` `copy` `trace` `deepcopy` @@ -37,24 +37,25 @@ The following identifiers are in use by the standard prelude: The following punctuation is taken: - `=` (assignment) - `.` (chaining) -- `,` (params) +- `,` (parameters) - `;` (statements) - `:` (types) - `#` (comment) +- `@` (attributes) - `_` (unused bindings) - `|` (generics) - `\` (string/char escaping) -- `()` (params, tuples) -- `{}` (scope, structs) +- `()` (parameters, tuples) - `[]` (generics, lists) +- `{}` (scope, structs) - `""` (strings) - `''` (chars) - ``` `` ``` (unquoting) -- unused: `~` `@` `$` `%` +- unused: `~` `$` `%` ## A Formal Grammar -We now shall take a look at a more formal description of Puck's syntax. +We now shall take a look at a more formal description of Puck's syntax. Syntax rules are described in [extended Backus–Naur form](https://en.wikipedia.org/wiki/Extended_Backus–Naur_form) (EBNF): however, most rules surrounding whitespace, and scope, and line breaks, are modified to how they would appear after a lexing step. @@ -105,61 +106,64 @@ Struct ::= '{' Ident ':' Expr (',' Ident ':' Expr)* '}' ### Variables ``` Decl ::= Let | Var | Const | Func | Type -Let ::= 'let' Pattern Annotation? '=' Expr -Var ::= 'var' Pattern Annotation? ('=' Expr)? -Const ::= 'pub'? 'const' Pattern Annotation? '=' Expr +Let ::= 'let' Pattern (':' Type)? '=' Expr +Var ::= 'var' Pattern (':' Type)? ('=' Expr)? +Const ::= 'pub'? 'const' Pattern (':' Type)? '=' Expr Pattern ::= Char | String | Number | Float | Ident | '(' Pattern (',' Pattern)* ')' Ident '(' Pattern (',' Pattern)* ')' ``` ### Declarations ``` -Func ::= 'pub'? 'func' Ident Generics? Parameters? Annotation? '=' Body -Macro ::= 'pub'? 'macro' Ident Generics? Parameters? Annotation? '=' Body -Generics ::= '[' Ident Annotation? (',' Ident Annotation?)* ']' -Parameters ::= '(' Ident Annotation? (',' Ident Annotation?)* ')' -Annotation ::= ':' Type +Func ::= 'pub'? 'func' Ident Generics? Parameters? (':' Type)? '=' Body +Macro ::= 'pub'? 'macro' Ident Generics? Parameters? (':' Type)? '=' Body +Generics ::= '[' Ident (':' Type)? (',' Ident (':' Type)?)* ']' +Parameters ::= '(' Ident (':' Type)? (',' Ident (':' Type)?)* ')' ``` +All arguments to functions must have a type. This is resolved at the semantic level, however. +(Arguments to macros may lack types. This signifies a generic node.) + ### Types ``` TypeDecl ::= 'pub'? 'type' Ident Generics? '=' Type -Type ::= StructType | TupleType | EnumType | UnionType | Interface | - (('distinct' | 'ref' | 'ptr' | 'mut' | 'static') (Type | ('[' Type ']'))?) -StructType ::= 'struct' ('[' Ident ':' Type (',' Ident ':' Type)* ']')? -UnionType ::= 'union' ('[' Ident ':' Type (',' Ident ':' Type)* ']')? -TupleType ::= 'tuple' ('[' (Ident ':')? Type (',' (Ident ':')? Type)* ']')? -EnumType ::= 'enum' ('[' Ident ('=' Expr)? (',' Ident ('=' Expr)?)* ']')? -Interface ::= 'interface' ('[' Signature (',' Signature)* ']')? -Signature ::= Ident Generics? ('(' Type (',' Type)* ')')? Annotation? +Type ::= TypeStruct | TypeTuple | TypeEnum | TypeUnion | TypeClass | + (Modifier* (Type | ('[' Type ']'))) +TypeStruct ::= 'struct' ('[' Ident ':' Type (',' Ident ':' Type)* ']')? +TypeUnion ::= 'union' ('[' Ident ':' Type (',' Ident ':' Type)* ']')? +TypeTuple ::= 'tuple' ('[' (Ident ':')? Type (',' (Ident ':')? Type)* ']')? +TypeEnum ::= 'enum' ('[' Ident ('=' Expr)? (',' Ident ('=' Expr)?)* ']')? +TypeClass ::= 'class' ('[' Signature (',' Signature)* ']')? +Modifier ::= 'distinct' | 'ref' | 'refc' | 'ptr' | 'lent' | 'mut' | 'static' +Signature ::= Ident Generics? ('(' Type (',' Type)* ')')? (':' Type)? ``` ## Control Flow ``` -If ::= 'if' Expr ':' Body ('elif' Expr ':' Body)* ('else' ':' Body)? -When ::= 'when' Expr ':' Body ('elif' Expr ':' Body)* ('else' ':' Body)? -Try ::= 'try' ':' Body - ('except' Ident ('as' Ident)? (',' Ident ('as' Ident)?)*) ':' Body)* - ('finally' ':' Body)? -Match ::= 'match' Expr ('of' Pattern (',' Pattern)* ('where' Expr)? ':' Body)+ -Block ::= 'block' Ident? ':' Body -Block ::= 'static' ':' Body -Loop ::= 'loop' ':' Body -While ::= 'while' Expr ':' Body -For ::= 'for' Pattern 'in' Expr Body +If ::= 'if' Expr 'then' Body ('elif' Expr 'then' Body)* ('else' Body)? +When ::= 'when' Expr 'then' Body ('elif' Expr 'then' Body)* ('else' Body)? +Try ::= 'try' Body + ('except' Ident ('as' Ident)? (',' Ident ('as' Ident)?)*) 'then' Body)+ + ('finally' Body)? +Match ::= 'match' Expr ('of' Pattern (',' Pattern)* ('where' Expr)? 'then' Body)+ +While ::= 'while' Expr 'do' Body +For ::= 'for' Pattern 'in' Expr 'do' Body +Loop ::= 'loop' Body +Block ::= 'block' Ident? Body +Static ::= 'static' Body +Quote ::= 'quote' QuoteBody ``` ## Modules ``` -Mod ::= 'pub'? 'mod' Ident ':' Body -Use ::= 'use' Ident ('/' Ident)* ('/' ('[' Ident (',' Ident)* ']'))? +Mod ::= 'pub'? 'mod' Ident Body +Use ::= 'use' Ident ('.' Ident)* ('.' ('[' Ident (',' Ident)* ']'))? ``` ### Operators ``` Operator ::= 'and' | 'or' | 'not' | 'xor' | 'shl' | 'shr' | - 'div' | 'mod' | 'rem' | 'is' | 'in' | - Opr+ + 'div' | 'mod' | 'rem' | 'is' | 'in' | Opr+ Opr ::= '=' | '+' | '-' | '*' | '/' | '<' | '>' | '@' | '$' | '~' | '&' | '%' | '|' | '!' | '?' | '^' | '.' | ':' | '\\' @@ -170,10 +174,10 @@ Opr ::= '=' | '+' | '-' | '*' | '/' | '<' | '>' | Call ::= Ident ('[' Call (',' Call)* ']')? ('(' (Ident '=')? Call (',' (Ident '=')? Call)* ')')? | Ident Call (',' Call)* | Call Operator Call? | - Call ':' Body + Call Body Expr ::= Let | Var | Const | Func | Type | Mod | Use | Block | Static | For | While | Loop | If | When | Try | Match | Call -Body ::= Expr | ('{' Expr (';' Expr)* '}') +Body ::= Expr | (Expr (';' Expr)*) ``` --- |