diff options
Diffstat (limited to 'grammar.ebnf')
| -rw-r--r-- | grammar.ebnf | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/grammar.ebnf b/grammar.ebnf index 1805abb..425c64a 100644 --- a/grammar.ebnf +++ b/grammar.ebnf @@ -1,16 +1,52 @@ (* Grammar definition in EBNF format. *) -Expression = TermExpression; +Program = {Statement} EOF; -TermExpression = FactorExpression { ("+" | "-") FactorExpression }; +(* Statement *) -FactorExpression = UnaryExpression { ("*" | "/") UnaryExpression }; +Statement = ExpressionStatement | ReturnStatement | PrintStatement; + +ExpressionStatement = Expression ";"; +ReturnStatement = "return" Expression ";"; +(* NOTE: Hard-coded PrintStatement until Rabbithole has an standard library *) +PrintStatement = "print" Expression ";"; + +(* Expression *) -UnaryExpression = ( "-" | "!" ) | GroupExpression; +Expression = AssignmentExpression; + +(* Expressions affected by precedence*) + +(* NOTE: Only allow IDENTIFIERs and their fields as the LValue for now. *) +AssignmentExpression = IDENTIFIER {"." IDENTIFIER} ("=" | ":=") AssignmentExpression | TermExpression; +TermExpression = FactorExpression { ("+" | "-") FactorExpression }; +FactorExpression = UnaryExpression { ("*" | "/") UnaryExpression }; +UnaryExpression = ( "-" | "!" ) UnaryExpression | UnitExpression ; -UnitExpression = NaturalDigit {Digit} | "(" Expression ")";; +(* Unaffected Expressions *) + +UnitExpression = FLOAT | INT | STR | GroupExpression | BlockExpression | FnExpression | | TypeExpression | FormExpression | IfExpression; + +GroupExpression = "(" Expression ")"; +BlockExpression = Block; +FnExpression = "fn" FnHeader Block; +TypeExpression = "type" TypeBlock; +(* NOTE: Will associated functions clash with fields? *) +FormExpression = "form" TypeBlock; +IfExpression = "if" Expression Block { "elif" Expression Block } [ "else" Block ]; + +(* Parts *) + +FnHeader = (FnSingleParameter | FnMultipleParameters) ["->" Type]; +FnSingleParameter = "self" | FnParameter; +FnMultipleParameters = "(" FnSingleParameter { "," FnParameter} ")"; +FnParameter = IdentiferType; + +(* Utils *) -(* Basics *) +Block = "{" { Statement } [Expression] "}"; +TypeBlock = "{" [ IdentiferType ] { "," IdentiferType } "}"; -NaturalDigit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"; -Digit = "0" | NaturalDigit ; \ No newline at end of file +IdentiferType = IDENTIFIER ":" Type; +(* NOTE: Type doesn't include anything other than simple named types for now. *) +Type = IDENTIFIER; |
