Add syntax about associated functions in the documentation
This commit is contained in:
		
							parent
							
								
									7b27f30b9e
								
							
						
					
					
						commit
						7e3a13cf55
					
				| @ -24,11 +24,11 @@ Syntax for Reid is very much inspired by rust, and examples of the language can | |||||||
| be found in the [examples](../examples/)-folder. | be found in the [examples](../examples/)-folder. | ||||||
| 
 | 
 | ||||||
| In Reid **modules** (or files) on the top-level are comprised of imports, type | In Reid **modules** (or files) on the top-level are comprised of imports, type | ||||||
| definitions, binop-definitions and functions. | definitions, binop-definitions, functions and type-associated function blocks. | ||||||
| 
 | 
 | ||||||
| In formal grammar | In formal grammar | ||||||
| ```bnf | ```bnf | ||||||
| <module> :: (<import> | <type-definition> | <binop-definition> | <function>)* | <module> :: (<import> | <type-definition> | <binop-definition> | <function> | <assoc-function-block>)* | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| Table of Contents: | Table of Contents: | ||||||
| @ -38,6 +38,7 @@ Table of Contents: | |||||||
|     - [Struct types](#struct-types) |     - [Struct types](#struct-types) | ||||||
| - [Binary operation Definitions](#binary-operation-definitions) | - [Binary operation Definitions](#binary-operation-definitions) | ||||||
| - [Function definitions](#function-definition) | - [Function definitions](#function-definition) | ||||||
|  |   - [Associated functions](#associated-functions) | ||||||
| - [Statement](#statement) | - [Statement](#statement) | ||||||
| - [Expression](#expression) | - [Expression](#expression) | ||||||
| 
 | 
 | ||||||
| @ -140,7 +141,7 @@ impl binop (lhs: u16) + (rhs: u32) -> u32 { | |||||||
| 
 | 
 | ||||||
| ### Function Definition | ### Function Definition | ||||||
| 
 | 
 | ||||||
| Rust syntax for defining functions is similar to rust. There are two types of functions: | Reid syntax for defining functions is similar to rust. There are two types of functions: | ||||||
| 1. `extern` functions which are defined in another module, used to define functions from outside modules such as `libc`. | 1. `extern` functions which are defined in another module, used to define functions from outside modules such as `libc`. | ||||||
| 2. `local` functions which are defined locally in the module in Reid. Their | 2. `local` functions which are defined locally in the module in Reid. Their | ||||||
|    definition is contained within a `block` which contains a list of |    definition is contained within a `block` which contains a list of | ||||||
| @ -153,8 +154,9 @@ In formal grammar: | |||||||
| <local-function> :: [ "pub" ] "fn" <signature> <block> | <local-function> :: [ "pub" ] "fn" <signature> <block> | ||||||
| 
 | 
 | ||||||
| <signature> :: <ident> "(" [ <params> ] ")" [ "->" <type> ] | <signature> :: <ident> "(" [ <params> ] ")" [ "->" <type> ] | ||||||
| <params> <param> ( "," <param> )* | <params> :: <param-or-self> ( "," <param> )* | ||||||
| <param> :: <ident> ":" <type> | <param-or-self> = <param> | ( [ "&" [ "mut" ] ] "self") | ||||||
|  | <param> :: (<ident> ":" <type>) | ||||||
| <block> :: "{" <statement>* "}" | <block> :: "{" <statement>* "}" | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| @ -167,6 +169,27 @@ fn main() -> u8 { | |||||||
| } | } | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
|  | #### Associated Functions | ||||||
|  | 
 | ||||||
|  | Reid also has a very similar syntax for defining associated functions as Rust | ||||||
|  | does. They are also the only types of functions where usage of initial | ||||||
|  | "self"-param is allowed, referring to a potential self-type. Associated | ||||||
|  | functions are functions that are defined within certain types such that you can | ||||||
|  | have multiple functions of the same name, as long as they are associated with a | ||||||
|  | different type. In formal grammar associated function blocks are: | ||||||
|  | ```bnf | ||||||
|  | <assoc-function-block> :: "impl" <type> "{" <function-definition>* "}" | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | An example of such a block could be: | ||||||
|  | ```rust | ||||||
|  | impl Test { | ||||||
|  |     fn get_field(&self) -> u32 { | ||||||
|  |         *self.field | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
| ### Statement | ### Statement | ||||||
| 
 | 
 | ||||||
| Statements in Reid is how you tell the program to do anything. Currently supported statements include: | Statements in Reid is how you tell the program to do anything. Currently supported statements include: | ||||||
| @ -222,6 +245,8 @@ calls, literals, or if-expressions. Types of supported expressions include: | |||||||
| - **Binary operations** (such as add/sub/mult) | - **Binary operations** (such as add/sub/mult) | ||||||
| - **Unary operations** (such as !value or -value) | - **Unary operations** (such as !value or -value) | ||||||
| - **Function calls**, to invoke a predefined function with given parameters | - **Function calls**, to invoke a predefined function with given parameters | ||||||
|  | - **Associated function calls**, to invoke a predefined function on a certain | ||||||
|  |   *associated type* with given parameters. | ||||||
| - **Block-expressions**, which can return a value to the higher-level expression | - **Block-expressions**, which can return a value to the higher-level expression | ||||||
|   if they have a statement with a soft-return. Otherwise they return void. |   if they have a statement with a soft-return. Otherwise they return void. | ||||||
| - **If-expressions**, which can execute one of two expressions depending on the | - **If-expressions**, which can execute one of two expressions depending on the | ||||||
| @ -238,8 +263,8 @@ In formal grammar: | |||||||
|     <array> | <struct> | |     <array> | <struct> | | ||||||
|     <indexing> | <accessing> | |     <indexing> | <accessing> | | ||||||
|     <binary-exp> | <unary-exp> | |     <binary-exp> | <unary-exp> | | ||||||
|     <function-call> | <block> | |     <function-call> | <assoc-function-call> | ||||||
|     <if-expr> | <cast> | |     <block> | <if-expr> | <cast> | | ||||||
|     ( "(" <expression> ")" ) |     ( "(" <expression> ")" ) | ||||||
| 
 | 
 | ||||||
| <variable> :: <ident> | <variable> :: <ident> | ||||||
| @ -253,6 +278,7 @@ In formal grammar: | |||||||
| <binary-exp> :: <expression> <binop> <expression> | <binary-exp> :: <expression> <binop> <expression> | ||||||
| <unary-exp> :: <unary> <expression> | <unary-exp> :: <unary> <expression> | ||||||
| <function-call> :: <expression> "(" [ <expression> ( "," <expression> )* ] ")" | <function-call> :: <expression> "(" [ <expression> ( "," <expression> )* ] ")" | ||||||
|  | <assoc-function-call> :: <type> "::" <function-call> | ||||||
| <if-expr> :: "if" <expression> <expression> [ "else" <expression> ] | <if-expr> :: "if" <expression> <expression> [ "else" <expression> ] | ||||||
| <cast> :: <expression> "as" <type> | <cast> :: <expression> "as" <type> | ||||||
| ``` | ``` | ||||||
| @ -269,6 +295,7 @@ test.first // Accessing | |||||||
| 7 + value // Binop | 7 + value // Binop | ||||||
| !bool_value // Unary | !bool_value // Unary | ||||||
| func(value, 14) // Function call | func(value, 14) // Function call | ||||||
|  | Test::get_field(&test); // Associated function call | ||||||
| if varname {} else {} // If-expression | if varname {} else {} // If-expression | ||||||
| value as u32 // cast | value as u32 // cast | ||||||
| (value + 2) // Binop within parenthesis | (value + 2) // Binop within parenthesis | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user