Conlang: Implement functions 1.0 (Resolves #12)

- [x] Call syntax
- [x] Function declaration syntax
TODO:
- [ ] Any form of type checking
- [ ] Static variable resolution
- [ ] Closures, etc.
This commit is contained in:
2023-10-29 23:47:00 -05:00
parent f3306e7ba4
commit ee5dabb4f3
7 changed files with 587 additions and 200 deletions

View File

@@ -11,16 +11,26 @@ Identifier = IDENTIFIER ;
(* statement *)
Stmt = Fn | Let | Expr ';' ;
Let = "let" "mut"? Identifier (':' Type)? ('=' Expr)? ';' ;
Fn = "fn" Identifier Block ; (* TODO: params, return value*)
Fn = "fn" Identifier '(' Params? ')' Block ;
(* TODO: Type system *)
Params = (Param ',')* Param? ;
Param = Identifier (*':' Type *) ;
(* # Expressions *)
(* expression *)
Expr = Assign ;
Block = '{' Stmt* Expr? '}' ;
Group = '(' Expr? ')' ;
Primary = Identifier | Literal
| Block | Group | Branch ;
(* expression::call *)
Call = FnCall | Primary ;
FnCall = Primary ('(' Tuple? ')')? ;
(* expression::tuple *)
Group = '(' Tuple? ')' ;
Tuple = Expr (',' Expr)* ;
(* expression::math *)
Assign = Identifier (AssignOp Assign) | Compare ;
Compare = Range (CompareOp Range )* ;
@@ -30,7 +40,7 @@ Bitwise = Shift (BitwiseOp Shift )* ;
Shift = Term (ShiftOp Term )* ;
Term = Factor (TermOp Factor )* ;
Factor = Unary (FactorOp Unary )* ;
Unary = (UnaryOp)* Primary ;
Unary = (UnaryOp)* Call ;
(* expression::math::operator *)
AssignOp = '=' | "+=" | "-=" | "*=" | "/=" |
@@ -50,7 +60,7 @@ Branch = While | If | For | Break | Return | Continue ;
If = "if" Expr Block (Else)? ;
While = "while" Expr Block (Else)? ;
For = "for" Identifier "in" Expr Block (Else)? ;
Else = "else" Block ;
Else = "else" Expr ;
Break = "break" Expr ;
Return = "return" Expr ;
Continue = "continue" ;