This repository was archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnowStarParser.g4
188 lines (153 loc) · 5.68 KB
/
SnowStarParser.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Snow* Compiler, a compiler made with ANTLR and LLVM for compiling Snow* source code
* Copyright (C) 2018 Mesabloo
*
* 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/>.
*/
parser grammar SnowStarParser;
options {
tokenVocab = SnowStarLexer;
}
/** Entry point/Basic concepts */
compilationUnit
: declarationSeq* EOF
;
/** Declarations */
declarationSeq
: functionDeclaration // type name (params*) {statements*}
| variableDeclaration // type name (= value)?;
| unitDeclaration // unit name {statements*}
| dtypeDeclaration // dtype name {vars*}
| withDeclaration // with name = type;
| importDeclaration // import file;
| emptyDeclaration // ;
;
variableDeclaration
: theType variableName variableInitializer? SEMI
;
variableName
: IDENTIFIER
;
variableInitializer
: ASSIGN expression
;
unitDeclaration
: UNIT unitName basicBlockDeclaration
;
unitName
: IDENTIFIER
;
basicBlockDeclaration
: LBRACE statementSeq* RBRACE
;
dtypeDeclaration
: DTYPE dtypeName dtypeBlockDeclaration
;
dtypeName
: IDENTIFIER
;
dtypeBlockDeclaration
: LBRACE variableDeclaration* RBRACE
;
functionDeclaration
: functionHeader TO_ARROW basicBlockDeclaration SEMI
;
functionHeader
: theType functionParamsTypes functionName ASSIGN functionParams
;
functionParamsTypes
: LPAREN (theType (COMMA theType)*)? RPAREN
;
functionName
: IDENTIFIER
;
functionParams
: LPAREN (parameterDeclaration (COMMA parameterDeclaration)*)? RPAREN
;
parameterDeclaration
: theType IDENTIFIER (ASSIGN value)?
;
withDeclaration
: WITH withName ASSIGN theType SEMI
;
withName
: IDENTIFIER
;
importDeclaration
: IMPORT importName SEMI
;
importName
: IDENTIFIER
;
returnDeclaration
: RET expression? SEMI
;
emptyDeclaration
: SEMI
;
/** Statement */
statementSeq
: variableDeclaration // type name (= value);
| withDeclaration // with name = type;
| importDeclaration // import file;
| returnDeclaration // return value;
| emptyDeclaration // ;
;
/** Expressions */
expression
: primaryExpression
| unary=(PLUS | LOGIC_NOT | BIN_NOT | MINUS) expression
| LPAREN theType RPAREN expression
| expression binary=(STAR | SLASH) expression
| expression binary=(PLUS | MINUS) expression
| expression binary=(GREATER | LOWER | GREATER_EQ | LOWER_EQ) expression
| expression binary=(EQUALS | NEQUALS) expression
| expression binary=(BIN_XOR | BIN_AND | BIN_OR) expression
| expression binary=(LOGIC_AND | LOGIC_OR) expression
;
primaryExpression
: LPAREN expression RPAREN
| IDENTIFIER
| value
;
/** Types and classes */
theType
: builtinTypes
| IDENTIFIER
;
builtinTypes
: BOOL
| CHR
| I8
| UI8
| I16
| UI16
| I32
| UI32
| I64
| UI64
| F32
| F64
| STR
| VOID
;
value
: BOOL_LITERAL
| CHAR_LITERAL
| DEC_LITERAL
| BIN_LITERAL
| HEX_LITERAL
| OCT_LITERAL
| FLOAT_LITERAL
| STRING_LITERAL
;