xbintsc 0.6.12 → 0.6.17
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/ast/expressions.d.ts +6 -0
- package/dist/src/checker/type-infer.js +1 -1
- package/dist/src/checker/type-infer.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/codegen/generator/calls/literals.js +4 -1
- package/dist/src/codegen/generator/calls/literals.js.map +1 -1
- package/dist/src/codegen/generator/expressions/assignment.js +3 -1
- package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
- package/dist/src/codegen/generator/expressions/operators.js +2 -6
- package/dist/src/codegen/generator/expressions/operators.js.map +1 -1
- package/dist/src/codegen/generator/expressions/primary.d.ts +9 -0
- package/dist/src/codegen/generator/expressions/primary.js +44 -7
- package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
- package/dist/src/driver/bundler/commonjs.js +1 -1
- package/dist/src/driver/bundler/commonjs.js.map +1 -1
- package/dist/src/driver/bundler/symbols.js +6 -0
- package/dist/src/driver/bundler/symbols.js.map +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/src/extensions/node/index.js +2 -0
- package/dist/src/extensions/node/index.js.map +1 -1
- package/dist/src/extensions/node/readline/index.d.ts +12 -0
- package/dist/src/extensions/node/readline/index.js +27 -0
- package/dist/src/extensions/node/readline/index.js.map +1 -0
- package/dist/src/parser/declarations/functions.js +6 -0
- package/dist/src/parser/declarations/functions.js.map +1 -1
- package/dist/src/parser/literals.js +1 -1
- package/dist/src/parser/literals.js.map +1 -1
- package/dist/tests/e2e/compile-modules.test.js +22 -0
- package/dist/tests/e2e/compile-modules.test.js.map +1 -1
- package/dist/tests/e2e/date-parse.test.js +17 -0
- package/dist/tests/e2e/date-parse.test.js.map +1 -1
- package/dist/tests/e2e/gui-browser.test.js +61 -0
- package/dist/tests/e2e/gui-browser.test.js.map +1 -1
- package/dist/tests/e2e/gui-css-coverage.test.js +39 -0
- package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -1
- package/dist/tests/e2e/gui-css-features.test.js +100 -0
- package/dist/tests/e2e/gui-css-features.test.js.map +1 -1
- package/dist/tests/e2e/gui-dom.test.js +34 -0
- package/dist/tests/e2e/gui-dom.test.js.map +1 -1
- package/dist/tests/e2e/gui-errors.test.d.ts +11 -0
- package/dist/tests/e2e/gui-errors.test.js +68 -0
- package/dist/tests/e2e/gui-errors.test.js.map +1 -0
- 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-webgl2-shadow-sampler.test.js +7 -1
- package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +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/e2e/harness.d.ts +4 -0
- package/dist/tests/e2e/harness.js +2 -0
- package/dist/tests/e2e/harness.js.map +1 -1
- package/dist/tests/e2e/readline.test.d.ts +7 -0
- package/dist/tests/e2e/readline.test.js +73 -0
- package/dist/tests/e2e/readline.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/api.md +15 -0
- package/doc/gui/dom.md +7 -2
- package/doc/gui/known-issues.md +2 -1
- package/doc/gui/layout.md +5 -1
- package/doc/gui/webgpu.md +121 -0
- package/doc/gui.md +12 -1
- package/doc/implemented/node/readline.md +34 -0
- package/doc/implemented/node.md +1 -0
- package/doc/unimplemented/node/modules.md +2 -3
- package/doc/unimplemented/node.md +2 -2
- package/doc/zh-CN/gui/api.md +11 -0
- package/doc/zh-CN/gui/dom.md +6 -2
- package/doc/zh-CN/gui/known-issues.md +2 -1
- package/doc/zh-CN/gui/layout.md +4 -1
- package/doc/zh-CN/gui/webgpu.md +111 -0
- package/doc/zh-CN/gui.md +10 -1
- package/doc/zh-CN/implemented/node/readline.md +33 -0
- package/doc/zh-CN/implemented/node.md +1 -0
- package/doc/zh-CN/unimplemented/node/modules.md +1 -2
- package/doc/zh-CN/unimplemented/node.md +2 -2
- package/llms.txt +1 -0
- package/package.json +7 -7
- package/runtime/ext_gui/browser.cpp +10 -0
- package/runtime/ext_gui/browser_abort.cpp +2 -2
- package/runtime/ext_gui/browser_loop.cpp +1 -0
- package/runtime/ext_gui/browser_media.cpp +1 -1
- 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/dom_api_canvas_image.cpp +2 -1
- package/runtime/ext_gui/dom_api_events.cpp +1 -1
- package/runtime/ext_gui/dom_api_internal.h +2 -0
- package/runtime/ext_gui/dom_api_node.cpp +18 -12
- package/runtime/ext_gui/dom_api_node_read.cpp +27 -0
- package/runtime/ext_gui/dom_api_proto.cpp +2 -0
- package/runtime/ext_gui/error_report.cpp +166 -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.cpp +16 -0
- package/runtime/ext_gui/gui_canvas.cpp +18 -0
- package/runtime/ext_gui/gui_engine.h +13 -0
- package/runtime/ext_gui/gui_events.cpp +4 -1
- package/runtime/ext_gui/layout_inline.cpp +46 -14
- package/runtime/ext_gui/paint.cpp +22 -0
- package/runtime/ext_gui/renderer_image.cpp +6 -0
- package/runtime/ext_gui/script.cpp +1 -1
- package/runtime/ext_gui/style.cpp +1 -0
- package/runtime/ext_gui/style.h +13 -0
- package/runtime/ext_gui/style_print.cpp +99 -0
- package/runtime/ext_gui/style_text.cpp +79 -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/webgl_texture.cpp +0 -8
- 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 +309 -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 +242 -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/runtime/ext_gui/window.cpp +22 -8
- package/runtime/ext_gui/window_emit.cpp +1 -1
- package/runtime/ext_node/fs/parts/platform.inc +2 -0
- package/runtime/ext_node/readline/readline.c +572 -0
- package/runtime/rt.h +15 -0
- package/runtime/rt_internal.h +3 -2
- package/runtime/xt_io/exception.inc +29 -6
- package/runtime/xt_loop.c +36 -1
- package/runtime/xt_promise.c +26 -2
- package/runtime/xt_stdlib2/date.inc +2 -2
- package/runtime/xt_typed_array/elements.inc +0 -9
- package/runtime/xt_url.c +0 -6
- package/runtime/xt_url_object.c +1 -1
- package/src/ast/expressions.ts +6 -0
- package/src/checker/type-infer.ts +1 -1
- package/src/codegen/generator/calls/literals.ts +4 -1
- package/src/codegen/generator/expressions/assignment.ts +3 -1
- package/src/codegen/generator/expressions/operators.ts +2 -6
- package/src/codegen/generator/expressions/primary.ts +58 -6
- package/src/driver/bundler/commonjs.ts +1 -1
- package/src/driver/bundler/symbols.ts +6 -0
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/features.ts +24 -0
- package/src/extensions/gui/index.ts +39 -0
- package/src/extensions/node/index.ts +2 -0
- package/src/extensions/node/readline/index.ts +31 -0
- package/src/parser/declarations/functions.ts +6 -0
- package/src/parser/literals.ts +1 -1
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — WGSL top-level declarations, types and attributes.
|
|
3
|
+
*
|
|
4
|
+
* Statements live in `wgsl_parse_stmt.cpp`, expressions in
|
|
5
|
+
* `wgsl_parse_expr.cpp`; this unit owns the module grammar that ties them
|
|
6
|
+
* together.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#include "wgsl_parser.h"
|
|
10
|
+
|
|
11
|
+
#include <cstdlib>
|
|
12
|
+
|
|
13
|
+
namespace xtgui {
|
|
14
|
+
namespace wgsl {
|
|
15
|
+
|
|
16
|
+
/* -- token helpers -------------------------------------------------------- */
|
|
17
|
+
|
|
18
|
+
const Token &Parser::token(size_t index) const {
|
|
19
|
+
if (index >= tokens_.size()) return tokens_.back();
|
|
20
|
+
return tokens_[index];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const Token &Parser::peek(int ahead) const {
|
|
24
|
+
const int index = (int)position_ + ahead;
|
|
25
|
+
if (index < 0) return tokens_.front();
|
|
26
|
+
if ((size_t)index >= tokens_.size()) return tokens_.back();
|
|
27
|
+
return tokens_[(size_t)index];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
bool Parser::at(const std::string &text) const { return peek().text == text; }
|
|
31
|
+
|
|
32
|
+
bool Parser::accept(const std::string &text) {
|
|
33
|
+
if (!at(text)) return false;
|
|
34
|
+
position_++;
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
bool Parser::expect(const std::string &text) {
|
|
39
|
+
if (accept(text)) return true;
|
|
40
|
+
fail("expected '" + text + "'");
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
bool Parser::expect_ident(std::string *out) {
|
|
45
|
+
if (peek().kind != Tok::Ident) {
|
|
46
|
+
fail("expected an identifier");
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
*out = peek().text;
|
|
50
|
+
position_++;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
void Parser::fail(const std::string &message) { fail_at(peek(), message); }
|
|
55
|
+
|
|
56
|
+
void Parser::fail_at(const Token &token, const std::string &message) {
|
|
57
|
+
if (failed_) return;
|
|
58
|
+
failed_ = true;
|
|
59
|
+
error_line_ = token.line;
|
|
60
|
+
error_column_ = token.column;
|
|
61
|
+
error_ = std::to_string(token.line) + ":" + std::to_string(token.column) + ": " + message;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/* -- module --------------------------------------------------------------- */
|
|
65
|
+
|
|
66
|
+
bool Parser::parse_module() {
|
|
67
|
+
while (peek().kind != Tok::End && !failed_) {
|
|
68
|
+
std::vector<Attribute> attrs;
|
|
69
|
+
if (!parse_attrs(&attrs)) return false;
|
|
70
|
+
if (peek().kind == Tok::End) break;
|
|
71
|
+
if (at("enable") || at("requires") || at("diagnostic")) {
|
|
72
|
+
while (peek().kind != Tok::End && !at(";")) position_++;
|
|
73
|
+
if (!expect(";")) return false;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (at("struct")) {
|
|
77
|
+
if (!parse_struct()) return false;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (at("alias")) {
|
|
81
|
+
if (!parse_alias()) return false;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (at("const") || at("override")) {
|
|
85
|
+
if (!parse_const_global()) return false;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (at("var")) {
|
|
89
|
+
if (!parse_global(attrs)) return false;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (at("fn")) {
|
|
93
|
+
if (!parse_function(attrs)) return false;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (accept(";")) continue;
|
|
97
|
+
fail("expected a WGSL declaration (struct, alias, const, var or fn)");
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
return !failed_;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
bool Parser::parse_struct() {
|
|
104
|
+
if (!expect("struct")) return false;
|
|
105
|
+
StructDecl decl;
|
|
106
|
+
if (!expect_ident(&decl.name)) return false;
|
|
107
|
+
if (!expect("{")) return false;
|
|
108
|
+
while (!at("}") && !failed_) {
|
|
109
|
+
std::vector<Attribute> attrs;
|
|
110
|
+
if (!parse_attrs(&attrs)) return false;
|
|
111
|
+
Member member;
|
|
112
|
+
if (!expect_ident(&member.name)) return false;
|
|
113
|
+
if (!expect(":")) return false;
|
|
114
|
+
member.type = parse_type();
|
|
115
|
+
if (member.type == nullptr) return false;
|
|
116
|
+
for (const Attribute &attr : attrs) {
|
|
117
|
+
if (attr.name == "location") member.location = attr.ints.empty() ? 0 : attr.ints[0];
|
|
118
|
+
else if (attr.name == "builtin" && !attr.words.empty()) member.builtin = attr.words[0];
|
|
119
|
+
else if (attr.name == "interpolate" && !attr.words.empty()) member.interpolate = attr.words[0];
|
|
120
|
+
else if (attr.name == "align" && !attr.ints.empty()) member.align = attr.ints[0];
|
|
121
|
+
else if (attr.name == "size" && !attr.ints.empty()) member.size = attr.ints[0];
|
|
122
|
+
}
|
|
123
|
+
decl.members.push_back(std::move(member));
|
|
124
|
+
if (!accept(",")) break;
|
|
125
|
+
}
|
|
126
|
+
if (!expect("}")) return false;
|
|
127
|
+
accept(";");
|
|
128
|
+
module_->structs.push_back(std::move(decl));
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
bool Parser::parse_alias() {
|
|
133
|
+
if (!expect("alias")) return false;
|
|
134
|
+
std::string name;
|
|
135
|
+
if (!expect_ident(&name)) return false;
|
|
136
|
+
if (!expect("=")) return false;
|
|
137
|
+
TypePtr type = parse_type();
|
|
138
|
+
if (type == nullptr) return false;
|
|
139
|
+
if (!expect(";")) return false;
|
|
140
|
+
module_->aliases.emplace_back(name, type);
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
bool Parser::parse_const_global() {
|
|
145
|
+
const bool is_override = at("override");
|
|
146
|
+
position_++;
|
|
147
|
+
GlobalDecl decl;
|
|
148
|
+
if (!expect_ident(&decl.name)) return false;
|
|
149
|
+
if (accept(":")) {
|
|
150
|
+
decl.type = parse_type();
|
|
151
|
+
if (decl.type == nullptr) return false;
|
|
152
|
+
}
|
|
153
|
+
if (accept("=")) {
|
|
154
|
+
decl.init = parse_expr();
|
|
155
|
+
if (decl.init == nullptr) return false;
|
|
156
|
+
}
|
|
157
|
+
(void)is_override;
|
|
158
|
+
if (!expect(";")) return false;
|
|
159
|
+
decl.space = Space::None;
|
|
160
|
+
module_->globals.push_back(std::move(decl));
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
bool Parser::parse_global(const std::vector<Attribute> &attrs) {
|
|
165
|
+
if (!expect("var")) return false;
|
|
166
|
+
GlobalDecl decl;
|
|
167
|
+
if (accept("<")) {
|
|
168
|
+
std::string space;
|
|
169
|
+
if (!expect_ident(&space)) return false;
|
|
170
|
+
if (space == "private") decl.space = Space::Private;
|
|
171
|
+
else if (space == "workgroup") decl.space = Space::Workgroup;
|
|
172
|
+
else if (space == "uniform") decl.space = Space::Uniform;
|
|
173
|
+
else if (space == "storage") decl.space = Space::Storage;
|
|
174
|
+
else {
|
|
175
|
+
fail("unsupported address space '" + space + "'");
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
if (accept(",")) {
|
|
179
|
+
if (!expect_ident(&decl.access)) return false;
|
|
180
|
+
}
|
|
181
|
+
if (!expect(">")) return false;
|
|
182
|
+
}
|
|
183
|
+
if (!expect_ident(&decl.name)) return false;
|
|
184
|
+
if (!expect(":")) return false;
|
|
185
|
+
decl.type = parse_type();
|
|
186
|
+
if (decl.type == nullptr) return false;
|
|
187
|
+
if (accept("=")) {
|
|
188
|
+
decl.init = parse_expr();
|
|
189
|
+
if (decl.init == nullptr) return false;
|
|
190
|
+
}
|
|
191
|
+
if (!expect(";")) return false;
|
|
192
|
+
for (const Attribute &attr : attrs) {
|
|
193
|
+
if (attr.name == "group") decl.group = attr.ints.empty() ? 0 : attr.ints[0];
|
|
194
|
+
else if (attr.name == "binding") decl.binding = attr.ints.empty() ? 0 : attr.ints[0];
|
|
195
|
+
}
|
|
196
|
+
decl.line = peek().line;
|
|
197
|
+
module_->globals.push_back(std::move(decl));
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
bool Parser::parse_workgroup_size(const Attribute &attr, int out[3]) {
|
|
202
|
+
out[0] = attr.ints.size() > 0 ? attr.ints[0] : 1;
|
|
203
|
+
out[1] = attr.ints.size() > 1 ? attr.ints[1] : 1;
|
|
204
|
+
out[2] = attr.ints.size() > 2 ? attr.ints[2] : 1;
|
|
205
|
+
return out[0] > 0 && out[1] > 0 && out[2] > 0;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
bool Parser::parse_function(const std::vector<Attribute> &attrs) {
|
|
209
|
+
if (!expect("fn")) return false;
|
|
210
|
+
FuncDecl decl;
|
|
211
|
+
if (!expect_ident(&decl.name)) return false;
|
|
212
|
+
if (!expect("(")) return false;
|
|
213
|
+
while (!at(")") && !failed_) {
|
|
214
|
+
std::vector<Attribute> param_attrs;
|
|
215
|
+
if (!parse_attrs(¶m_attrs)) return false;
|
|
216
|
+
Param param;
|
|
217
|
+
if (!expect_ident(¶m.name)) return false;
|
|
218
|
+
if (!expect(":")) return false;
|
|
219
|
+
param.type = parse_type();
|
|
220
|
+
if (param.type == nullptr) return false;
|
|
221
|
+
for (const Attribute &attr : param_attrs) {
|
|
222
|
+
if (attr.name == "location") param.location = attr.ints.empty() ? 0 : attr.ints[0];
|
|
223
|
+
else if (attr.name == "builtin" && !attr.words.empty()) param.builtin = attr.words[0];
|
|
224
|
+
else if (attr.name == "interpolate" && !attr.words.empty()) param.interpolate = attr.words[0];
|
|
225
|
+
}
|
|
226
|
+
decl.params.push_back(std::move(param));
|
|
227
|
+
if (!accept(",")) break;
|
|
228
|
+
}
|
|
229
|
+
if (!expect(")")) return false;
|
|
230
|
+
if (accept("->")) {
|
|
231
|
+
std::vector<Attribute> ret_attrs;
|
|
232
|
+
if (!parse_attrs(&ret_attrs)) return false;
|
|
233
|
+
decl.ret = parse_type();
|
|
234
|
+
if (decl.ret == nullptr) return false;
|
|
235
|
+
for (const Attribute &attr : ret_attrs) {
|
|
236
|
+
if (attr.name == "location") decl.ret_location = attr.ints.empty() ? 0 : attr.ints[0];
|
|
237
|
+
else if (attr.name == "builtin" && !attr.words.empty()) decl.ret_builtin = attr.words[0];
|
|
238
|
+
else if (attr.name == "interpolate" && !attr.words.empty()) {
|
|
239
|
+
decl.ret_interpolate = attr.words[0];
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
for (const Attribute &attr : attrs) {
|
|
244
|
+
if (attr.name == "vertex") {
|
|
245
|
+
decl.is_entry = true;
|
|
246
|
+
decl.stage = "vertex";
|
|
247
|
+
} else if (attr.name == "fragment") {
|
|
248
|
+
decl.is_entry = true;
|
|
249
|
+
decl.stage = "fragment";
|
|
250
|
+
} else if (attr.name == "compute") {
|
|
251
|
+
decl.is_entry = true;
|
|
252
|
+
decl.stage = "compute";
|
|
253
|
+
} else if (attr.name == "workgroup_size") {
|
|
254
|
+
decl.has_workgroup = parse_workgroup_size(attr, decl.workgroup);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (decl.is_entry && decl.stage == "compute" && !decl.has_workgroup) {
|
|
258
|
+
fail("a @compute entry point needs @workgroup_size");
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
if (!expect("{")) return false;
|
|
262
|
+
while (!at("}") && !failed_) {
|
|
263
|
+
StmtPtr statement = parse_stmt();
|
|
264
|
+
if (statement == nullptr) return false;
|
|
265
|
+
decl.body.push_back(std::move(statement));
|
|
266
|
+
}
|
|
267
|
+
if (!expect("}")) return false;
|
|
268
|
+
module_->funcs.push_back(std::move(decl));
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/* -- attributes ----------------------------------------------------------- */
|
|
273
|
+
|
|
274
|
+
bool Parser::parse_attrs(std::vector<Attribute> *attrs) {
|
|
275
|
+
while (peek().kind == Tok::Ident && !peek().text.empty() && peek().text[0] == '@') {
|
|
276
|
+
Attribute attr;
|
|
277
|
+
attr.name = peek().text.substr(1);
|
|
278
|
+
position_++;
|
|
279
|
+
if (accept("(")) {
|
|
280
|
+
while (!at(")") && !failed_) {
|
|
281
|
+
if (peek().kind == Tok::Int) {
|
|
282
|
+
attr.ints.push_back((int)strtol(peek().text.c_str(), nullptr, 0));
|
|
283
|
+
attr.words.push_back(peek().text);
|
|
284
|
+
position_++;
|
|
285
|
+
} else if (peek().kind == Tok::Ident) {
|
|
286
|
+
attr.words.push_back(peek().text);
|
|
287
|
+
position_++;
|
|
288
|
+
} else {
|
|
289
|
+
fail("expected an attribute argument");
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
if (!accept(",")) break;
|
|
293
|
+
}
|
|
294
|
+
if (!expect(")")) return false;
|
|
295
|
+
}
|
|
296
|
+
attrs->push_back(std::move(attr));
|
|
297
|
+
}
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/* -- types ---------------------------------------------------------------- */
|
|
302
|
+
|
|
303
|
+
bool Parser::expect_close_angle() {
|
|
304
|
+
if (accept(">")) return true;
|
|
305
|
+
if (at(">>")) {
|
|
306
|
+
/* Close one level now and remember the second for the outer type. */
|
|
307
|
+
position_++;
|
|
308
|
+
pending_angles_++;
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
fail("expected '>'");
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
TypePtr Parser::parse_texture_type(const std::string &name) {
|
|
316
|
+
auto type = std::make_shared<Type>();
|
|
317
|
+
type->kind = Kind::Texture;
|
|
318
|
+
if (name.find("storage") != std::string::npos) type->kind = Kind::StorageTexture;
|
|
319
|
+
if (name.find("depth") != std::string::npos) type->depth = true;
|
|
320
|
+
if (name.find("_1d") != std::string::npos) type->dim = TextureDim::D1;
|
|
321
|
+
else if (name.find("_2d_array") != std::string::npos) type->dim = TextureDim::D2Array;
|
|
322
|
+
else if (name.find("_3d") != std::string::npos) type->dim = TextureDim::D3;
|
|
323
|
+
else if (name.find("cube_array") != std::string::npos) type->dim = TextureDim::CubeArray;
|
|
324
|
+
else if (name.find("cube") != std::string::npos) type->dim = TextureDim::Cube;
|
|
325
|
+
else if (name.find("multisampled") != std::string::npos) type->dim = TextureDim::Multisampled2D;
|
|
326
|
+
else type->dim = TextureDim::D2;
|
|
327
|
+
|
|
328
|
+
if (!accept("<")) return type;
|
|
329
|
+
if (type->kind == Kind::StorageTexture) {
|
|
330
|
+
std::string format;
|
|
331
|
+
if (!expect_ident(&format)) return nullptr;
|
|
332
|
+
if (format == "rgba8unorm") type->format = ImageFormat::Rgba8Unorm;
|
|
333
|
+
else if (format == "rgba8snorm") type->format = ImageFormat::Rgba8Snorm;
|
|
334
|
+
else if (format == "rgba8uint") type->format = ImageFormat::Rgba8Uint;
|
|
335
|
+
else if (format == "rgba8sint") type->format = ImageFormat::Rgba8Sint;
|
|
336
|
+
else if (format == "rgba16float") type->format = ImageFormat::Rgba16Float;
|
|
337
|
+
else if (format == "rgba32float") type->format = ImageFormat::Rgba32Float;
|
|
338
|
+
else if (format == "r32float") type->format = ImageFormat::R32Float;
|
|
339
|
+
else if (format == "r32uint") type->format = ImageFormat::R32Uint;
|
|
340
|
+
else if (format == "r32sint") type->format = ImageFormat::R32Sint;
|
|
341
|
+
else if (format == "rgba16uint") type->format = ImageFormat::Rgba16Uint;
|
|
342
|
+
else if (format == "rgba16sint") type->format = ImageFormat::Rgba16Sint;
|
|
343
|
+
else if (format == "bgra8unorm") type->format = ImageFormat::Bgra8Unorm;
|
|
344
|
+
else {
|
|
345
|
+
fail("unsupported storage texture format '" + format + "'");
|
|
346
|
+
return nullptr;
|
|
347
|
+
}
|
|
348
|
+
if (accept(",")) {
|
|
349
|
+
std::string access;
|
|
350
|
+
if (!expect_ident(&access)) return nullptr;
|
|
351
|
+
if (access == "read") type->access = 1;
|
|
352
|
+
else if (access == "write") type->access = 2;
|
|
353
|
+
else if (access == "read_write") type->access = 3;
|
|
354
|
+
} else {
|
|
355
|
+
type->access = 2;
|
|
356
|
+
}
|
|
357
|
+
} else if (!type->depth) {
|
|
358
|
+
TypePtr element = parse_type();
|
|
359
|
+
if (element == nullptr) return nullptr;
|
|
360
|
+
if (element->kind != Kind::Scalar) {
|
|
361
|
+
fail("a sampled texture's element type must be f32, i32 or u32");
|
|
362
|
+
return nullptr;
|
|
363
|
+
}
|
|
364
|
+
type->scalar = element->scalar;
|
|
365
|
+
}
|
|
366
|
+
if (!expect_close_angle()) return nullptr;
|
|
367
|
+
return type;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
TypePtr Parser::parse_type() {
|
|
371
|
+
if (peek().kind != Tok::Ident) {
|
|
372
|
+
fail("expected a type");
|
|
373
|
+
return nullptr;
|
|
374
|
+
}
|
|
375
|
+
const std::string name = peek().text;
|
|
376
|
+
position_++;
|
|
377
|
+
if (name.compare(0, 7, "texture") == 0) return parse_texture_type(name);
|
|
378
|
+
if (name == "ptr") {
|
|
379
|
+
if (!expect("<")) return nullptr;
|
|
380
|
+
std::string space;
|
|
381
|
+
if (!expect_ident(&space)) return nullptr;
|
|
382
|
+
if (!expect(",")) return nullptr;
|
|
383
|
+
TypePtr inner = parse_type();
|
|
384
|
+
if (inner == nullptr) return nullptr;
|
|
385
|
+
if (!expect_close_angle()) return nullptr;
|
|
386
|
+
auto type = std::make_shared<Type>();
|
|
387
|
+
type->kind = Kind::Ptr;
|
|
388
|
+
type->elem = inner;
|
|
389
|
+
return type;
|
|
390
|
+
}
|
|
391
|
+
if (name == "array") {
|
|
392
|
+
if (!expect("<")) return nullptr;
|
|
393
|
+
TypePtr elem = parse_type();
|
|
394
|
+
if (elem == nullptr) return nullptr;
|
|
395
|
+
int length = 0;
|
|
396
|
+
if (accept(",")) {
|
|
397
|
+
if (peek().kind != Tok::Int) {
|
|
398
|
+
fail("an array length must be an integer literal");
|
|
399
|
+
return nullptr;
|
|
400
|
+
}
|
|
401
|
+
length = (int)strtol(peek().text.c_str(), nullptr, 0);
|
|
402
|
+
position_++;
|
|
403
|
+
}
|
|
404
|
+
if (!expect_close_angle()) return nullptr;
|
|
405
|
+
return make_array(elem, length);
|
|
406
|
+
}
|
|
407
|
+
if (accept("<")) {
|
|
408
|
+
Scalar scalar = Scalar::F32;
|
|
409
|
+
if (peek().kind == Tok::Ident) {
|
|
410
|
+
const std::string arg = peek().text;
|
|
411
|
+
if (arg == "f32" || arg == "f16") scalar = Scalar::F32;
|
|
412
|
+
else if (arg == "i32") scalar = Scalar::I32;
|
|
413
|
+
else if (arg == "u32") scalar = Scalar::U32;
|
|
414
|
+
else if (arg == "bool") scalar = Scalar::Bool;
|
|
415
|
+
else {
|
|
416
|
+
fail("unsupported type argument '" + arg + "'");
|
|
417
|
+
return nullptr;
|
|
418
|
+
}
|
|
419
|
+
position_++;
|
|
420
|
+
} else {
|
|
421
|
+
fail("expected a scalar type argument");
|
|
422
|
+
return nullptr;
|
|
423
|
+
}
|
|
424
|
+
if (!expect_close_angle()) return nullptr;
|
|
425
|
+
if (name.compare(0, 3, "vec") == 0 && name.size() == 4) {
|
|
426
|
+
return make_vector(scalar, name[3] - '0');
|
|
427
|
+
}
|
|
428
|
+
if (name.compare(0, 3, "mat") == 0) {
|
|
429
|
+
int cols = 4;
|
|
430
|
+
int rows = 4;
|
|
431
|
+
if (name.size() >= 5 && name[4] == 'x') {
|
|
432
|
+
cols = name[3] - '0';
|
|
433
|
+
rows = name[5] - '0';
|
|
434
|
+
} else if (name.size() == 4) {
|
|
435
|
+
cols = rows = name[3] - '0';
|
|
436
|
+
}
|
|
437
|
+
return make_matrix(scalar, cols, rows);
|
|
438
|
+
}
|
|
439
|
+
if (name == "atomic") {
|
|
440
|
+
auto type = std::make_shared<Type>();
|
|
441
|
+
type->kind = Kind::Atomic;
|
|
442
|
+
type->scalar = scalar;
|
|
443
|
+
return type;
|
|
444
|
+
}
|
|
445
|
+
fail("type '" + name + "' does not take type arguments");
|
|
446
|
+
return nullptr;
|
|
447
|
+
}
|
|
448
|
+
TypePtr shorthand = type_by_name(*module_, name);
|
|
449
|
+
if (shorthand == nullptr) {
|
|
450
|
+
fail("unknown type '" + name + "'");
|
|
451
|
+
return nullptr;
|
|
452
|
+
}
|
|
453
|
+
return shorthand;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
} // namespace wgsl
|
|
457
|
+
} // namespace xtgui
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — recursive-descent WGSL parser (shared declarations).
|
|
3
|
+
*/
|
|
4
|
+
#ifndef XT_GUI_WGSL_PARSER_H
|
|
5
|
+
#define XT_GUI_WGSL_PARSER_H
|
|
6
|
+
|
|
7
|
+
#include <string>
|
|
8
|
+
#include <vector>
|
|
9
|
+
|
|
10
|
+
#include "wgsl_ast.h"
|
|
11
|
+
#include "wgsl_lexer.h"
|
|
12
|
+
|
|
13
|
+
namespace xtgui {
|
|
14
|
+
namespace wgsl {
|
|
15
|
+
|
|
16
|
+
class Parser {
|
|
17
|
+
public:
|
|
18
|
+
Parser(const std::vector<Token> &tokens, Module *module)
|
|
19
|
+
: tokens_(tokens), module_(module) {}
|
|
20
|
+
|
|
21
|
+
bool parse_module();
|
|
22
|
+
|
|
23
|
+
/* -- token helpers ------------------------------------------------------ */
|
|
24
|
+
const Token &peek(int ahead = 0) const;
|
|
25
|
+
bool at(const std::string &text) const;
|
|
26
|
+
bool accept(const std::string &text);
|
|
27
|
+
bool expect(const std::string &text);
|
|
28
|
+
bool expect_ident(std::string *out);
|
|
29
|
+
void fail(const std::string &message);
|
|
30
|
+
void fail_at(const Token &token, const std::string &message);
|
|
31
|
+
bool ok() const { return !failed_; }
|
|
32
|
+
|
|
33
|
+
/* -- declarations ------------------------------------------------------- */
|
|
34
|
+
bool parse_struct();
|
|
35
|
+
bool parse_global(const std::vector<Attribute> &attrs);
|
|
36
|
+
bool parse_const_global();
|
|
37
|
+
bool parse_alias();
|
|
38
|
+
bool parse_function(const std::vector<Attribute> &attrs);
|
|
39
|
+
|
|
40
|
+
/* -- types and attributes ----------------------------------------------- */
|
|
41
|
+
bool parse_attrs(std::vector<Attribute> *attrs);
|
|
42
|
+
TypePtr parse_type();
|
|
43
|
+
TypePtr parse_texture_type(const std::string &name);
|
|
44
|
+
bool expect_close_angle();
|
|
45
|
+
bool parse_workgroup_size(const Attribute &attr, int out[3]);
|
|
46
|
+
|
|
47
|
+
/* -- statements and expressions ----------------------------------------- */
|
|
48
|
+
StmtPtr parse_stmt();
|
|
49
|
+
StmtPtr parse_block();
|
|
50
|
+
StmtPtr parse_simple_stmt();
|
|
51
|
+
ExprPtr parse_expr(int min_precedence = 0);
|
|
52
|
+
ExprPtr parse_unary();
|
|
53
|
+
ExprPtr parse_postfix();
|
|
54
|
+
ExprPtr parse_primary();
|
|
55
|
+
|
|
56
|
+
const std::string &error() const { return error_; }
|
|
57
|
+
int error_line() const { return error_line_; }
|
|
58
|
+
int error_column() const { return error_column_; }
|
|
59
|
+
|
|
60
|
+
private:
|
|
61
|
+
const Token &token(size_t index) const;
|
|
62
|
+
bool consume_angle();
|
|
63
|
+
|
|
64
|
+
const std::vector<Token> &tokens_;
|
|
65
|
+
Module *module_ = nullptr;
|
|
66
|
+
size_t position_ = 0;
|
|
67
|
+
int pending_angles_ = 0;
|
|
68
|
+
bool failed_ = false;
|
|
69
|
+
std::string error_;
|
|
70
|
+
int error_line_ = 0;
|
|
71
|
+
int error_column_ = 0;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
} // namespace wgsl
|
|
75
|
+
} // namespace xtgui
|
|
76
|
+
|
|
77
|
+
#endif /* XT_GUI_WGSL_PARSER_H */
|