xlang/bootstrap/xlang.g4

105 lines
2.3 KiB
Plaintext
Raw Normal View History

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
;
block : LeftBrace statement* RightBrace;
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
| Return value Semicolon
| Print value Semicolon
| value Semicolon
;
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
| 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
| variable
2023-01-06 16:44:57 +00:00
| LeftParen expr RightParen
;
variable : Identifier
| Identifier Define value
| Identifier Assign value
2023-01-07 15:07:24 +00:00
| Identifier Increment expr
| Identifier Decrement expr
| Identifier LeftParen argumentList? RightParen
;
argumentList : value (Comma value)*;
2023-01-07 07:54:53 +00:00
TypeInteger : 'int';
TypeBoolean : 'bool';
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';
Return : 'return';
Print : 'print';
2023-01-06 16:44:57 +00:00
And : 'and';
Or : 'or';
Xor : 'xor';
Not : 'not';
True : 'true';
False : 'false';
LeftParen : '(';
RightParen : ')';
2023-01-07 07:54:53 +00:00
Colon : ':';
LeftBrace : '{';
RightBrace : '}';
2023-01-07 07:54:53 +00:00
Define : ':=';
Assign : '=';
Less : '<';
LessEqual : '<=';
Greater : '>';
GreaterEqual : '>=';
Equal : '==';
NotEqual : '!=';
Plus : '+';
Minus : '-';
2023-01-06 16:44:57 +00:00
BitAnd : '&';
BitOr : '|';
BitXor : '^';
ShiftLeft : '<<';
ShiftRight : '>>';
BitNot : '~';
Mul : '*';
Div : '/';
2023-01-07 08:56:04 +00:00
Rem : '%';
2023-01-07 15:07:24 +00:00
Increment : '=+';
Decrement : '=-';
Comma : ',';
Semicolon : ';';
2023-01-05 16:44:30 +00:00
Identifier : [_a-zA-Z][_a-zA-Z0-9]*;
Integer : [0-9]+;
Comment : '//' ~[\n]* '\n' -> skip;
Whitespace : [ \t\r\n]+ -> skip;
2023-01-05 20:22:28 +00:00
Unknown : .;