-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPinn.g4
159 lines (131 loc) · 3.35 KB
/
Pinn.g4
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
grammar Pinn;
TWODOTS : '@' ;
file : ( function | varDecl ';' )+ EOF ;
function
: 'func' ID LPAREN (fvarDecl (',' fvarDecl)*)? ')' kind? block ;
testRule
: ID ID ;
block
: '{' statement* '}' ;
fvarDecl
: ID kind ;
varDecl
: ID kind ('=' exprList)?
| ID CE expr ;
LSQUARE : '[' ;
LPAREN : '(' ;
kind
: (LSQUARE ( MAP | SLICE | FILL | expr) ']')? TYPES ;
MAP
: 'map' ;
SLICE
: 'slice' ;
FILL
: 'fill' ;
TYPES
: ('int' | 'bool' | 'unit' | 'string' | 'big' | 'float' | 'char' ) ;
simpleStatement
: pset
| ID (LSQUARE expr ']')? ('++' | '--') ;
indexExpr :
ID LSQUARE first=expr? (TWODOTS | COLON) second=expr? ']'
| ID LSQUARE expr ']' ;
funcExpr
: 'print' LPAREN exprList ')'
| 'printB' LPAREN expr ')'
| 'printH' LPAREN expr ')'
| 'delete' LPAREN ID ',' expr ')'
| 'len' LPAREN ID ')'
| 'strLen' LPAREN expr ')'
| 'stringValue' LPAREN expr ')' ;
expr
:
funcExpr
| ID LPAREN exprList? ')'
| indexExpr
| ('+' | '-' | '!' | '^') expr
| expr ('+' | '-' | '^' | BINOP) expr
| expr ('==' | '!=' | '>' | '<' | '>=' | '<=' ) expr
| expr ('&&' | '||') expr
| expr '?' expr COLON expr
| LPAREN expr ')'
| firstExpr=expr (TWODOTS | COLON) secondExpr=expr
| ID
| FLOAT
| INT
| BOOL
| STRING
| CHAR
| IOTA
;
exprList
: expr (',' expr)* ;
returnStatement
: 'return' expr? ';' ;
ifStatement
: 'if' expr statement ('else' statement)?;
guardStatement
: 'guard' expr 'else' block;
whStatement
: 'while' expr block ;
repeatStatement
: 'repeat' block 'while' expr ;
RANGE : 'range' ;
foStatement
: 'for' (varDecl | fss=simpleStatement)? ';' expr ';' sss=simpleStatement block
| 'for' ID ',' ID '=' RANGE expr block |
'for' ID '=' RANGE expr block ;
caseStatement
: 'case' exprList COLON statement* ;
switchStatement
: 'switch' expr '{' caseStatement* ('default' COLON statement*)? '}' ;
statement
: expr ';'
| varDecl ';'
| simpleStatement ';'
| ifStatement
| guardStatement
| whStatement
| repeatStatement ';'
| switchStatement
| returnStatement
| foStatement
| block
| 'break' ';'
| 'continue' ';'
| 'fallthrough' ';'
| 'debug' LPAREN ')' ';'
| ';' ;
pset
: ID (LSQUARE expr ']')? '=' expr #simpleSet
| ID (LSQUARE expr ']')? ('+' | '-' | '^' | BINOP) '=' expr #compoundSet ;
COLON : ':' ;
CE : ':=' ;
IOTA : 'iota' ;
BINOP : ('*' | '/' | '%' | '&' | '|' | '<<' | '>>' ) ;
BOOL : 'true' | 'false' ;
ID : [a-zA-Z_]([a-zA-Z_0-9])* ;
CHAR : '\''[a-zA-Z_0-9]'\'' ;
INT : '0'
| [1-9] ('_'? DECIMAL_DIGITS)?
| '0x' '_'? HEX_DIGIT ('_'? HEX_DIGIT)*
| '0b' '_'? BINARY_DIGIT ('_'? BINARY_DIGIT)*
| '0o' '_'? OCTAL_DIGIT ('_'? OCTAL_DIGIT)* ;
FLOAT : DECIMAL_DIGITS '.' DECIMAL_DIGITS? DECIMAL_EXPONENT?
| DECIMAL_DIGITS DECIMAL_EXPONENT
| '.' DECIMAL_DIGITS DECIMAL_EXPONENT?
| '0x' HEX_MANTISSA HEX_EXPONENT ;
WS : ([ \t\n]+ | '//' ~('\n')* '\n' | '/*' .*? '*/' )-> skip ;
STRING : '"' ~('"')* '"' ;
fragment DECIMAL_DIGIT : [0-9] ;
fragment DECIMAL_DIGITS : DECIMAL_DIGIT ('_'? DECIMAL_DIGIT)* ;
fragment DECIMAL_EXPONENT : 'e' [+-]? DECIMAL_DIGITS ;
fragment HEX_DIGIT : [0-9a-f] ;
fragment HEX_DIGITS: HEX_DIGIT ('_'? HEX_DIGIT)* ;
fragment HEX_MANTISSA : '_'? HEX_DIGITS '.' HEX_DIGITS?
| '_'? HEX_DIGITS
| '.' HEX_DIGITS ;
fragment HEX_EXPONENT : 'p' [+-]? DECIMAL_DIGITS
| 'h' [+-]? HEX_DIGITS ;
fragment OCTAL_DIGIT : [0-7] ;
fragment BINARY_DIGIT : [01] ;