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;
|
|
|
|
parameterList : Identifier Colon type (Comma Identifier Colon type)*;
|
|
|
|
type : TypeInteger
|
|
|
|
| TypeBoolean
|
|
|
|
;
|
2023-01-04 22:32:55 +00:00
|
|
|
block : LeftBrace statement* RightBrace;
|
2023-01-07 14:23:02 +00:00
|
|
|
statement : If condition block (Else block)?
|
2023-01-06 16:44:57 +00:00
|
|
|
| While condition block
|
2023-01-07 16:10:03 +00:00
|
|
|
| For value Semicolon condition Semicolon value block
|
2023-01-07 16:43:25 +00:00
|
|
|
| Break Integer? Semicolon
|
|
|
|
| Continue Integer? Semicolon
|
2023-01-06 17:46:45 +00:00
|
|
|
| Return value Semicolon
|
|
|
|
| Print value Semicolon
|
2023-01-07 14:23:02 +00:00
|
|
|
| value Semicolon
|
2023-01-04 22:32:55 +00:00
|
|
|
;
|
2023-01-06 17:46:45 +00:00
|
|
|
value : expr
|
|
|
|
| condition
|
|
|
|
;
|
2023-01-06 16:44:57 +00:00
|
|
|
condition : condition (And|Or|Xor) boolean
|
|
|
|
| boolean
|
|
|
|
;
|
|
|
|
boolean : Not boolean
|
|
|
|
| True
|
|
|
|
| False
|
2023-01-06 17:56:53 +00:00
|
|
|
| expr (Less|LessEqual|Greater|GreaterEqual|Equal|NotEqual) expr
|
2023-01-06 17:46:45 +00:00
|
|
|
| variable
|
2023-01-06 16:44:57 +00:00
|
|
|
| LeftParen condition RightParen
|
|
|
|
;
|
|
|
|
expr : expr (Plus|Minus|BitAnd|BitOr|BitXor|ShiftLeft|ShiftRight) term
|
|
|
|
| term
|
2023-01-05 16:44:30 +00:00
|
|
|
;
|
2023-01-07 08:56:04 +00:00
|
|
|
term : term (Mul|Div|Rem) factor
|
2023-01-05 16:44:30 +00:00
|
|
|
| factor
|
|
|
|
;
|
2023-01-06 16:44:57 +00:00
|
|
|
factor : Minus factor
|
|
|
|
| BitNot factor
|
|
|
|
| Integer
|
2023-01-06 17:46:45 +00:00
|
|
|
| variable
|
2023-01-06 16:44:57 +00:00
|
|
|
| LeftParen expr RightParen
|
2023-01-04 22:32:55 +00:00
|
|
|
;
|
2023-01-06 17:46:45 +00:00
|
|
|
variable : Identifier
|
2023-01-07 14:23:02 +00:00
|
|
|
| Identifier Define value
|
|
|
|
| Identifier Assign value
|
2023-01-07 15:07:24 +00:00
|
|
|
| Identifier Increment expr
|
|
|
|
| Identifier Decrement expr
|
2023-01-06 17:46:45 +00:00
|
|
|
| Identifier LeftParen argumentList? RightParen
|
|
|
|
;
|
|
|
|
argumentList : value (Comma value)*;
|
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 : .;
|