35 auto type = (t.text().find(
'.') != std::string::npos ||
36 t.text().find(
'e') != std::string::npos)
39 return Const::create(t.range,
d(t.doubleValue()),
c(type, t.range, {}));
45 if (L.cur().kind == TK_NUMBER) {
46 prefix = parseConst();
47 }
else if (L.cur().kind ==
'(') {
51 }
else if (shared.isScalarType(L.cur().kind)) {
53 auto type = parseScalarType();
55 auto value = parseExp();
59 prefix = parseIdent();
60 auto range = L.cur().range;
61 if (L.cur().kind ==
'(') {
63 }
else if (L.nextIf(
'.')) {
64 auto t = L.expect(TK_NUMBER);
73 auto true_branch = parseExp();
75 auto false_branch = parseExp(binary_prec);
76 return c(
'?', range, {cond, true_branch, false_branch});
85 if (shared.isUnary(L.cur().kind, &unary_prec)) {
86 auto kind = L.cur().kind;
87 auto pos = L.cur().range;
89 prefix = c(kind, pos, {parseExp(unary_prec)});
91 prefix = parseBaseExp();
94 while (shared.isBinary(L.cur().kind, &binary_prec)) {
95 if (binary_prec <= precedence)
99 int kind = L.cur().kind;
100 auto pos = L.cur().range;
102 if (shared.isRightAssociative(kind))
107 prefix = parseTrinary(prefix, pos, binary_prec);
111 prefix = c(kind, pos, {prefix, parseExp(binary_prec)});
117 auto r = L.cur().range;
120 if (L.cur().kind != end) {
123 elements.push_back(parse(i++));
124 }
while (L.nextIf(sep));
133 elements.push_back(parse(i++));
134 }
while (L.nextIf(sep));
135 auto range = elements.at(0)->range();
139 return parseList(
'(',
',',
')', [&](
int i) {
return parseExp(); });
142 return parseList(
'(',
',',
')', [&](
int i) {
return parseIdent(); });
145 auto id = parseIdent();
153 auto ident = parseIdent();
155 auto exp = parseExp();
159 auto lookahead = L.lookahead();
160 if (lookahead.kind ==
'=') {
161 return parseLetBinding();
162 }
else if (lookahead.kind == TK_IN) {
163 return parseRangeConstraint();
166 auto exp = parseExp();
171 if (L.cur().kind == TK_IDENT) {
172 auto ident = parseIdent();
174 ident->range(), ident, c(TK_INFERRED, ident->range(), {}));
176 auto typ = parseType();
177 auto ident = parseIdent();
181 if (L.nextIf(TK_WHERE)) {
182 return parseNonEmptyList(
',', [&](
int i) {
return parseWhereClause(); });
187 auto r = L.cur().range;
188 if (L.nextIf(TK_EQUIVALENT)) {
189 auto name = L.expect(TK_IDENT);
190 auto accesses = parseExpList();
193 return c(TK_OPTION, r, {});
197 switch (L.cur().kind) {
207 return c(L.next().kind, L.cur().range, {});
209 L.reportError(
"a valid assignment operator");
215 auto ident = parseIdent();
216 TreeRef list = parseOptionalIdentList();
217 auto assign = parseAssignment();
218 auto rhs = parseExp();
219 TreeRef equivalent_statement = parseEquivalent();
220 TreeRef range_statements = parseWhereClauses();
221 TreeRef empty_reduction_variables = c(TK_LIST, ident->range(), {});
229 equivalent_statement,
230 empty_reduction_variables);
233 if (shared.isScalarType(L.cur().kind)) {
235 return c(t.kind, t.range, {});
237 L.reportError(
"a scalar type");
242 if (L.cur().kind ==
'(') {
243 list = parseIdentList();
250 return parseList(
'(',
',',
')', [&](
int i) {
251 if (L.cur().kind == TK_NUMBER) {
260 if (L.cur().kind ==
'(') {
261 list = parseDimList();
268 auto st = parseScalarType();
269 auto list = parseOptionalDimList();
274 auto name = parseIdent();
276 parseList(
'(',
',',
')', [&](
int i) {
return parseParam(); });
279 parseList(
'(',
',',
')', [&](
int i) {
return parseParam(); });
281 auto r = L.cur().range;
283 while (!L.nextIf(
'}')) {
284 stmts.push_back(parseStmt());
287 return Def::create(name->range(), name, paramlist, retlist, stmts_list);
static TreeRef create(const SourceRange &range, TreeRef name, TreeRef paramlist, TreeRef retlist, TreeRef stmts_list)
Definition: tree_views.h:348
static TreeRef create(const SourceRange &range, const std::string &name, TreeRef accesses)
Definition: tree_views.h:244
static TreeRef create(const SourceRange &range, TreeRef name, TreeRef index)
Definition: tree_views.h:369
TreeRef parseExpList()
Definition: parser.h:138
static TreeRef create(const SourceRange &range, TreeRef ident, TreeRef type)
Definition: tree_views.h:215
TreeRef s(const std::string &s)
Definition: parser.h:297
TreeRef parseDimList()
Definition: parser.h:249
static TreeRef create(Args &&...args)
Definition: tree.h:113
TreeRef parseOptionalDimList()
Definition: parser.h:258
TreeRef parseStmt()
Definition: parser.h:214
TreeRef parseEquivalent()
Definition: parser.h:186
TreeRef parseLetBinding()
Definition: parser.h:152
TreeRef parseAssignment()
Definition: parser.h:196
TreeRef parseNonEmptyList(int sep, std::function< TreeRef(int)> parse)
Definition: parser.h:129
Parser(const std::string &str)
Definition: parser.h:24
TreeRef parseBaseExp()
Definition: parser.h:43
Lexer L
Definition: parser.h:290
TreeRef parseIdent()
Definition: parser.h:26
static TreeRef create(const SourceRange &range, TreeRef ident, TreeRef start, TreeRef end)
Definition: tree_views.h:261
Token expect(int kind)
Definition: lexer.h:405
static TreeRef create(const SourceRange &range, TreeRef name, TreeRef arguments)
Definition: tree_views.h:149
static TreeRef create(Args &&...args)
Definition: tree.h:100
static TreeRef create(const SourceRange &range, TreeRef value, TreeRef type)
Definition: tree_views.h:399
static TreeRef create(const SourceRange &range, TreeRef name, TreeRef rhs)
Definition: tree_views.h:414
static TreeRef create(const SourceRange &range, TreeRef value, TreeRef type)
Definition: tree_views.h:384
TreeRef parseRangeConstraint()
Definition: parser.h:144
TreeRef parseType()
Definition: parser.h:267
TreeRef parseIdentList()
Definition: parser.h:141
TreeRef parseFunction()
Definition: parser.h:272
SharedParserData & shared
Definition: parser.h:303
TreeRef parseWhereClauses()
Definition: parser.h:180
TreeRef parseWhereClause()
Definition: parser.h:158
TreeRef parseParam()
Definition: parser.h:170
TreeRef parseExp(int precedence=0)
Definition: parser.h:82
static TreeRef create(const SourceRange &range, TreeRef ident, TreeRef indices, TreeRef assignment, TreeRef rhs, TreeRef range_constraints, TreeRef equivalent, TreeRef reduction_variables)
Definition: tree_views.h:279
TreeRef parseOptionalIdentList()
Definition: parser.h:240
std::vector< TreeRef > TreeList
Definition: tree.h:45
TreeRef parseScalarType()
Definition: parser.h:232
static TreeRef create(const SourceRange &range, const std::string &name)
Definition: tree_views.h:127
static TreeRef create(const SourceRange &range, TreeRef scalar_type_, TreeRef dims_)
Definition: tree_views.h:191
static TreeRef create(const SourceRange &range, TreeList elements)
Definition: tree_views.h:85
TreeRef c(int kind, const SourceRange &range, TreeList &&trees)
Definition: parser.h:300
SharedParserData & sharedParserData()
TreeRef parseTrinary(TreeRef cond, const SourceRange &range, int binary_prec)
Definition: parser.h:72
static TreeRef create(int kind, const SourceRange &range_, TreeList &&trees_)
Definition: tree.h:155
TreeRef d(double v)
Definition: parser.h:294
static TreeRef create(const SourceRange &range, TreeRef exp)
Definition: tree_views.h:426
std::shared_ptr< Tree > TreeRef
Definition: tree.h:44
TreeRef parseConst()
Definition: parser.h:33
TreeRef parseList(int begin, int sep, int end, std::function< TreeRef(int)> parse)
Definition: parser.h:116