93 lines
1.9 KiB
ANTLR
93 lines
1.9 KiB
ANTLR
grammar xlang;
|
|
|
|
file : function+ EOF;
|
|
function : Identifier LeftParen parameterList? RightParen Colon type block;
|
|
parameterList : Identifier Colon type (Comma Identifier Colon type)*;
|
|
type : TypeInteger
|
|
| TypeBoolean
|
|
;
|
|
block : LeftBrace statement* RightBrace;
|
|
statement : Identifier Define value Semicolon
|
|
| Identifier Assign value Semicolon
|
|
| If condition block (Else block)?
|
|
| While condition block
|
|
| Return value Semicolon
|
|
| Print value Semicolon
|
|
;
|
|
value : expr
|
|
| condition
|
|
;
|
|
condition : condition (And|Or|Xor) boolean
|
|
| boolean
|
|
;
|
|
boolean : Not boolean
|
|
| True
|
|
| False
|
|
| expr (Less|LessEqual|Greater|GreaterEqual|Equal|NotEqual) expr
|
|
| variable
|
|
| LeftParen condition RightParen
|
|
;
|
|
expr : expr (Plus|Minus|BitAnd|BitOr|BitXor|ShiftLeft|ShiftRight) term
|
|
| term
|
|
;
|
|
term : term (Mul|Div) factor
|
|
| factor
|
|
;
|
|
factor : Minus factor
|
|
| BitNot factor
|
|
| Integer
|
|
| variable
|
|
| LeftParen expr RightParen
|
|
;
|
|
variable : Identifier
|
|
| Identifier LeftParen argumentList? RightParen
|
|
;
|
|
argumentList : value (Comma value)*;
|
|
|
|
TypeInteger : 'int';
|
|
TypeBoolean : 'bool';
|
|
If : 'if';
|
|
Else : 'else';
|
|
While : 'while';
|
|
Return : 'return';
|
|
Print : 'print';
|
|
And : 'and';
|
|
Or : 'or';
|
|
Xor : 'xor';
|
|
Not : 'not';
|
|
True : 'true';
|
|
False : 'false';
|
|
|
|
LeftParen : '(';
|
|
RightParen : ')';
|
|
Colon : ':';
|
|
LeftBrace : '{';
|
|
RightBrace : '}';
|
|
Define : ':=';
|
|
Assign : '=';
|
|
Less : '<';
|
|
LessEqual : '<=';
|
|
Greater : '>';
|
|
GreaterEqual : '>=';
|
|
Equal : '==';
|
|
NotEqual : '!=';
|
|
Plus : '+';
|
|
Minus : '-';
|
|
BitAnd : '&';
|
|
BitOr : '|';
|
|
BitXor : '^';
|
|
ShiftLeft : '<<';
|
|
ShiftRight : '>>';
|
|
BitNot : '~';
|
|
Mul : '*';
|
|
Div : '/';
|
|
Comma : ',';
|
|
Semicolon : ';';
|
|
|
|
Identifier : [_a-zA-Z][_a-zA-Z0-9]*;
|
|
Integer : [0-9]+;
|
|
|
|
Comment : '//' ~[\n]* '\n' -> skip;
|
|
Whitespace : [ \t\r\n]+ -> skip;
|
|
Unknown : .;
|