-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zas.g
96 lines (80 loc) · 2.53 KB
/
Zas.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2009 Marshall Vandegrift
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
grammar Zas;
options {
language = Python;
output = AST;
ASTLabelType = CommonTree;
}
tokens {
INDIRECT;
INSN;
}
program
: statement+
;
statement
: label eol -> label
| label? insnlist eol -> label? insnlist
| ID eq='=' expr eol -> ^(INSN ID[$eq, ".set"] ID expr)
| NEWLINE ->
;
eol : ';'? NEWLINE | EOF -> ;
label
: ID c=':' -> ^(INSN ID[$c, ".set"] ID ID[$c, "."])
| v=INT c=':' -> ^(INSN ID[$c, ".set"] ID[$v] ID[$c, "."])
;
insnlist
: insn ( ';' insn )* -> insn+
;
insn: ID arglist? -> ^(INSN ID arglist?) ;
arglist
: arg (',' arg)* -> arg+
;
arg : REG
| lp='(' REG ')' -> ^(INDIRECT REG)
| QSTR
| RETBOOL
| expr
;
expr: expr1 ('|'^ expr1)* ;
expr1: expr2 ('^'^ expr2)* ;
expr2: expr3 ('&'^ expr3)* ;
expr3: expr4 (('<<'^ | '>>'^) expr4)* ;
expr4: expr5 (('+'^ | '-'^) expr5)* ;
expr5: expr6 (('*'^ | '/'^ | '%'^) expr6)* ;
expr6: atom | (('~'^ | '+'^ | '-'^ | '@'^) atom) ;
atom: INT
| ID
| l=LOCAL -> ID[$l]
| '(' expr ')' -> expr
;
REG : '%' ((('l' | 'g') '0'..'9' '0'..'9'* ) | 'sp') ;
QSTR: '"' ( ( '\\' . ) | ~('"' | '\\') )* '"'
| '\'' ( ( '\\' . ) | ~('\'' | '\\') )* '\'' ;
RETBOOL
: ':' ( 'rtrue' | 'rfalse' ) ;
ID : ('a'..'z' | 'A'..'Z' | '.' | '_')
('a'..'z' | 'A'..'Z' | '.' | '_' | '$' | '!' | '0'..'9')* ;
LOCAL: '1'..'9' '0'..'9'* ( 'f' | 'b' ) ;
INT : '0'..'9'+
| ('0x' ('0'..'9' | 'A'..'F' | 'a'..'f')+)
| ('0b' ('0'..'1')+) ;
NEWLINE
: ('\r'? '\n') ;
COMMENT
: ('//' | '#') ( ~'\n' )* { $channel = "hidden" } ;
WS : ( ( ' ' | '\t' )+
| ( '\\' ( ' ' | '\t' )* NEWLINE ) ) { $channel = "hidden" } ;