79 lines
1.6 KiB
ANTLR
79 lines
1.6 KiB
ANTLR
grammar xlang;
|
|
|
|
file : function+ EOF;
|
|
function : Identifier LeftParen argumentList? RightParen block;
|
|
argumentList : Identifier (Comma Identifier)*;
|
|
block : LeftBrace statement* RightBrace;
|
|
statement : Identifier Assign expr Semicolon
|
|
| If condition block (Else block)?
|
|
| While condition block
|
|
| Return expr Semicolon
|
|
| Print expr Semicolon
|
|
;
|
|
condition : condition (And|Or|Xor) boolean
|
|
| boolean
|
|
;
|
|
boolean : Not boolean
|
|
| expr (Less|LessEqual|Greater|GreaterEqual|Equal|NotEqual) expr
|
|
| True
|
|
| False
|
|
| 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
|
|
| Identifier
|
|
| Identifier LeftParen exprList? RightParen
|
|
| LeftParen expr RightParen
|
|
;
|
|
exprList : expr (Comma expr)*;
|
|
|
|
If : 'if';
|
|
Else : 'else';
|
|
While : 'while';
|
|
Return : 'return';
|
|
Print : 'print';
|
|
And : 'and';
|
|
Or : 'or';
|
|
Xor : 'xor';
|
|
Not : 'not';
|
|
True : 'true';
|
|
False : 'false';
|
|
|
|
LeftParen : '(';
|
|
RightParen : ')';
|
|
LeftBrace : '{';
|
|
RightBrace : '}';
|
|
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 : .;
|