xbintsc 0.6.13 → 0.6.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/gui/features.d.ts +2 -0
- package/dist/src/extensions/gui/features.js +24 -0
- package/dist/src/extensions/gui/features.js.map +1 -1
- package/dist/src/extensions/gui/index.js +39 -0
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/tests/e2e/gui-example-pelican3d.test.d.ts +6 -4
- package/dist/tests/e2e/gui-example-pelican3d.test.js +103 -44
- package/dist/tests/e2e/gui-example-pelican3d.test.js.map +1 -1
- package/dist/tests/e2e/gui-example-webgpu.test.d.ts +9 -0
- package/dist/tests/e2e/gui-example-webgpu.test.js +59 -0
- package/dist/tests/e2e/gui-example-webgpu.test.js.map +1 -0
- package/dist/tests/e2e/gui-helpers.d.ts +1 -1
- package/dist/tests/e2e/gui-webgpu.test.d.ts +11 -0
- package/dist/tests/e2e/gui-webgpu.test.js +402 -0
- package/dist/tests/e2e/gui-webgpu.test.js.map +1 -0
- package/dist/tests/extensions/gui-features.test.js +18 -1
- package/dist/tests/extensions/gui-features.test.js.map +1 -1
- package/doc/ai/extensions.md +7 -1
- package/doc/ai/zh-CN/extensions.md +6 -1
- package/doc/gui/webgpu.md +121 -0
- package/doc/gui.md +12 -1
- package/doc/zh-CN/gui/webgpu.md +111 -0
- package/doc/zh-CN/gui.md +10 -1
- package/llms.txt +1 -0
- package/package.json +7 -7
- package/runtime/ext_gui/browser.cpp +10 -0
- package/runtime/ext_gui/dom_api_canvas.cpp +21 -0
- package/runtime/ext_gui/dom_api_canvas_extra.cpp +61 -0
- package/runtime/ext_gui/gpu_feature.cpp +2 -0
- package/runtime/ext_gui/gpu_shader_api.h +6 -0
- package/runtime/ext_gui/gui_canvas.cpp +18 -0
- package/runtime/ext_gui/renderer_image.cpp +6 -0
- package/runtime/ext_gui/webgl_glsl.h +8 -0
- package/runtime/ext_gui/webgl_glsl_compile.cpp +11 -5
- package/runtime/ext_gui/webgl_msl.cpp +20 -9
- package/runtime/ext_gui/webgl_msl.h +4 -0
- package/runtime/ext_gui/webgpu.h +371 -0
- package/runtime/ext_gui/webgpu_api.h +42 -0
- package/runtime/ext_gui/webgpu_bindings.cpp +364 -0
- package/runtime/ext_gui/webgpu_bindings.h +59 -0
- package/runtime/ext_gui/webgpu_bridge.c +21 -0
- package/runtime/ext_gui/webgpu_buffer.cpp +293 -0
- package/runtime/ext_gui/webgpu_canvas.cpp +305 -0
- package/runtime/ext_gui/webgpu_command.cpp +510 -0
- package/runtime/ext_gui/webgpu_compute_pass.cpp +138 -0
- package/runtime/ext_gui/webgpu_convert.cpp +313 -0
- package/runtime/ext_gui/webgpu_device.cpp +370 -0
- package/runtime/ext_gui/webgpu_feature.cpp +74 -0
- package/runtime/ext_gui/webgpu_objects.cpp +125 -0
- package/runtime/ext_gui/webgpu_pass.cpp +295 -0
- package/runtime/ext_gui/webgpu_pass.h +33 -0
- package/runtime/ext_gui/webgpu_pipeline.cpp +463 -0
- package/runtime/ext_gui/webgpu_queue.cpp +222 -0
- package/runtime/ext_gui/webgpu_render_pass.cpp +417 -0
- package/runtime/ext_gui/webgpu_resources.cpp +315 -0
- package/runtime/ext_gui/webgpu_shader.cpp +246 -0
- package/runtime/ext_gui/wgsl.h +99 -0
- package/runtime/ext_gui/wgsl_api.cpp +137 -0
- package/runtime/ext_gui/wgsl_ast.h +233 -0
- package/runtime/ext_gui/wgsl_emit.cpp +452 -0
- package/runtime/ext_gui/wgsl_emit.h +128 -0
- package/runtime/ext_gui/wgsl_emit_call.cpp +281 -0
- package/runtime/ext_gui/wgsl_emit_expr.cpp +192 -0
- package/runtime/ext_gui/wgsl_emit_stmt.cpp +209 -0
- package/runtime/ext_gui/wgsl_entry.cpp +189 -0
- package/runtime/ext_gui/wgsl_infer.cpp +171 -0
- package/runtime/ext_gui/wgsl_lexer.cpp +179 -0
- package/runtime/ext_gui/wgsl_lexer.h +32 -0
- package/runtime/ext_gui/wgsl_parse_expr.cpp +215 -0
- package/runtime/ext_gui/wgsl_parse_stmt.cpp +259 -0
- package/runtime/ext_gui/wgsl_parser.cpp +457 -0
- package/runtime/ext_gui/wgsl_parser.h +77 -0
- package/runtime/ext_gui/wgsl_type.cpp +232 -0
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/features.ts +24 -0
- package/src/extensions/gui/index.ts +39 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — WGSL expressions.
|
|
3
|
+
*
|
|
4
|
+
* A precedence-climbing parser. Calls are recorded as `Call` nodes; a method
|
|
5
|
+
* call such as `array.length()` is stored with the synthetic callee
|
|
6
|
+
* `.length` and the receiver as the first argument, which keeps the emitter's
|
|
7
|
+
* name resolution in one place.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
#include "wgsl_parser.h"
|
|
11
|
+
|
|
12
|
+
#include <cstdlib>
|
|
13
|
+
|
|
14
|
+
namespace xtgui {
|
|
15
|
+
namespace wgsl {
|
|
16
|
+
namespace {
|
|
17
|
+
|
|
18
|
+
int precedence(const std::string &op) {
|
|
19
|
+
if (op == "||") return 1;
|
|
20
|
+
if (op == "&&") return 2;
|
|
21
|
+
if (op == "|") return 3;
|
|
22
|
+
if (op == "^") return 4;
|
|
23
|
+
if (op == "&") return 5;
|
|
24
|
+
if (op == "==" || op == "!=") return 6;
|
|
25
|
+
if (op == "<" || op == ">" || op == "<=" || op == ">=") return 7;
|
|
26
|
+
if (op == "<<" || op == ">>") return 8;
|
|
27
|
+
if (op == "+" || op == "-") return 9;
|
|
28
|
+
if (op == "*" || op == "/" || op == "%") return 10;
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
bool is_binary(const Token &token) {
|
|
33
|
+
return token.kind == Tok::Punct && precedence(token.text) > 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
} // namespace
|
|
37
|
+
|
|
38
|
+
ExprPtr Parser::parse_expr(int min_precedence) {
|
|
39
|
+
ExprPtr left = parse_unary();
|
|
40
|
+
if (left == nullptr) return nullptr;
|
|
41
|
+
while (!failed_ && is_binary(peek())) {
|
|
42
|
+
const std::string op = peek().text;
|
|
43
|
+
const int prec = precedence(op);
|
|
44
|
+
if (prec < min_precedence) break;
|
|
45
|
+
const int line = peek().line;
|
|
46
|
+
position_++;
|
|
47
|
+
ExprPtr right = parse_expr(prec + 1);
|
|
48
|
+
if (right == nullptr) return nullptr;
|
|
49
|
+
auto node = std::make_unique<Expr>();
|
|
50
|
+
node->kind = Expr::K::Binary;
|
|
51
|
+
node->text = op;
|
|
52
|
+
node->line = line;
|
|
53
|
+
node->args.push_back(std::move(left));
|
|
54
|
+
node->args.push_back(std::move(right));
|
|
55
|
+
left = std::move(node);
|
|
56
|
+
}
|
|
57
|
+
return left;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
ExprPtr Parser::parse_unary() {
|
|
61
|
+
if (peek().kind == Tok::Punct &&
|
|
62
|
+
(at("-") || at("!") || at("~") || at("&") || at("*"))) {
|
|
63
|
+
auto node = std::make_unique<Expr>();
|
|
64
|
+
node->kind = Expr::K::Unary;
|
|
65
|
+
node->text = peek().text;
|
|
66
|
+
node->line = peek().line;
|
|
67
|
+
position_++;
|
|
68
|
+
ExprPtr operand = parse_unary();
|
|
69
|
+
if (operand == nullptr) return nullptr;
|
|
70
|
+
node->args.push_back(std::move(operand));
|
|
71
|
+
return node;
|
|
72
|
+
}
|
|
73
|
+
return parse_postfix();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
ExprPtr Parser::parse_postfix() {
|
|
77
|
+
ExprPtr value = parse_primary();
|
|
78
|
+
if (value == nullptr) return nullptr;
|
|
79
|
+
while (!failed_) {
|
|
80
|
+
if (accept("[")) {
|
|
81
|
+
ExprPtr index = parse_expr();
|
|
82
|
+
if (index == nullptr) return nullptr;
|
|
83
|
+
if (!expect("]")) return nullptr;
|
|
84
|
+
auto node = std::make_unique<Expr>();
|
|
85
|
+
node->kind = Expr::K::Index;
|
|
86
|
+
node->line = peek().line;
|
|
87
|
+
node->args.push_back(std::move(value));
|
|
88
|
+
node->args.push_back(std::move(index));
|
|
89
|
+
value = std::move(node);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (accept(".")) {
|
|
93
|
+
std::string member;
|
|
94
|
+
if (!expect_ident(&member)) return nullptr;
|
|
95
|
+
auto node = std::make_unique<Expr>();
|
|
96
|
+
node->kind = Expr::K::Member;
|
|
97
|
+
node->member = member;
|
|
98
|
+
node->line = peek().line;
|
|
99
|
+
node->args.push_back(std::move(value));
|
|
100
|
+
value = std::move(node);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (accept("(")) {
|
|
104
|
+
std::vector<ExprPtr> args;
|
|
105
|
+
while (!at(")") && !failed_) {
|
|
106
|
+
ExprPtr arg = parse_expr();
|
|
107
|
+
if (arg == nullptr) return nullptr;
|
|
108
|
+
args.push_back(std::move(arg));
|
|
109
|
+
if (!accept(",")) break;
|
|
110
|
+
}
|
|
111
|
+
if (!expect(")")) return nullptr;
|
|
112
|
+
auto node = std::make_unique<Expr>();
|
|
113
|
+
node->kind = Expr::K::Call;
|
|
114
|
+
node->line = peek().line;
|
|
115
|
+
if (value->kind == Expr::K::Member) {
|
|
116
|
+
node->text = "." + value->member;
|
|
117
|
+
node->args.push_back(std::move(value->args[0]));
|
|
118
|
+
} else if (value->kind == Expr::K::Ident) {
|
|
119
|
+
node->text = value->text;
|
|
120
|
+
} else {
|
|
121
|
+
fail("only named functions and methods can be called");
|
|
122
|
+
return nullptr;
|
|
123
|
+
}
|
|
124
|
+
for (ExprPtr &arg : args) node->args.push_back(std::move(arg));
|
|
125
|
+
value = std::move(node);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
ExprPtr Parser::parse_primary() {
|
|
134
|
+
const Token token = peek();
|
|
135
|
+
if (token.kind == Tok::Int || token.kind == Tok::Float) {
|
|
136
|
+
position_++;
|
|
137
|
+
auto node = std::make_unique<Expr>();
|
|
138
|
+
node->kind = token.kind == Tok::Int ? Expr::K::Int : Expr::K::Float;
|
|
139
|
+
node->text = token.text;
|
|
140
|
+
node->line = token.line;
|
|
141
|
+
return node;
|
|
142
|
+
}
|
|
143
|
+
if (token.kind == Tok::Ident && (token.text == "true" || token.text == "false")) {
|
|
144
|
+
position_++;
|
|
145
|
+
auto node = std::make_unique<Expr>();
|
|
146
|
+
node->kind = Expr::K::Bool;
|
|
147
|
+
node->text = token.text;
|
|
148
|
+
node->line = token.line;
|
|
149
|
+
return node;
|
|
150
|
+
}
|
|
151
|
+
if (token.kind == Tok::Ident && token.text == "array" && peek(1).text == "<") {
|
|
152
|
+
position_ += 2;
|
|
153
|
+
TypePtr elem = parse_type();
|
|
154
|
+
if (elem == nullptr) return nullptr;
|
|
155
|
+
int length = 0;
|
|
156
|
+
if (accept(",")) {
|
|
157
|
+
if (peek().kind != Tok::Int) {
|
|
158
|
+
fail("an array length must be an integer literal");
|
|
159
|
+
return nullptr;
|
|
160
|
+
}
|
|
161
|
+
length = std::atoi(peek().text.c_str());
|
|
162
|
+
position_++;
|
|
163
|
+
}
|
|
164
|
+
if (!expect(">")) return nullptr;
|
|
165
|
+
auto node = std::make_unique<Expr>();
|
|
166
|
+
node->kind = Expr::K::ArrayCtor;
|
|
167
|
+
node->type = make_array(elem, length);
|
|
168
|
+
node->line = token.line;
|
|
169
|
+
if (accept("(")) {
|
|
170
|
+
while (!at(")") && !failed_) {
|
|
171
|
+
ExprPtr arg = parse_expr();
|
|
172
|
+
if (arg == nullptr) return nullptr;
|
|
173
|
+
node->args.push_back(std::move(arg));
|
|
174
|
+
if (!accept(",")) break;
|
|
175
|
+
}
|
|
176
|
+
if (!expect(")")) return nullptr;
|
|
177
|
+
}
|
|
178
|
+
return node;
|
|
179
|
+
}
|
|
180
|
+
if (token.kind == Tok::Ident && token.text == "bitcast" && peek(1).text == "<") {
|
|
181
|
+
position_ += 2;
|
|
182
|
+
TypePtr target = parse_type();
|
|
183
|
+
if (target == nullptr) return nullptr;
|
|
184
|
+
if (!expect(">")) return nullptr;
|
|
185
|
+
if (!expect("(")) return nullptr;
|
|
186
|
+
ExprPtr arg = parse_expr();
|
|
187
|
+
if (arg == nullptr) return nullptr;
|
|
188
|
+
if (!expect(")")) return nullptr;
|
|
189
|
+
auto node = std::make_unique<Expr>();
|
|
190
|
+
node->kind = Expr::K::Bitcast;
|
|
191
|
+
node->type = target;
|
|
192
|
+
node->line = token.line;
|
|
193
|
+
node->args.push_back(std::move(arg));
|
|
194
|
+
return node;
|
|
195
|
+
}
|
|
196
|
+
if (token.kind == Tok::Ident) {
|
|
197
|
+
position_++;
|
|
198
|
+
auto node = std::make_unique<Expr>();
|
|
199
|
+
node->kind = Expr::K::Ident;
|
|
200
|
+
node->text = token.text;
|
|
201
|
+
node->line = token.line;
|
|
202
|
+
return node;
|
|
203
|
+
}
|
|
204
|
+
if (accept("(")) {
|
|
205
|
+
ExprPtr inner = parse_expr();
|
|
206
|
+
if (inner == nullptr) return nullptr;
|
|
207
|
+
if (!expect(")")) return nullptr;
|
|
208
|
+
return inner;
|
|
209
|
+
}
|
|
210
|
+
fail("expected an expression");
|
|
211
|
+
return nullptr;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
} // namespace wgsl
|
|
215
|
+
} // namespace xtgui
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — WGSL statements.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#include "wgsl_parser.h"
|
|
6
|
+
|
|
7
|
+
namespace xtgui {
|
|
8
|
+
namespace wgsl {
|
|
9
|
+
namespace {
|
|
10
|
+
|
|
11
|
+
bool is_assignment(const std::string &text) {
|
|
12
|
+
return text == "=" || text == "+=" || text == "-=" || text == "*=" || text == "/=" ||
|
|
13
|
+
text == "%=" || text == "&=" || text == "|=" || text == "^=" || text == "<<=" ||
|
|
14
|
+
text == ">>=";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
StmtPtr make_stmt(Stmt::K kind, int line) {
|
|
18
|
+
auto statement = std::make_unique<Stmt>();
|
|
19
|
+
statement->kind = kind;
|
|
20
|
+
statement->line = line;
|
|
21
|
+
return statement;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
} // namespace
|
|
25
|
+
|
|
26
|
+
StmtPtr Parser::parse_block() {
|
|
27
|
+
if (!expect("{")) return nullptr;
|
|
28
|
+
auto block = make_stmt(Stmt::K::Block, peek().line);
|
|
29
|
+
while (!at("}") && !failed_) {
|
|
30
|
+
StmtPtr statement = parse_stmt();
|
|
31
|
+
if (statement == nullptr) return nullptr;
|
|
32
|
+
block->body.push_back(std::move(statement));
|
|
33
|
+
}
|
|
34
|
+
if (!expect("}")) return nullptr;
|
|
35
|
+
return block;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
StmtPtr Parser::parse_simple_stmt() {
|
|
39
|
+
ExprPtr expr = parse_expr();
|
|
40
|
+
if (expr == nullptr) return nullptr;
|
|
41
|
+
if (peek().kind == Tok::Punct && is_assignment(peek().text)) {
|
|
42
|
+
auto statement = make_stmt(Stmt::K::Assign, peek().line);
|
|
43
|
+
statement->op = peek().text;
|
|
44
|
+
position_++;
|
|
45
|
+
statement->expr = std::move(expr);
|
|
46
|
+
statement->value = parse_expr();
|
|
47
|
+
if (statement->value == nullptr) return nullptr;
|
|
48
|
+
return statement;
|
|
49
|
+
}
|
|
50
|
+
auto statement = make_stmt(Stmt::K::Expr, expr->line);
|
|
51
|
+
statement->expr = std::move(expr);
|
|
52
|
+
return statement;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
StmtPtr Parser::parse_stmt() {
|
|
56
|
+
const int line = peek().line;
|
|
57
|
+
if (at("{")) return parse_block();
|
|
58
|
+
|
|
59
|
+
if (at("if")) {
|
|
60
|
+
position_++;
|
|
61
|
+
auto statement = make_stmt(Stmt::K::If, line);
|
|
62
|
+
if (!expect("(")) return nullptr;
|
|
63
|
+
statement->expr = parse_expr();
|
|
64
|
+
if (statement->expr == nullptr) return nullptr;
|
|
65
|
+
if (!expect(")")) return nullptr;
|
|
66
|
+
statement->body.push_back(parse_block());
|
|
67
|
+
if (statement->body.back() == nullptr) return nullptr;
|
|
68
|
+
if (accept("else")) {
|
|
69
|
+
if (at("if")) {
|
|
70
|
+
statement->otherwise.push_back(parse_stmt());
|
|
71
|
+
} else {
|
|
72
|
+
statement->otherwise.push_back(parse_block());
|
|
73
|
+
}
|
|
74
|
+
if (statement->otherwise.back() == nullptr) return nullptr;
|
|
75
|
+
}
|
|
76
|
+
return statement;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (at("for")) {
|
|
80
|
+
position_++;
|
|
81
|
+
auto statement = make_stmt(Stmt::K::For, line);
|
|
82
|
+
if (!expect("(")) return nullptr;
|
|
83
|
+
if (at("var") || at("let") || at("const")) {
|
|
84
|
+
statement->init = parse_stmt();
|
|
85
|
+
if (statement->init == nullptr) {
|
|
86
|
+
/* `parse_stmt` consumed the terminator; rewind is impossible, so the
|
|
87
|
+
* local-declaration path below parses without it instead. */
|
|
88
|
+
return nullptr;
|
|
89
|
+
}
|
|
90
|
+
} else if (!at(";")) {
|
|
91
|
+
statement->init = parse_simple_stmt();
|
|
92
|
+
if (statement->init == nullptr) return nullptr;
|
|
93
|
+
if (!expect(";")) return nullptr;
|
|
94
|
+
} else {
|
|
95
|
+
if (!expect(";")) return nullptr;
|
|
96
|
+
}
|
|
97
|
+
if (!at(";")) {
|
|
98
|
+
statement->cond = make_stmt(Stmt::K::Expr, peek().line);
|
|
99
|
+
statement->cond->expr = parse_expr();
|
|
100
|
+
if (statement->cond->expr == nullptr) return nullptr;
|
|
101
|
+
}
|
|
102
|
+
if (!expect(";")) return nullptr;
|
|
103
|
+
if (!at(")")) {
|
|
104
|
+
statement->update = parse_simple_stmt();
|
|
105
|
+
if (statement->update == nullptr) return nullptr;
|
|
106
|
+
}
|
|
107
|
+
if (!expect(")")) return nullptr;
|
|
108
|
+
statement->body.push_back(parse_block());
|
|
109
|
+
if (statement->body.back() == nullptr) return nullptr;
|
|
110
|
+
return statement;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (at("while")) {
|
|
114
|
+
position_++;
|
|
115
|
+
auto statement = make_stmt(Stmt::K::While, line);
|
|
116
|
+
if (!expect("(")) return nullptr;
|
|
117
|
+
statement->expr = parse_expr();
|
|
118
|
+
if (statement->expr == nullptr) return nullptr;
|
|
119
|
+
if (!expect(")")) return nullptr;
|
|
120
|
+
statement->body.push_back(parse_block());
|
|
121
|
+
if (statement->body.back() == nullptr) return nullptr;
|
|
122
|
+
return statement;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (at("loop")) {
|
|
126
|
+
position_++;
|
|
127
|
+
auto statement = make_stmt(Stmt::K::Loop, line);
|
|
128
|
+
if (!expect("{")) return nullptr;
|
|
129
|
+
while (!at("}") && !at("continuing") && !failed_) {
|
|
130
|
+
StmtPtr child = parse_stmt();
|
|
131
|
+
if (child == nullptr) return nullptr;
|
|
132
|
+
statement->body.push_back(std::move(child));
|
|
133
|
+
}
|
|
134
|
+
if (accept("continuing")) {
|
|
135
|
+
statement->continuing = parse_block();
|
|
136
|
+
if (statement->continuing == nullptr) return nullptr;
|
|
137
|
+
}
|
|
138
|
+
if (!expect("}")) return nullptr;
|
|
139
|
+
return statement;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (at("switch")) {
|
|
143
|
+
position_++;
|
|
144
|
+
auto statement = make_stmt(Stmt::K::Switch, line);
|
|
145
|
+
if (!expect("(")) return nullptr;
|
|
146
|
+
statement->expr = parse_expr();
|
|
147
|
+
if (statement->expr == nullptr) return nullptr;
|
|
148
|
+
if (!expect(")")) return nullptr;
|
|
149
|
+
if (!expect("{")) return nullptr;
|
|
150
|
+
while (!at("}") && !failed_) {
|
|
151
|
+
ExprPtr selector;
|
|
152
|
+
if (accept("default")) {
|
|
153
|
+
selector = nullptr;
|
|
154
|
+
} else {
|
|
155
|
+
if (!expect("case")) return nullptr;
|
|
156
|
+
selector = parse_expr();
|
|
157
|
+
if (selector == nullptr) return nullptr;
|
|
158
|
+
while (accept(",")) {
|
|
159
|
+
ExprPtr next = parse_expr();
|
|
160
|
+
if (next == nullptr) return nullptr;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (!expect(":")) return nullptr;
|
|
164
|
+
StmtPtr body = at("{") ? parse_block() : parse_stmt();
|
|
165
|
+
if (body == nullptr) return nullptr;
|
|
166
|
+
statement->cases.push_back(std::move(selector));
|
|
167
|
+
statement->case_bodies.push_back(std::move(body));
|
|
168
|
+
accept(",");
|
|
169
|
+
}
|
|
170
|
+
if (!expect("}")) return nullptr;
|
|
171
|
+
return statement;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (at("break")) {
|
|
175
|
+
position_++;
|
|
176
|
+
if (accept("if")) {
|
|
177
|
+
auto statement = make_stmt(Stmt::K::If, line);
|
|
178
|
+
if (!expect("(")) return nullptr;
|
|
179
|
+
statement->expr = parse_expr();
|
|
180
|
+
if (statement->expr == nullptr) return nullptr;
|
|
181
|
+
if (!expect(")")) return nullptr;
|
|
182
|
+
if (!expect(";")) return nullptr;
|
|
183
|
+
statement->body.push_back(make_stmt(Stmt::K::Break, line));
|
|
184
|
+
return statement;
|
|
185
|
+
}
|
|
186
|
+
if (!expect(";")) return nullptr;
|
|
187
|
+
return make_stmt(Stmt::K::Break, line);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (at("continue")) {
|
|
191
|
+
position_++;
|
|
192
|
+
if (!expect(";")) return nullptr;
|
|
193
|
+
return make_stmt(Stmt::K::Continue, line);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (at("discard")) {
|
|
197
|
+
position_++;
|
|
198
|
+
if (!expect(";")) return nullptr;
|
|
199
|
+
return make_stmt(Stmt::K::Discard, line);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (at("return")) {
|
|
203
|
+
position_++;
|
|
204
|
+
auto statement = make_stmt(Stmt::K::Return, line);
|
|
205
|
+
if (!at(";")) {
|
|
206
|
+
statement->expr = parse_expr();
|
|
207
|
+
if (statement->expr == nullptr) return nullptr;
|
|
208
|
+
}
|
|
209
|
+
if (!expect(";")) return nullptr;
|
|
210
|
+
return statement;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (at("var") || at("let") || at("const")) {
|
|
214
|
+
auto statement = make_stmt(Stmt::K::Var, line);
|
|
215
|
+
statement->is_const = !at("var");
|
|
216
|
+
position_++;
|
|
217
|
+
if (accept("<")) {
|
|
218
|
+
std::string space;
|
|
219
|
+
if (!expect_ident(&space)) return nullptr;
|
|
220
|
+
if (accept(",")) {
|
|
221
|
+
std::string access;
|
|
222
|
+
if (!expect_ident(&access)) return nullptr;
|
|
223
|
+
statement->access = access;
|
|
224
|
+
}
|
|
225
|
+
if (!expect(">")) return nullptr;
|
|
226
|
+
}
|
|
227
|
+
if (!expect_ident(&statement->name)) return nullptr;
|
|
228
|
+
if (accept(":")) {
|
|
229
|
+
statement->type = parse_type();
|
|
230
|
+
if (statement->type == nullptr) return nullptr;
|
|
231
|
+
}
|
|
232
|
+
if (accept("=")) {
|
|
233
|
+
statement->expr = parse_expr();
|
|
234
|
+
if (statement->expr == nullptr) return nullptr;
|
|
235
|
+
}
|
|
236
|
+
if (statement->type == nullptr && statement->expr == nullptr) {
|
|
237
|
+
fail("a declaration needs a type or an initializer");
|
|
238
|
+
return nullptr;
|
|
239
|
+
}
|
|
240
|
+
if (!expect(";")) return nullptr;
|
|
241
|
+
return statement;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
StmtPtr statement = parse_simple_stmt();
|
|
245
|
+
if (statement == nullptr) return nullptr;
|
|
246
|
+
if ((at("++") || at("--")) && statement->kind == Stmt::K::Expr) {
|
|
247
|
+
auto increment = make_stmt(Stmt::K::Assign, line);
|
|
248
|
+
increment->op = peek().text;
|
|
249
|
+
position_++;
|
|
250
|
+
increment->expr = std::move(statement->expr);
|
|
251
|
+
if (!expect(";")) return nullptr;
|
|
252
|
+
return increment;
|
|
253
|
+
}
|
|
254
|
+
if (!expect(";")) return nullptr;
|
|
255
|
+
return statement;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
} // namespace wgsl
|
|
259
|
+
} // namespace xtgui
|