2023-01-04 22:32:55 +00:00
|
|
|
grammar xlang;
|
|
|
|
|
|
|
|
file : function+ EOF;
|
2023-01-07 07:54:53 +00:00
|
|
|
function : Identifier LeftParen parameterList? RightParen Colon type block;
|
2023-01-12 23:07:00 +00:00
|
|
|
parameterList : parameter (Comma parameter)*;
|
|
|
|
parameter : Identifier Colon type;
|
2023-01-07 07:54:53 +00:00
|
|
|
type : TypeInteger
|
|
|
|
| TypeBoolean
|
|
|
|
;
|
2023-01-04 22:32:55 +00:00
|
|
|
block : LeftBrace statement* RightBrace;
|
2023-01-12 23:07:00 +00:00
|
|
|
statement : If expr block (Else block)?
|
|
|
|
| While expr block
|
|
|
|
| For expr Semicolon expr Semicolon expr block
|
2023-01-07 16:43:25 +00:00
|
|
|
| Break Integer? Semicolon
|
|
|
|
| Continue Integer? Semicolon
|
2023-01-12 23:07:00 +00:00
|
|
|
| Return expr Semicolon
|
|
|
|
| Print expr Semicolon
|
|
|
|
| expr Semicolon
|
2023-01-04 22:32:55 +00:00
|
|
|
;
|
2023-01-12 23:07:00 +00:00
|
|
|
expr : Identifier assignmentOp expr
|
|
|
|
| booleanExpr
|
2023-01-05 16:44:30 +00:00
|
|
|
;
|
2023-01-12 23:07:00 +00:00
|
|
|
assignmentOp : Define | Assign | Increment | Decrement;
|
|
|
|
booleanExpr : comparisonExpr (booleanOp comparisonExpr)*;
|
|
|
|
booleanOp : And | Or | Xor;
|
|
|
|
comparisonExpr : Not comparisonExpr
|
|
|
|
| True
|
|
|
|
| False
|
|
|
|
| additiveExpr (comparisonOp additiveExpr)?
|
|
|
|
;
|
|
|
|
comparisonOp : Less | LessEqual | Greater | GreaterEqual | Equal | NotEqual;
|
|
|
|
additiveExpr : multiplicativeExpr (additiveOp multiplicativeExpr)*;
|
|
|
|
additiveOp : Plus | Minus | BitAnd | BitOr | BitXor | ShiftLeft | ShiftRight;
|
|
|
|
multiplicativeExpr : factor (multiplicativeOp factor)*;
|
|
|
|
multiplicativeOp : Mul | Div | Rem;
|
2023-01-06 16:44:57 +00:00
|
|
|
factor : Minus factor
|
|
|
|
| BitNot factor
|
|
|
|
| Integer
|
2023-01-12 23:07:00 +00:00
|
|
|
| Identifier
|
|
|
|
| Identifier LeftParen argumentList? RightParen
|
2023-01-06 16:44:57 +00:00
|
|
|
| LeftParen expr RightParen
|
2023-01-04 22:32:55 +00:00
|
|
|
;
|
2023-01-12 23:07:00 +00:00
|
|
|
argumentList : expr (Comma expr)*;
|
2023-01-04 22:32:55 +00:00
|
|
|
|
2023-01-07 07:54:53 +00:00
|
|
|
TypeInteger : 'int';
|
|
|
|
TypeBoolean : 'bool';
|
2023-01-04 22:32:55 +00:00
|
|
|
If : 'if';
|
|
|
|
Else : 'else';
|
|
|
|
While : 'while';
|
2023-01-07 16:10:03 +00:00
|
|
|
For : 'for';
|
2023-01-07 08:56:27 +00:00
|
|
|
Break : 'break';
|
|
|
|
Continue : 'continue';
|
2023-01-04 22:32:55 +00:00
|
|
|
Return : 'return';
|
|
|
|
Print : 'print';
|
2023-01-06 16:44:57 +00:00
|
|
|
And : 'and';
|
|
|
|
Or : 'or';
|
|
|
|
Xor : 'xor';
|
|
|
|
Not : 'not';
|
|
|
|
True : 'true';
|
|
|
|
False : 'false';
|
2023-01-04 22:32:55 +00:00
|
|
|
|
|
|
|
LeftParen : '(';
|
|
|
|
RightParen : ')';
|
2023-01-07 07:54:53 +00:00
|
|
|
Colon : ':';
|
2023-01-04 22:32:55 +00:00
|
|
|
LeftBrace : '{';
|
|
|
|
RightBrace : '}';
|
2023-01-07 07:54:53 +00:00
|
|
|
Define : ':=';
|
2023-01-04 22:32:55 +00:00
|
|
|
Assign : '=';
|
|
|
|
Less : '<';
|
|
|
|
LessEqual : '<=';
|
|
|
|
Greater : '>';
|
|
|
|
GreaterEqual : '>=';
|
|
|
|
Equal : '==';
|
|
|
|
NotEqual : '!=';
|
|
|
|
Plus : '+';
|
|
|
|
Minus : '-';
|
2023-01-06 16:44:57 +00:00
|
|
|
BitAnd : '&';
|
|
|
|
BitOr : '|';
|
|
|
|
BitXor : '^';
|
|
|
|
ShiftLeft : '<<';
|
|
|
|
ShiftRight : '>>';
|
|
|
|
BitNot : '~';
|
2023-01-04 22:32:55 +00:00
|
|
|
Mul : '*';
|
|
|
|
Div : '/';
|
2023-01-07 08:56:04 +00:00
|
|
|
Rem : '%';
|
2023-01-07 15:07:24 +00:00
|
|
|
Increment : '=+';
|
|
|
|
Decrement : '=-';
|
2023-01-04 22:32:55 +00:00
|
|
|
Comma : ',';
|
|
|
|
Semicolon : ';';
|
|
|
|
|
2023-01-05 16:44:30 +00:00
|
|
|
Identifier : [_a-zA-Z][_a-zA-Z0-9]*;
|
2023-01-04 22:32:55 +00:00
|
|
|
Integer : [0-9]+;
|
|
|
|
|
|
|
|
Comment : '//' ~[\n]* '\n' -> skip;
|
|
|
|
Whitespace : [ \t\r\n]+ -> skip;
|
2023-01-05 20:22:28 +00:00
|
|
|
Unknown : .;
|